You are on page 1of 7

ELT-41306

Communication Theory, Matlab Exercise #3


MATLAB (www.mathworks.com)
1.

Studying white noise and colored noise

Generate a white Gaussian noise signal with variance 3


o Plot the noise signal
o Plot the histogram of the noise signal to see that it is Gaussian distributed
Implement a low pass FIR filter
o E.g., using filter order of 60 and a transition band from 0.1*Fs/2 to
0.2*Fs/2
Filter the noise signal using the above filter
o Remember the filter delay here
Plot the filtered noise signal
o How this is different to the original noise signal (e.g. plot the signals in
the same figure and compare)
Plot the histogram of the filtered noise signal
o How it is distributed? Recap: What is the distribution of the output signal
of an LTI system, if the input signal is Gaussian distributed?
Plot the autocorrelation function of the original white noise signal
o What type of autocorrelation function does the white noise have? (see
lecture notes p. 134)
Plot the autocorrelation function of the filtered (i.e., colored) noise signal
o Compare this with the impulse response of the filter
In the end, plot the power spectral density functions (i.e., amplitude spectrum
squared) of the two noise signals in decibel scale
o Notice that both signals are still Gaussian distributed (see the previous
histograms), but the power spectra are different.
What is the connection between the correlation function and the
power spectral density function?
o Nevertheless, make sure you understand the difference between the
following concepts: Gaussian noise and white noise

Example code:
N = 10000; % Number of generated samples
noise_var = 3; % Desired noise variance
noise = sqrt(noise_var)*randn(1,N); % Noise signal generation

N_samples_plot = 100;% Number of samples used in the plots


% Plot the first "N_samples_plot" samples of the noise realization:
figure
plot(noise(1:N_samples_plot))
xlabel('sample index')
xlabel('noise amplitude')
% Plot the histogram of the noise realization:
figure
hist(noise,20)
xlabel('noise amplitude')
ylabel('histogram count')
title('Noise histogram')
% Generate a FIR filter:
N_filter = 60; %even number
h = firpm(N_filter,[0 0.1 0.2 1],[1 1 0 0]);
% Filter the noise signal:
filtered_noise = filter(h,1,noise);
filtered_noise = filtered_noise(N_filter/2+1:end); % Remember to consider the
filter's delay!
% Plot the first "N_samples_plot" samples of the filtered noise (compared
with the white noise):
figure
plot(noise(1:N_samples_plot))
hold on
plot(filtered_noise(1:N_samples_plot),'r')
legend('White noise','Colored noise')
xlabel('sample index')
xlabel('noise amplitude')
% Plot the histogram of the filtered noise:
figure
hist(filtered_noise,20)
xlabel('noise amplitude')
ylabel('histogram count')
title('Filtered noise histogram')

% Calculate and plot the autocorrelation function for the white noise:
[corr_fun, lags] = xcorr(noise);
figure,plot(lags,corr_fun)
xlabel('\tau')
ylabel('R(\tau)')
title('Autocorrelation of white noise')
xlim([-30 30])
% Calculate and plot the autocorrelation function for the filtered noise:
[corr_fun, lags] = xcorr(filtered_noise);
figure,plot(lags,corr_fun)
xlabel('\tau')

ylabel('R(\tau)')
title('Autocorrelation of filtered (colored) noise')
xlim([-30 30])
% Notice the similarity between the impulse response and the autocorrelation
% function of colored noise
% Thus, we plot the impulse response of the filter:
figure,stem(-N_filter/2:N_filter/2,h)
xlabel('filter tap index')
ylabel('h(k)')
title('Impulse response of the filter')

% Define and plot the noise amplitude spectra in dB:


noise_abs_spec = 10*log10(abs(fft(noise(1:length(filtered_noise)))));
filtered_noise_abs_spec = 10*log10(abs(fft(filtered_noise)));
%Define the frequency vector values (normalized between -1 and 1):
freq_vec = -1:2/length(noise_abs_spec):1-2/length(noise_abs_spec);
figure
plot(freq_vec,fftshift(noise_abs_spec))
hold on
plot(freq_vec,fftshift(filtered_noise_abs_spec),'r')
hold off
xlabel('Normalized freqeuncy (Fs/2=1)')
ylabel('dB')
title('Noise spectra')
legend('White noise','Filtered (coloured) noise')

