简体   繁体   English

如何使用python获取像audacity这样的音频频谱?

[英]How to get an audio frequency spectrum like audacity with python?

When I used the fft for both numpy and scipy to generate an audio frequency spectrum ,I don't get the same result as an audacity one. 当我同时使用numpy和scipy的fft生成音频频谱时,我没有得到与大胆的结果相同的结果。 I get positive dBm, I must have negative ones. 我得到正dBm,必须有负dBm。

Here is my code 这是我的代码

import numpy as np
from numpy import fft
from scipy.io import wavfile
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt

rate,audData = wavfile.read("test.wav")
n = len(audData)
fourier=fft.fft(audData)
fourier = fourier / float(n)
freqArray = np.arange(0, (n/2), 1.0) * (rate*1.0/n);
freq = freqArray/1000
power = 10*np.log10(fo
power = np.abs(power)
plt.plot(freq, power, color='#ff7f00', linewidth=0.02)
plt.xlabel('Frequency (kHz)')
plt.ylabel('(dBm)')
plt.show()

And I tried in another way to use periodogram from scipy.signal 我以另一种方式尝试使用scipy.signal中的周期图

from scipy import signal
freqs, Pxx = signal.periodogram(audio, fs=rate, window='hanning',
                                      detrend=False, scaling='density')

freqs=freqs / 1000
Pxx = 10 * np.log10(Pxx)

plt.plot(freqs, Pxx, color='#ff7f00', linewidth=0.02)
plt.xlabel('Frequency (kHz)')
plt.ylabel('Power (dB)')

plt.show()

I expected to have a plot like this for audacity with negative dBm 我期望有一个这样的图可以显示负dBm的胆量 在此处输入图片说明

but what I got is a range of dBm from negative to positive: 但是我得到的是从负到正的dBm范围: 在此处输入图片说明

And this by sending values and plotting them from javascript: 通过发送值并通过javascript绘制它们来实现:

在此处输入图片说明

Different FFT implementations use different scale factors. 不同的FFT实现使用不同的比例因子。 If you need a different scale factor than provided by default, rescale the input as needed. 如果您需要的缩放因子与默认设置不同,请根据需要重新缩放输入。

See: https://electronics.stackexchange.com/questions/25900/scaling-fft-output-by-number-of-points-in-fft/25941#25941 参见: https : //electronics.stackexchange.com/questions/25900/scaling-fft-output-by-number-of-points-in-fft/25941#25941

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM