Without toolboxes: select some threshold you deem acceptable, then simply do abs(x)>threshold
, which will give you a logical array to index into freq
, giving the frequencies above the threshold. For increased accuracy you can then do things like diff()
on the resulting array, and find neighbouring indices, then select the maximum value on those consecutive indices on that sequence as the "peak" there.
Alternatively you can sort()
the values, retain the indices of the maximum n
(in your case 2) values and index that into the frequency arrays. Again not a very robust method, but quick and dirty.
Combining the two above techniques you could iteratively lower the threshold as per the sorted array, then check for things as proximity of peaks, their prominence etc.
If you don't want to implement this all yourself, see below for a one-stop function to do this.
If you are willing to use the signal processing toolbox you can use findpeaks()
. This gives you indices, which you can then use to index your frequency array to get the desired frequencies. Alternatively use the peaks = findpeaks(data,x)
syntax to directly extract the x
location (frequencies in your case).