Created
February 22, 2020 23:58
-
-
Save 0xfe/86a9c94f75bd06bde76cb6a612744d49 to your computer and use it in GitHub Desktop.
Python Spectrogram
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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