0% found this document useful (0 votes)
2 views20 pages

Basic Simulation Lab (2)

The document outlines the generation and manipulation of various signals and sequences, including step, impulse, ramp, exponential, sinusoidal, square, sinc, sawtooth, sweep, and triangular signals. It also covers operations on signals such as addition, multiplication, scaling, shifting, folding, and convolution, along with energy and power calculations. Additionally, it discusses the even and odd parts of signals, real and imaginary components, and auto and cross-correlation between sequences.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views20 pages

Basic Simulation Lab (2)

The document outlines the generation and manipulation of various signals and sequences, including step, impulse, ramp, exponential, sinusoidal, square, sinc, sawtooth, sweep, and triangular signals. It also covers operations on signals such as addition, multiplication, scaling, shifting, folding, and convolution, along with energy and power calculations. Additionally, it discusses the even and odd parts of signals, real and imaginary components, and auto and cross-correlation between sequences.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

NAME: GENERATION OF VARIOUS SIGNALS AND SEQUENCES

ROLL NO:

N = input('enter the length of sequence in even number') STEP SIGNAL


2
% step sequence

amplitude
x = ones(1,N) ; 1.5

n=0:1:N-1; 1

figure(1) 0.5
subplot(2,1,1)
0
plot(n,x) 0 0.5 1 1.5
samples ------- n
2 2.5 3

title('STEP SIGNAL') 2
STEP SEQUENCE

xlabel('samples ------- n')

amplitude
ylabel('amplitude') 1

grid
subplot(2,1,2) 0

stem(n,x) -1
-1 -0.5 0 0.5 1 1.5 2 2.5 3 3.5 4
axis([-1 N -1 2]) samples ------- n

title('STEP SEQUENCE') IMPULSE SIGNAL


1
xlabel('samples ------- n')
ylabel('amplitude')

amplitude
grid 0.5
% impulse sequence
N1=N/2;
x=[zeros(1,N1) ones(1,1) zeros(1,N1)]; 0
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
n=-N1:1:N1; samples ------- n
IMPULSE SEQUENCE
figure(2) 1
subplot(2,1,1)
amplitude

plot(n,x)
title('IMPULSE SIGNAL') 0.5

xlabel('samples ------- n')


ylabel('amplitude')
0
grid -2 -1.5 -1 -0.5 0 0.5 1 1.5 2
samples ------- n
subplot(2,1,2)
RAMP SIGNAL
stem(n,x) 3
title('IMPULSE SEQUENCE')
xlabel('samples ------- n') 2
amplitude

ylabel('amplitude')
grid 1
% ramp sequence
n=0:1:N-1;
0
x=n; 0 0.5 1 1.5 2 2.5 3
figure(3) samples ------- n
RAMP SEQUENCE
subplot(2,1,1) 4
plot(n,x)
3
title('RAMP SIGNAL')
amplitude

xlabel('samples ------- n') 2

ylabel('amplitude') 1
grid 0
subplot(2,1,2)
-1
stem(n,x) -1 -0.5 0 0.5 1 1.5 2 2.5 3 3.5 4
axis([-1 N -1 N]) samples ------- n

title('RAMP SEQUENCE') 1
EXPONENTIAL SIGNAL

xlabel('samples ------- n')


amplitude

ylabel('amplitude')
grid 0.5

% exponential sequence
n=0:1:N-1; 0
0 0.5 1 1.5 2 2.5 3
x=exp(-n); samples ------- n
figure(4) 2
EXPONENTIAL SIGNAL

subplot(2,1,1)
amplitude

plot(n,x) 1

title('EXPONENTIAL SIGNAL') 0
xlabel('samples ------- n')
ylabel('amplitude') -1
-1 -0.5 0 0.5 1 1.5 2 2.5 3 3.5 4
grid samples ------- n

subplot(2,1,2)
stem(n,x)
axis([-1 N -1 2])
title('EXPONENTIAL SIGNAL')
xlabel('samples ------- n')
ylabel('amplitude')
grid

%signal generation
f=input('enter the frequency:');
fs=10*f;
n=0:1/fs:1;
ss=sin(2*pi*f*n);

