Department of Electrical Engineering: Communication Systems Lab

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Department of Electrical Engineering

Communication Systems Lab

Random Variables, Random Processes, Expectation and


Autocorrelation using MATLAB

Name : Usama Bin Sohaib


Roll Number : BSEE-2019-43
Instructor : Engr. Arbaz Shb
Date : 20-02-2021

NAMAL UNIVERSITY MIANWALI


Introduction:
MATLAB has random number generators for uniform and Gaussian random variables
(randn) and random integers (randi). Random variables are very important in statistics
and probability and a must-have if anyone is looking forward to understanding
probability distributions. The mean (or expected value) of a discrete random variable
as the weighted average of all the outcomes of that random variable based on their
probabilities. Autocorrelation measures the correlation of neighbor samples in the same
signal.
Task 1: Random Variables Generation
Exercise 1

MATLAB Code to generate 5000 samples of random variables uniformly


distributed over 0 to 2pi interval
Code
X1 = 0+(2*pi-(0))*rand(5000,1); figure, %Random variables generation
hist(X1,10);
xlabel('Interval (0 to 2pi)');
ylabel('Random Values');

Graph

MATLAB Code to generate 5000 samples of cards chosen from deck of 52 cards
Code
X2 = randi(52,5000,1); figure, %Random integer variables generation
hist(X2,52)
xlabel('Number of Cards');
ylabel('Magnitude');
grid on
title('Histogram of 5000 random samples from 52 Cards');
Graph

MATLAB Code to generate 5000 samples of Gaussian Random Variables


Code
X3 = 0+1.*randn(5000,1); %Random gaussian variables generation
figure, hist(X3,10);
xlabel('Randomly produced Numbers');
ylabel('Gauss Distribution');
grid on
title('Histogram of 5000 samples of Gaussian Variable');

Graph
Decreasing the number of samples
As there are infinite real numbers between any two integers, so by decreasing the
number of samples decreases the number of random variables generated within the two
integers for the rand and gaussian rand functions. For the Randi function, the decrement
in the number of samples decreases the number of integers generated within a specified
interval.

Task 2: Random Signal Generation


Exercise 2
Code
A= 0+1.*randn(1,1); %Generation of 1 sample of Guassian Random Variable
t=-1:0.001:1;
f=5;
theta=0;
x=A*cos(2*pi*f*t+theta); %Random Process (Sinusoid signal)
plot(t,x);
xlabel('Time (Seconds)');
ylabel('Amplitude');
title('Random Process for Sinusoid');
grid on

Graph
Observation
It is observed that amplitude is changed each time. This is because the Amplitude which
is applied to the signal is generated using the Gaussian random function. Every time
this function generates random values instead of keeping the previous record.

Task 3: Expectation
Exercise 3
Code
syms A; %Symbolic Variable A
theeta = 0;
f = 2;
t = 0:0.001:0.02; %Time interval
mu = 0;
sigma = 1;
PD = makedist('Normal','mu',0,'sigma', 1); %Probablility Distribution
a = pdf(PD, A); %Density Function
Xt = A*cos(2*pi*f*t + theeta); %Input Signal
Y = double(int(Xt*a,A,-inf,+inf)); %Expectation's Calculation
figure(1);
plot(Y);
title('Expectation');
xlabel('Time (seconds)');
ylabel('Magnitude');

Graph

The limits for expectation calculation are set to minus infinity up to positive
infinity. The first reason is that it is according to formula secondly be integrating
the function from negative infinity up to positive infinity function will definitely
converge at some point.
Task 4: Autocorrelation
Code
X1 = -1/2+(1/2-(-1/2))*rand(1000,1); %Generation od Discrete Time Sequence
(Random)
a=xcorr(X1); %Cross correlation's estimation
figure (1)
plot(a);
grid on
xlabel('Random Values');
ylabel('Magnitude');
title('Autocorrelation of Discrete Time Sequence');
grid on

Graph

Code
f=5;
t=0:0.001:0.02; %Time Interval
theta=0;
x=10*cos(2*pi*f*t+theta); %Input signal
x1=xcorr(x); %Cross correlation's estimation
figure (2)
plot(x1);
xlabel('Time (Seconds)');
ylabel('Magnitude');
title('Autocorrelation of Sinusoidal Signal');
grid on
Graph

Observation
Autocorrelation is basically the correlation of a signal with a delayed copy of itself as a
function of delay. The randomness of the signal can be easily observed from the first
graph in which basically a thousand samples of random values are generated between -
1/2 and ½. The periodicity of the input signal can also be observed by using this
function. The second graph is basically telling the periodicity of the input sinusoid.

Conclusions
Electronics and Communication are all about Random Variables and Random
Processes. The signal that is to be communicated (especially if it is digital) is one of
several possible choices, and thus it makes sense to model it as a sample function of a
Random Process. “Autocorrelation” is a term that encompasses a variety of
mathematical techniques used across different fields and different means of
representing a signal within that domain. The common goal of autocorrelation is to
compare a signal with itself at different time periods within the signal to determine if
there is a pattern to the signal.

You might also like