Signals and Systems - Labmanual - Matlab PDF
Signals and Systems - Labmanual - Matlab PDF
Signals and Systems - Labmanual - Matlab PDF
Program:
>> x=rand(3,4)
Program:
Output:
>> x = [ 67 75 78 12 13; 21 22 64 87 87]
x=
Output:
0.9649
0.9572
0.1419
0.7922
0.1576
0.4854
0.4218
0.9595
0.9706
0.8003
0.9157
0.6557
x=
67
75
78
12
13
21
22
64
87
87
Output:
close all;
x=
t=0:0.01:1;
x=sin(2*pi*t);
plot(t,x); % plots vector Y versus vector X.
Figure:
(i)Program:
>> y=x'
Output:
y=
2
(ii) Program:
>> z=x+y
Ex.5. Write a Matlab Code to generate and
display (i) Unit Impulse (ii)Step signal
(iii)Rectangular Pulse (iv) Triangular Pulse (v)
square wave (vi) Decreasing exponential:
Output:
z=
4
12
15
(i) Unit Impulse:
12
10
11
15
11
Program:
%Unit Impulse:
Ex.3 Write a Matlab Code to generate Random
sequence.
PROF.KINJAL VAGADIA
n=-10:0.001:10;
ASSISTANT PROF.
GEC,BHAVNAGAR
LABORATORY MANUAL
delta=(n==0);
subplot(2,1,1);
plot(n,delta);% continuous time
xlabel('time-->');
ylabel('signal value');
title('unit impulse signal');
subplot(2,1,2);
(iii)Program:
% Rectangular pulse
xlabel('time-->');
clc;
ylabel('signal value');
clear all;
close all;
Figure:
close all;
Program :
fs = 10;
clc;
ts = [0:1/fs:5 5:1/fs:10];
clear all;
x = [zeros(1,51) ones(1,51)];
close all;
stairs(ts,x); % draws a stairstep graph
of the elements in vector Y at the
locations specified in X.
Figure:
PROF.KINJAL VAGADIA
ASSISTANT PROF.
GEC,BHAVNAGAR
LABORATORY MANUAL
Program:
Program:
clc;
clc;
close all;
clear all;
clear all;
close all;
%syms a t w
t = -10:0.001:10;
t=0:0.1:10;
y=exp(-5*t);
plot(t,x);
plot(t,y);
title('Square Wave');
Figure:
xlabel('Time-->');
ylabel('Signal value');
axis([-10 10 0 1.5]);% sets scaling for the x- and yaxes on the current plot.
Figure:
PROF.KINJAL VAGADIA
ASSISTANT PROF.
GEC,BHAVNAGAR
LABORATORY MANUAL
subplot(3,1,3);
Program:
xlabel('Time-->');
clc;
ylabel('Signal Value');
clear all;
close all;
Figures:
plot(t,x3);
t=-2*pi:0.001:+2*pi;
% Rectangular Signal
x=square(2*pi*1/(2*pi)*t);
subplot(3,1,1);
plot(t,x);
xlabel('time-->');
ylabel('Signal value');
title('periodic rectangular signal');
N=10 (No.of harmonics)
N=15
stem(n1,x1);
xlabel('value');
ylabel('Freq. index')
title('Discrete fourier spectrum');
%Reconstruction
x3(1,:)=sum(x2);
PROF.KINJAL VAGADIA
ASSISTANT PROF.
GEC,BHAVNAGAR
LABORATORY MANUAL
N=55
N=105
k=0:L-1;
wn=exp(-j*2*pi/L);
nk=n'*k;
wLnk=wn.^nk;
df=x*wLnk;
subplot(3,1,1);
stem(x,abs(df));
title('DTFT Mag');
xlabel('k');
ylabel('Magnitude');
N=500
subplot(3,1,2);
stem(x,angle(df));
PROF.KINJAL VAGADIA
ASSISTANT PROF.
GEC,BHAVNAGAR
LABORATORY MANUAL
title('DTFT phase');
xlabel('k');
ylabel('phase');
wn=exp(j*2*pi/L);
nk=n'*k;
Ex.9. Write a Matlab code to Show Continuous
and Discrete Sine Wave.
wLnk=wn.^nk;
inversdf=(x*wLnk)/L;
Program:
subplot(3,1,3);
n=-10:10; % row vector, time points,
stem(x,abs(inversdf));
theta=2*pi/20;%frequency parameter
title('Inverse DTFT');
xlabel('N');
x=0.8*sin(theta*n);%sinusoidal
column vector x
sig.
stored
ylabel('Magnitude');
Figure::
in
xlabel('n'), ylabel('x[n]');
plot(n,x,'r');% continuous time plot in red colour
Figure:
close all;
PROF.KINJAL VAGADIA
ASSISTANT PROF.
GEC,BHAVNAGAR
LABORATORY MANUAL
hold on
stem(t1,x_ana);
f_samp1 = 50;
t_samp1 = 0:1/f_samp1:1;
xs1 = sin(2*pi*5*t_samp1);
figure;
plot(t_samp1,xs1,'--b');
hold on
stem(t_samp1,xs1,'r');
f_samp2 = 7.5;
t_samp2 = 0:1/f_samp2:1;
xs2 = sin(2*pi*5*t_samp2);
figure;
plot(t_samp2,xs2,'r')
hold on
stem(t_samp2,xs2,'b')
hold off
clear all;
close all;
t = -10:0.01:10;% sampling rate
10KHz
x2 = rectpuls(t,1);
subplot(211);
plot(t,x2); %continuous time
stem(t,x2);%discrete
xlabel('Time ');
Figures:
ylabel('Signal Value');
title('Rectangular Pulse');
x3 = rectpuls(t-4,1);
subplot(212);
plot(t,x3); %continuous time
stem(t,x3);%discrete
Figure:
Continuous
Time:
Discrete Time:
PROF.KINJAL VAGADIA
ASSISTANT PROF.
GEC,BHAVNAGAR
LABORATORY MANUAL
Program:
clear all;
clear all;
close all;
close all;
t = -5:0.001:5;
clc;
x1 = rectpuls(t,3);
t =[ -8:0.001:8];
subplot(221);
subplot(3,1,1);
xlabel('Time ');
ylabel('Signal Value');
xlabel('Time-->');
title('Rectangular Pulse Continuous');
ylabel('sig.value');
subplot(222);
subplot(3,1,2);
stem(t,x1);%discrete
plot (t ,( sin ( -2*(t -2))).*((t >2)))% d e l a y i n g
x(t) by 2 s e c ond
xlabel('Time ');
title('delayed signal');
ylabel('Signal Value');
title('Rectangular Pulse Discrete');
subplot(3,1,3)
plot (t ,( sin ( -2*(t+2))) .*(t > -2)) % advanc ing
x(t) by 2 s e c ond
x2 = rectpuls(4*t,3);
subplot(223);
xlabel('time-->');
xlabel('Time ');
ylabel('Signal Value');
Figures:
PROF.KINJAL VAGADIA
Program:
ASSISTANT PROF.
GEC,BHAVNAGAR
LABORATORY MANUAL
%Expansion
Figure:
clear all;
close all;
t = -5:0.001:5;
x1 = rectpuls(t,3);
subplot(221);
plot(t,x1); %continuous time
xlabel('Time ');
ylabel('Signal Value');
title('Rectangular Pulse Continuous');
subplot(222);
stem(t,x1);%discrete
xlabel('Time ');
ylabel('Signal Value');
title('Rectangular Pulse Discrete');
x2 = rectpuls(t/2,3);
subplot(223);
xlabel('Time-->');
xlabel('Time ');
ylabel('sig.value');
ylabel('Signal Value');
subplot(3,1,2);
subplot(224);
stem(t,x2);%discrete
xlabel('Time ');
title('compressed signal');
subplot(3,1,3);
ylabel('Signal Value');
xlabel('time-->');
ylabel('sig.value');
title ('expanded signal');
PROF.KINJAL VAGADIA
ASSISTANT PROF.
GEC,BHAVNAGAR
LABORATORY MANUAL
Figure:
(iii) Compression
Sinusoidal Signal:
Figure:
and
Expansion
using
Program:
clear all;
Ex.13. Write a Matlab code to perform Folding
in Time Domain.
close all;
clc;
Program:
t =[ -8:0.001:8];
%Time reversal: Folding
subplot(3,1,1);
clear all;
plot (t , sin(t) );%original signal plot
close all;
title( 'original s i g n a l' );
t=-5:0.001:5;
xlabel('Time-->');
subplot(3,1,2);
ylabel('sig.value');
x1=exp(t);
subplot(221);
title('compressed signal');
subplot(3,1,3)
xlabel('Time ');
ylabel('Signal Value');
xlabel('time-->');
title('Original signal');
ylabel('sig.value');
subplot(222);
stem(t,x1);%discrete
xlabel('Time ');
ylabel('Signal Value');
title('Exponential sequence Discrete');
x2 = fliplr(x1);
subplot(223);
PROF.KINJAL VAGADIA
ASSISTANT PROF.
GEC,BHAVNAGAR
LABORATORY MANUAL
clear all;
xlabel('Time ');
t=-10:0.001:10;
ylabel('Signal Value');
title('time reversed');
u=(t>=0);
subplot(224);
subplot(411);
stem(t,x2);%discrete
plot(t,u);
xlabel('Time ');
x1=exp(-1.5*t).*u;
ylabel('Signal Value');
subplot(4,1,2);
title('time reversed');
plot(t,x1);
Figure:
title('x1(t)');
%Time reversed signal x1(-t)
x2=fliplr(x1);
%Even component x1e(t)
x1e=0.5*(x1+x2);
subplot(4,1,3);
plot(t,x1e);
title('even component');
%Odd component x1o(t)
t=-5:0.1:5;
x1o=0.5*(x1-x2);
subplot(4,1,4);
plot(t,x1o);
xlabel('time');
ylabel('signal value');
title('odd component');
Figure:
Ex.14. Write a Matlab code to find Even and
Odd functions of (i) Exponential signal (ii) Sine
wave.
(i)Exponential signal:
Program:
%Even_odd
clc;
close all;
PROF.KINJAL VAGADIA
ASSISTANT PROF.
GEC,BHAVNAGAR
LABORATORY MANUAL
x1=sin(t).*u;
Program:
subplot(4,1,2);
plot(t,x1);
clc;
title('x1(t)');
clear all;
close all;
x2=fliplr(x1);
x1=3.005;
A=0.654
x1e=0.5*(x1+x2);
omega=0:0.001*pi:2*pi
subplot(4,1,3);
x2=[2.5 -1.766]
plot(t,x1e);
x3=1-(A.*exp((-j).*(omega)))
title('even component');
x3=x1./x3
x4=conj(x3);
x1o=0.5*(x1-x2);
mag=abs(x3);
subplot(4,1,4);
phase_angle=angle(x3);
plot(t,x1o);
freqz(x1,x2)
xlabel('time');
figure,plot(omega,20*log10(mag))
magnitude vs frequency
% absolute value of c
% angle of c
% plot the
ylabel('signal value');
xlabel('frequency-angular');
title('odd component');
ylabel('magnitude');
Figure:
title('magnitude vs angular-Frequency')
grid on;
figure,plot(omega,phase_angle)
PROF.KINJAL VAGADIA
ASSISTANT PROF.
GEC,BHAVNAGAR
LABORATORY MANUAL
xlabel('angular Frequency');
ylabel('Phase_angle_variations');
title('Variations in Phase vs angular-Frequencyl')
%plot phase vs frequency
grid on;
energy=x3.*x4
figure,plot(omega,20*log10(energy))
energy vs frequency
% plot
xlabel('angular Frequency');
ylabel('Energy');
title('Energy vs Frequency')
grid on;
Figure:
PROF.KINJAL VAGADIA
ASSISTANT PROF.
GEC,BHAVNAGAR
LABORATORY MANUAL
subplot(2,1,1);
plot(n,h);
subplot(2,1,2);
stem (n,h);
subplot(3,1,1);
stem(n,y);
Figure:
axis([0 10 -5 5]);
xlabel('n-->');
ylabel('signal value');
title('o/p of system for x[n]');
subplot(3,1,2);
stem(n,yt);
axis([0 10 -5 5]);
xlabel('n-->');
ylabel('signal value');
(a)Program:
xlabel('n-->');
clear all;
ylabel('difference value');
close all;
clc;
Figure:
n=0:10;
a=2;
b=-3;
x1=cos(2*pi*.1*n);%i/p1
x2=cos(2*pi*.5*n); %i/p2
x=a.*x1+b.*x2;%homogenity and superposition
y=sqrt(x);% square root system---o/p(1)
y1=sqrt(x1); %square root system
PROF.KINJAL VAGADIA
(b) Program:
clc;
ASSISTANT PROF.
GEC,BHAVNAGAR
LABORATORY MANUAL
clear all;
x1=sin(2*pi*.1*n);%i/p1
close all;
x2=cos(2*pi*.5*n); %i/p2
x1=[1 6 8 9];
x2=[5 8 2 0];
y=0.5*x; %o/p(1)
y1=sqrt(x1);
y1=0.5*x1;
y2=sqrt(x2);
y2=0.5*x2;
x=x1+x2;
(a) Program:
clc;
clear all;
close all;
n=0:10;
stem(n,d);
axis([0 10 -5 5]);
xlabel('n-->');
ylabel('difference value');
title('difference between o/ps');
Figure:
a=2;
b=-3;
PROF.KINJAL VAGADIA
ASSISTANT PROF.
GEC,BHAVNAGAR
LABORATORY MANUAL
(c) Program:
clc;
clear all;
close all;
x1=[1 6 8 9];
% Amplitude scaling system:
x2=[5 8 2 0];
(b) Program:
y1=0.5*(x1);
clc;
y2=0.5*(x2);
clear all;
x=7*x1+0.8*x2;
close all;
y=0.5*(x);
x1=[1 6 8 9];
y3=y1+y2;
x2=[5 8 2 0];
y4=y3-y;
y1=0.5*(x1);
y2=0.5*(x2);
if y4<0.0000000001
disp('linear');
else
x=x1+x2;
disp('nonlinear');
y=0.5*(x);
end
y3=y1+y2;
Output:
y4=y-y3;
if y4==0
disp('linear');
else
disp('nonlinear');
end
PROF.KINJAL VAGADIA
ASSISTANT PROF.
GEC,BHAVNAGAR
LABORATORY MANUAL
clc;
clear all;
close all;
x1=[1 6 8 9];
if y3==y2
x2=[5 8 2 0];
disp('Time Invariant');
else
y2=log(x2);
disp('Time Variant');
x=x1+x2;
end
y=log(x);
Output:
y3=y1+y2;
y4=y-y3;
if y4==0
disp('linear');
else
disp('nonlinear');
end
Output:
clc;
Program:
clear all;
PROF.KINJAL VAGADIA
ASSISTANT PROF.
GEC,BHAVNAGAR
LABORATORY MANUAL
close all;
syms z n; % symbolic objects
x=(1/16)^n;
disp('original is (1/16^)n ');
disp('Z.T. is ');
X=ztrans((1/16)^n) %Z.T.
% inverse Z.T.
disp('inverse z.t. is');
x1=iztrans(16*z/16*z-1)
% Roots
b=[1];
a=[1 -1/16];
display('poles');
A=roots(a)
display('zeros');
B=roots(b)
zplane(b,a);
(b)Program:
clc;
clear all;
close all;
syms b n;
g = b^n;
ztrans(g)
Output:
PROF.KINJAL VAGADIA
ASSISTANT PROF.
GEC,BHAVNAGAR
LABORATORY MANUAL
close all;
clear all;
syms a t w
x = sin(a*t)*exp(-5*t);
L = laplace(x)
Output:
(iii) sin(a*t)*exp(5*t).
Program:
x = sin(a*t)*exp(5*t);
(i)exp(-a*t)
L = laplace(x)
clc;
Output:
close all;
clear all;
syms a t w
x = exp(-a*t); %EXP(X) is the exponential of the
lements of X
L = laplace(x)
%LAPLACE(F) is the Laplace
ransform of the scalar sym F with default
independent variable t.
The default return
is a function of s.
Output:
(iii)
sin(a*t)*exp(-5*t)
Program:
clc;
PROF.KINJAL VAGADIA
ASSISTANT PROF.
GEC,BHAVNAGAR
LABORATORY MANUAL
PROF.KINJAL VAGADIA
clc;
clear all;
close all;
t=-40:0.1:40;
x1=sin(t);
a=1:0.1:40;
x2=exp(a);
y=conv(x1,x2)
subplot(2,2,1);plot(t,x1);
ylabel('signal value');
xlabel('time-->');
title('first signal');
subplot(2,2,2);plot(a,x2)
ylabel('signal value');
xlabel('time-->');
title('second signal');
ASSISTANT PROF.
GEC,BHAVNAGAR
LABORATORY MANUAL
subplot(2,2,3:4);plot(y);
stem(con_cir)
ylabel('signal value');
xlabel('time-->');
Output:
title('convolved signal');
Figure:
Figure:
Program:
clc;
close all;
(ii)
clear all;
Program:
a= [2 5 9 6];
clc;
b = [1 2 3];
close all;
con_lin = conv(a,b);
clear all;
subplot(2,1,1);
a= [2 5 9 6];
stem(con_lin);
b = [1 2 3 5];
con_lin = conv(a,b);
subplot(2,1,1);
stem(con_lin);
con_cir = ifft(fft(a1).*fft(b1));
subplot(2,1,2);
display(con_lin);
PROF.KINJAL VAGADIA
ASSISTANT PROF.
GEC,BHAVNAGAR
LABORATORY MANUAL
stem(con_lin);
con_cir = ifft(fft(a1).*fft(b1));
display(con_lin);
subplot(2,1,2);
stem(con_cir)
con_cir = ifft(fft(a1).*fft(b1));
display(con_cir);
subplot(2,1,2);
Output:
stem(con_cir)
title('Circular Convolution of a1 and b1');
display(con_cir);
Output:
Figure:
Figure:
(iii)
Program:
clc;
close all;
clear all;
a= [2 5 87];
clc;
b = [1 2 3];
clear all;
con_lin = conv(a,b);
close all;
subplot(2,1,1);
PROF.KINJAL VAGADIA
ASSISTANT PROF.
GEC,BHAVNAGAR
LABORATORY MANUAL
syms x y
f = exp(-x^2);
fourier(f, x, y)
Output:
(i)
Auto Correlation of sine wave with
frequency 10 Hz, sampling frequency of 1000 Hz
Program:
clc;
clear all;
close all;
Program:
F_Samp=1000; % Sampling Frequency
clc;
n=0:N-1; % Sample index
clear all;
close all;
x=sin(2*pi*f_original*n/F_Samp); % sinusoidal
signal
syms x t y
f = exp(-x^2)*exp(-t^2);
title('Original Sinwave');
Rxx=xcorr(x); % autocorrelation
subplot(2,1,2); % for figure
plot(Rxx); % Plot the autocorrelation
xlabel('lags');
ylabel('Autocorrelated Value');
title('Autocorrelation function of the sinewave');
Figure:
PROF.KINJAL VAGADIA
ASSISTANT PROF.
GEC,BHAVNAGAR
LABORATORY MANUAL
clear all;
close all;
Program:
title('Autocorrelation function');
clc;
clear all;
close all;
% cross correlation
N=512; % Number of samples
f_original_sine=10; % Frequency of the original
sinewave
F_Samp=1000; % Sampling frequency
n=0:N-1; % Sampling index
x=sin(2*pi*f_original_sine*n/F_Samp);
%
sinusoidal signal x[n]
y=x+10*randn(1,N); % Generate random noise
y(n)
subplot(3,1,1); % for figure
plot(x);
xlabel('time-->');
ylabel('Signal value');
title('Original Sinewave');
subplot(3,1,2);
plot(y); % Noise
xlabel('time-->');
ylabel('Signal value');
title('Sinewave + Noise');
Rxy=xcorr(x,y); % Estimate the cross correlation
subplot(3,1,3);
plot(Rxy);
xlabel('time-->');
ylabel('Correlated value');
title('Cross correlated signal');
Figure:
Figure:
PROF.KINJAL VAGADIA
ASSISTANT PROF.
GEC,BHAVNAGAR
LABORATORY MANUAL
PROF.KINJAL VAGADIA
ASSISTANT PROF.
GEC,BHAVNAGAR