Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab # 1 Introduction To Communications Principles Using Matlab

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

Lab # 1 Introduction to Communications Principles

Using Matlab

OBJECTIVES:

The purpose of this Lab is to review MATLAB for those that have used it before, and to
provide a brief introduction to MATLAB for those that have not used it before. After using this
lab session, you should be able to:

 Enter matrices
 Perform matrix operations
 Make plots
 Use MATLAB functions
 Write simple m-files
 Signal Generation in MATLAB

PRE-LAB:

MATLAB is an interactive program for numerical computation and data visualization. It was
originally developed in FORTRAN as a Matrix Laboratory for solving numerical linear algebra
problems. The original application may seem boring (except to linear algebra enthusiasts), but
MATLAB has advanced to solve nonlinear problems and provide detailed graphics. It is easy to
use, yet very powerful. A few short commands can accomplish the same results that required a
major programming effort only a few years ago. MATLAB features a family of add-on application-
specific solutions called toolboxes. These toolboxes allow you to learn and apply specialized
technology. Toolboxes are comprehensive collections of MATLAB functions (M-files) that extend
the MATLAB environment to solve particular classes of problems. Areas in which toolboxes are
available include communications, signal processing, control systems, neural networks, fuzzy
logic, simulation, and many others.

Starting MATLAB
On Windows platforms, start MATLAB by double-clicking the MATLAB shortcut icon on your
Windows desktop.
Signal’s representation
A signal in MATLAB is represented by a row vector:
Examples:
>> x = [2, 3, -5, -3, 1]
>> n = 2:17 = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17]
Default step size is 1
>> n = 2:3:17 = [2, 5, 8, 11, 14, 17]

The basic plot command


Two-dimensional line and symbol plots are created with the plot command. In its simplest form
plot takes two arguments
>> plot(xdata,ydata)

where xdata and ydata are vectors containing the data. Note that xdata and ydata must be the same
length and both must be the same type, i.e., both must be either row or column vectors. Additional
arguments to the plot command provide other options including the ability to plot multiple data
sets, and a choice of colors, symbols and line types for the data.

PRE-LAB:

Example
Plot the following signal
>> x = 10sin π t

MATLAB CODE:
clear all
close all
clc

t = [-2:0.002:2];
x = 10 * sin (pi * t)
plot(t , x)
title('Example Sinusoid')
xlabel('time(sec)')
ylabel('Amplitude')

MATLAB FIGURE :

Figure 1:sinusoidal signal having time period of 2 sec.


A simple line plot
Here are the MATLAB commands to create a simple plot of
>> y = sin (3*pi*x)
from 0 to 2*pi.

MATLAB CODE :
clear all % to clear all simulations
close all
clc
x = 0:pi/30:2*pi; % x vector, 0 <= x <= 2*pi, increments of
pi/30
y = sin(3*x); % vector of y values
plot(x,y) % create the plot
xlabel('x (radians)'); % label the x-axis
ylabel('sine function'); % label the y-axis
title('sin(3*x)'); % put a title on the plot

MATLAB FIGURE :

Figure 1: sinusoidal signal having three cycles in it and time period is same as in above figure 2 sec.

CHANGING SYMBOL OR LINE TYPES


The symbol or line type for the data can be changed by passing an optional third argument to the
plot command. For example
>> plot(x,y,'o');
plots data in the x and y vectors using circles drawn in the default color (yellow), and
>> plot(x,y,'r:');
plots data in the x and y vectors by connecting each pair of points with a red dashed line.
DT Plots
Plot the DT sequences:

MATLAB CODE :
clear all % to clear all simulations
close all
clc

x = [2, 3, -1, 5, 4, 2, 3, 4, 6, 1] % number oof samples to plot and x is


