Number of peaks in the interval

5 views (last 30 days)
There is a long vector (20002 values long) that I need to split into intervals (100 each) and find out the number of peaks in this interval
% P3 may data
step = 100;
for i = 1:size(T,2)-step
i1 = P3(i:i+step);
[psk(i),length(locsI(i))] = findpeaks(i1,'MinPeakHeigh',-0.55,'MinPeakDistance',5);
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
Error in DataLen (line 2146)
[psk(i),length(locsI(i))] = findpeaks(i1,'MinPeakHeigh',-0.55,'MinPeakDistance',5);
I tried this code, but it shows this error
Thank you in advance!

Accepted Answer

Star Strider
Star Strider on 15 Aug 2022
A simpler way (if you have the Signal Processing Toolbox) would be to use the buffer function to segment the vector. Then, simply index into its columns with findpeaks, saving them as cell arrays —
I1bfr = buffer(i1, 100);
for k = 1:size(i1bfr,2)
[psk{i},locs{i}] = findpeaks(i1bfr(:,k),'MinPeakHeigh',-0.55,'MinPeakDistance',5);
I created a function that will do the most basic result of buffer and will post it if you need it.
Star Strider
Star Strider on 15 Aug 2022
I generally use ‘k’ asd a loop counter and I overlooked that. Changing it to ‘i’
i1 = randn(1, 20002); % Create Vector
i1bfr = buffer(i1, 100);
for i = 1:size(i1bfr,2)
[psk{i},locs{i}] = findpeaks(i1bfr(:,i),'MinPeakHeight',-0.55,'MinPeakDistance',5);
Check_col = randi(size(i1bfr,2)) % Random Column
Check_col = 56
Check_psk = psk{Check_col}
Check_psk = 13×1
3.2909 0.3846 2.4034 0.9977 1.3251 1.4506 0.7487 -0.0439 1.2453 1.4611
Check_locs = locs{Check_col}
Check_locs = 13×1
4 11 18 28 35 42 48 54 60 72
That should work.

Sign in to comment.

More Answers (2)

Benjamin Thompson
Benjamin Thompson on 15 Aug 2022
Use MinPeakHeight as the parameter name for findpeaks. You do not show the definition of locsI or psk in your sample code.

Image Analyst
Image Analyst on 15 Aug 2022
Twenty thousand is not a long vector. You can use findpeaks on the whole thing and split it up later. This will avoid any "edge effects" from where you split your vector.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
save('answers.mat', 'T', 'p3');
And, as Star showed, you can't use length in the output argument list. And you need to use cell arrays (use braces instead of parentheses). See the FAQ:
Corrected code
[psk{i}, locsI{i} = findpeaks(i1,'MinPeakHeight',-0.55,'MinPeakDistance',5);
If you need the length of it (but it does not look like you do), then compute it afterwards

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!