Lab Manual MATLAB - 241004 - 114937
Lab Manual MATLAB - 241004 - 114937
MATLAB Laboratory
List of Experiments
1
Experiment No: 1
Basic Operations on Signals and sequences
Signal Addition
Addition: any two signals can be added to form a third signal, z (t) = x (t) + y (t)
Multiplication
Multiplication of two signals can be obtained by multiplying their values at every instant,
z(t) = x (t) y (t)
Time reversal/Folding
Time reversal of a signal x(t) can be obtained by folding the signal about t=0.Y(t)=y(-t)
Time shifting:
The time shifting of x(n) obtained by delay or advance the signal in time by using
y(n)=x(n+k)
If k is a positive number, y(n) shifted to the right i e the shifting delays the signal
If k is a negative number, y(n ) it gets shifted left. Signal Shifting advances the signal
Energy:
Average power:
2
Program:
clc; clear all;close all;
t=0:.01:1;
x1=sin(2*pi*4*t) x2=sin(2*pi*8*t);
subplot(2,2,1); plot(t,x1);
xlabel('time'); ylabel('amplitude'); title('input signal 1');subplot(2,2,2);plot(t,x2);
xlabel('time'); ylabel('amplitude'); title('input signal 2');
% addition of signals
y1=x1+x2;
subplot(2,2,3);plot(t,y1);
xlabel('time');ylabel('amplitude'); title('addition of two signals');
% multiplication of signals
y2=x1.*x2;
subplot(2,2,4);plot(t,y2);
xlabel('time'); ylabel('amplitude'); title('multiplication of two signals');
% scaling of a signal1
A=2; y3=A*x1;
figure; subplot(2,2,1);plot(t,x1);
xlabel('time');ylabel('amplitude');title('input signal');
subplot(2,2,2);plot(t,y3);
xlabel('time');ylabel('amplitude'); title('amplified input signal');
% folding of a signal1
h=length(x1);
nx=0:h-1;
subplot(2,2,3) plot(nx,x1);
xlabel('nx');ylabel('amplitude');title('input signal');
y4=fliplr(x1); nf=-fliplr(nx);
subplot(2,2,4);plot(nf,y4);
xlabel('nf');ylabel('amplitude'); title('folded signal');
%shifting of a signal 1
figure; subplot(3,1,1);plot(t,x1);
xlabel('time t');ylabel('amplitude'); title('input signal');subplot(3,1,2); plot(t+2,x1);
xlabel('t+2');ylabel('amplitude'); title('right shifted signal');
subplot(3,1,3);plot(t-2,x1);
xlabel('t-2');ylabel('amplitude'); title('left shifted signal');
3
%operations on sequences
% addition of sequences
s3=s1+s2;
subplot(2,2,3);stem(n1,s3);
xlabel('n1');ylabel('amplitude');title('sum of two sequences');
% multiplication of sequences
s4=s1.*s2;
subplot(2,2,4);stem(n1,s4);
xlabel('n1');ylabel('amplitude'); title('product of two sequences');
OUTPUT:
enter the input sequence [1 3 2 4 1]
energy of given sequence is E = 31 Units
energy of given signal is = 4.0388 Units
power of given sequence is p1 = 6.2000
power of given signal is p2 = 0.3672
4
Output:
5
6
Experiment No-2
Even and odd parts of signal and sequence & Real and imaginary parts of Signal
AIM: Finding even and odd part of the signal and sequence and also find real and imaginary
parts of signal.
Even and odd part of a signal: Any signal x(t) can be expressed as sum of even and odd
components i.e ., x(t)=xe(t)+xo(t)
Program:
clc; close all; clear all;
t=0:.001:4*pi;
x=sin(t)+cos(t); % x(t)=sint(t)+cos(t)
subplot(2,2,1) plot(t,x);
xlabel('t'); ylabel('amplitude'); title('input signal');
y=sin(-t)+cos(-t); % y(t)=x(-t)
subplot(2,2,2); plot(t,y);
xlabel('t'); ylabel('amplitude'); title('input signal with t= -t')
even=(x+y)/2;
subplot(2,2,3); plot(t,even);
xlabel('t'); ylabel('amplitude'); title('even part of the signal');
odd=(x-y)/2;
subplot(2,2,4); plot(t,odd);
xlabel('t'); ylabel('amplitude'); title('odd part of the signal');
n=-3:3;
y1= fliplr(x1);%y1(n)=x1(-n) figure;
subplot(2,2,1); stem(n,x1);
xlabel('n'); ylabel('amplitude'); title('input sequence');
subplot(2,2,2); stem(n,y1);
7
xlabel('n'); ylabel('amplitude'); title('input sequence with n= -n');
even1=.5*(x1+y1);
odd1=.5*(x1-y1);
subplot(2,2,3); stem(n,even1);
xlabel('n'); ylabel('amplitude'); title('even part of sequence');
subplot(2,2,4); stem(n,odd1);
xlabel('n'); ylabel('amplitude'); title('odd part of sequence');
figure;
subplot(3,1,1); plot(t,x2);
xlabel('t'); ylabel('amplitude'); title('input signal');
subplot(3,1,2); plot(t,real(x2));
xlabel('time'); ylabel('amplitude'); title('real part of signal');
subplot(3,1,3); plot(t,imag(x2));
xlabel('time'); ylabel('amplitude'); title('imaginary part of siganl');
RESULT: Even and odd part of the signal and sequence, real and imaginary parts of signal are
computed.
Output:
8
9
Experiment No-3(a)
Verification of Linearity of a Discrete System
Theory:
LINEARITY PROPERTY:
Any system is said to be linear if it satisfies the superposition principal. superposition
principal state that Response to a weighted sum of input signal equal to the corresponding
weighted sum of the outputs of the system to each of the individual input signals.
%y(n)=n.x(n);
10
Output:
enter the scaling factor a1=3
enter the scaling factor a2=5
given system [y(n)=n.x(n)]is Linear
given system is [y(n)=x(n).^2 ]non Linear
11
Experiment No -3(b)
Theory:
TIME INVARIENT SYSTEMS(TI):
A system is called time invariant if its input – output characteristics do not change with
Program:
yd=[zeros(1,d),y];%y(n-k)
disp('transformation of delay signal yd:');
disp(yd);
dy=xd.^2; % T[x(n-k)]
y=n.*x;
yd=[zeros(1,d),y(n)];
disp('transformation of delay signal yd:');
disp(yd);
n1=1:length(xd);
dy=n1.*xd;
disp('delay of transformation signal dy:');
disp(dy);
if yd==dy
disp('given system [y(n)=nx(n)]is a time invariant');
else;
disp('given system [y(n)=nx(n)]not a time invariant');
end
12
Output:
transformation of delay signal yd: [0 0 0 1 4 9 16 25 36 49 64
81]
delay of transformation signal dy: [0 0 0 1 4 9 16 25 36 49 64 81]
13
Experiment No-4
Unit sample, unit step and sinusoidal response of the given LTI system and verifying its
stability
AIM: Compute the Unit sample, unit step and sinusoidal response of the given LTI
system and verifying its stability
Theory:
A discrete time system performs an operation on an input signal based on predefined
criteria to produce a modified output signal. The input signal x(n) is the system
excitation, and y(n) isthe system response. The transform operation is shown as,
If the input to the system is unit impulse i.e. x(n) = δ(n) then the output of the system is
known as impulse response denoted by h(n) where,
h(n) = T[δ(n)]
we know that any arbitrary sequence x(n) can be represented as a weighted sum of
discrete impulses. Now the system response is given by,
14
Program:
x1=(n==0);
%impulse response
y1=filter(b,a,x1);subplot(3,1,1);stem(n,y1);
xlabel('n'); ylabel('y1(n)'); title('impulse response');
x2=(n>0);
% step response
y2=filter(b,a,x2);subplot(3,1,2);stem(n,y2);
xlabel('n'); ylabel('y2(n)') title('step response');
% sinusoidal response
y3=filter(b,a,x3);
subplot(3,1,3); stem(t,y3);
xlabel('n'); ylabel('y3(n)'); title('sin response');
% verifing stability
figure; zplane(b,a);
Result: The Unit sample, unit step and sinusoidal response of the given LTI system is
computed and its stability verified. Hence all the poles lie inside the unit circle, so
system is stable.
15
Output:
16
Experiment No-5
Convolution between signals & sequences
Aim: Write the program for convolution between two signals and also between
twosequences.
Software Required: Matlab software
Theory:
Program:
clc; close all; clear all;
t=0:0.1:10;
x1=sin(2*pi*t);h1=cos(2*pi*t);y1=conv(x1,h1);
figure;
subplot(3,1,1);plot(x1);
xlabel('t'); ylabel('x(t)'); title('input signal')
subplot(3,1,2); plot(h1);
17
xlabel('t');ylabel('h(t)'); title('impulse response')
subplot(3,1,3); plot(y1);
xlabel('n'); ylabel('y(n)'); title('linear convolution');
18
Experiment No-6
Auto correlation and Cross correlation
Aim: To compute Auto correlation and Cross correlation between signals and sequences.
Theory:
Correlations of sequences:
It is a measure of the degree to which two sequences are similar. Given two real-valued
sequences x(n) and y(n) of finite energy,
Convolution involves the following operations.
1. Shifting
2. Multiplication
3. Addition
These operations can be represented by a Mathematical Expression as follows:
Cross correlation
rx,y = ∑+∞
−∞ 𝑥 (𝑛)𝑦(𝑛 − 𝑙)
Autocorrelation
Rx,x = ∑+∞
−∞ 𝑥 (𝑛)𝑥(𝑛 − 𝑙)
The index l is called the shift or lag parameter
Program:
y=xcorr(x,h);
subplot(2,2,3) stem(y);
xlabel('n'); ylabel('y(n)');
title(' cross correlation between two sequences ');
z=xcorr(x,x);
subplot(2,2,4) stem(z);
19
xlabel('n'); ylabel('z(n)');
title('auto correlation of input sequence');
t=0:0.2:10;
x1=3*exp(-2*t); h1=exp(t);
figure; subplot(2,2,1);plot(t,x1);
xlabel('t'); ylabel('x1(t)');
title('input signal');
subplot(2,2,2); plot(t,h1);
xlabel('t'); ylabel('h1(t)');
title('impulse signal');
% cross correlation
z1=xcorr(x1,h1); subplot(2,2,3);plot(z1);
xlabel('t'); ylabel('z1(t)');
title('cross correlation ');
% auto correlation
z2=xcorr(x1,x1);
subplot(2,2,4); plot(z2);
xlabel('t'); ylabel('z2(t)');
title('auto correlation ');
Result: Auto correlation and Cross correlation between signals and sequences is computed.
20
21
Experiment No-7
Gibbs phenomenon
Program:
% Gibb's phenomenon.
clc;
clear all;
close all;
t=linspace(-2,2,2000);
u=linspace(-2,2,2000);
sq=[zeros(1,500),2*ones(1,1000),zeros(1,500)];k=2;
N=[1,3,7,19,49,70];
for n=1:6;an=[];
for m=1:N(n)
an=[an,2*k*sin(m*pi/2)/(m*p)];
end;
fN=k/2;
for m=1:N(n)
fN=fN+an(m)*cos(m*pi*t/2);
end;
nq=int2str(N(n)); subplot(3,2,n),
plot(u,sq,'r');
hold on;
plot(t,fN);
hold off;
axis([-2 2 -0.5 2.5]);
grid;
xlabel('Time');
ylabel('y_N(t)');
title(['N= ',nq]);
end;
Result: In this experiment Gibbs phenomenona have been demonstrated using MATLAB.
22
Output:
23
Experiment No-8
Finding the Fourier Transform of a given signal and plotting its magnitude and phase
spectrum
AIM: To find the Fourier Transform of a given signal and plotting its magnitude and
phase spectrum.
Program:
k=0:N-1;
Xmag=abs(x1);
subplot(3,1,2); plot(k,Xmag); grid;
xlabel('t'); ylabel('amplitude'); title('magnitude of fft signal')
%phase spectrum
Xphase=angle(x1);
subplot(3,1,3);plot(k,Xphase);
grid; xlabel('t'); ylabel('angle'); title('phase of fft signal');
24
Output:
25
Experiment No-09
Sampling Theorem Verification
The maximum frequency component of g(t) is fm. To recover the signal g(t) exactly from its
samples it has to be sampled at a rate fs ≥ 2fm.
The minimum required sampling rate fs = 2fm is called ' Nyquist rate
Figure 2: (a) Original signal g(t) (b) Spectrum G(w)δ (t) is the sampling signal with fs = 1/T > 2fm.
Figure 5: Recovery of signal by filtering with a fiter of width 2wm Aliasing ws < 2wm.
Aliasing leads to distortion in recovered signal. This is the reason why sampling frequency
should be atleast twice thebandwidth of the signal. Oversampling ws >2wm. This condition
avoid aliasing.
Program:
OUTPUT:
28