%sinusoidal signal
figure(5);
subplot(2,1,1);
plot(n,ss);
title('sinusoidal signal generation');
xlabel('time');
ylabel('amplitude');
subplot(2,1,2);
stem(n,ss);
title('sinusoidal sequence');
xlabel('samples');
ylabel('amplitude');
grid;

%square signal
sq=square(2*pi*f*n);
figure(6);
subplot(2,1,1);
plot(n,sq);
title('square wave');
xlabel('time');
ylabel('amplitude');
subplot(2,1,2);
stem(n,sq);
title('square sequence');
xlabel('samples');
ylabel('amplitude');
grid;

%sinc signal
x=-5:0.1:5;
ts=sinc(x);
figure(7);
subplot(2,1,1);
plot(x,ts);
title('sinc waveform');
xlabel('time');
ylabel('amplitude');
subplot(2,1,2);
stem(x,ts);
title('sinc sequence');
xlabel('samples');
ylabel('amplitude');
grid;

%sawtooth signal
saw=sawtooth(2*pi*2*n);
figure(8);
subplot(2,1,1);
plot(n,saw);
title('sawtooth signal');
xlabel('time');
ylabel('amplitude');
subplot(2,1,2);
stem(n,saw);
title('sawtooth sequence');
xlabel('samples');
ylabel('amplitude');
grid;

%sweep signal
ch=chirp(n,0,1,10);
figure(9);
subplot(2,1,1);
plot(n,ch);
title('sweep signal');
xlabel('time');
ylabel('amplitude');
subplot(2,1,2);
stem(n,ch);
title('sweep sequence');
xlabel('samples');
ylabel('amplitude');
grid;

%triangular signal
d=0:0.2:1;
y1=pulstran(n,d,'tripuls',0.1);
d=0.1:0.2:1;
y2=pulstran(n,d,'tripuls',0.1);
tri=y1-y2;
figure(10);
subplot(2,1,1);
plot(n,tri);
title('triangular waveform');
xlabel('time');
ylabel('amplitude');
subplot(2,1,2);
stem(n,tri);
title('triangular sequence');
xlabel('samples');
ylabel('amplitude');
grid;

RESULT:
Enter the length of the sequence in even numbers: 4
Enter the frequency: 5

PROGRAM: OPERATIONS ON SIGNALS AND SEQUENCES


ROLL NO:
% Program: Addition, Multiplication, Scaling, Shifting, Folding
t=-2:0.01:2;
f=input('enter the frequency');
x=sin(2*pi*f*t);
x1=x';
x_sum=x+x;
x_pro=x*x1;
x_sca=2*x;
x_shi=(0.2-x);
x_fol=-x;
figure(1);
subplot(3,2,1);
plot(t,x);
xlabel('t');
ylabel('x(t)');
subplot(3,2,2);
plot(t,x_sum);
grid on;
title('addition');
xlabel('t');
ylabel('x_sum(t)');
subplot(3,2,3);
plot(t,x_pro);
grid on;
title('multiplication');
xlabel('t');
ylabel('x_pro(t)');
subplot(3,2,4);
plot(t,x_sca);
grid on;
title('scaling');
xlabel('t');
ylabel('x_sca(t)');
subplot(3,2,5);
plot(t,x_shi);
grid on;
title('shifted');
xlabel('t');
ylabel('x(t)');
subplot(3,2,6);
plot(t,x_fol);
grid on;
title('folded');
xlabel('t');
ylabel('x(t)');

% computation of energy
t=-2:0.01:2;
f=input('enter the fundamental frequency');
a=input('enter the amplitude');
x=a*sin(2*pi*f*t);
figure(2);
subplot(2,1,1);
title('test signal for energy calculation');
xlabel('t');
ylabel('x(t)');
plot(t,x);
grid;
subplot(2,1,2);
area(t,x.^2);
xlabel('t');
ylabel('x^2(t)');
title('the coloured area gives the energy');
grid;
%power calculation
t=-2:0.01:2;
x1=sin(2*pi*t);
n=-40:40;
x2=sin(pi*1/10*n);
figure(3);
subplot(2,1,1);
plot(t,x1);
title('analog signal');
xlabel('t');
ylabel('x1(t)');
subplot(2,1,2);
stem(n,x2);
xlabel('n');
ylabel('x2(t)');
title('discrete sine');
figure(4);
subplot(2,1,1);
plot(t,x1.^2);
hold on;
%red lines marking 0 and 1
plot([0,0],[-2,1.2],'r',[1,1],[-2,1.2],'r');
hold off;
axis([-2 2 -.2 1.2]);
title('squared lines');
xlabel('t');
ylabel('x_1^2(t)');
subplot(2,1,2);
stem(n,x2.^2);
hold on;
% red lines marking 1 and 20
plot([1,1],[-.2 1.2],'r',[20 20],[-.2 1.2],'r');
hold off;
axis([-40 40 -.2 1.2]);
xlabel('n');
ylabel('x2^2(n)');
% calculating power of one period
disp('power,one period');
p=1/2*sum(x2(1:20).^2)
Name: Finding the even and odd parts of the signal and
Sequences, real and imaginary part of the signal.
ROLL NO:

%for even and odd part of the signal

n=-1:0.01:2;
s1=sin(2*pi*1*n)+cos(2*pi*2*n)%x(n);
s2=sin(2*pi*1*(-n))+cos(2*pi*2*(-n))%x(n);
figure(1);
subplot(2,1,1);
plot(n,s1);
title('x(n)signal');
xlabel('t');
ylabel('amplitude');
grid;
subplot(2,1,2);
plot(n,s2);
title('x(-n)');
xlabel('t');
ylabel('amplitude');
grid;

%even part of the signal


x1=(s1+s2)/2;

%odd part of the signal


x2=(s1-s2)/2;
figure(2);
subplot(2,1,1);
plot(n,x1);
title('even signal');
xlabel('t');
ylabel('amplitude');
grid;
subplot(2,1,2);
plot(n,x2);
title('odd signal');
xlabel('t');
ylabel('amplitude');
grid;

%seperation of complex signals


n=0:0.01:1;
x1=cos(2*pi*n);
x2=sin(2*pi*n);
x3=complex(x1,x2);
r=real(x3);
i=imag(x3);
figure(3);
subplot(2,1,1);
plot(x1);
title('signal 1 as real part');
xlabel('t');
ylabel('amplitude');
grid;
subplot(2,1,2);
plot(x2);
title('signal 2 as imaginary part');
xlabel('t');
ylabel('amplitude');
grid;
figure(4);
subplot(3,1,1);
plot(x3);
title('complex signals');
xlabel('real axis');
ylabel('imaginary axis');
grid;
subplot(3,1,2);
plot(r);
title('real part of complex signal');
xlabel('t');
ylabel('amplitude');
grid;
subplot(3,1,3);
plot(i);
title('imaginary of complex signal');
xlabel('t');
ylabel('amplitude');
grid;
Name: convolution between signals and sequences
[Link]:
clc;
%linear convulution
x=input('enter the input sequences:');
h=input('enter the impulse sequences:');
n1=length(x);
disp(n1);
n2=length(h);
disp(n2);
n3=n1+n2-1;
disp('the resultant length is=');n3
y=conv(x,h);
figure(1);
subplot(3,1,1);
t1=0:1:(n1-1);
stem(t1,x);
xlabel('n');
ylabel('x(n)');
title('input sequence');
subplot(3,1,2);
t2=0:1:(n2-1);
stem(t2,h);
xlabel('n');
ylabel('h(n)');
title('impulse sequences');
subplot(3,1,3);
t3=0:1:(n3-1);
stem(t3,y);
xlabel('n');
ylabel('y(n)');
title('linear convulution response');
disp('the resultant signal is:');y

%circular convulution
x=input('enter the first sequence');
y=input('enter the second sequence');
n1=length(x);
n2=length(y);
N=max(n1,n2);
if n3>=0
y=[y,zeros(1,n3)];
else
x=[x,zeros(1,n3)];
end
a=length(x);
figure(2);
subplot(3,1,1);
t1=0:1:(a-1);
stem(t1,x);
xlabel('n');
ylabel('x(n)');
title('first sequence after zero padding');
subplot(3,1,2);
b=length(y);
t2=0:1:(b-1);
stem(t2,y);
xlabel('n');
ylabel('y(n)');
title('second sequence after zero padding');
for n=1:N;
m(n)=0;
for i=1:N
j=n-i+1;
if j<=0
j=N+j;
end
m(n)=m(n)+x(i)*y(j);
end
end
subplot(3,1,3);
t3=0:1:a-1;
stem(t3,m);
xlabel('n');
ylabel('x(n)');
title('circular convuluted result');

%convolution between signals


f=input('enter the fundamental frequency');
title('impulse sequence');
t=-pi:0.01:pi;
x=sin(2*pi*f*t);
h=cos(2*pi*f*t);
n1=length(x);
disp(n1);
n2=length(h);
disp(n2);
n3=n1+n2-1;
disp('resultant length is:');n3
y1=conv(x,h);
figure(3);
subplot(3,1,1);
t1=0:1:(n1-1);
stem(t1,x);
xlabel('n');
ylabel('x(n)');
title('input sequence');
subplot(3,1,2);
t2=0:1:(n2-1);
stem(t2,h);
xlabel('n');
ylabel('h(n)');
title('impulse sequence');
t3=0:1:(n3-1);
subplot(3,1,3);
stem(t3,y1);
xlabel('n');
ylabel('y(n)');
title('linear convuluted response');
disp('the resultant signal is:');y1
RESULT:
Enter the input sequences : [1 2 3 4]
Enter the impulse sequences : [3 5 6]
4
3
The resultant length is = n3 = 6

The resultant signal is :


y =3 11 25 39 38 24
Enter the first sequence : [1 -1 -2 3 -1]
Enter the second sequence : [1 2 3]

Enter the fundamental frequency : 2


6 2 9
6 2 9
Resultant length is: n3 = 1 2 5 7
Name: Auto and cross correlation between signals and sequences
[Link]:

clc;
%cross correlation signal
x=input('enter the first sequence');
y=input('enter the second sequence');
n1=length(x)-1;
n2=length(y)-1;
k=-n1:n2;
figure(1);
subplot(3,1,1);
t1=0:1:n1;
stem(t1,x);
xlabel('n');
ylabel('amplitude');
title('seq1');
subplot(3,1,2);
t2=0:1:n2;
stem(t2,y);
xlabel('n');
ylabel('amplitude');
title('seq2');
r=xcorr(x,y);
subplot(3,1,3);
stem(k,r);
xlabel('log index');
ylabel('amplitude');
title('cross correlated signal');
disp('cross correlated result');

%auto correalation signal


t=-pi:0.01:pi;
f=input('enter the fundamental frequency');
x=sin(2*pi*f*t);
n1=length(x)-1;
k=-n1:n1;
figure(2);
t1=0:1:n1;
subplot(2,1,1);
stem(t1,x);
xlabel('n');
ylabel('amplitude');
title('input for auto correlation');
r=xcorr(x,x);
subplot(2,1,2);
stem(k,r);
xlabel('log index');
ylabel('amplitude');
title('auto correlated signal');
disp('auto correlated result');

INPUTS:
Enter the first sequence : [1 2 3]
Enter the second sequence : [4 5 6]
enter the fundamental frequency = 3
Name: Verification of linearity and time invariant
[Link]:

%time variant
x=input('enter the input applied to the system:');
p=input('enter the time at which input is arrived:');
m=input('enter the time instant at which you want to calculate the output:');
k=m-p;
x(p)=x;
f=menu('choose a function','cos(x)','(n+n(m))');
if f==1
y1=cos(x(p));
y2=cos(x(m-k));
if y1==y2
disp('the given system y(n)=cos(r(n)) is time invariant');
else
disp('the given system y(n)=cos(x(n)) is the time variant');
end
else
y1=p*x(m-k);
if y1==y2
disp('the given system y(n)=n*x(n) is time invarient');
else
disp('the given system y(n)=n*x(n) is time varient');
end
end

