KEC-453 S&S Lab Manual
KEC-453 S&S Lab Manual
DEPARTMENT
OF
ELECTRONICS & COMMUNICATION ENGINEERING
1 Introduction to MATLAB: To define & use variables, vectors, Matrices & its 1-5
functions in MATLAB. To study various arithmetic operators and CO1
mathematical functions in MATLAB. To create & use m-files.
2 Basic plotting of signals: To study various MATLAB commands for creating 6-9
two and three dimensional plots.
Write a MATLAB program to plot the following continuous time and discrete CO1
time Signals. .i. Step Function ii. Impulse Function iii. Exponential Function
iv. Ramp Function v. Sine Function
3 Write a MATLAB program to obtain linear convolution of the given 10-11
sequences. CO2
5 Write a MATLAB program to obtain Cross correlation of sequence x(n) and 16-18
y(n) & autocorrelation of a sequence x (n) of the given sequences & verify the CO4
property.
6 Write a MATLAB program to generate Fourier series of a Square Wave. 19-20
CO3
7 Write a MATLAB program to Calculate and plot using MATLAB Fourier 21-23
Transform and Z-Transform of a given signal. CO3
8 Write a MATLAB program to find the impulse response and step response of 24-25
a system from its difference equation. CO4
Compute and plot the response of a given system to a given input.
9 Write a MATLAB program to plot magnitude and phase response of a given 26-28
system. CO5
EXPERIMENT NO. 1
OBJECT:
Introduction to MATLAB.
(i) To define and use variables and functions in MATLAB.
(ii) To define and use vectors, matrices and polynomials in MATLAB.
(iii)To study various MATLAB arithmetic operators and mathematic function.
(iv) To create and use m-files.
SOFTWARE USED: MATLAB 7.9
It has numerous built-in commands and math functions that help you in mathematical
calculations, generating plots, and performing numerical methods.
The clear command deletes all (or the specified) variable(s) from the memory.
Row vectors
Column vectors
Row vectors are created by enclosing the set of elements in square brackets, using space or
comma to delimit the elements.
For example,
r = [7 8 9 10 11]
Column vectors are created by enclosing the set of elements in square brackets, using
semicolon(;) to delimit the elements.
Creating Matrices
A matrix is a two-dimensional array of numbers.
m = [1 2 3; 4 5 6; 7 8 9]
MATLAB ENVIRONMENT:
Command Window
Command History
Workspace
Current Directory
Figure Window
Command Window:
Whenever MATLAB is invoked, the main window called command window is activated. The
command window displays the command prompt’>>’ and a cursor where commands are entered
and are executed instantaneously.
Workspace:
A workspace is a collection of all the variables that have been generated so far in the current
MATLAB session and shows their data type and size. All the commands executed from
Command Window and all the script files executed from the Command Window share common
workspace, so they can share all the variables.
Current Directory:
The Current Directory window contains all the files and folders present in the Current Directory.
To run any file, it must either be in the Current Directory or on the search path.
Edit Window:
An Edit Window is used to create a new program file, or to modify existing files. In this window,
programs can be written, edited and saved. The programs written using the MATLAB editor are
automatically assigned an extension (.m) by the editor and are known as M- files.
Figure Window:
A Figure Window is a separate window with default white background and is used to display
MATLAB graphics. The results of all the graphic commands executed are displayed in the figure
window.
Matrix arithmetic operations are same as defined in linear algebra. Array operations are executed
element by element, both on one dimensional and multi-dimensional array.
The matrix operators and arrays operators are differentiated by the period (.) symbol. However,
as the addition and subtraction operation is same for matrices and arrays, the operator is same for
both cases.
Description
Operator
Addition or unary plus. A+B adds the values stored in variables A and B. A and B
+ must have the same size, unless one is a scalar. A scalar can be added to a matrix
of any size.
Subtraction or unary minus. A-B subtracts the value of B from A. A and B must
- have the same size, unless one is a scalar. A scalar can be subtracted from a
matrix of any size.
For non-scalar A and B, the number of columns of A must be equal to the number
of rows of B. A scalar can multiply a matrix of any size.
.* Array multiplication. A.*B is the element-by-element product of the arrays A and
B. A and B must have the same size, unless one of them is a scalar.
Slash or matrix right division. B/A is roughly the same as B*inv(A). More
/
precisely, B/A = (A'\B')'.
Array right division. A./B is the matrix with elements A(i,j)/B(i,j). A and B must
./
have the same size, unless one of them is a scalar.
Backslash or matrix left division. If A is a square matrix, A\B is roughly the same
as inv (A)*B, except it is computed in a different way. If A is an n-by-n matrix
\ and B is a column vector with n components, or a matrix with several such
columns, then X = A\B is the solution to the equation AX = B. A warning message
is displayed if A is badly scaled or nearly singular.
Array left division. A.\B is the matrix with elements B(i,j)/A(i,j). A and B must
.\
have the same size, unless one of them is a scalar.
Array power. A. ^B is the matrix with elements A (i,j) to the B(i,j) power. A and
.^
B must have the same size, unless one of them is a scalar.
Matrix transpose. A' is the linear algebraic transpose of A. For complex matrices,
'
this is the complex conjugate transpose.
Array transpose. A' is the array transpose of A. For complex matrices, this does
.'
not involve conjugation.
Apart from the above-mentioned arithmetic operators, MATLAB provides the following
commands/functions used for similar purpose −
Function Description
Unary plus; increments by the amount a
uplus(a)
plus (a,b) Plus; returns a + b
uminus(a) Unary minus; decrements by the amount a
minus(a, b) Minus; returns a - b
times(a, b) Array multiply; returns a.*b
mtimes(a, b) Matrix multiplication; returns a* b
ECE Department, Prepared By Checked By
SRMGPC, Lko Page 4
PROGRAM:-
a = 10;
b = 20;
c=a+b
d=a-b
e=a*b
f=a/b
g=a\b
x = 7;
y = 3;
z=x^y
The M Files
Scripts − script files are program files with .m extension. In these files, you write series
of commands, which you want to execute together. Scripts do not accept inputs and do
not return any outputs. They operate on data in the workspace.
Functions − functions files are also program files with .m extension. Functions can
accept inputs and return outputs. Internal variables are local to the function.
You can use the MATLAB editor or any other text editor to create your .m files.
RESULTS:- All the operations have been performed a using MATLAB and results have been
seen on the command window.
PRECAUTIONS:-
1) Program must be written carefully to avoid errors.
2) Programs can never be saved as standard function name.
3) Functions in MATLAB are case sensitive so commands must be written in proper
format.
RELATED QUESTIONS:-
EXPERIMENT NO. 2
OBJECT:-
i) Plot the basic signals (Impulse, Step function and Ramp function)
ii) To create 2-D and 3-D plots.
PROCEDURE:-
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window
THEORY:-
a) Impulse Function
The impulse function is defined as
and
δ(t = 0 for t ≠ 0)
That is the impulse function has zero amplitude everywhere expect at t = 0.
δ(t)
0 t
b) Step Function
The unit step function is defined as
U(t) = 1 for t ≥ 0
= 0 for t > 0
ut
1.0
0 1 2 3 4 5 t
Program:
t=(-2:0.01:10);
impulse = t==0;
unitstep = t>=0;
plot(t, impulse)
plot(t, unitstep)
ramp = t.*unitstep;
plot(t,ramp)
xlabel('Time')
ylabel('Amplitude')
title('impulse function')
title('unit step function')
title('ramp function')
c) 2-D plot
Define x as a vector of linearly spaced values between 0 and . Use an increment
of π/10 between the values. Define y as sine values of x.
Program:
clc
clear all
x=(0:pi/10:2*pi)
y=sin(x)
plot(x,y)
title('2D Plot')
xlabel('Time')
ylabel('Amplitude')
d) 3-D plot
A three-dimensional plot may refer to
a) A graph or plot embedded into a three-dimensional space
b) The plot of a function of two variables, embedded into a three-dimensional space
Program:
clc
clear all
t=(-4:0.01:4)
x=t.^2
y=4*t
plot3(x,y,t)
grid on
xlabel('x-axis')
ylabel('y-axis')
zlabel('z-axis')
title('3D Plot')
RESULTS:
Results have been seen on the command window.
Figure-1
Figure-2
Figure-3
ECE Department, Prepared By Checked By
SRMGPC, Lko Page 9
2D Plot
1
0.8
0.6
0.4
0.2
Amplitude 0
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6 7
Time
Figure-4
3D Plot
2
z-axis
-2
-4
20
10 20
0 15
10
-10 5
y-axis -20 0
x-axis
Figure-5
PRECAUTIONS:-
1) Program must be written carefully to avoid errors.
2) Programs can never be saved as standard function name.
3) Functions in MATLAB are case sensitive so commands must be written in proper
format.
RELATED QUESTIONS:-
EXPERIMENT NO. 3
THEORY:
Convolution is a mathematical operation used to express the relation between input and
output of an LTI system. It relates input, output and impulse response of an LTI system
as
y(t)=x(t)∗h(t)
Where y (t) = output of LTI
x (t) = input of LTI
h (t) = impulse response of LTI
b) Discrete Convolution
PROGRAM:
clc;
clear all;
close;
disp('enter the length of the first sequence m=');
m=input('');
disp('enter the first sequence x[m]=');
for i=1:m
x(i)=input('');
end
disp('enter the length of the second sequence n=');
n=input('');
disp('enter the second sequence h[n]=');
for j=1:n
h(j)=input('');
end
y=conv(x,h);
figure;
subplot(3,1,1);
stem(x);
ylabel ('amplitude---->');
xlabel('n---->');
title('x(n) Vs n');
subplot(3,1,2);
stem(h);
ylabel('amplitude---->');
xlabel('n---->');
title('h(n) Vs n');
subplot(3,1,3);
stem(y);
ylabel('amplitude---->');
xlabel('n---->');
title('y(n) Vs n');
disp('linear convolution of x[m] and h[n] is y');
INPUT:--
Enter the length of the first sequence m=
6
Enter the length of first sequence x[m]=
1
2
3
4
5
6
Enter the length of the second sequence n=
6
Enter the length of second sequence h[n]=
1
2
3
4
5
6
OUTPUT:-
RESULTS: - Thus the program for linear convolution is written using MATLAB and verified.
PRECAUTIONS:
1) Program must be written carefully to avoid errors.
RELATED QUESTIONS:
1. What is the MATLAB working environment?
2. What is the function of MATLAB function clear all?
3. What function can be used in MATLAB to calculate the linear convolution of two
sequences?
4. State mathematical formula for convolution of 2 discrete time signals X1 (n) & X2 (n)
5. What is the use of subplot function?
EXPERIMENT NO. 4
PROCEDURE:-
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window
THEORY:
Amplitude Scaling
In the amplitude scaling, a signal y(t) resulting from a signal x(t), on amplitude scaling
by a real value k becomes:
y(t)=k x(t)
Program:
clc
clear all
t=(-3:0.01:7)
unitstep=t>=0
u1=3*unitstep
ramp = t.*unitstep
r1=2*ramp
plot(t,unitstep)
hold on
plot(t,ramp)
hold on
plot(t,u1)
hold on
plot(t,r1)
title('amplitude-scaling')
xlabel('Time')
ylabel('Amplitude')
Time Shifting
The time shifting of the signal x(t) is done by replacing t by t-t0 . Thus x(t-t0) represents
x(t) time shifting by t0. If t0 is positive, the shift is to the right, i.e. x(t- t0) represents the
signal x(t) delayed by t0 and if t0 is negative, the shift is to the left, i.e., x(t+ t0) represents
the signal x(t) advanced by t0.
Program:
clc
clear all
t=(-1:0.01:10)
impulse=t==0
unitstep=t>=0
u1=t>=3
u2=t>=5
plot(t,unitstep,'r')
hold on
plot(t,u1,'g')
hold on
plot(t,u2,'m')
legend('unitstep','u1','u2')
title('Time-Shifting')
xlabel('Time')
ylabel('Amplitude')
Time scaling
clear all;
close all;
clc;
k=2;
x1=[1 2 3 4 5];
a=length(x1);
n=0:1:a-1;
subplot(3,1,1);
stem(x1);
xlabel('number of samples');
ylabel('amplitude');
title('input signal');
x2=k*x1;
subplot(3,1,2);
stem(x2);
xlabel('number of samples');
ylabel('amplitude');
title('amplified signal');
x3=x1/k;
subplot(3,1,3);
stem(x3);
xlabel('number of samples');
ylabel('amplitude');
title('attenuated signal');
ECE Department, Prepared By Checked By
SRMGPC, Lko Page 17
display(x1);
display(x2);
display(x3);
RESULTS:-
Results have been seen on the command window.
amplitude-scaling
14
12
10
8
Amplitude
0
-3 -2 -1 0 1 2 3 4 5 6 7
Time
Figure-1
Time-Shifting
1
unitstep
0.9 u1
u2
0.8
0.7
0.6
Amplitude
0.5
0.4
0.3
0.2
0.1
0
-2 0 2 4 6 8 10
Time
Figure-2
Time scaling
Figure-3
PRECAUTIONS:-
1) Program must be written carefully to avoid errors.
2) Programs can never be saved as standard function name.
3) Functions in MATLAB are case sensitive so commands must be written in proper
format.
RELATED QUESTIONS
EXPERIMENT NO. 5
OBJECT:-Write a MATLAB program to compute autocorrelation and cross correlation of a
sequence.
PROCEDURE:-
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window
THEORY:
Auto-correlation: Autocorrelation function gives the measure of similarity, match or
coherence between a signal and its delayed replica. This means that autocorrelation function
is a special from of a cross- correlation function. It is defined as the correlation of a signal
with itself.
Consider a signal x(t).Then the autocorrelation function of this signal with its delayed version
will be
R(τ) = lim
T→∞
The autocorrelation function is defined separately for energy signals and for power or periodic
signals.
Cross-Correlation: The correlation or more precisely cross-correlation between two different
waveforms or two signals is the measure of match or similarity or regularity between one
signal and time delayed version of the other signal.
Let us consider two general complex signals x1(t) and x2(t) is defined as
R12(τ) = lim
T→∞
PROGRAM:
stem(x1)
xlabel('Discrete time')
ylabel('Amplitude')
title('Input Sequence')
subplot(2,1,2)
ECE Department, Prepared By Checked By
SRMGPC, Lko Page 20
stem(y)
title('Autocorelation of the Input Sequence')
xlabel('Discrete time')
xlabel('Discrete time')
RESULTS:
Results have been seen on the command window.
Input Sequence
4
3
Amplitude
0
1 1.5 2 2.5 3 3.5 4
Discrete time
Autocorelation of the Input Sequence
30
Amplitude
20
10
0
1 2 3 4 5 6 7
Discrete time
Cross-correlation
r = xcorr(x,y)
r = xcorr(x)
r = xcorr(___,maxlag)
r = xcorr(___,scaleopt)
[r,lags] = xcorr(___)
example
Cross Correlation of two vectors
Create a vector x and a vector y that is equal to x shifted by 5 elements to the right. Compute and
plot the estimated cross-correlation of x and y. The largest spike occurs at the lag value when the
elements of x and y match exactly (-5).
n = 0:15;
x = 0.84.^n;
y = circshift(x,5);
[c,lags] = xcorr(x,y);
stem(lags,c)
Normalized Cross-Correlation
Compute and plot the normalized cross-correlation of vectors x and y with unity peak, and
specify a maximum lag of 10.
n = 0:15;
x = 0.84.^n;
y = circshift(x,5);
[c,lags] = xcorr(x,y,10,'normalized');
stem(lags,c)
PRECAUTIONS:
ECE Department, Prepared By Checked By
SRMGPC, Lko Page 22
RELATED QUESTIONS:
EXPERIMENT NO. 6
OBJECT: Write a MATLAB program to generate Fourier series of a Square Wave.
PROCEDURE:-
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window
THEORY:
Let’s assume we have a square wave with following characteristics:
Period=2ms
Peak−to−Peak Value=2 V
Average Value=0 V
So, we can express it as:
If g(t) is given by
Now, a Mat lab code can be written for g(t) between 0 and 4ms with an interval of 0.05 ms to
demonstrate that g(t) is a decent approximation of original function x(t).
PROGRAM:
for m = 1: tpts % Here, we'll consider all & quot;t& quot; points to cover &
quot;from 0 to 4ms interval & quot;
s1(n,m) = (4/pi)*(1/(2*n - 1))*sin((2*n - 1)*2*pi*f*dt*(m-1)); % Approximate Fourier
Series g(t)
end
end
for m = 1:tpts
a1 = s1(:,m); % VERY IMPORTANT ! Here, we are assigning a1 for each
single column (total 81)
a2(m) = sum(a1); % Here, we are summing up the whole column to one single
value (adding all 12 values in one column)
end
% Now, we have a row vector "a2" with total values
"81"
f1 = a2'; % Here, we have final values "f1" (total 81 points) as
transpose of a2 computed above
t = 0.0:5.0e-5:4.0e-3; % it's already given in text (0 to 4ms with interval of 0.05 ms)
plot(t,f1)
xlabel('Time, s')
ylabel('Amplitude, V')
title('Fourier Series Expansion')
RESULTS:
Results have been seen on the figure window.
PRECAUTIONS:
1) Program must be written carefully to avoid errors.
2) Programs can never be saved as standard function name.
3) Functions in MATLAB are case sensitive so commands must be written in proper format.
RELATED QUESTIONS:
EXPERIMENT NO. 7
OBJECT:
Write a MATLAB program to Calculate and plot using MATLAB Fourier Transform and
Z-Transform of a given signal.
PROCEDURE:-
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window
THEORY:
Z transform:-
Fourier Transform:-
The Fourier transform of the function f is traditionally denoted by adding
a circumflex: There are several common conventions for defining the Fourier
transform of an integral function f : ℝ → ℂ. Here we will use the following definition:
PROGRAM:
clc
clear all
syms x
f=exp(-2*x^2) %our function
ezplot(f,[-2,2]) % plot of our function
FT=fourier(f) %Fourier transform
ezplot(FT)
z transform of
clc
clear all
syms z n
ztrans(1/4^n)
Inverse z transform of
clc
clear all
syms z n
ECE Department, Prepared By Checked By
SRMGPC, Lko Page 28
iztrans(2*z/(2*z-1))
1.2
0.8
0.6
0.4
0.2
-6 -4 -2 0 2 4 6
w
Figure-1
RESULTS:
Z- Transform of x(n) = ¼n u(n)
x(z = z/(z-1/4)
Inverse Z- Tranforms of x(z) = 2z/2z-1
X(n) = (1/2)n
PRECAUTIONS:
1) Program must be written carefully to avoid errors.
2) Programs can never be saved as standard function name.
3) Functions in MATLAB are case sensitive so commands must be written in proper
format.
RELATED QUESTIONS:
1. What are functions used in MATLAB to find z transform and Fourier transform?
2. Find Z Transform of X(n) = nu(n).
3. Find the inverse Fourier transform of f(t)=1.
4. What is the set of all values of z for which X(z) attains a finite value?
5. The Z-Transform X(z) of a discrete time signal x(n) is defined as ____________.
EXPERIMENT NO. 8
OBJECT: Write a MATLAB program to find the impulse response and step response of a
system from its difference equation.
PROCEDURE:-
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window
THEORY:
Step Response
Step Response is the output from a system excited with a step input. The output step response
lasts for infinite time.
Impulse Response
Impulse Response is the output from a system excited with a unit impulse input. The output
impulse response lasts for infinite time.
PROGRAM:
Step Response: Determine the first 50 values of the step response of a system with a known
impulse response h[n]=0.5n *u[n].
clc
clear all
h=0.5.^[0:49]
x=ones(1,50)
y=conv(x,h)
stem([0:49],y(1:50))
Impulse Response: Determine the first 50 values of the step response of a system with a known
impulse response h[n] =0.5n *u[n]. Here define delta as a vector containing 21 elements.
Element number 11 is one the rest are zeros.)
clc
clear all
delta=[ zeros(1,10), 1, zeros(1,10)]
h=0.5.^[0:49]
y=conv(delta,h)
stem([0:49],y(1:50))
RESULTS:
Results have been seen on the command window.
2
1.8
1.6
1.4
1.2
0.8
0.6
0.4
0.2
0
0 5 10 15 20 25 30 35 40 45 50
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
0 5 10 15 20 25 30 35 40 45 50
PRECAUTIONS:
1) Program must be written carefully to avoid errors.
2) Programs can never be saved as standard function name.
3) Functions in MATLAB are case sensitive so commands must be written in proper format.
RELATED QUESTIONS:
1. What is the relation between the step response and impulse response?
2. What is the use of conv function in MATLAB?
3. What will be the step response of the impulse response of a ramp function?
4. To save a file, what should be the extension of file?
5. In MATLAB, the impulse response of the step response of a system is ___ to the step response
of the impulse response of the system.
EXPERIMENT NO. 9
OBJECT: Write a MATLAB program to plot magnitude and phase response of a given
system.
PROCEDURE:-
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window
THEORY:
and
(t)= dΩ for all t
Where
(jΩ) is real part of X(jΩ)
and
X (jΩ) is imaginary part of X(jΩ)
X(jΩ) =
X(jΩ) = tan-1
The plot between | X (jΩ) | vs Ω is known as amplitude spectrum and the plot between
X (jΩ) vs Ω is known as phase spectrum.
ECE Department, Prepared By Checked By
SRMGPC, Lko Page 33
PROGRAM:
clc
clear all
t = 0:1/100:10-1/100;
x = sin(2*pi*15*t) + sin(2*pi*40*t);
y = fft(x);
m = abs(y);
p = unwrap(angle(y));
f = (0:length(y)-1)'*100/length(y);
subplot(2,1,1), plot(f,m)
ylabel('Abs. Magnitude')
grid on
hold on;
subplot(2,1,2), plot(f,p*180/pi)
ylabel('Phase [Degrees]')
grid on
xlabel('Frequency [Hertz]')
RESULTS:
Results have been seen on the command window.
600
Abs. Magnitude
400
200
0
0 10 20 30 40 50 60 70 80 90 100
4
x 10
3
Phase [Degrees]
-1
0 10 20 30 40 50 60 70 80 90 100
Frequency [Hertz]
Figure-1
PRECAUTIONS:
RELATED QUESTIONS:
EXPERIMENT NO. 10
OBJECT: Checking Linearity/Non-Linearity of a system using SIMULINK.
PROCEDURE:-
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window
THEORY:
That is,
T[a (t) + b (t)] = aT[ (t)] + bT[ (t)]
PROGRAM:
clc
clear all
a=2
n=3
S1 = @(x) a*x.^n %// anonymous function 1 describing your system
x1 = 1:4 %// test input signal
x2 = 11:14 %// test input signal. Same length
r1 = S1(x1)+S1(x2)
r2 = S1(x1+x2)
S2 = @(x) a*x %// anonymous function 2 describing your system
r1 = S2(x1)+S2(x2)
r2 = S2(x1+x2)
RESULTS:
a=
2
n=
3
S1 =
@(x)a*x.^n
x1 =
1 2 3 4
x2 =
11 12 13 14
r1 =
r2 =
S2 =
@(x)a*x
r1 =
24 28 32 36
r2 =
24 28 32 36
ECE Department, Prepared By Checked By
SRMGPC, Lko Page 37
PRECAUTIONS:
RELATED QUESTIONS: