我使用以下SingleTone.wav
命令生成一个音频文件sox
。
sox -n SingleTone.wav synth 10 sin 525
然后我执行以下程序对这种音调的样本执行fft。
import matplotlib.pyplot as plt
from scipy.fftpack import fft, rfft
from scipy.io import wavfile
from sys import argv
# FFT sample count
N = 8192
# Sampling frequency
samples_per_second = 44100
# Frequency resolution
freq_resolution = samples_per_second / N
fs, data = wavfile.read(argv[1])
a = data.T
b=[(ele/2**8.)*2-1 for ele in a]
c = fft(b, N)
d = len(c)/2 - 1
frequencies = [x*freq_resolution for x in xrange(d)]
print '\n'.join(",".join([str(f),str(x)]) for f, x in zip(frequencies, abs(c[:d])))
plt.plot(frequencies, abs(c[:d]),'r')
plt.show()
我得到的输出是一个440 Hz至455 Hz的频率尖峰,而不是我期望的525 Hz
。
造成差异的原因是什么?
有效答案将指向对FFT的误解,代码中的错误或与设置有关的任何不正确内容。
答案 0 :(得分:1)
尝试以下操作:使用wav文件中的采样率,为方便起见并减少错误,请使用fft库提供的频率列表。
这是上面的代码,
#!/usr/bin/python
import matplotlib.pyplot as plt
from scipy.fftpack import fft, rfft, fftfreq, rfftfreq
from scipy.io import wavfile
from sys import argv
samples_per_second, data = wavfile.read(argv[1])
# FFT sample count
N = 8192
a = data.T
b=[(ele/2**8.)*2-1 for ele in a]
c = fft(b, N)
d = len(c)/2 - 1
frequencies = fftfreq(N,1./samples_per_second)
#print '\n'.join(",".join([str(f),str(x)]) for f, x in zip(frequencies[:d], abs(c[:d])))
plt.plot(frequencies[:d], abs(c[:d]),'r')
plt.show()