` a veriable containing array of samples
figure()
Stem(x); % command used to plot the discrete
` values
title('discrete value plot')
xlabel('number of samples')
ylabel('amplitude')
n= -6:3; % number of values to plot.start from -
6 and ends on 3 with increment of 1.
x = [2, 3, -1, 5, 4, 2, 3, 4, 6, 1] % number oof samples to plot and x is
a veriable containing array of samples
figure()
Stem(x); % command used to plot the discrete
values
title('discrete value plot')
xlabel('number of samples')
ylabel('amplitude')

MATLAB FIGURES :

Figure 1:above figure represent the discrete value graph having value on absolute time axis(values).
Figure 2:above figure represent the discrete value graph with defined horizontal axis.

Generate following elementary DT signals:


– Unit Impulse
– Unit Step
– Unit Ramp
– Exponential Sequence

UNIT IMPULSE :

MATLAB CODE :
clear all % to clear all simulations
close all
clc

n=-5:5; %n number of samples to plot unit


impulse
stem (n, [zeros(1,5) 1 zeros(1,5)]) %command used to plot discrete values
title('unit impulse function')
xlabel('number of samples')
ylabel('amplitude')
MATLAB FIGURE :

Figure 2:above figure represents the unit impulse signal (discrete valued).

UNIT STEP :
MATLAB CODE :
clear all % to clear all simulations
close all
clc
n=-5:5; %n number of samples to plot unit impulse
stem (n, [zeros(1,5) ones(1,6)]) %command used to plot discrete values
title('unit step function')
xlabel('number of samples')
ylabel('amplitude')

MATLAB FIGURE :

Figure 1:Above figure represent the unit impulse signal(discrete valued).


UNIT RAMP FUNCTION :

MATLAB CODE :

clear all % to clear all simulations


close all
clc

n=0:5; %n number of samples to plot unit impulse


stem (n) %command used to plot discrete values
title('unit ramp function')
xlabel('number of samples')
ylabel('amplitude')

MATLAB FIGURE :

Figure 1: Above figure represent the unit ramp signal having increasing order (x(t) = t).

EXPONENTIAL FUNCTION :

MATLAB CODE :
clear all % to clear all simulations
close all
clc

n=0:5; %n number of samples to plot unit


impulse
stem (n,exp(n)) %command used to plot discrete
values
title('exponential function')
xlabel('number of samples')
ylabel('amplitude')
MATLAB FIGURE :

Figure 1:Above figure represent the exponential signal graph. The signal espreession is (x(t) = exp(t)).

LAB-TASKS :

TASK 1 :
Create a symbol plot with the data generated by adding noise to the given function:
y = 5*x.*exp(-3*x) Where x = 0:0.01:2;

MATLAB CODE :
clear all
close all
clc

x = 0:0.01:2; % length of horizental axis defined as x

y = 5*x.*exp(-3*x); % given expression to plot defined as y

noise = randint(1,length(x)); %inializing noising function using


random function

y1 = (5*x.*exp(-3*x))+noise; %real signal containing noise with


it
figure();
plot(x,y,'o',x,y1,'r') %command used to plot the obtained
expression for defined length
xlabel('time axis')
ylabel('amplitude axis')
title('original and noisy signal')
MATLAB FIGURE :

Figure 1:Above figure represent the original signal and noisy signal. Blue line signal is a real original (exponential
signal) and red line signal in which we add noise in original signal.

Examples:
Sinusoidal signals:

MATLAB CODE :
clear all
close all
clc

A=[5 2 1]; %vary the amplitude. make them equal


p=[0 pi/2 pi]; %vary the phase.
f=40; %frequency can vary and T accordingly.
n=1; %change the cycle number to display.
t=[n*(-0.025):0.001:n*(0.025)]; %choose the range according to the
%number of cycles n. You can display
%only positive t values.
for k=1:3
x{k} = A(k).*sin((2*pi*n*f*t)+p(k));
plot(t,x{k})
xlabel('time')
ylabel('amplitude')
hold on
end
%Additional plot for all signals with different colors
figure
plot(t,x{1},t,x{2},t,x{3})
title('different sine plots')
xlabel('time')
ylabel('amplitude')

