как рассчитать площадь под спектром мощности, чтобы проверить, равно ли оно среднеквадратичному значению (FFT, python, обработка сигнала) - PullRequest
0 голосов
/ 26 апреля 2018

У меня есть эта проблема для класса физики, я застрял на последнем пункте.

Генерация сигнала длительностью 2 с с частотой дискретизации 500 Гц, содержащего (i) белый фоновый шум, создаваемый сопротивлением 1 МВт при 300 К и шириной полосы 10 кГц, (ii) синусоидальный сигнал на частоте 80 Гц с амплитудой 12 мВ, (iii) синусоидальный сигнал на частоте 170 Гц с амплитудой 6 мВ. - Рассчитать спектр мощности от сигнала в единицах V2 / Гц. Проверьте, равна ли площадь спектра мощности среднеквадратичному значению сигнала.

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, я не получаю тот же результат? Что я делаю неправильно?

...