如何计算功率谱下的面积来测试它是否等于有效值(FFT,python,信号处理)

时间:2018-04-26 08:51:19

标签: python fft physics frequency-analysis

我对物理课有这个问题,我坚持到最后一点。

生成一个2s长的信号,采样频率为500 Hz,包含 (i)由300千瓦的1MW电阻和10千赫的带宽产生的白色背景噪音, (ii)频率为80赫兹,幅度为12毫伏的正弦波信号, (iii)频率为170赫兹,振幅为6毫伏的正弦波信号。 - 以V2 / Hz为单位计算信号的功率谱。  测试功率谱的面积是否等于信号的均方根值。

import numpy as np 
import matplotlib.pyplot as plt
import random 
#Generate a signal of 2s length with sampling frequency 500 Hz that contains

#sampling frequency 500 Hz
Fs=500 #the number of points taken 

#Generate a signal of 2s length
time=2
t=np.linspace(0,time,Fs*time) 



# (i) a white background noise as produced by a resistance of 1MW at 300K and a badwidth of 10kz
Kb=1.3806504e-23 #Boltzman's constant
T=300 #temperature kelving
R=1000 #Ohms resistance
DF=10000 #bandwith HZ
N=np.power((4*Kb*DF*R*T),0.5) #Noise in VOLTS

#Noise in uV from V
N=N
print ('Noise in uV: ' + str(N))

#the noise should move from twice its height, to -2 times its height... 2000 point up 2000 down, normalized with e-3

Noise=N*1e-3*np.asarray(random.sample(range(-2000,2000),len(t))) #array of random noise, size Fs makes noise from 0.8 to -0.8 uV


#(ii) a sine wave signal at frequency 80 Hz with amplitude 12 mV

#frequency 80 Hz
f1 = 80 #sine wave signal at frequency 80 Hz

#amplitude 12 mV
a1=12e-6 #amplitude 12 microV

# a sine wave signal
x1 = a1*np.sin(2*np.pi*t*f1) #sin wave from time formula (the t gives it the array shape)



#♦(iii)a sine wave signal at frequency 170 Hz with amplitude 6 mV

#frequency 170 Hz
f2=170 #a sine wave signal at frequency 170 Hz
#with amplitude 6 mV
a2=6e-6 # with amplitude 6 mV  IVE PUT IT IN VOLTS NOT uV
#a sine wave signal 
x2=a2*np.sin(2*np.pi*t*f2) #sin wave from time formula 


#- Calculate the power-spectrum from the signal in units of V2/Hz and dB. 

#total signal 
x=x2+x1#+Noise

#number of frequencies to look at
nfft=1024 #number of frequencies

#do the fft
X=np.fft.fft(x,nfft)


#new normalized version, Mx is the normalized X
Mx=abs(X)*2*(Fs*time)/(np.square(Fs*time))
Mx=Mx[1:nfft//2]

#cut away the second half because its an fft
X=X[1:nfft//2]

#create a vector for the frequencies, normalized
F=np.arange(0,nfft//2-1)*Fs/nfft


#creates the root mean square single complex value, for the rms. 
rms = np.sqrt(np.mean(Mx**2))
rms=[rms]*len(X)

#plot the wave 
plt.plot(t,x1+x2+Noise)
plt.title('Noise, 80HZ 170HZ')
plt.ylabel('microvolts')
plt.xlabel('time')
plt.show()
#FFT vs the frequencies, normalized in microvolts squared
plt.plot(F,Mx)
plt.plot(F,rms)
plt.figure(1)
plt.title('Power Specturm of a Sine Wave')
plt.xlabel('Frequency (Hz)')
plt.ylabel(('Power uV^2'))
plt.show()

此时我必须:测试功率谱的面积是否等于信号的均方根值。我想是这样的:

#area of power spectrum: 
area=sum(Mx)

然而,当我将它与rms进行比较时,我得不到相同的结果?我究竟做错了什么?

0 个答案:

没有答案