2010-02-04 66 views
5

我想写一个滑动窗口算法用于活动识别。活动识别的滑动​​窗口算法

培训数据是< 1xN>所以我想我只需要采取(说window_size=3window_size的数据和训练。我以后也想在矩阵 上使用这个算法。

我是新来的matlab,所以我需要任何关于如何正确实现这个建议/方向。

回答

10

简短的回答:

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

idx将尺寸的矩阵NWIND-通过-K其中ķ是滑动窗口的数目(即,每个柱包含一个滑动窗口的索引)。

请注意,在上面的代码中,如果最后一个窗口的长度小于所需的长度,它将被丢弃。滑动窗口也不重叠。

一个例子来说明:

%# 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); 

编辑:

对于交叠窗口,让:

noverlap = number of overlapping elements 

那么上面简单地改变为:

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


一个例子来说明结果:

>> 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 
+0

@Amro感谢,这将是有用的:) – csc 2010-02-10 09:47:37

+0

@Amro我将如何适应这个让窗口重叠现象? – csc 2010-03-20 01:44:42

+0

是啊,对不起,我忘了:S – csc 2010-04-13 00:00:32