我有一个这样的频谱图,我想总结给定频率的所有出现:
我试图勾勒出最终毫无意义的问题。我为此道歉。
好吧,图片显示了我加载的 wav 文件的 dB 信息。图片是specgram方法的结果。
据我所知,从 0.8 秒开始,我可以看到 4 个主要特征。在 7k 赫兹、4.5 赫兹、2.5 赫兹、900 赫兹。好吧,看起来它在这段时间内保持不变,所以我想添加所有这些事件
我现在的源代码是这样的。您可以看到我得到了一些频率信息,但这与图形值不对应(在间隔 7k Hz、4.5 kz、2.5 kz、900 hz 中)
for i in range(0, int(RATE / CHUNK_SIZE * RECORD_SECONDS)):
# little endian, signed shortdata_chunk
data_chunk = array('h', stream.read(CHUNK_SIZE))
if byteorder == 'big':
data_chunk.byteswap()
data_all.extend(data_chunk)
# Take the fft and square each value
fftData=abs(np.fft.rfft(data_chunk))**2
# find the maximum
which = fftData[1:].argmax() + 1
# use quadratic interpolation around the max
if which != len(fftData)-1:
print "which %f and %f." % (which,which)
y0,y1,y2 = np.log(fftData[which-1:which+2:])
x1 = (y2 - y0) * .5 / (2 * y1 - y2 - y0)
# find the frequency and output it
#== THIS IS NOT the real freq ======================================
#== How can I get the values of the freq ???========================
thefreq = (which+x1)*RATE/CHUNK_SIZE
print "The freq is %f Hz. and %d" % (thefreq,int(thefreq) )
else:
thefreq = which*RATE/CHUNK_SIZE
print "The freq is %f Hz." % (thefreq)
Fs = 16000
f = np.arange(1, 9) * 2000
t = np.arange(RECORD_SECONDS * Fs) / Fs
x = np.empty(t.shape)
for i in range(8):
x[i*Fs:(i+1)*Fs] = np.cos(2*np.pi * f[i] * t[i*Fs:(i+1)*Fs])
w = np.hamming(512)
Pxx, freqs, bins = mlab.specgram(data_all, NFFT=512, Fs=Fs, window=w,
noverlap=464)
#plot the spectrogram in dB
Pxx_dB = np.log10(Pxx)
pyplot.subplots_adjust(hspace=0.4)
pyplot.subplot(311)
ex1 = bins[0], bins[-1], freqs[0], freqs[-1]
pyplot.imshow(np.flipud(Pxx_dB), extent=ex1)
#pyplot.axis('auto')
pyplot.axis('tight')
pyplot.xlabel('time (s)')
pyplot.ylabel('freq (Hz)')
#== EXTRA LOG ======================================
print ("The max number is >>>>>", np.max(Pxx), " - ", np.max(bins))
Pxx_dB = np.log10(Pxx)
print ("The max number is >>>>>", np.max(Pxx_dB))
np.savetxt("./tmp__PXX", Pxx, fmt = '%f')
np.savetxt("./tmp__PXX_dB", Pxx_dB, fmt = '%f')
pyplot.show()
我想做这样的事情,你可以在另一个问题中找到 Removing specific frequency between a range,问题是我如何计算所有这些频率。
谢谢你。