MATLAB: Isolating k fourier peaks

audiofourier transformprocessingsignals

I have imported an audio file of a musical triad (three notes) and then taken its fourier transform to find the fundamental frequencies of the chord. From this transform, I would like to automatically select the three highest peaks in magnitude and determine where they fall on the x (frequency) axis.
My initial thought was to utilize maxk, but this obviously chooses the three highest points overall, even if they are on the same "peak".
Ultimately, I would like the return to be a vector (or array) with the three most prominent frequencies (and their magnitude, if it's an array).
clear all
%import file
[x,Fs] = audioread('eqt-CMajor.wav');
sound(x,Fs);
T = 1/Fs;
L = length(x);
t = (0:L-1)*T;
%change two channel input to one channel output
xMono = sum(x, 2) / size(x, 2);
%fourier transform and plot
Y = fft(xMono);
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
f = Fs*(0:(L/2))/L;
figure(2)
plot(f,P1)
%attempt to identify frequency of the three highest magnitude peaks
[M, Hz] = maxk(f,3,'ComparisonMethod','abs')

Best Answer

Use the Signal Processing Toolbox findpeaks function, with the name-value pair arguments SortStr and NPeaks.