PSD Autocorrelation Noise
PSD Autocorrelation Noise
1
Power spectral density
In two lines of code you can compute the power spectral density (PSD):
X=1/fs*fftshift(fft(x,N)) (MATLAB) (8.a)
PSD=(abs(X).^2)/((N0-1)*Ts) (MATLAB) (8.b)
Alternatively, you can also use MATLAB’s built in “periodogram” function:
PSD=periodogram(x,[],N,fs,’centered’) (MATLAB) (9)
Average Power
Once you have the PSD, you can compute the average power (Pg):
Pg=fs/N*sum(PSD) (MATLAB) (10)
Autocorrelation
The autocorrelation function can be calculated with the following MATLAB command.
Rxx=1/fs*xcorr(x,x) (MATLAB) (11)
Below are the equations to calculate the signal’s energy and power spectral density using the
autocorrelation function:
Total energy Eg [J]:
Eg=max(Rxx) (MATLAB) (12)
PSD [W/Hz]:
PSD=1/(N0-1)*abs(fftshift(fft(Rxx,N))) (MATLAB) (13)
Noise
You can create white Gaussian noise in MATLAB with the following commands:
noise_PSD = ... %choose noise PSD [W/Hz] (MATLAB) (14.a)
variance = ... noise_PSD*fs (MATLAB) (14.b)
sigma = ... sqrt(variance) (MATLAB) (14.c)
2
noise = transpose(sigma*randn(N0,1)) (MATLAB) (14.d)
In the MATLAB code below, I create a noisy sinusoid and compute/generate the following
information:
• Plot histogram of the noise
• Plot of noisy sinusoid in the time domain
• Signal-to-noise ratio (SNR)
• Plots of PSD computing using methods presented above
• Average PSD of noise
• Average value of noise
%Clear variables. clear command window, close all figures:
clc;
clear all;
close all;
%%%Setup and define variables
f0=10; %frequency of sinusoidal signal (Hz)
fs=100; %sampling frequency (Hz)
Ts=1/fs; %sampling period (seconds)
N0=3000; %number of samples
t=[0:Ts:Ts*(N0-1)]; %Sample times
noise_PSD=.5; %This is the desired noise power spectral density in W/Hz.
variance=noise_PSD*fs;% Variance = sigma^2
sigma=sqrt(variance);
noise=transpose(sigma*randn(N0,1));%create sampled white Gaussian noise.
xsignal=20*sin(2*pi*f0*t); %create sampled sinusoidal signal
x=xsignal+noise; %Add signal to noise
figure(1)
histogram(noise,30) %plot histogram
set(gca,'FontSize',14) %set font size of axis tick labels to 18
xlabel('Noise amplitude','fontsize',14)
ylabel('Frequency of occurance','fontsize',14)
title('Simulated histogram of white Gaussian noise','fontsize',14)
%Plot power spectral density (PSD) of noise using three different methods:
%
3
%Method 1. Calcululate PSD from amplitude spectrum
N=2^16; %Number of discrete points in the FFT.)
y=fft(x,N)/fs; %fft of noise
z=fftshift(y);%center noise spectrum
f_vec=[0:1:N-1]*fs/N-fs/2; %designate sample frequencies
amplitude_spectrum=abs(z); %compute two-sided amplitude spectrum
ESD1=amplitude_spectrum.^2; %ESD = |F(w)|^2;
PSD1=ESD1/((N0-1)*Ts);% PSD=ESD/T where T = total time of sample
figure(3)
plot(f_vec,10*log10(PSD1));
xlabel('Frequency [Hz]','fontsize',14)
ylabel('dB/Hz','fontsize',14)
title('Power spectral density - method 1','fontsize',14)
grid on
set(gcf,'color','w'); %set background color from grey (default) to white
axis tight
%calculate average power using PSD calclated from method 1:
Average_power_method_1=sum(PSD1)*fs/N% Pav=sum(PSD)*delta_f where
delta_f=fs/N
%
%Method 2 - Calculate PSD from autocorrelation
time_lag=((-length(x)+1):1:(length(x)-1))*Ts;
auto_cor=xcorr(x,x)/fs; %Use xcorr function to find PSD
y=1/fs*fft(auto_cor,N); %fft of auto correlation function
PSD2=abs(1/(N0-1)*fftshift(fft(auto_cor,N)));
figure(4)
plot(f_vec,10*log10(PSD2));%use convolution
xlabel('Frequency [Hz]','fontsize',14)
ylabel('dB/Hz','fontsize',14)
title('Power spectral density - method 2','fontsize',14)
grid on
set(gcf,'color','w'); %set background color from grey (default) to white
axis tight
%calculate average power using PSD calclated from method 1:
Average_power_method_2=sum(PSD2)*fs/N %Pav=sum(PSD)*delta_f where
delta_f=fs/N
%
%Method 3 - Calculate PSD using built in pwelch function
figure(5)
PSD3=periodogram(x,[],N,fs,'centered');
plot(10*log10(PSD3))
xlabel('Frequency [Hz]','fontsize',14)
ylabel('dB/Hz','fontsize',14)
title('Power spectral density - method 3','fontsize',14)
grid on
set(gcf,'color','w'); %set background color from grey (default) to white
axis tight
Average_power_method_3=sum(PSD3)*fs/N %Pav=sum(PSD)*delta_f where
delta_f=fs/N
%
%Calculate mean and average PSD of noise:
PSD_noise=periodogram(noise,[],N,fs,'centered');
Average_noise_PSD=mean(PSD_noise)
Mean_noise=mean(noise)
4
Results of running MATLAB code:
SNR_try1 =
6.3039
SNR_try2 =
6.3039
Average_power_method_1 =
245.5948
Average_power_method_2 =
245.5948
Average_power_method_3 =
245.5129
Average_noise_PSD =
0.4684
Mean_noise =
0.0983
5
Simulated histogram of white Gaussian noise
300
250
Frequency of occurance
200
150
100
50
0
-30 -20 -10 0 10 20 30
Noise amplitude
Noisey sinusoid
40
30
20
10
Amplitude
-10
-20
-30
-40
0 5 10 15 20 25 30
Time (s)
6
Power spectral density - method 1
30
20
10
-10
dB/Hz
-20
-30
-40
-50
-60