gpt4 book ai didi

python - 如何计算频谱图中给定频率的出现

转载 作者:行者123 更新时间:2023-12-03 02:37:59 24 4
gpt4 key购买 nike

我有一个这样的频谱图,我想总结一个给定频率的所有出现:

我试图概述最终没有意义的问题。我对此表示歉意。

好了,图片显示了我加载的wav文件的dB信息。图片是specgram方法的结果。

据我所知,从0.8秒开始,我可以看到4个主要功能。在7k Hz,4.5 kz,2.5 kz,900 hz中。好吧,它似乎在一段时间内保持不变,所以我想添加所有出现的情况

我现在的源代码就是这样。您可以看到我得到了一些频率信息,但这与图形值不对应(以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 frequencies between a range,问题是如何计算所有这些频率。

谢谢。

最佳答案

使用SO中这个问题的答案并将其转储到一个简单的矩阵中,我解决了这个问题,非常感谢您的帮助

关于python - 如何计算频谱图中给定频率的出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22819498/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com