np.fft.fftfreq告诉您与系数相关的频率:
import numpy as npx = np.array([1,2,1,0,1,2,1,0])w = np.fft.fft(x)freqs = np.fft.fftfreq(len(x))for coef,freq in zip(w,freqs): if coef: print('{c:>6} * exp(2 pi i t * {f})'.format(c=coef,f=freq))# (8+0j) * exp(2 pi i t * 0.0)# -4j * exp(2 pi i t * 0.25)# 4j * exp(2 pi i t * -0.25)
OP询问如何找到以赫兹为单位的频率。我相信公式是
frequency (Hz) = abs(fft_freq * frame_rate)。
这是一些证明这一点的代码。
首先,我们制作一个440 Hz的波形文件:
import mathimport waveimport structif __name__ == '__main__': # http://stackoverflow.com/questions/3637350/how-to-write-stereo-wav-files-in-python # http://www.sonicspot.com/guide/wavefiles.html freq = 440.0 data_size = 40000 fname = "test.wav" frate = 11025.0 amp = 64000.0 nchannels = 1 sampwidth = 2 framerate = int(frate) nframes = data_size comptype = "NONE" compname = "not compressed" data = [math.sin(2 * math.pi * freq * (x / frate)) for x in range(data_size)] wav_file = wave.open(fname, 'w') wav_file.setparams( (nchannels, sampwidth, framerate, nframes, comptype, compname)) for v in data: wav_file.writeframes(struct.pack('h', int(v * amp / 2))) wav_file.close()
这将创建文件
test.wav。现在我们读取数据,对其进行FFT,找到具有最大功率的系数,并找到相应的fft频率,然后转换为赫兹:
import waveimport structimport numpy as npif __name__ == '__main__': data_size = 40000 fname = "test.wav" frate = 11025.0 wav_file = wave.open(fname, 'r') data = wav_file.readframes(data_size) wav_file.close() data = struct.unpack('{n}h'.format(n=data_size), data) data = np.array(data) w = np.fft.fft(data) freqs = np.fft.fftfreq(len(w)) print(freqs.min(), freqs.max()) # (-0.5, 0.499975) # Find the peak in the coefficients idx = np.argmax(np.abs(w)) freq = freqs[idx] freq_in_hertz = abs(freq * frate) print(freq_in_hertz) # 439.8975
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)