# Findpeaks the reason why my index is floating

5 Ansichten (letzte 30 Tage)
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
locs = 0.1500
pks
pks = 12
% Manual analysis
fprintf('%20s|%20s|%20s|\n--------------------+--------------------+--------------------+\n', ...
'indx','raw_data', 'scaled_data');
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
1| 8429.999786| 33.000000| 2| 2771.087211| 11.000000| 3| 607.721985| 2.000000| 4| 3118.100821| 12.000000| 5| 1112.991245| 4.000000| 6| 1013.033319| 4.000000| 7| 350.156147| 1.000000| 8| 368.204981| 1.000000| 9| 822.470058| 3.000000| 10| 971.052424| 4.000000| 11| 1472.593343| 6.000000| 12| 406.350435| 2.000000| 13| 893.834342| 3.000000| 14| 612.080075| 2.000000| 15| -13.394000| 0.000000| 16| 138.237194| 1.000000| 17| 61.093121| 0.000000| 18| 127.999481| 0.000000| 19| 222.903100| 1.000000| 20| 313.873293| 1.000000|
##### 2 KommentareKeine anzeigenKeine ausblenden
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.
Life is Wonderful am 3 Mai 2023
Thank you for helping me out

Melden Sie sich an, um zu kommentieren.

### 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
locs = 4
pks
pks = 12
% Manual analysis
fprintf('%20s|%20s|%20s|\n--------------------+--------------------+--------------------+\n', ...
'indx','raw_data', 'scaled_data');
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
1| 8429.999786| 33.000000| 2| 2771.087211| 11.000000| 3| 607.721985| 2.000000| 4| 3118.100821| 12.000000| 5| 1112.991245| 4.000000| 6| 1013.033319| 4.000000| 7| 350.156147| 1.000000| 8| 368.204981| 1.000000| 9| 822.470058| 3.000000| 10| 971.052424| 4.000000| 11| 1472.593343| 6.000000| 12| 406.350435| 2.000000| 13| 893.834342| 3.000000| 14| 612.080075| 2.000000| 15| -13.394000| 0.000000| 16| 138.237194| 1.000000| 17| 61.093121| 0.000000| 18| 127.999481| 0.000000| 19| 222.903100| 1.000000| 20| 313.873293| 1.000000|
.
##### 2 KommentareKeine anzeigenKeine ausblenden
Life is Wonderful am 3 Mai 2023
Thank you so much for your invaluable help..
Star Strider am 3 Mai 2023
As always, my pleasure!

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Descriptive Statistics finden Sie in Help Center und File Exchange

R2023a

### Community Treasure Hunt

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

Start Hunting!

Translated by