0

从理论上我们知道白噪声的频谱是恒定的。S(w)=S0

我已经编写了代码,在其中定义了一个白噪声,虽然是 numpy random。然后我对其应用快速傅里叶变换。我期望某种恒定函数,但我得到 w=0 的峰值。当我将 fft 应用于随机输入时,为什么我会得到 freq=0 的峰值?

这是我的代码:

import matplotlib.pyplot as plt
import numpy as np
import scipy
from scipy.fft import fft, fftfreq, ifft
from scipy import  signal
#Define the time limits to create the array
tin=0
tfin=1
#the code works better with power of 2 (64;128;256;512;etc.)
Nt=127
#time span
T=tfin-tin
#time interval
dt=T/Nt
#array of time
t=np.arange(tin,tfin, dt)
#noise
noise=0.3*np.random.rand(Nt)
#array of the function to be transformed
x=noise
#creating the spectrum ordinate array
y=fft(x)
#Obtaining the frequency array correlated to the spectrum
freq=fftfreq(Nt, d=dt)
#Creating the time history from the spectrum
xinv=ifft(y)
#Plotting the spectrum X(w)
#I only want the positive freq values:
freq=freq[:Nt//2]
y=y[0:Nt//2]
#I want freq in rad/s
freq=freq*2*np.pi
#I want the absolute value of the spectrum
y=abs(y)
plt.plot(freq,y , label='Spectrum')
plt.legend(loc='upper left')
plt.xlabel('w[rad/s]')
plt.ylabel('X(w)')
plt.title('')
plt.grid()
plt.show()
4

0 回答 0