Number of peaks in the interval

조회 수: 6(최근 30일)
Lev Mihailov
Lev Mihailov 2022년 8월 15일
답변: Image Analyst 2022년 8월 15일
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!

채택된 답변

Star Strider
Star Strider 2022년 8월 15일
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.
  댓글 수: 3
Star Strider
Star Strider 2022년 8월 15일
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.

댓글을 달려면 로그인하십시오.

추가 답변(2개)

Benjamin Thompson
Benjamin Thompson 2022년 8월 15일
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 2022년 8월 15일
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


Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by