find frequencies from audio file in python

To find frequencies from an audio file in Python, we first need to perform some audio processing techniques such as FFT (Fast Fourier Transform). NumPy provides a convenient way to perform FFT on audio data. Here is an example code that reads an audio file and returns the frequency spectrum.

main.py
import numpy as np
import wave

# Open the WAV file
with wave.open('audio_file.wav', 'rb') as wav_file:
    # Get the number of frames, channels and sample rate
    num_frames = wav_file.getnframes()
    num_channels = wav_file.getnchannels()
    sample_rate = wav_file.getframerate()
    
    # Read the audio frames
    wav_frames = wav_file.readframes(num_frames)
    wav_frames = np.frombuffer(wav_frames, dtype=np.int16)
    
    # Apply FFT to each channel
    fft_frames = []
    for channel in range(num_channels):
        # Get the channel data
        channel_data = wav_frames[channel::num_channels]
        # Apply FFT on the channel data
        fft_data = np.fft.rfft(channel_data)
        fft_frames.append(fft_data.real)
    
    # Get the frequency axis
    freq_axis = np.fft.rfftfreq(num_frames, d=1/sample_rate)
    
# The FFT frames contain the frequency spectra for each channel
print(fft_frames)
919 chars
29 lines

In this code, we first open the audio file using the wave module. We then read the audio frames using wave.readframes() and convert the byte array to NumPy array using np.frombuffer().

We then apply FFT to each channel using np.fft.rfft(). The resulting FFT data is stored in fft_frames. We also obtained the frequency axis using np.fft.rfftfreq().

We can then use the resulting fft_frames and freq_axis to extract relevant frequency information from the audio file.

gistlibby LogSnag