2. Studying random processes: random walk model (classroom exercise #4 (task 1))

Lets first study the random walk process presented in the classroom exercise #4
(task 1).
Generate the random process for 2000 samples and 5000 realizations
o Use first p=0.5 and s=1, but write the code so that you can conveniently
test the process with other values too
Plot five example realizations of the random walk process
Calculate the ensemble mean and ensemble variance of the process
o Plot the observed mean and variance in the same figure with the
theoretical values (these were found with the random walk definition)
Re-run the process with different values of p, s, and the number of realizations
o Due to memory issues, do not try too many realizations at the same time
(stay below 100000 with 2000 samples per realization)
o What if the number of realizations is very low?
Is the process stationary (in wide sense)? Why?/Why not?

Example code:
N_samples = 2000; %Number of samples for each realization
N_ensemble = 5000; %Number of signal realizations (i.e., the size of
ensemble)
%Step probability and step size:
p = 0.5; % P(Wi=s) = p, P(Wi=-s) = 1-p
s = 1; %step length
n = 1:N_samples; % vector of samples indices
% Generating matrix of randomly generated steps:
W = rand(N_ensemble,N_samples); % uniformly distributed random values between
0 and 1
indices_with_positive_s = W<p; % find out steps going "up"
W(indices_with_positive_s) = s; % Define steps for going "up"
W(~indices_with_positive_s) = -s; % Define steps for going "down"
X = cumsum(W,2); % The overall "random walk" is achieved by taking the
cumulative sum over the steps
% (Notice that now each row describes one random walk realization)
% Plotting 5 realizations of the process as an example
figure
for ind = 1:5
subplot(5,1,ind)
plot(n,X(ind,:))
xlabel('n')
ylabel('X(n)')
grid on
end
mean_theory = n*s*(2*p-1); % Theoretical mean
var_theory = n*(2*s)^2*p*(1-p); % Theoretical variance
mean_observed = mean(X); % Empirical mean (i.e., what we observe)
var_observed = var(X); % Empirical variance (i.e., what we observe)
% Plot the process mean as a funtion of sample indices:
figure
plot(n,mean_observed,'b','LineWidth',3)
hold on
plot(n,mean_theory,'r:','LineWidth',2)
hold off
legend('observed mean','theoretical mean')
% Plot the process variance as a funtion of sample indices:
figure
plot(n,var_observed,'b','LineWidth',3)

hold on
plot(n,var_theory,'r:','LineWidth',2)
hold off
legend('observed variance','theoretical variance')
xlabel('n')

3. Studying random processes: sinusoidal wave with random phase (lecture notes p. 126-127)

Lets then study the sinusoidal signal with random phase shown in lecture notes
p. 126-217.
Generate the random process with 200 samples and 5000 realizations
o Define the sinusoidal to be a cosine wave with frequency fc=100Hz
o Use sampling frequency of Fs=10000Hz
Calculate the ensemble mean of the process
o Plot the observed mean in the same figure with the theoretical value (this
is actually just zero as found in the lecture notes)
Calculate the autocorrelation of the process for different time moment pairs
(ti,tj)
o For different values of ti, plot the autocorrelation functions R(ti,ti+) in
the same figure ( in the x-axis)
Here we want to see that does the autocorrelation function
change as function of time.
Does it?
Plot the theoretical autocorrelation in the same figure (see lecture note p. 217)
Is the process stationary (in wide sense)? Why?/Why not?
Again, try how changing the number of realizations affects the results
o Good to know: If the number of realizations is desired to be increased to
millions/billions (etc.), the code has to be written differently. For example,
we can use for-loops and do the averaging inside the loop (by this way we
do not have to save each realization separately). This would be better
from the memory allocation point of view, but it takes longer to process
(and we lose the information of individual realizations).

Example code:
N_samples = 200; %Number of samples for each reaslization
N_ensemble = 5000; %Number of signal realizations (size of ensemble)
Fs = 10000; %Sampling frequency
fc = 100; % Cosine frequency

Ts=1/Fs; %Sampling time


t = 0:Ts:N_samples*Ts-Ts; %time vector
% Generate random phases (uniformly distributed between [0,2*pi])
phi = 2*pi*rand(N_ensemble,1); %N_ensemble x 1 random vector of phase:
[0,2*pi]
t_repeated = repmat(t,N_ensemble,1); % N_ensemble x N_samples matrix
including time vector "t" repeated in each row
phi_repeated = repmat(phi,1,N_samples); % N_ensemble x N_samples matrix
including the phases repeated in each column
% Generating the set of sinusoidals with the random phase (each row is now
one realization of the process)
x_ensemble = cos(2*pi*fc*t_repeated + phi_repeated); % N_ensemble x N_samples
matrix representing the ensemble
%Ensemble average:
ensemble_average = mean(x_ensemble);
% Plot the empirical and theoretical averages:
figure
plot(t,ensemble_average)
hold on
plot(t,zeros(1,length(t)),'r')
hold off
xlabel('time [s]')
ylabel('E[x(t)]')
title('Ensemble average of a sinosoidal with random phase')
axis([0 N_samples*Ts-Ts -1.1 1.1])
legend('Observed average','Theoretical average')
% Studying the autocorrelation of the process:
tau_vec = 0:100; % vector describing the delays that are studied
%We initialize 3D matrix "R_ensemble", where
% the 1st dimension gives different process realizations
% the 2nd dimension gives the studied time index t1
% the 3rd dimension gives the studied time index t2 (=t1+tau)
% Thus, denoting the process as x(t), we desire to find E[x(t1)*x(t2)].
% (see the lecture slides p. 127)
R_ensemble = zeros(N_ensemble,N_samples-tau_vec(end),length(tau_vec));
for time_index = 1:N_samples-tau_vec(end)
for tau_index = 1:length(tau_vec)
tau = tau_vec(tau_index);
R_ensemble(:,time_index,tau_index) =
x_ensemble(:,time_index).*x_ensemble(:,time_index+tau);
end
end
%Calculate the mean (i.e. "take the expectation")
R_ave = squeeze(mean(R_ensemble,1));

% Plot the observed autocorrelation functions for different time moment pairs
(t1,t2)
figure
plot_handle1 = plot(tau_vec,R_ave,'b');
%...and plot the the theoretical autocorrelation function (see lecture notes
p. 127)
hold on
plot_handle2 = plot(tau_vec,0.5*cos(2*pi*fc*t(tau_vec+1)),'r','LineWidth',2);
hold off
xlabel('\tau')
ylabel('R(\tau)')
title('Autocorrelation function (blue curves are for different (t_1,t_2)pairs (\tau=t_2-t_1))')
legend([plot_handle1(1) plot_handle2],'Observed functions','Theoretical
functions')

You might also like