Blog‎ > ‎

Spectrogram of Bird Sound from Xeno-Canto

posted Jun 25, 2019, 1:28 AM by MUHAMMAD MUN`IM AHMAD ZABIDI   [ updated Jun 25, 2019, 2:26 AM ]
The following code download the sound of the Asian Koel from Xeno-Canto in MP3 format, converts it to WAV, downmixes the stereo to mono then plots the spectrogram.
You must have pydub installed to convert MP3 to WAV.

import urllib.request
from pydub import AudioSegment
import scipy
import matplotlib.pyplot as plt

sound = AudioSegment.from_mp3('temp.mp3')
sound.export('temp.wav', format='wav')
sample_rate, audiodata ='temp.wav')
print (sample_rate, audiodata.shape )
if True: # mixdown stereo to mono
    X = audiodata[:,0]/2 + audiodata[:,1]/2
    plt.specgram(X, Fs=sample_rate, xextent=(0,30))
else:    # separate plots for each channel
    L = audiodata[:,0]
    R = audiodata[:,1]
    plt.specgram(L, Fs=sample_rate, xextent=(0,30))
    plt.specgram(R, Fs=sample_rate, xextent=(0,30))


44100 (1097856, 2)
/home/raden/anaconda3/lib/python3.7/site-packages/matplotlib/axes/ RuntimeWarning: divide by zero encountered in log10
  Z = 10. * np.log10(spec)

Ignore the warnings. They appear because some of the data elements in the wave file are zero, and the spectrograms compute log10.

Here's the time domain stereo signal fro Xeno-Canto.