Findpeaks the reason why my index is floating
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Life is Wonderful
am 3 Mai 2023
Kommentiert: Star Strider
am 3 Mai 2023
Hi there,
I have a question about the findpeaks function. To my surprise, my location index is receiving a float, which is causing all of my calculations to be incorrect.
Please assist me in fixing the coding error. Expectation is that the peak value's position should be an integer number.
dctCoef = [8429.999786
2771.087211
607.721985
3118.100821
1112.991245
1013.033319
350.156147
368.204981
822.470058
971.052424
1472.593343
406.350435
893.834342
612.080075
-13.394000
138.237194
61.093121
127.999481
222.903100
313.873293];
minHeigth = mean(round(abs(dctCoef / 2.^8))) + std(round(abs(dctCoef / 2.^8)))*8e-1;
[pks,locs] = findpeaks(round(abs(dctCoef / 2.^8)),20,'MinPeakHeight',minHeigth);
locs
pks
% Manual analysis
fprintf('%20s|%20s|%20s|\n--------------------+--------------------+--------------------+\n', ...
'indx','raw_data', 'scaled_data');
for i = 1:size(dctCoef,1)
fprintf('%20d|%20.6f|%20.6f|\n',i,dctCoef(i), round(abs(dctCoef(i)/ 2.^8)))
end
2 Kommentare
Stephen23
am 3 Mai 2023
You are supplying the sample rate as the 2nd input: "If you specify a sample rate, Fs, then locs is a numeric vector of time instants with a time difference of 1/Fs between consecutive samples."
Your example seems to be consistent with the documentation.
Akzeptierte Antwort
Star Strider
am 3 Mai 2023
The findpeaks function is interpreting the ‘20’ argument as a sampling frequency, and so is returning the position of the peak as the coordinate of the signal sampled at that frequency (20 cycles per x-coordinate unit).
Remove the ‘20’ and you get the result you expect —
dctCoef = [8429.999786
2771.087211
607.721985
3118.100821
1112.991245
1013.033319
350.156147
368.204981
822.470058
971.052424
1472.593343
406.350435
893.834342
612.080075
-13.394000
138.237194
61.093121
127.999481
222.903100
313.873293];
minHeigth = mean(round(abs(dctCoef / 2.^8))) + std(round(abs(dctCoef / 2.^8)))*8e-1;
[pks,locs] = findpeaks(round(abs(dctCoef / 2.^8)),'MinPeakHeight',minHeigth);
locs
pks
% Manual analysis
fprintf('%20s|%20s|%20s|\n--------------------+--------------------+--------------------+\n', ...
'indx','raw_data', 'scaled_data');
for i = 1:size(dctCoef,1)
fprintf('%20d|%20.6f|%20.6f|\n',i,dctCoef(i), round(abs(dctCoef(i)/ 2.^8)))
end
.
2 Kommentare
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!