MATLAB: Find Peaks in Frequency & Amplitude

  • MATLAB
  • Thread starter Ophyron
  • Start date
  • Tags
    Matlab
In summary, The conversation is about using Matlab to perform FFT on .wav files and graph the frequency and amplitude. The code provided is used to generate graphs and the user is seeking help on finding the frequency and amplitude of peaks in the first 5000Hz range with a minimum amplitude of 20dB. Suggestions are given on using logical indexing and the findpeaks command to achieve this.
  • #1
Ophyron
1
0
hi,

i'm pretty new to MATLAB - but I've been using it to perform FFT's on .wav files to graph frequency and amplitude. I've been using the following code to generate the graphs:
Matlab:
[h,Fs] = wavread('x.wav');

H = fft(h);
N = length(h);
f = (0:(N-1))*(Fs/N);
t = (0:(N-1))/Fs;

figure(1);

clf;
plot(f,20*log10(abs(H)));

grid;

xlabel('Frequency (Hz)');
ylabel('Amplitude (dB)');

is there any way i can find the frequency at which peaks appear, and the amplitude of these peaks with code? I'm only really interested in the peaks in the first 5000Hz or so, above about 20dB. anyone got any ideas? any pointers would be much appreciated! thanks

take care

Nick
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
When you say peaks, do you mean the events in time that are the loudest or the places on a FFT plotted frequency spectrum where certain frequency have higher incidences.

In the case of the second example, you could programmatically walk through the array of frequencies and list only those above the 20db mark and zero the others so that they show up more clearly in your plot.

Here's more on using the FFT function:

https://www.mathworks.com/help/matlab/ref/fft.html?searchHighlight=fft&s_tid=doc_srchtitle
 
  • #3
This may not be a complete specification of what you mean by "peak", but you can write a vector statement that picks out points that are greater than their immediate neighbors to either side.

First of all, your H is complex. You probably want the amplitude X = abs(H);

OK, now there's a really useful trick in Matlab called logical indexing. If you have a vector I of 1's and 0's the same length as X, then X(I) will pick out the elements corresponding to the 1's. Of course, you probably want the locations of those elements for your question. find(I) will do that.

OK, so now I'm going to build a statement which will generate such a vector of 1's and 0's. I'll append the value ##+\infty## to each end of X so that the peaks don't occur there. The expression X > [X(2:end),inf] compares each element of X to the element to its immediate right. The insertion of inf is to pad out the second vector to the right length, and choosing the value ##\infty## as I said guarantees that the comparison will fail at the endpoint.

Finally, I'll use element-wise AND (a single ampersand) to combine this comparison with the one in the other direction. Thus, the whole statement is this:

Peaks = find( (X > [X(2:end), inf]) & (X > [inf, X(1:end-1)] );

As I said, you might decide you need to add other criteria to define your peaks, also connected with "&".

Disclaimer: I don't have Matlab on this computer to test that I got all the syntax right.
 
Last edited:
  • Like
Likes jedishrfu
  • #4
The command find(max()) will return the indices of the max of an array.
 
  • #5
First you need to unwrap your fft with fftshift. Then you can use the findpeaks command to find and label peaks. You can then compare to a threshold that you set so as to ignore small peaks.
 
  • #6
Check out the findpeaks command here.
 

1. How can I find the peaks in frequency and amplitude using MATLAB?

To find the peaks in frequency and amplitude using MATLAB, you can use the findpeaks function. This function takes in a signal or data set as input and returns the locations and values of the peaks. You can then use the returned values to plot the peaks on a frequency and amplitude graph.

2. Can I adjust the sensitivity of the peak detection in MATLAB?

Yes, you can adjust the sensitivity of the peak detection in MATLAB by specifying the MinPeakHeight and MinPeakDistance parameters in the findpeaks function. The MinPeakHeight parameter sets the minimum height threshold for a peak to be detected, while the MinPeakDistance parameter sets the minimum distance between two peaks for them to be considered separate peaks.

3. How can I plot the identified peaks on a graph in MATLAB?

After using the findpeaks function to identify the peaks in frequency and amplitude, you can use the plot function in MATLAB to plot the identified peaks on a graph. You can specify the peak locations and values as the input for the plot function to plot the peaks on the graph.

4. Can I save the identified peaks as a separate data set in MATLAB?

Yes, you can save the identified peaks as a separate data set in MATLAB by using the findpeaks function with the output argument. This will return the peak locations and values as separate arrays, which you can then save as a data set using the save function in MATLAB.

5. Is there a way to automatically label the identified peaks in MATLAB?

Yes, you can use the text function in MATLAB to automatically label the identified peaks on a graph. This function allows you to place text labels at specific locations on a graph, which you can specify as the peak locations returned by the findpeaks function. You can also customize the labels by specifying the font, size, and color.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
746
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
Replies
5
Views
362
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
826
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
5K
Back
Top