0% found this document useful (0 votes)
147 views7 pages

PSD Autocorrelation Noise

This document discusses power, energy, spectra, autocorrelation, and noise analysis of signals in MATLAB. It provides equations and MATLAB code for calculating instantaneous and average power, total energy, power spectral density (PSD) using the discrete Fourier transform and autocorrelation, and generating white Gaussian noise. MATLAB code is presented that creates a noisy sinusoid, calculates the signal-to-noise ratio, and plots the PSD using different methods to analyze and characterize the noise properties.

Uploaded by

M Mov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
147 views7 pages

PSD Autocorrelation Noise

This document discusses power, energy, spectra, autocorrelation, and noise analysis of signals in MATLAB. It provides equations and MATLAB code for calculating instantaneous and average power, total energy, power spectral density (PSD) using the discrete Fourier transform and autocorrelation, and generating white Gaussian noise. MATLAB code is presented that creates a noisy sinusoid, calculates the signal-to-noise ratio, and plots the PSD using different methods to analyze and characterize the noise properties.

Uploaded by

M Mov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

PSD,

Autocorrelation, and Noise in MATLAB


Aaron Scher

Energy and power of a signal
Consider a continuous time deterministic signal 𝑥 𝑡 . We are specifically interested in analyzing
the characteristics of the signal over the time range [Tbegin to Tend]. To do this, let us defined a
“windowed” version of 𝑥 𝑡 :

𝑥 𝑡 for 𝑇*+,-. ≤ 𝑡 ≤ 𝑇+.0
𝑥# 𝑡 = (1)
0 for all other times

Suppose further that we sample the signal with a sampling frequency 𝑓; . We can write 𝑥# 𝑛 =
𝑥[𝑛] = 𝑥 𝑡? , where 𝑡? are the sampling times:

𝑡? = 𝑇*+,-. + 𝑛 − 1 𝑇; , 𝑛 = 1 … 𝑁F (2)

The last time 𝑡 GH = 𝑇I?J .

Instantaneous power [W] at each discrete time tn.
𝑝[𝑛] = 𝑥[𝑛]L (3.a)
p=x.^2 (MATLAB) (3.b)
Average power Pg [W]:
O L
𝑃N = ? 𝑥[𝑛] (4.a)
GH
Pg=(1/N0).*sum(x.^2) (MATLAB) (4.b)

Total energy Eg [J]:
𝐸N = 𝑇; ? 𝑥[𝑛]L (5.a)
Eg=1/fs*sum(x.^2) (MATLAB) (5.b)


Spectrum of a signal
The two-sided discrete Fourier transform of a signal can be found in MATLAB can be found with
the following command:
X=1/fs*fftshift(fft(x,N)) (MATLAB) (6)

where N is the number of frequency points in the FFT. The discrete Fourier transform X has
length 𝑁 = length(X), and is defined at discrete frequency points 𝑓Q :

R R
𝑓Q = − S + 𝑘 − 1 S , 𝑛 = 1 … 𝑁 (7)
L GUO

Recall that 𝑁F = length(x) is the number of discrete time points of the original signal 𝑥[𝑛].
In general, 𝑁F ≠ 𝑁, and you are free to choose N as large as you want (so long as your
computer can handle it). In fact, you will generally set N>>N0. A good value for N is N=215.

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)

SNR_try1=snr(xsignal,noise) %calculate SNR using built in "snr" function.


SNR_try2=10*log10(sum(xsignal.^2)/sum(noise.^2)) %manually calculate SNR.
%If everything is correct, the two SNR calculations above should agree.

%Plot noise in time-domain


figure(2)
plot(t,x)
set(gca,'FontSize',14) %set font size of axis tick labels to 18
xlabel('Time (s)','fontsize',14)
ylabel('Amplitude','fontsize',14)
title('Noisey sinusoid','fontsize',14)
grid on

%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

-50 -40 -30 -20 -10 0 10 20 30 40


Frequency [Hz]


The power spectral density plots for methods 2 and 3 exactly match that for method 1 (shown
above).

You might also like