Load an audio file and find the frequency

Question:

I have a following code:

rate, data = wav.read('C.wav')
Fourier = abs(fftpk.fft(data))

max = np.argmax(Fourier, axis=None, out=None)
print(max) # get 787


freq = fftpk.fftfreq(len(Fourier), (1.0/rate))

plt.plot(freq[range(len(Fourier)//2)], Fourier[range(len(Fourier)//2)])
plt.xlabel('Frequency (Hz)')
plt.ylabel('Amplitude')
plt.show()

I want this program to give me the frequency in Hz with the highest amplitude, but instead of getting value around 260, I get 787. I do not know what the problem is.

Plot of the file:

Plot of the file

Asked By: krolik2002

||

Answers:

np.argmax gives you the index of the maximum element in the Fourier frequency, not the actual frequency. The relation to obtain the frequency from the index is frequency = index*rate/len(Fourier). So, applying this in your case should give you the desired frequency:

max = np.argmax(Fourier, axis=None, out=None)
print(max) # get 787
maxfreq = max*rate/len(Fourier) # should give ~260
Answered By: SleuthEye
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.