diff options
Diffstat (limited to 'python/spectrum.py')
| -rw-r--r-- | python/spectrum.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/python/spectrum.py b/python/spectrum.py new file mode 100644 index 0000000..514092b --- /dev/null +++ b/python/spectrum.py @@ -0,0 +1,32 @@ +import sys +import librosa +import numpy +import scipy +import matplotlib.pyplot as plt + +if len(sys.argv) < 2: + print "python spectrum.py file.wav" + sys.exit() + +FILENAME = sys.argv[1] + +N_FFT = 2048 + +def read_audio_spectum(filename): + fs, x = scipy.io.wavfile.read(filename) + S = librosa.stft(numpy.transpose(x)[0], N_FFT) + S = numpy.log1p(numpy.abs(S)) + return S, fs + +a_spectrum, fs = read_audio_spectum(FILENAME) +a_spectrum = numpy.flipud(a_spectrum) + +print a_spectrum.shape + +fig = plt.figure(figsize=(5, int(len(a_spectrum[0])/100))) + +img = plt.imshow(a_spectrum) +img.set_cmap('hot') +plt.set_cmap('hot') +plt.axis('off') +plt.savefig(FILENAME + '.png', transparent=True, bbox_inches='tight', pad_inches=0) |