MATLAB FIGURE :

Figure 1: This figure represent the three different sinusoidal signal of different amplitude and frequency using FOR
loop.

Figure 2: Above figure also represent the three sinusoidal signal of different amplitude and frequency. These plots are obtain by
using plot command for multiple graphs in single figure.
SIGNUM FUNCTION :

MATLAB CODE :
clear all
close all
clc

% signum funnction

t=[-10:0.001:10]; %time range. try to increase and


decrease range
y = sign(t); %signum function
figure();
plot(t,y);
xlabel('time')
ylabel('amplitude')
title('signum signal')

MATLAB FIGURE :

Figure 1: This figure represent the signum function graph having value -1 and 1 as amplitude.

SQUARE WAVE (single period):

MATLAB CODE :
clear all
close all
clc
A=3; %amplitude
f0=1/10; %frequency of the square wave
T0=1/f0; %duration of single pulse
t=[-T0/2:0.01:T0/2] %range is fixed for one symbol duration
%implementation of square wave.
for k=1:length(t) %using loop to get values (repeatedly)
if (abs(t)<=T0)
x(k)=A;
else
x(k)=0;
end
end
%to connect the last point to zero to give a rectangular shape
%also experiment with the plot properties to see the wave
y=[0,x,0];
t=[-T0/2,t,T0/2];
figure();
plot(t,y)
xlabel('time axis')
ylabel('amlitude axis')
title('square wave')

MATLAB FIGURE :

Figure 1: Above figure is a plot of square wave signal having amplitude 3 .


SIGNAL OPERATIONS :

MATLAB CODE :
clear all
close all
clc

% scaling of the signal


t = [-2:0.001:2];
s = t;
s1 = 5*t;
plot (t,s)
hold on
plot (t,s1,'r')
xlabel('time')
ylabel('amplitude')

% chnaging the amplitude of the signal


x = sin(2*pi*0.5*t);
figure;
plot (t,x)
hold on
plot (t,5*x,'r')
hold on
plot (t,0.2*x,'g')
title('changing the amplitude of signal')
xlabel('time')
ylabel('amplitude')

% Adding the signals


p1 = s1 + x ;
p2 = x + 2;
figure;
plot(t,p1)
hold on
plot(t,p2)
title('addition of the signal')
xlabel('time')
ylabel('amplitude')

% modulation of the signal


y = 20*cos (2*pi*3*t);
z = s .* y;
figure;
plot(t,z,'m')
xlabel('time')
ylabel('amplitude')
title('modulation of the signal')
MATLAB FIGURES :

Figure 1:Above figure represent the scaling operation of the signal.blue line signal is a original sinal and red line signal is a
scaled signal.

Figure 2:Above figure represent the amplitude change of sinusoidal signal.the impact of amplitude changing is described in this
diagram.
Figure 3:Above figure represent the addition operation of the two different signals.

Figure 4: This figure represent the modulation of te real signal. In modulation the real signal is multiply with carrier signal to
shift the original signal to carrier signal frequency.

Post Lab Tasks:

Task A: Manipulating Sinusoids with MATLAB: In this task, you will generate
sinusoids with different characteristics (amplitudes and frequencies), and their composite signal.
Perform different steps as mentioned below.

MATLAB CODE :
clear all
close all
clc

A = [2 3 1 1.5 2.5]; %amplitude of the five sinusoids


F = [1 3 2 5 12]; %frequency of the five sinusoids

t1 = 0:0.01: 1/F(1); %defining timeperiod for all sinusoids

% first sinusoid

X1 = A(1).*sin(2*pi*t1); % first sinusoid containing A(1) and F(1)


subplot(3,2,1) % command used to plot multiple plots
plot(t1,X1)
title('first sinusoidal graph')
xlabel('time axis')
ylabel('amplitude')

% second sinusoid

X2 = A(2).*sin(2*pi*(F(2)*t1)); % second sinusoid containing A(2) and F(2)


subplot(3,2,2); % command used to plot multiple plots
plot(t1,X2)
title('second sinusoidal graph')
xlabel('time axis')
ylabel('amplitude')

% third sinusoid

X3 = A(3).*sin(2*pi*(F(3)*t1)); % third sinusoid containing A(3) and F(3)


subplot(3,2,3); % command used to plot multiple plots
plot(t1,X3)
title('third sinusoidal graph')
xlabel('time axis')
ylabel('amplitude')

% fourth sinusoid

X4 = A(4).*sin(2*pi*(F(4)*t1)); % fourth sinusoid containing A(4) and F(4)


subplot(3,2,4); % command used to plot multiple plots
plot(t1,X4)
title('fourth sinusoidal graph')
xlabel('time axis')
ylabel('amplitude')

% fifth sinusoid

X5 = A(5).*sin(2*pi*(F(5)*t1)); % fifth sinusoid containing A(5) and F(5)


subplot(3,2,5);
plot(t1,X5)
title('fifth sinusoidal graph')
xlabel('time axis')
ylabel('amplitude')
% addind all five sinusoid

X = X1+X2+X3+X4+X5; %summed sinusoids of all five frequencies


subplot(3,2,6); %command used for multiple plots
plot(t1,X)
title('summed sinusoidal graph')
xlabel('time axis')
ylabel('amplitude')

MATLAB FIGURE :

Figure 1: Above figure represent the five different sinusoidal signal using indiviousual calculation of all five signal. The last
graph represent the summation of all the previous ffive sinusoidal signals.

USING FAR LOOP :

MATLAB CODE :
clear all % commands used to remove all previous simulations
close all % to remove previous graphs
clc
A = [2 3 1 1.5 2.5]; % initializing the amplitudes
F = [1 3 2 5 12]; % different frequencies of sinusoidals
sum = 0; % a dummy veriable

t1 = 0:0.01: 1/F(1); %obtaining a one time on which mapping take place

for i = 1:length(A) %For loop for repeatition and ease


X = A(i).*sin(2*pi*(F(i)*t1)) %for the desired signal expression
subplot(3,2,i)
plot(t1,X)
title('sinusoidal graphs')
xlabel('time axis')
ylabel('amplitude')
sum = sum+X;
end

subplot(3,2,6) %plotting the summation of all the sinusoidal graphs


plot(t1,sum)

MATLAB FIGURES :

Figure 2: This figure shows the five different sinusoidal signal same as above but in this case we use FOR loop to calculate the
values. In last graph the summation of all the sinusoidal signal graph is represented.

Task B: Convolution and Frequency Analysis:


1) Generate two rectangular pulses of duration 1msec in time domain, plot them properly
(properly define time index, their axis, title, and labels)
2) Convolve these two signals, and properly plot the result.
3) By processing in frequency domain, multiply both signals. You will have to convert both
signals into Fourier domain, using fft function.
MATLAB CODE :
clear all %to remove all previous simulations
close all
clc

t = 0:0.01:1; % time defined for rect signal to turn on


t1 = 1:0.01:2; %time defined for rect signal to turn to 0.
t2 = [t t1]; %merge the time to get full rect. signal
x = [ones(1,length(t)) zeros(1,length(t1))]; %rect signal expression
figure();
subplot(3,1,1)
plot(t2,x)
xlabel('Time')
ylabel('Amplitude')
title('Rectangular Pulse# 1')

y = 0.5*x; %defining second rect signal but having half amplitude


subplot(3,1,2)
plot(t2,y)
xlabel('Time')
ylabel('Amplitude')
title('Rectangular Pulse# 2')

z = conv(x,y) %conv command used for convolving the two signals


