此类问题最好构建一个综合示例,因此您不必发布大数据文件,人们仍然可以关注您的问题 ( MCVE )。
绘制中间结果也很重要,因为我们讨论的是复数运算,所以我们经常需要分别取 re、im 部分或绝对值和角度。
实函数的傅里叶变换很复杂,但对于正频率和负频率是对称的。人们也可以从信息论的角度来看待这一点:你不希望 N 个独立的实数及时产生 2N 个独立的实数来描述频谱。
虽然您通常绘制频谱的绝对或绝对平方(电压与功率),但您可以在应用滤波器时使其复杂化。在通过 IFFT 反向转换为时间后,要绘制它,您必须再次将其转换为实数,在这种情况下采用绝对值。
如果您在时域中设计滤波器内核(高斯的 FFT 将是高斯),滤波器的 FFT 与频谱的乘积的 IFFT 将只有非常小的虚部,然后您可以取实部(从物理学的角度来看,这更有意义,你从实部开始,以实部结束)。
import numpy as np
import matplotlib.pyplot as p
%matplotlib inline
T=3 # secs
d=0.04 # secs
n=int(T/d)
print(n)
t=np.arange(0,T,d)
fr=1 # Hz
y1= np.sin(2*np.pi*fr*t) +1 # dc offset helps with backconversion, try setting it to zero
y2= 1/5*np.sin(2*np.pi*7*fr*t+0.5)
y=y1+y2
f=np.fft.fftshift(np.fft.fft(y))
freq=np.fft.fftshift(np.fft.fftfreq(n,d))
filter=np.exp(- freq**2/6) # simple Gaussian filter in the frequency domain
filtered_spectrum=f*filter # apply the filter to the spectrum
filtered_data = np.fft.ifft(filtered_spectrum) # then backtransform to time domain
p.figure(figsize=(24,16))
p.subplot(321)
p.plot(t,y1,'.-',color='red', lw=0.5, ms=1, label='signal')
p.plot(t,y2,'.-',color='blue', lw=0.5,ms=1, label='noise')
p.plot(t,y,'.-',color='green', lw=4, ms=4, alpha=0.3, label='noisy signal')
p.xlabel('time (sec)')
p.ylabel('amplitude (Volt)')
p.legend()
p.subplot(322)
p.plot(freq,np.abs(f)/n, label='raw spectrum')
p.plot(freq,filter,label='filter')
p.xlabel(' freq (Hz)')
p.ylabel('amplitude (Volt)');
p.legend()
p.subplot(323)
p.plot(t, np.absolute(filtered_data),'.-',color='green', lw=4, ms=4, alpha=0.3, label='cleaned signal')
p.legend()
p.subplot(324)
p.plot(freq,np.abs(filtered_spectrum), label = 'filtered spectrum')
p.legend()
p.subplot(326)
p.plot(freq,np.log( np.abs(filtered_spectrum)), label = 'filtered spectrum')
p.legend()
p.title(' in the log plot the noise is still visible');