简体   繁体   中英

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. I get positive dBm, I must have negative ones.

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

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 在此处输入图片说明

but what I got is a range of dBm from negative to positive: 在此处输入图片说明

And this by sending values and plotting them from javascript:

在此处输入图片说明

Different FFT implementations use different scale factors. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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