在对Audacity做了一些逆向工程后,这里给出了一些答案。首先,他们使用Welch algorithm来估计PSD。简而言之,它将信号分割成重叠段,应用一些窗口函数,应用FFT并对结果进行平均。主要是因为这有助于在存在噪声时获得更好的结果。总之,在提取了必要的参数后,这里的解决方案近似于Audacity的光谱图:import numpy as np
from scipy.io import wavfile
from scipy import signal
from matplotlib import pyplot as plt
segment_size = 512
fs, x = wavfile.read('sine3k6k.wav')
x = x / 32768.0 # scale signal to [-1.0 .. 1.0]
noverlap = segment_size / 2
f, Pxx = signal.welch(x, # signal
fs=fs, # sample rate
nperseg=segment_size, # segment size
window='hanning', # window type to use
nfft=segment_size, # num. of samples in FFT
detrend=False, # remove DC part
scaling='spectrum', # return power spectrum [V^2]
noverlap=noverlap) # overlap between segments