Getting Frequencies Corresponding to Peaks In FFT Plot

Hey everyone,
So I'm currently working on a project where I need to find the frequencies for specific peaks in my FFT plot of an audio file. However, I'm not sure how to actually get the frequencies that correspond to the peaks I'm interested in.
This is my code: https://pastebin.com/zZW8B5Th
And this is a picture of my FFT plot: https://imgur.com/0ajtful
Essentially, I'm trying to construct a Fourier Series from the signal. Although I know how to find the coefficients of the sine/cosine terms, I'm just a little confused on how to find the frequency.
Thanks everyone!

 Accepted Answer

Use the findpeaks (link) function to determine the frequencies and amplitudes of the peaks.
Note that the correct implementation of your fft call is:
X = X(1:length(y)/2+1)/length(y);
That will produce the correct amplitudes.

4 Comments

Wow, thanks! I was quite worried before because I was getting absolutely massive magnitudes - to the tune of 6000!!!!
Just before I accept your answer, I had a question regarding the findpeaks. I looked through the documentation, and the examples seem to point out that it returns the y-value (in this case, the magnitudes) of the peaks. While I do need this, I also need the corresponding x-value of the peaks, as I need to know their frequencies.
Thanks!
My pleasure!
If you give your frequency vector as the second argument to findpeaks, the second output ‘locs’, will be in terms of frequency. See the documentation example Find Peaks with Minimum Separation (link) for details.
Note that you must specify the data and frequency vectors to be the same ones you used in your plot call, so in your code:
[pks,frqs] = findpeaks(abs(X),freq);
will produce a vector of amplitudes, and a corresponding vector of their frequencies.
Include other name-value pair arguments to findpeaks to get the result you want.
Perfect!!!!
I found an alternative method to do so - using the 'Find Peaks with Minimum Separation' to display the location of the maxima (I don't need a billion terms in my Fourier Series), and then using the Data Cursor tool to click on the peaks to find their corresponding x and y values :)))
Ooh also - someone else pointed out to me that i should double the magnitudes since I'm moving from a 2-sided spectrum to a 1-sided spectrum.
I really can't thank you enough for your help. I'm just a high school senior, so I really didn't know much about this!
As always, my pleasure!
In my last Comment I linked you to ‘Find Peaks with Minimum Separation’ as well. I added the normalisation by length, although forgot to mention multiplying the 1-sided amplitudes by 2. (Not quite fully awake then.)
I envy you. I wish I’d had computers and MATLAB in high school!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!