how to implement a moving window with a condition ?
조회 수: 6 (최근 30일)
이전 댓글 표시
I have an row vector 1x10000. this is a representation of series of spikes with its magnitudes being normalized(maximum amplitude 1) . My aim is to find out number of spikes within every 100 samples. I have defined a spike if the magnitude of spike crosses 0.7 or else neglected. First I should take 1st 100 samples and check for number of spikes. Then I should take samples from 2 to 101 and do the same. Then choose samples from 3 to 102 and repeat this till the sample reaches 10000. How can I implement this.
댓글 수: 0
답변 (2개)
Guillaume
2018년 3월 29일
편집: Guillaume
2018년 3월 29일
You can either use conv (i.e. a convolution) to compute a count of something over a sliding window, or since R2016a, movsum:
% FAKE DATA
x = rand(1000,1)
% PARAMETERS
window = 100
threshold = 0.7
%no-loop code:
abovethreshold = x >= threshold;
%with conv:
windowcount = conv(abovethreshold, ones(window, 1), 'valid');
%with movsum:
windowcount = movsum(abovethreshold, window, 'Endpoints', 'discard');
댓글 수: 0
Pawel Jastrzebski
2018년 3월 29일
편집: Pawel Jastrzebski
2018년 3월 29일
Consider the following code:
% FAKE DATA
x = rand(100,1)
% PARAMETERS
window = 50
threshold = 0.7
LoopCntr = length(x)-window+1
% PREALLOCATION
m = zeros(LoopCntr, window); % moving range
PksCntr = zeros(LoopCntr,1); % peak conter
RowNames = cell(1, LoopCntr); % row names for the table
ColNames = cell(1,window) ; % column names for the table
for i = 1:LoopCntr
% get your values for the moving range into a matrix
m(i,:) = x(i:i+window-1);
% check them for the conditions and count the peaks
PksCntr(i) = nnz(m(i,:)>threshold)
% get the names for rows and columns
RowNames(i) = {[num2str(i) '-' num2str(i+window-1)]};
ColNames(i) = {['V' num2str(i)]}
end
ColNames(end) = {'NoOfPeaks'};
% Convert matrixec to the table
tVal = array2table([m PksCntr]);
% add names to columns and rows
tVal.Properties.VariableNames = ColNames;
tVal.Properties.RowNames = RowNames;
Remarks:
- I'm not sure but chances are you can achieve the same thing without a loop
- Variables window and theshold might me the reserved words if you have any toolboxes installed (not sure)
- Storing everything in the table at the of the code is not necessary but it helps keep the data organised
댓글 수: 1
Guillaume
2018년 3월 29일
"I'm not sure but chances are you can achieve the same thing without a loop"
Of course, it can be done without a loop.
참고 항목
카테고리
Help Center 및 File Exchange에서 Electrophysiology에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!