Skip to content

Instantly share code, notes, and snippets.

@utahka
Created December 4, 2015 12:23
Show Gist options
  • Save utahka/b44bc73b8e39df5d5f38 to your computer and use it in GitHub Desktop.
Save utahka/b44bc73b8e39df5d5f38 to your computer and use it in GitHub Desktop.
Plot a log amplitude spectrum
import sys
import scipy.fftpack
import scipy.io.wavfile
import numpy as np
import matplotlib.pyplot as plt
if __name__ == "__main__":
wav_file = sys.argv[1]
f_s, x = scipy.io.wavfile.read(wav_file)
N = 2048
"""
T = N / f_s
where, T[sec] is window size.
"""
X = scipy.fftpack.fft(x[:N])
X = scipy.fftpack.fftshift(X) # リストの順を自然なものに変更
log_amp_spectrum = np.log10(np.abs(X))
freqs = scipy.fftpack.fftfreq(N, d=1/f_s)
freq_axis = scipy.fftpack.fftshift(freqs)
plt.plot(freq_axis[freq_axis.size//2:], log_amp_spectrum[freq_axis.size//2:])
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment