Basic Simulation Raghav
Basic Simulation Raghav
ECE, PVKKIT
1.AIM: Generate various signals and sequences (Periodic and aperiodic), such as Unit
Impulse,Unit Step, Square, Saw tooth, Triangular, Sinusoidal, Ramp, Sinc.
PROGRAM:
% Generation of signals and sequences
clc;
clear all;
close all;
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%generation of unit impulse signal
t1=-1:0.01:1
y1=(t1==0);
subplot(2,2,1);
plot(t1,y1);
xlabel('time');
ylabel('amplitude');
title('unit impulse signal');
%generation of impulse sequence
subplot(2,2,2);
stem(t1,y1);
xlabel('n');
ylabel('amplitude');
title('unit impulse sequence');
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%generation of unit step signal
t2=-10:1:10;
y2=(t2>=0);
subplot(2,2,3);
plot(t2,y2);
xlabel('time');
ylabel('amplitude');
title('unit step signal');
%generation of unit step sequence
subplot(2,2,4);
stem(t2,y2);
xlabel('n');
ylabel('amplitude');
title('unit step sequence');
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%generation of square wave signal
t=0:0.002:0.1;
y3=square(2*pi*50*t);
figure;
subplot(2,2,1);
plot(t,y3);
axis([0 0.1 -2 2]);
xlabel('time');
ylabel('amplitude');
title('square wave signal');
%generation of square wave sequence
subplot(2,2,2);
stem(t,y3);
Basic simulation lab Manual Prepared by S.Raghavendra , Asst.Prof. ECE, PVKKIT
ylabel('amplitude');
title('sin wave sequence');
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%generation of ramp signal
y7=t;
figure;
subplot(2,2,1);
plot(t,y7);
xlabel('time');
ylabel('amplitude');
title('ramp signal');
%generation of ramp sequence
subplot(2,2,2);
stem(t,y7);
xlabel('n');
ylabel('amplitude');
title('ramp sequence');
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%generation of sinc signal
t3=linspace(-5,5);
y8=sinc(t3);
subplot(2,2,3);
plot(t3,y8);
xlabel('time');
ylabel('amplitude');
title(' sinc signal');
%generation of sinc sequence
subplot(2,2,4);
stem(y8);
xlabel('n');
ylabel('amplitude');
title('sinc sequence');
Result: Various signals & sequences generated using Matlab software.
Basic simulation lab Manual Prepared by S.Raghavendra , Asst.Prof. ECE, PVKKIT
Basic simulation lab Manual Prepared by S.Raghavendra , Asst.Prof. ECE, PVKKIT
Basic simulation lab Manual Prepared by S.Raghavendra , Asst.Prof. ECE, PVKKIT
2.AIM: perform the operations on signals and sequences such as addition, multiplication,
scaling, shifting, folding and also compute energy and power
Program:
clc;
clear all;
close all;
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% generating two input signals
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
Basic simulation lab Manual Prepared by S.Raghavendra , Asst.Prof. ECE, PVKKIT
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');
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%operations on sequences
n1=1:1:9;
s1=[1 2 3 0 5 8 0 2 4];
figure;
subplot(2,2,1);
stem(n1,s1);
xlabel('n1');
ylabel('amplitude');
title('input sequence1');
s2=[1 1 2 4 6 0 5 3 6];
subplot(2,2,2);
stem(n1,s2);
xlabel('n2');
ylabel('amplitude');
title('input sequence2');
% addition of sequences
s3=s1+s2;
subplot(2,2,3);
stem(n1,s3);
Basic simulation lab Manual Prepared by S.Raghavendra , Asst.Prof. ECE, PVKKIT
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');
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% program for energy of a sequence
z1=input('enter the input sequence');
e1=sum(abs(z1).^2);
disp('energy of given sequence is');e1
% program for energy of a signal
t=0:pi:10*pi;
z2=cos(2*pi*50*t).^2;
e2=sum(abs(z2).^2);
disp('energy of given signal is');e2
% program for power of a sequence
p1= (sum(abs(z1).^2))/length(z1);
disp('power of given sequence is');p1
% program for power of a signal
p2=(sum(abs(z2).^2))/length(z2);
disp('power of given signal is');p2
OUTPUT:
enter the input sequence[1 3 2 4 1]
energy of given sequence is
e1 = 31
energy of given signal is
e2 = 4.0388
power of given sequence is
p1 = 6.2000
power of given signal is
p2 = 0.3672
Result: Various operations on signals and sequences are performed.
Basic simulation lab Manual Prepared by S.Raghavendra , Asst.Prof. ECE, PVKKIT
Basic simulation lab Manual Prepared by S.Raghavendra , Asst.Prof. ECE, PVKKIT
Basic simulation lab Manual Prepared by S.Raghavendra , Asst.Prof. ECE, PVKKIT
3.AIM: Finding even and odd part of the signal and sequence and also find real and
imaginary parts of signal.
Program:
clc
close all;
clear all;
%Even and odd parts of a signal
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');
% Even and odd parts of a sequence
x1=[0,2,-3,5,-2,-1,6];
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);
xlabel('n');
ylabel('amplitude');
title('input sequence with n= -n');
even1=.5*(x1+y1);
odd1=.5*(x1-y1);
% plotting even and odd parts of the sequence
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');
Basic simulation lab Manual Prepared by S.Raghavendra , Asst.Prof. ECE, PVKKIT
ylabel('amplitude');
title('odd part of sequence');
% plotting real and imginary parts of the signal
x2=sin(t)+j*cos(t);
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.
Basic simulation lab Manual Prepared by S.Raghavendra , Asst.Prof. ECE, PVKKIT
Basic simulation lab Manual Prepared by S.Raghavendra , Asst.Prof. ECE, PVKKIT
4.Aim: Write the program for convolution between two signals and also between two
Sequences
Program:
clc;
close all;
clear all;
%program for convolution of two sequences
x=input('enter input sequence: ');
h=input('enter impulse response: ');
y=conv(x,h);
subplot(3,1,1);
stem(x);
xlabel('n');
ylabel('x(n)');
title('input sequence')
subplot(3,1,2);
stem(h);
xlabel('n');
ylabel('h(n)');
title('impulse response sequence')
subplot(3,1,3);
stem(y);
xlabel('n');
ylabel('y(n)');
title('linear convolution')
disp('linear convolution y=');
disp(y)
%program for signal convolution
t=0:0.1:10;
Basic simulation lab Manual Prepared by S.Raghavendra , Asst.Prof. ECE, PVKKIT
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);
xlabel('t');
ylabel('h(t)');
title('impulse response')
subplot(3,1,3);
plot(y1);
xlabel('n');
ylabel('y(n)');
title('linear convolution');
5.Aim: To compute Auto correlation and Cross correlation between signals and sequences.
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
Autocorrelation
Program:
clc;
Basic simulation lab Manual Prepared by S.Raghavendra , Asst.Prof. ECE, PVKKIT
close all;
clear all;
% two input sequences
x=input('enter input sequence');
h=input('enter the impulse suquence');
subplot(2,2,1);
stem(x);
xlabel('n');
ylabel('x(n)');
title('input sequence');
subplot(2,2,2);
stem(h);
xlabel('n');
ylabel('h(n)');
title('impulse sequence');
% cross correlation between two sequences
y=xcorr(x,h);
subplot(2,2,3);
stem(y);
xlabel('n');
ylabel('y(n)');
title(' cross correlation between two sequences ');
% auto correlation of input sequence
z=xcorr(x,x);
subplot(2,2,4);
stem(z);
xlabel('n');
ylabel('z(n)');
title('auto correlation of input sequence');
plot(z1);
xlabel('t');
ylabel('z1(t)');
title('cross correlation ');
% auto correlation
subplot(2,2,4);
z2=xcorr(x1,x1);
plot(z2);
xlabel('t');
ylabel('z2(t)');
title('auto correlation ');
Result: Auto correlation and Cross correlation between signals and sequences is computed.
Output: enter input sequence [1 2 5 7]
enter the impulse sequence [2 6 0 5 3]
Basic simulation lab Manual Prepared by S.Raghavendra , Asst.Prof. ECE, PVKKIT
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.
If x(n) is a input signal and y(n) is a output signal then
y(n)=T[x(n)]
y1(n)=T[x1(n)] and y2(n)=T[x2(n)]
x3=[a*x1(n) +b *x2(n) ]
Y3(n)= T [x3(n)]
T [a*x1(n)+b*x2(n) ] = a y1(n)+ b y2(n)
Program:
% Verification of Linearity of a given System.
% a) y(n)=nx(n) b) y=x^2(n)
clc;
clear all;
close all;
n=0:40;
a1=input('enter the scaling factor a1=');
a2=input('enter the scaling factor a2=');
x1=cos(2*pi*0.1*n);
x2=cos(2*pi*0.4*n);
x3=a1*x1+a2*x2;
%y(n)=n.x(n);
y1=n.*x1;
y2=n.*x2;
y3=n.*x3;
yt=a1*y1+a2*y2;
Basic simulation lab Manual Prepared by S.Raghavendra , Asst.Prof. ECE, PVKKIT
yt=round(yt);
y3=round(y3);
if y3==yt
disp('given system [y(n)=n.x(n)]is Linear');
else
disp('given system [y(n)=n.x(n)]is non Linear');
end
%y(n)=x(n).^2
x1=[1 2 3 4 5];
x2=[1 4 7 6 4];
x3=a1*x1+a2*x2;
y1=x1.^2;
y2=x2.^2;
y3=x3.^2;
yt=a1*y1+a2*y2;
if y3==yt
disp('given system [y(n)=x(n).^2 ]is Linear');
else
disp('given system is [y(n)=x(n).^2 ]non Linear');
end
Result: The Linearity of a given Discrete System is verified.
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
7. AIM: Finding even and odd part of the signal and sequence and also find real and
imaginary parts of signal.
TIME INVARIENT SYSTEMS(TI):
A system is called time invariant if its input – output characteristics do not change with time
X(t)---- input : Y(t) ---output
X(t-k) -----delay input by k seconds : Y(t-k) ------ Delayed output by k seconds
If Y(t)=T[X(t)] then Y(t-k)=T[X(t-k)] then system is time invariant system.
Program:
% Verification of Time Invariance of a Discrete System
% a)y=x^2(n) b) y(n)=nx(n)
clc;
clear all;
close all;
n=1:9;
x(n)=[1 2 3 4 5 6 7 8 9];
d=3; % time delay
xd=[zeros(1,d),x(n)];%x(n-k)
y(n)=x(n).^2;
yd=[zeros(1,d),y];%y(n-k)
disp('transformation of delay signal yd:');disp(yd)
dy=xd.^2; % T[x(n-k)]
disp('delay of transformation signal dy:');disp(dy)
if dy==yd
disp('given system [y(n)=x(n).^2 ]is time invariant');
else
disp('given system is [y(n)=x(n).^2 ]not time invariant');
end
y=n.*x;
yd=[zeros(1,d),y(n)];
disp('transformation of delay signal yd:');disp(yd);
Basic simulation lab Manual Prepared by S.Raghavendra , Asst.Prof. ECE, PVKKIT
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
Result: The Time Invariance of a given Discrete System is verified.
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
given system [y(n)=x(n).^2 ]is time invariant
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 4 10 18 28 40 54 70 88 108
given system [y(n)=nx(n)]not a time invariant