0
$\begingroup$

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

samplerate, data = wavfile.read("13khz.wav")
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

$\endgroup$

1 Answer 1

2
$\begingroup$

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)
```
$\endgroup$
1
  • 1
    $\begingroup$ Best to select this as the answer, then! $\endgroup$
    – Peter K.
    Jun 19, 2022 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.