Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
833 views
in Technique[技术] by (71.8m points)

matlab - Sliding window algorithm for activity recognition

I want to write a sliding window algorithm for use in activity recognition.

The training data is <1xN> so I'm thinking I just need to take (say window_size=3) the window_size of data and train that. I also later want to use this algorithm on a matrix .

I'm new to matlab so i need any advice/directions on how to implement this correctly.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The short answer:

%# nx = length(x)
%# nwind = window_size
idx = bsxfun(@plus, (1:nwind)', 1+(0:(fix(nx/nwind)-1))*nwind)-1;

idx will be a matrix of size nwind-by-K where K is the number of sliding windows (ie each column contains the indices of one sliding window).

Note that in the code above, if the last window's length is less than the desired one, it is dropped. Also the sliding windows are non-overlapping.

An example to illustrate:

%# lets create a sin signal
t = linspace(0,1,200);
x = sin(2*pi*5*t);

%# compute indices
nx = length(x);
nwind = 8;
idx = bsxfun(@plus, (1:nwind)', 1+(0:(fix(nx/nwind)-1))*nwind)-1;

%'# loop over sliding windows
for k=1:size(idx,2)
    slidingWindow = x( idx(:,k) );
    %# do something with it ..
end

%# or more concisely as
slidingWindows = x(idx);

EDIT:

For overlapping windows, let:

noverlap = number of overlapping elements

then the above is simply changed to:

idx = bsxfun(@plus, (1:nwind)', 1+(0:(fix((nx-noverlap)/(nwind-noverlap))-1))*(nwind-noverlap))-1;


An example to show the result:

>> nx = 100; nwind = 10; noverlap = 2;
>> idx = bsxfun(@plus, (1:nwind)', 1+(0:(fix((nx-noverlap)/(nwind-noverlap))-1))*(nwind-noverlap))-1
idx =
     1     9    17    25    33    41    49    57    65    73    81    89
     2    10    18    26    34    42    50    58    66    74    82    90
     3    11    19    27    35    43    51    59    67    75    83    91
     4    12    20    28    36    44    52    60    68    76    84    92
     5    13    21    29    37    45    53    61    69    77    85    93
     6    14    22    30    38    46    54    62    70    78    86    94
     7    15    23    31    39    47    55    63    71    79    87    95
     8    16    24    32    40    48    56    64    72    80    88    96
     9    17    25    33    41    49    57    65    73    81    89    97
    10    18    26    34    42    50    58    66    74    82    90    98

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...