%linear property
x1=input('enter the value of first input:');
x2=input('enter the value of second input');
a1=input('enter the weighing value for first input a1=');
a2=input('enter the weighing value for second input a2=');
f=menu('choose a function','cos(x)','2*x(n)');
if f==1
y1=a1*cos(x1)+a2*cos(x2);
y2=cos((a1*x1)+(a2*x2));
if y1==y2
disp('y=cos(x) is linear system');
else
disp('y=cos(x) is non linear system');
end
else
y1=a1*2*x1+a2*2*x2;
y2=2*((a1*x1)+(a2*x2));
if y1==y2
disp('y=2*x is linear system');
else
disp('y=2*x is non linear system');
end
end
RESULT:
Enter the input applied to the system: 5
Enter the time at which input is arrived: 4
Enter the time instant at which you want to calculate the output :
3
The given system y(n) = cos(r(n)) is time invariant.
Enter the value of first input : 5
Enter the value of second input : 2
Enter the weighing value for first input a1 = 4
Enter the weighing value for second input a2 = 1
Y = cos(x) is non linear system.
Enter the input applied to the system : 3
Enter the time at which input is arrived : 4
Enter the time instant at which you want to calculate the output :
1
The given system y(n) = n*x(n) is time variant.
Enter the value of first input : 3
Enter the value of second input : 5
Enter the weighing value for first input a1 = 1
Enter the weighing value for second input a2 = 2
Y = 2*x is linear system.
Name: Computation of unit sample, unit step and sinusoidal responses of given
linear time invariant system and verify its stability properties.

[Link]:

clc;
num=input('enter the numerator polynomial=');
den=input('enter the denominator polynomial=');
sys=tf(num,den)
%to find step response
s=step(sys);
figure(1);
plot(s);
title('step response');
xlabel('time');
ylabel('amplitude');
grid;
%to find impulse response
i=impulse(sys);
figure(2);
plot(i);
title('impulse response');
xlabel('time');
ylabel('amplitude');
%to find sinusoidal response
n=0:0.01:1;
x=sin(2*pi*1*n);
s=lsim(sys,x,n);
figure(3);
subplot(2,1,1);
plot(n,x);
subplot(2,1,2);
plot(n,s);
title('sinusoidal response');
xlabel('time');
ylabel('amplitude');
grid;
%alternate method to plot system response
ltiview({'step','impulse'},sys);
%to find stability of system
p=pole(sys);
p=real(p);
p1=max(p);
if p1>0
disp('given system is unstable');
else
disp('the given system is stable');
end
OUTPUT:
Enter the numerator polynomial=[1 2]
Enter the denominator polynomial=[3 1]

Transfer function:
s + 2
-------
3 s + 1
The given system is stable.
Name: Finding the Fourier transform of a given signal and plotting its
magnitude phase spectrum.
Roll No:

clc;
n=input('enter the length of input signal:');
%prepare to sample signal for two seconds at a rate of 100 sample per second
fs=100
t=[0:2*fs+1]/fs;
x=sin(2*pi*t)+sin(4*pi*t);
figure(1);
subplot(4,1,1);
plot(t,x);
title('signal in time constant');
xk=fft(x,n);
disp(xk);
m=abs(xk);
subplot(4,1,2);
plot(m);
title('magnitude spectrum');
a=angle(xk);
subplot(4,1,3);
plot(a);
title('phase spectra');
xn=ifft(xk,n);
disp(xn);
subplot(4,1,4);
plot(xn);
title('inverse fourier transform');
grid;

%FFT AND IFET complex signal


f1=input('enter the frequency of signal 1:');
f2=input('enter the frequency of signal 2:');
fs=5*max(f1,f2);
n=fs;
t=0:1/fs:1-1/fs;
x=sin(2*pi*f1*t)+sin(2*pi*f2*t);
figure(2);
subplot(4,1,1);
plot(t,x);
title('signal is time domain');
grid;
y=fft(x,n);
disp(y);
m=abs(y);
f=(0:n-1)/(n/fs);
subplot(4,1,2);
plot(t,m);
title('magnitude spectra');
a=angle(y);
subplot(4,1,3);
plot(f,a);
title('phase spectra');
xn=ifft(y);
disp(xn);
subplot(4,1,4);
plot(t,xn);
title('inverse fourier transform');
grid;
OUTPUT:
Enter the length of input signal : 4
fs = 100
1.1177 -0.3740 + 0.3674i -0.3696 -0.3740 - 0.3674i
-0.0000 0.1881 0.3740 0.5555
Enter the frequency of signal 1 : 50
Enter the frequency of signal 2 : 50
1.0e+002 *
NAME: WAVEFORM SYNTHESIS USING LAPLACE TRANSFORM
ROLL NO:

