Skip to main content
 首页 » 编程设计

python之如果我的数据是 1/f 噪声,如何根据功率谱密度确定

2024年10月01日7Leo_wl

假设您有以下一系列值:

import numpy as np 
data1=np.array([1,  0.28571429,  0.20408163,  0.16326531,  0.14285714, 
        0.10204082,  0.08163265,  0.06122449,  0.04081633,  0.02040816]) 

您现在想要绘制 Spectral Density data1 使用 numpy.fft:

ps1 = np.abs(np.fft.fft(data1))**2 
time_step = 1 / 30 
freqs1 = np.fft.fftfreq(data1.size, time_step) 
idx1 = np.argsort(freqs1) 
fig,ax=plt.subplots() 
ax.plot(freqs1[idx1], ps1[idx1],color='red',label='data1') 
plt.grid(True, which="both") 
ax.set_yscale('log') 
ax.set_xscale('log') 
plt.xlabel("Frequency") 
plt.ylabel("Power") 
plt.xlim(0,15) 

我的问题:如果该图代表我系列的信号,如果这是1/,我如何根据功率谱密度确定f(或任何其他)噪声?

请您参考如下方法:

If I look at the image I am tempted to conclude this series has something like a power law behavior, but to say this you need to prove it has 1/f noise.

让我们看看是否可以验证这个假设:

  1. 为 1/f 噪声构建噪声模型:scale/(1 + f ** alpha)。这是一个1/f噪声的数学模型,参数为scale(振幅)和alpha(描述高低频的比例)

  2. 将噪声模型拟合到数据(在本例中将其拟合到功率谱密度)

  3. 检查结果。看起来模型是否很好地描述了数据?如果不是 - 尝试寻找不同的 niose 型号。但要注意 overfitting !

截图:

from scipy.optimize import curve_fit 
import numpy as np 
import matplotlib.pyplot as plt 
 
 
data1 = np.array([1,  0.28571429,  0.20408163,  0.16326531,  0.14285714, 
                  0.10204082,  0.08163265,  0.06122449,  0.04081633,  0.02040816]) 
 
 
def one_over_f(f, alpha, scale): 
    return scale / f ** alpha     
 
 
time_step = 1 / 30 
ps1 = np.abs(np.fft.fft(data1)) ** 2 
freqs1 = np.fft.fftfreq(data1.size, time_step) 
 
# remove the DC bin because 1/f cannot fit at 0 Hz 
ps1 = ps1[freqs1 != 0] 
freqs1 = freqs1[freqs1 != 0] 
 
params, _ = curve_fit(one_over_f, np.abs(freqs1), ps1) 
ps2 = one_over_f(freqs1, *params) 
 
idx1 = np.argsort(freqs1) 
 
fig, ax = plt.subplots() 
ax.plot(freqs1[idx1], ps1[idx1], color='red', label='data1') 
ax.plot(freqs1[idx1], ps2[idx1], color='blue', label='fit') 
plt.grid(True, which="both") 
ax.set_yscale('log') 
ax.set_xscale('log') 
plt.xlabel("Frequency") 
plt.ylabel("Power") 
plt.xlim(0,15) 
plt.legend() 
 
print('Alpha:', params[0], '\nScale:', params[1]) 

从视觉上看,该模型似乎非常适合 1/f 频谱。这不是证据。确实很难证明数据具有一定的分布。但您可以自行判断所选噪声模型是否在视觉上与数据拟合得足够好,足以满足您的需求。