subplot(3,1,3)
plot(z)
title('result after convolution of two rect signals')
xlabel('time axis')
ylabel('amplitude')

% fourier transform

X = fft(x); %fourier transform of first signal


Y = fft(y); %fourier transform of second signal

Z = X.*Y; %multiply both signal in frequency domain


figure();
subplot(2,1,1)
plot(abs(Z))
title('result after multiplication of two fft signals')
xlabel('frequency axis')
ylabel('amplitude')

z1 = ifft (Z); %taking inverse fourier to get back in time domain

subplot(2,1,2)
plot(z1)
title('result after ifft of resulting signal')
xlabel('time axis')
ylabel('amplitude')
x1 = ifft(X); %inverse fourier of first signal
y1 = ifft(Y); inverse fourier of second signal

MATLAB FIGURES :

Figure 1: This figure shows the convolution of two rect. Signal and represents the resultant tranglur signal in the last graph.

Figure 2: Above figure shows the convolution but this was obtained by the inverse fourier transform.if we take fourier
transform of both the signal and multiply them. The resultant signal inverse fft gives the same result as convolution.
Q. Convert back the output signal into time domain, and compare both outputs.

A. Both signals have same amplitude and same number of sample on time axis, so this states that
if we convolve two signal in time domain will also equal to that process in which if we first take
fft of time domain signal and then multiply both signal and taking inverse of that resulted
signal.the whole process is established for the reason that multiplication is quite easy in
comparison of convolution.

Q. What is zero padding?

A. We add extra zero’s at the end of a vector or defined function manually so that we can match
the dimensions/lengths of the function.

Q. What is relation between frequency resolution and number of samples?

A. The relationship between frequency resolution (f) and number of samples (t or n) is as


follows :

f = 1/t or f = 1/n
inverse relation exist between frequency and number of samples.

CRIRITICAL ANALYSIS :

In this lab we learnt about the fundamental concepts and basics of the
MATLAB to obtain the basics of communication signals. In In-lab task we observe the real
massage signal of exponential form and then we observed the noisy signal. We observed that the
real signal we obtained is identical and is form of exponential type and can be extracted on the
receiver side without involvement of noise. Then we add some noise in the real original signal
using random function. Random function take random values for all the samples and add these
values to the original signal, so that every sample of the original signal distorted and randomize
the signal. After getting the noisy signal we can’t predict what is the real shape of the original
signal at the receiver end so that’s why we try to eliminate noise from the desired systems. After
this task we perform certain operation on the given signal in In-Lab. We perform scaling of the
signal by which we change the amplitude ofdifferent signal and by plotting these signals we
observe how amplitude of different signal varies, we also perform addition and modulation of
different signal. Addition is quite same as we add two different number but modultion is a
important parameter. In modulation we multiply two signals and gets the resultant signal. The
resultant signal obtained the frequency which is higher in both multiplicated signal and the
amplitude of the resultant signal varies accordingly.
In Post-Lab task first we generate five different sinusoidal signal having
different amplitude and frequency. As the frequency of the signal increases the number of cycles
or repetition increases in every sinusoidal graph and amplitude changing only impacts on the peek
values. One important part of this is the addition of all the five different sinusoidal signal, we
observe that we get a random signal which didn’t have a particular behavior. Then we use FOR
loop to implement same functionality and we see that by using FOR loop it is quite easy and simple
to obtain the desired output rather than we implement every signal indiviously . inn the last task
we have to perform convolution and we see that when two different rectangle signal gets convolve
we get a triangle signal in result. First we used conv command to get the desired ouptput and then
we use the fourier transform method. If we take fft of the given two signal and multiply the
resultant signal, and then by taking inverse fourier transform we get the same result as of
convolution. So the reason of all this procedure is that the multiplication is easier than convolution
and we have to create ease in signal operations thats why we use fourier transform.
So after this lab we are very much familiar with basic building blocks of the MATLAB and the
fundamental concept of different signal operations.

You might also like