我正在尝试测试一个 Python 脚本,该脚本有望从 wav 文件中生成音频频谱图。我假设我需要输入一个 wav 的路径,但是我收到一个错误:IndexError: list index out of range 当我通过在此处输入它来尝试它时:
sr,x = scipy.io.wavfile.read('mySoundFile.wav')
我还尝试将路径作为命令行中的参数,但我没有做对。有什么帮助吗?
http://mail.python.org/pipermail/chicago/2010-December/007314.html
"""
Compute and display a spectrogram.
Give WAV file as input
"""
import matplotlib.pyplot as plt
import scipy.io.wavfile
import numpy as np
import sys
wavfile = sys.argv[1]
sr,x = scipy.io.wavfile.read('BeatBoy01.wav')
## Parameters: 10ms step, 30ms window
nstep = int(sr * 0.01)
nwin = int(sr * 0.03)
nfft = nwin
window = np.hamming(nwin)
## will take windows x[n1:n2]. generate
## and loop over n2 such that all frames
## fit within the waveform
nn = range(nwin, len(x), nstep)
X = np.zeros( (len(nn), nfft/2) )
for i,n in enumerate(nn):
xseg = x[n-nwin:n]
z = np.fft.fft(window * xseg, nfft)
X[i,:] = np.log(np.abs(z[:nfft/2]))
plt.imshow(X.T, interpolation='nearest',
origin='lower',
aspect='auto')
plt.show()