2010-02-04 21 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