# How to extract the dominant frequency from the audio WAV file using numpy

So, I recorded this audio on my phone while I was running a tone generator on my PC at a frequency of 13Khz, now I want to extract this frequency which is dominant from the recorded WAV file.

So, far I tried to read the wav file using scipy and then I tried to calculate FFT to get the frequency spectrum

Here's my code:

import numpy as np
from scipy.io import wavfile
import matplotlib.pyplot as plt

print(f"Sample rate: {samplerate}")

length = data.shape[0] / samplerate
print(f"length = {length}s")

fft_data = np.fft.fft(data)


Now, I want to extract the dominant frequency, 13 Khz in this case. How do I achieve this? Any help would be appreciated. Thanks

I found it after searching on lot of GitHub repositories

def extract_peak_frequency(data, sampling_rate):
fft_data = np.fft.fft(data)
freqs = np.fft.fftfreq(len(data))

peak_coefficient = np.argmax(np.abs(fft_data))
peak_freq = freqs[peak_coefficient]

return abs(peak_freq * sampling_rate)
$$$$
`
• Best to select this as the answer, then!
– Peter K.
Commented Jun 19, 2022 at 15:10