numpy – How to plot Spectrogram using STFT in python?

numpy – How to plot Spectrogram using STFT in python?

what you can do is use this

first method:

import scipy.io.wavfile as wav
import scipy.signal as signal
from matplotlib import pyplot as plt

sample_rate, samples = wav.read(filename)
f, t, Zxx = signal.stft(samples, fs=sample_rate)
plt.pcolormesh(t, f, np.abs(Zxx), cmap=cmap)

or the second method:

plt.specgram(samples, cmap=cmap, Fs=sample_rate)

I found the second one have a better visualization effect.
Not sure how to make the first one look as good as the second one though.

import os
import librosa
import librosa.display
import IPython.display as ipd
import numpy as np
import matplotlib.pyplot as plt

filename = <yourfile name/ location>
x, sr = librosa.load(filename)

import librosa.display                   #explicitly import librosa.display
X = librosa.stft(x)                      #perform short-term fourier transfrom               
Xdb = librosa.amplitude_to_db(abs(X))    #convert an amplitude spectrogram to dB-scaled spectrogram.
plt.figure(figsize=(15, 5))              #initialize the fig size
librosa.display.specshow(Xdb, sr=sr, x_axis=time, y_axis=hz)
plt.colorbar()```

numpy – How to plot Spectrogram using STFT in python?

Leave a Reply

Your email address will not be published.