Skip to content

Instantly share code, notes, and snippets.

@0xfe
Created Feb 22, 2020
Embed
What would you like to do?
Python Spectrogram
DefaultConfig = Config(
rows=513,
cols = 90,
s_nperseg = 256,
s_nfft = 1024,
s_noverlap = 200,
resample = 16000)
def spectrogram(file, config=DefaultConfig):
fs, data = wavfile.read(file)
if config.resample > 0:
number_of_samples = round(len(data) * float(config.resample) / fs)
data = signal.resample(data, number_of_samples)
fs = config.resample
f, t, Sxx = signal.spectrogram(data, fs,
window=('hann'),
nperseg=config.s_nperseg,
nfft=config.s_nfft,
noverlap=config.s_noverlap,
mode='magnitude')
plt.pcolormesh(t, f, Sxx)
plt.ylabel('Frequency (Hz)')
plt.xlabel('Time (sec)')
plt.show()
print(Sxx.shape)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment