Understanding How Sliding Window works
35 次查看(过去 30 天)
显示 更早的评论
0
down vote
favorite
1
I am noob and i found very fragmentated information on stack on Slinding Window.
I have a mXn matrix, where m is fixed(latitude, longitude, ax, ay, az), n could change from different logs.
1) How can i create a sliding window <https://stackoverflow.com/questions/7099590/how-do-i-select-n-elements-of-a-sequence-in-windows-of-m-matlab (in this examples > only for az without extracting the vector and then analyzing it?
2) If i want to save all the rows where the az standard deviation go over a defined thresholds how can i do that?
3) If logs length is not fixed how can i deal with that? (ex. one file contains 932 rows, another 953)
4) I read a lot of questions, i am studying how bsxfun works in this case but is very unclear for me (in this examples i only undestood that a new matrix is created, based on the window size and then the new matrix is analyzed)(this last question is strongly related to my civil engineer background)
Here what i learned, and tried to aggregate.
Sliding Window is a powerful tool that allows to analyze a signal or an image. When I tried to describe to my girlfriend what I was doing I explained “Is like reading a book with a magnifier, the magnifier has a defined dimension and you analyze the text”
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/164380/image.jpeg)
The basic way on Matlab, not the most efficient, to do that is
1. Define your vector dimensions
2. Define you window dimension
3. Define the number of steps
Here a basic example that i wrote
a= randi(100,[1,50]); %Generic Vector
win_dim=3; %Generic window size
num_stps=(length(a)-win_dim) %number of "slides", we need to subtract win_dim to avoid that the window will go over the signal
threshold= 15 %Generic number only for the example
for i= 1:num_stps
mean_win(i)=mean(a(i:i+win_dim -1); %we subtract 1 or we make an error, and the code analyzes a segment bigger than one unit
std_win(i)=std( a(i:i+win_dim -1); %example for i=2 if we don't subtract 1 our segment starts from 2 until 5, so we analyze
% 2 3 4 5, but we defined a window dimension of 3
If std_win(i)> threshold
std_anomalies=std_win(i) %here i think there is an error
end
This way the code slides over the entire signal, but windows will overlap.
How to decide the "overlap ratio" (or slide increment)?
We can define this like "how much informations two adjacent windows share?"
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/164383/image.jpeg)
The following examplei have done is completely wrong, but i tried to code something before asking here, the goal would have liked to be an overlap for Half of the segment or no overlap
%Half segment overlap
a= randi(100,[1,20]); %Generic Vector
win_dim=4; %generic window size
%v is the increment vector in our case we desire to have 50% of overlap
l= win_dim
if l%2==0
v=l/2
else
v=(l+1)/2
end
for i= 1:num_stps
if (i==1)
mean_win(i)=mean(a(1:1+win_dim -1);
else
mean(i)= mean(a (i+v:i+win_dim+v-1);
end
0 个评论
采纳的回答
Guillaume
2017-5-24
I'm not entirely sure what your question is.
To calculate a sliding standard deviation over a signal, you can use movstd with whichever window size you want. If you want a non-standard step for your sliding window (i.e. more than 1), you can simply discards slices of the result of movstd, e.g.:
windowsize = 10;
windowstep = 5;
mstd = movstd(yourmatrix(5, :), windowsize, 'Endpoints', 'discard');
mstd = mstd(1:windowstep:end); %only keep every windowstep window
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!