Sliding window algorithm for activity recognition

£可爱£侵袭症+ 提交于 2019-11-30 09:02:39

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/2202463/sliding-window-algorithm-for-activity-recognition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!