clc;
%verifying laplace transform
%finding laplace transform
syms('t','s');
f=exp(-2*t);
F=laplace(f,t,s);
simplify(F);
pretty(ans);
%finding inverse laplace transform
a=(1/(s+2));
pretty(a);
f=ilaplace(a);
simplify(f);
pretty(f);

OUTPUT:
1
-----
s + 2

1
-----
s + 2

1
--------
exp(2 t)
LOCATING POLES, ZEROS AND POLE ZERO MAP IN S-PLANE AND Z-PLANE FOR GIVEN
TRANSFER FUNCTION.
[Link]:

clc;
num=input('enter the numerator polynomial:');
den=input('enter the denominator polynomial');
h=tf(num,den);
p=pole(h);
z=zero(h);
hpz=zpk(h);
hz=tf(num,den,0.1,'variable','z^-1');
pa=pole(hz);
za=zero(hz);
hzpz=zpk(hz);
subplot(1,2,1);
pzmap(h);
subplot(1,2,2);
pzmap(hz);

OUTPUT:
Enter the numerator polynomial : [3 5 9]
Enter the denominator polynomial : [2 8 5]
SAMPLING THEOREM VERIFICATION
[Link]:

clc;
f=input('enter the frequency');
fs=input('enter the sampling frequency');
t=0:1:fs+f-1;
if fs>=2*f
disp('sampling theorem is satisfied');
%to plot sinusoidal
n=0:1/fs:pi;
x=sin(2*pi*n);
figure(1);
plot(n,x);
title('sinusoidal signal');
xlabel('time');
ylabel('amplitude');
%to plot frequency spectrum
s1=0:1/f:1-1/f;
s2=(1-1/f)-s1;
w1=[s2,zeros(1,fs)];
w2=[zeros(1,(fs-f)),s1,s2];
figure(2);
plot(t,w1,'r',t,w2,'b');
title('frequency plot');
xlabel('frequency');
ylabel('amplitude');
legend('original signal','lower side band and upper side band');
else
disp('sampling theorem is not satisfied');
%to plot frequency spectrum
s1=0:1/f:1-1/f;
s2=1-s1;
w1=[s2,zeros(1,fs)];
w2=[zeros(1,(fs-f)),s1,s2];
title('frequency plot');
xlabel('frequency');
ylabel('amplitude');
legend('original signal','lower side band and upper side band');
end
OUTPUT:
Case 1:
Enter the frequency : 5
Enter the sampling frequency : 50
Sampling theorem is satisfied.

Case 2:
Enter the frequency : 20
Enter the sampling frequency : 30
Sampling theorem is not satisfied.
PRO:13 [Link]:
DEMONSTRATION OF GIBB’S PHENOMENON
Roll No:

clc;
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*pi)];
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','linewidth',2);
box;
hold on;
plot(t,fn,'linewidth',2);
hold off;
axis([-2 2 -0.5 2.5]);
grid;
xlabel('time');
ylabel('y-n(n)');
title(['N=',nq]);
end

OUTPUT:
NAME: TO SIMULATE WEINER KHINCHEN’S THEOREM
[Link]:

clear all;
t=0:0.1:2*pi;
x=sin(2*t);
subplot(3,2,1);
title('original signal---x(t)');
plot(x);
au=xcorr(x,x);
subplot(3,2,2);
title('auto correlation signal---Rxx(tau)');
plot(au);
v=fft(au);
subplot(3,2,3);
title('PSD signal---Sxx(t)');
plot(abs(v));
fw=ifft(v);
subplot(3,2,4);
title('auto correlation signal---Rxx(tau)');
plot(fw);

OUTPUT:

You might also like