how to put range for findpeaks function

2 ビュー (過去 30 日間)
Omer hakan Yaran
Omer hakan Yaran 2022 年 6 月 28 日
回答済み: Pratyush Swain 2022 年 6 月 29 日
I have a frequency data from 0 Hz to 5 Hz but I want to find peaks only in between 0.2 Hz and 5 Hz. I have seen some people using the method below:
[pks,locs] = findpeaks(data(0:135))
But the problem is I am using two different vectors, one for the magnitute of the frequency and the other for the corresponding frequency. For example
0.0100 12.567
this row means a data point with 0.0100 Hz frequency and 12.567 magnitute. So I don't know the indices of 0.2 Hz or 5 Hz to put a range.


Star Strider
Star Strider 2022 年 6 月 28 日
One approach
f = linspace(0, 25, 250).'; % Frequency Vector
data = exp(-0.1*f) .* sin(2*pi*f*120); % Amplitude Vector
[pks,locs] = findpeaks(data);
DesiredVals = f(locs) >=0 & f(locs)<=5;
DesiredLocs = locs(DesiredVals);
Results = table(f(DesiredLocs), data(DesiredLocs), 'VariableNames',{'Frequency','Amplitude'})
Results = 3×2 table
Frequency Amplitude _________ _________ 0.50201 0.94951 2.6104 0.77011 4.7189 0.62103
plot(f, data)
hold on
plot(f(DesiredLocs), data(DesiredLocs), '^r')
hold off
Experiment with your data.

その他の回答 (1 件)

Pratyush Swain
Pratyush Swain 2022 年 6 月 29 日
As you have two vectors for frequency and magntude,you can use sorting vectors by same order,mentioned here -- sorting and also filter the indexes as per the given range requirement.
Note here findpeaks function expects the X-value(the frequency vector) to be strictly increasing hence the sorting needs to be carried out. Also the order of the steps involved here can be interchanged that is first the vectors can be filtered as per required range then sorted and vice-versa.
%taking an example frequency and magnitude array%
frequencies=[0.0 0.9 1.2 0.5 0.2 4.9 0.1];
magnitudes=[2 4 5 6 1 2 8];
%sort by order the frequencies and magnitudes%
[freqsorted,I] = sort(frequencies);
magnitudes = magnitudes(I);
%selected the indexes containing desired range of freq%
%now using the selected indexes obtain the final filtered freq array%
%similary filter out the magnitude array%
%finally filter out the peaks and their respective frequencies%
[pks,locs] = findpeaks(magnitudes,freqsorted)
Here we can take a look how the frequencies and magnitudes vector are ordered and filtered.
Result after calling findpeaks function by passing in the magnitudes and freqsorted vectors as input arguments.
You can go through the documentation reagarding peak func here Peaks.
Hope this helps.





Community Treasure Hunt

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

Start Hunting!

Translated by