我查看了一些文档,包括this链接。我还发现了一些漂浮在互联网上的代码,这些代码允许我使用wav文件来生成一些非常酷的频谱图。这是代码...
"""Generate a Spectrogram image for a given WAV audio sample.
A spectrogram, or sonogram, is a visual representation of the spectrum
of frequencies in a sound. Horizontal axis represents time, Vertical axis
represents frequency, and color represents amplitude.
"""
import os
import wave
import pylab
def graph_spectrogram(wav_file):
sound_info, frame_rate = get_wav_info(wav_file)
pylab.figure(num=None, figsize=(19, 12))
pylab.subplot(111)
pylab.title('spectrogram of %r' % wav_file)
(spect, freqs, time, im) = pylab.specgram(sound_info, Fs=frame_rate)
print "size of freqs", len(freqs)
#print "freqs",freqs
print "size of spect", len(spect)
print "size of spect[0]", len(spect[0])
print "size of spect[1]", len(spect[1])
#print "spect",spect
print "size of time", len(time)
#print "time",time
print "im",im
pylab.savefig('spectrogram.png')
def get_wav_info(wav_file):
wav = wave.open(wav_file, 'r')
frames = wav.readframes(-1)
sound_info = pylab.fromstring(frames, 'Int16')
frame_rate = wav.getframerate()
wav.close()
return sound_info, frame_rate
if __name__ == '__main__':
wav_file = 'output.wav'
graph_spectrogram(wav_file)
你可以看到我已经打印出specgram返回的内容,希望能够理解它,但我仍然很丢失。频率的大小始终为129,但在图像上,它在频率轴上的分辨率似乎更高,范围从0Hz到20000Hz。为什么我不断为频率数组获得129的长度?时间数组是有意义的,因为它根据我的wav文件的长度而变化。 Spect阵列只是一个二维数组,对应于频率和时间。感谢您的帮助,如果有人看到我可以更好地编写代码的方式,请告诉我。如果有人对这段代码的实际作用有一个很好的解释,我也会很感激帮助。我对pylab非常不熟悉。再次感谢。