Understand Audio Amplitude Spectrogram and Compute it in Python – Python Tutorial

By | August 30, 2023

The audio amplitude spectrogram is a powerful tool for visualizing and understanding the frequency content and time-varying behavior of an audio signal. In this tutorial, we will discuss its effect and how to get it in python.

The effect of amplitude spectrogram

The amplitude spectrogram is a visual representation of the frequency of an audio signal over time. It provides valuable information about the distribution of energy across different frequencies at different points in time. The amplitude spectrogram is typically represented as a 2D plot, where the x-axis represents time, the y-axis represents frequency, and the color or intensity represents the amplitude or energy of the signal at each point.

How to compute it in python?

In order to get amplitude spectrogram, we can apply the Short-Time Fourier Transform (STFT) to the audio signal, then compute the absolute value of the STFT coefficients to get it.

In python, we can use librosa package to do it.

Here is an example:

Step 1: get audio data

import librosa
import numpy as np

wave_file = "music-jamendo-0039.wav"

wave_data, sr = librosa.load(wave_file, sr = 8000, mono=True)
print(wave_data.shape)

Here wave_data is singal data of wav file “music-jamendo-0039.wav”, it is (2560416,)

Step 2: apply stft on audio data

#apply stft
n_fft = 1024
win_length = 1024
hop_length = 256
s = librosa.stft(wave_data, n_fft=n_fft, window='hann', win_length=win_length, hop_length=hop_length)
print(s)

In this code, we have used librosa.stft() function, you can read this tutorial to understand how to use it.

Understand librosa.stft() with Examples – Librosa Tutorial

Here s is:

[[ 1.5120709e-01+0.00000000e+00j  1.9087272e+00+0.00000000e+00j
   2.5030863e+00+0.00000000e+00j ... -7.6589012e-04+0.00000000e+00j
   6.4973207e-04+0.00000000e+00j -2.0434251e-03+0.00000000e+00j]
 [ 1.3417800e-01+7.55372867e-02j -6.3729626e-01+1.61460042e+00j
  -2.1279929e+00-7.68266082e-01j ... -3.0256985e-03+1.83317089e-03j
   1.6637504e-03+1.16395968e-05j -3.6061599e-04-2.80110538e-03j]
 [ 8.5972451e-02+1.37072265e-01j -8.6581826e-01-1.31431186e+00j
   1.1379051e+00+2.19129181e+00j ...  1.0891910e-02-5.39325934e-04j
  -6.6563962e-03-4.16032597e-03j  2.9073772e-03+6.52559055e-03j]
 ...
 [2.7540791e-05 1.8072098e-04 2.5519845e-04 ... 3.8825619e-06
  1.6179248e-06 1.4675795e-05]
 [2.0832442e-05 9.4623312e-05 1.6873896e-04 ... 2.3047669e-06
  4.0169120e-06 1.7150585e-05]
 [2.1858445e-05 4.0249666e-05 1.3417416e-04 ... 6.6585130e-08
  8.8313317e-07 1.6225922e-05]]

Step 3: compute amplitude spectrogram

#get amplitude spectrogram
amp = np.abs(s)
print(amp)

It is:

[[1.5120709e-01 1.9087272e+00 2.5030863e+00 ... 7.6589012e-04
  6.4973207e-04 2.0434251e-03]
 [1.5397927e-01 1.7358229e+00 2.2624292e+00 ... 3.5377063e-03
  1.6637911e-03 2.8242229e-03]
 [1.6180256e-01 1.5738668e+00 2.4691269e+00 ... 1.0905255e-02
  7.8495806e-03 7.1439608e-03]
 ...
 [2.7540791e-05 1.8072098e-04 2.5519845e-04 ... 3.8825619e-06
  1.6179248e-06 1.4675795e-05]
 [2.0832442e-05 9.4623312e-05 1.6873896e-04 ... 2.3047669e-06
  4.0169120e-06 1.7150585e-05]
 [2.1858445e-05 4.0249666e-05 1.3417416e-04 ... 6.6585130e-08
  8.8313317e-07 1.6225922e-05]]

How to display amplitude spectrogram?

We can use code below to display amplitude spectrogram.

#display amplitude spectrogram
import librosa.display
from matplotlib import pyplot as plt
plt.figure(figsize=(10, 5))
librosa.display.specshow(librosa.amplitude_to_db(amp, ref=np.max), y_axis='linear', x_axis='time', sr=sr)
plt.show()

Run this code, we will see:

How to display amplitude spectrogram