Skip to content

Instantly share code, notes, and snippets.

@0xfe
Created February 22, 2020 23:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0xfe/86a9c94f75bd06bde76cb6a612744d49 to your computer and use it in GitHub Desktop.
Save 0xfe/86a9c94f75bd06bde76cb6a612744d49 to your computer and use it in GitHub Desktop.
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