Spectral Analysis of Audio Files using Python, Scipy and Matplotlib

Spectral analysis is a fundamental technique in the domain of audio processing, allowing us to examine the frequency content of a signal. In this article, we’ll demonstrate how to perform spectral analysis on WAV files using Python, Matplotlib, and Scipy’s wavfile module.

Prerequisites

Ensure you have the necessary libraries installed:

pip install numpy scipy matplotlib

Getting Started

Let’s begin by importing the necessary modules:

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

Reading the WAV File

Using wavfile.read(), you can easily extract the sample rate and data of a WAV file:

sample_rate, audio_data = wavfile.read('path_to_your_audio_file.wav')
audio_data = audio_data[:,0]  # use only one channel

Do note that wavfile.read() reads PCM-encoded files, meaning files with data encoded in integer values. For floating-point encoded WAV files, consider using other libraries like librosa.

Spectral Analysis

For spectral analysis, we’ll compute the Fast Fourier Transform (FFT) of the audio signal.

Often, we’re interested in observing specific frequency ranges, e.g., the human audible range (20 Hz – 20 kHz).

# Compute the FFT of the audio data
audio_fft = np.fft.fft(audio_data)

# Compute the frequencies associated with the FFT values
frequencies = np.fft.fftfreq(len(audio_fft), 1/sample_rate)

# Filter out only the positive frequencies
positive_freq_idxs = np.where(frequencies > 0)

plt.figure(figsize=(10, 4))
plt.plot(frequencies[positive_freq_idxs], np.abs(audio_fft[positive_freq_idxs]))
plt.xlabel('Frequency (Hz)')
plt.ylabel('Magnitude')
plt.title('Spectrum of Audio Signal')
plt.xscale('log')
plt.xlim([20, 20000]) 
plt.show()

Note: The use of a logarithmic scale on the x-axis makes certain features more visually apparent.

Using Spectrogram for Time-Frequency Analysis

A spectrogram provides a 2D representation, showing how frequencies evolve over time. It can be especially useful for analyzing non-stationary signals.

from scipy.signal import spectrogram

# Compute the spectrogram
frequencies, times, Sxx = spectrogram(audio_data, fs=sample_rate, nperseg=3000, noverlap=1024, window='hann')

plt.figure(figsize=(10, 4))
plt.pcolormesh(times, frequencies, 10 * np.log10(Sxx), shading='gouraud')
plt.colorbar(label='Intensity (dB)')
plt.ylabel('Frequency (Hz)')
plt.xlabel('Time (s)')
plt.title('Spectrogram')
plt.tight_layout()
plt.show()

The spectrogram provides a more granular view of how different frequency components evolve over time. This can be particularly useful for identifying transient events or understanding the temporal structure of an audio clip.

Conclusion

Spectral analysis is an essential tool in the audio processing arsenal. With Python and its powerful libraries like Matplotlib and Scipy, you can easily visualize and analyze the frequency content of audio signals. Whether you’re examining raw waveforms, frequency spectra, or time-frequency representations, Python provides a versatile platform to meet all your audio analysis needs.

Similar Posts

One Comment

Comments are closed.