You are on page 1of 32

DELHI TECHNOLOGICAL

UNIVERSITY

ELECTRONIC & COMMUNICATION


ENGINEERING
FIRST YEAR FIRST SEMESTER

Submitted by:
Submitted to:
Rahul Kumar Gupta MATLAB
2K11/CEEC/178 (Prof. Rajeev Kapoor)

INDEX
1
Sr. PROGRAM Page
No. No.
1 Write a MATLAB code to generate different types of
signals.
2 Write a MATLAB code to perform an arithmetic
operation on signals addition, subtraction, multiplication
and division.
3 Write a MATLAB code to perform signal processing
operations: time shifting, time scaling and time
inversion.
4 Write a MATLAB code to perform convolution of two
signals.
5 Write a MATLAB code to determine LTI response of
system for a given input signal.
6 Write a MATLAB code to determine Fourier transform
of a given input signal and plot its magnitude and phase
spectra.
7 Write a MATLAB code to generate frequency spectrum
of a periodic signal.
8 Write a MATLAB code to generate inverse Fourier
transform of a signal.
9 Write a MATLAB code to determine frequency response
of FIR system.
10 Write a MATLAB code to determine frequency response
of infinite impulse Response system.
11 Write a MATLAB code to determine z-transform and
plot its poles and zeros location in z-plane.

Experiment: 01
2
Object: Write a MATLAB code to generate different types of signals.
a) Unit Impulse
b) Unit Step Function
c) Rising Exponential Function
d) Falling Exponential Function
e) Sinusoidal Function
f) Ramp Function

Apparatus Required:
Personal Computer
MATLAB Software 7.6
Printer

Program Code:

% Experiment No.-01
% Generate different types of signals
% Rahul Kumar Gupta
% 2K11/CEEC/178
clc % clear the command window.
clear all % clear the workspace
n=-1:5; % array
x=[0 1 0 0 0 0 0]; % define array
subplot(2,3,1); % breaks the figure window into mxn
matrix
stem(n,x); % used to get the samples
title('Impulse Signal'); % add the text of the top
xlabel('n'); % add text besides x axis
ylabel('x[n]') % add text besides y axis
x=[0 1 1 1 1 1 1]; % define array for unit step
subplot(2,3,2)
stem(n,x)
title('Unit Step Signal');
xlabel('n')
ylabel('x[n]')
n=-5:5/20:5;
x=exp(n);
subplot(2,3,3)
stem(n,x)
title('Rising Exponential')
xlabel('n')
ylabel('x[n]')
x=exp(-n);
subplot(2,3,4);
stem(n,x)
title('Falling Exponential');
xlabel('n')
ylabel('x[n]')

3
x=sin(n);
subplot(2,3,5);
stem(n,x)
title('Sinusoidal Signal');
xlabel('n')
ylabel('x[n]')
x=n;
subplot(2,3,6);
stem(n,x)
title('Ramp Signal');
xlabel('n')
ylabel('x[n]')

Output:

4
Experiment: 02
Object:

Write a MATLAB code to perform an arithmetic operation on signals addition,


subtraction, multiplication and division.

Apparatus Required:
Personal Computer
MATLAB Software 7.6
Printer
Theory:

The theoretical results of various arithmetic operations such as addition, multiplication,


subtraction of two signals.
Let us consider two signals

x1[n]=A*sin(n+)
x2[n]=B*cos(n+)

1) Addition

z[n] = x1[n]+x2[n]

2) Subtraction

z[n] = x1[n] - x2[n]

3) Addition

z[n] = x1[n].*x2[n]

5
4) Addition

z[n] = x1[n]./x2[n]

Program Code:
% Experiment No.-01
% Arithmetic operation on signals
% Rahul Kumar Gupta
% 2K11/CEEC/178
clear all % clear all windows
close all % close all figures
wn=0:pi/20:2*pi; % define integral from 0 to
2pi
A=1; % assign the values
B=1; % assign the values
phi=pi/4;
x1=A*sin(wn+phi);
subplot(2,3,1);
stem(wn,x1)
xlabel('wn')
ylabel('x1')
title('Sine function')
x2=B*cos(wn+phi);
subplot(2,3,2)
stem(wn,x2);
xlabel('wn')
ylabel('x2')
title('Cosine function')
y1=x1+x2;
subplot(2,3,3)
stem(wn,y1);
xlabel('wn')
ylabel('y1')
title('Addition of two given function')
y1=x1-x2;
subplot(2,3,4)
stem(wn,y1);
xlabel('wn')
ylabel('y1')

6
title('Subtraction of two given function')
y1=x1.*x2;
subplot(2,3,5)
stem(wn,y1);
xlabel('wn')
ylabel('y1')
title('Multiplication of two given function')
y1=x1./x2;
subplot(2,3,6)
stem(wn,y1);
xlabel('wn')
ylabel('y1')
title('Division of two given function')

Output

7
Experiment: 03
Object:

Write a MATLAB code to perform signal processing operations: time shifting, time
scaling and time inversion.

Apparatus Required:
Personal Computer
MATLAB Software 7.6
Printer
Theory:

Various signal processing operation that can be performed on a signal are:

1) Time Inversion:

If x(t) is the original signal then x(-t) is the time inverted signal.

2) Time Scaling:

If x(t) is the original signal then x(at) is the time scaled signal by a.

3) Time Shifting:

If x(t) is the original signal then x(t t0) is the time shifted signal by t0.
Delayed x(t - t0)
Advanced x(t + t0)

8
Program Code:
% Experiment No.-03
% Signal processing Operations
% Rahul Kumar Gupta
% 2K11/CEEC/178
clc
clear all
f=100;
w=2*pi*f;
t=0:1/(1000*f):1/f;
x=sin(w*t);
subplot(2,3,1)
plot(t,x)
xlabel('t')
ylabel('Amplitude')
title('Given signal')
y=sin(w*(t+1/1000));
subplot(2,3,2)
plot(t,y)
xlabel('t')
ylabel('Amplitude')
title('Time Shifting Operation-Delay')
y=sin(w*(t-1/1000));
subplot(2,3,3)
plot(t,y)
xlabel('t')
ylabel('Amplitude')
title('Time Shifting Operation-Advance')
y=sin(-w*t);
subplot(2,3,4)
plot(t,y)
xlabel('t')
ylabel('Amplitude')
title('Time Inversion Operation')
y=sin(4*w*t);

9
subplot(2,3,5)
plot(t,y)
xlabel('t')
ylabel('Amplitude')
title('Time Scaling Operation')
y=sin(-4*w*(t+1/1000));
subplot(2,3,6)
plot(t,y)
xlabel('t')
ylabel('Amplitude')
title('Shifting+Inversion+Scaling')

Output:

10
Experiment: 04
Object:

Write a MATLAB code to perform convolution of two signals.

Apparatus Required:
Personal Computer
MATLAB Software 7.6
Printer
Theory:

The below equations defines the integral convolution of 2 time signals x(t) and h(t)
denoted by:

y(t) = x(t) * h(t) = x( ).h(t )

or

y[n] = x[t] * h[t] = x[k ].h[n k ]( )

Program Code:

% Experiment No: 04
% Convolution of two signals
% Rahul Kumar Gupta
% 2K11/CEEC/2011
a=[1 2 3]
b=[4 5 6]
c=conv(a,b);
i=length(a);
j=length(b);
A=[a,zeros(1,i)];
B=[b,zeros(1,j)];

11
for k=1:i+j-1
y(k)=0;
for l=1:i
if(k-l+1>0)
y(k)=y(k)+A(l)*B(k-l+1);
else
end
end
end
stem(y)
ylabel('k');
xlabel('------>n')
title('Convolution of two signals')

Output:

12
Experiment: 05
Object:

Write a MATLAB code to determine LTI response of system for a given input signal.

Apparatus Required:
Personal Computer
MATLAB Software 7.6
Printer
Theory:

LTI system or Linear Time Invariant system can be obtained bt

y(t) = x(t)*h(t)
i.e. convolution of two signals x(t) and h(t) is given as

m = length(x) n = length(h) the vector y is of length m+n-1, kth element is

y(k) = x(j)*h(k+1-j)
y(1) = x(1)*h(1)
y(2) = x(1)h(2)*x(2)h(1)
y(n) = x(1)*h(n) + x(2)h(n-1) + .+x(n)*h(1)

Program Code:
% Experiment No: 05
% LTI Response
% Rahul Kumar Gupta
% 2K11/CEEC/2011
clc

13
clear all
close all
t=0:0.01:10;
x=exp(-t);
h=[ones(1,1001)]
Lx=length(x);
Lh=length(h);
Ly=Lx+Lh-1;
for p=1:Ly
sum=0;
for k=1:p
if (k<=Lx && k>=(p+1-Lh))
sum=sum+x(k).*h(p+1-k);
end
end
y(p)=sum;
end
t1=0:0.01:(Ly-1)*0.01;
%input signal
subplot(2,2,1);
plot(t,x);
title('Input Signal')
xlabel('Time')
ylabel('Amplitude of the Signal')
% Impulse Response
subplot(2,2,2)
plot(t,h)
title('Impulse Response')
xlabel('Time')
ylabel('Amplitude of the Signal')
%output of the LTI System
subplot(2,2,[3:4])
plot(t1,y)
title('Output of the LTI system')
xlabel('Time')
ylabel('Amplitude of the Signal')

Output:

14
Experiment: 06
Object:

Write a MATLAB code to determine Fourier transform of a given input signal and plot its
magnitude and phase spectra.

Apparatus Required:
Personal Computer
MATLAB Software 7.6
Printer
Theory:

Fourier Transform is divided into 2 parts:

1) Continuous time Fourier Transform


This is applicable for aperiodic functions and periodic functions:

1
X (e
j
x (t ) ).e jn d
2 2

and the corresponding function is:

15
x(t)= Fourier Transform of a signal can be obtained by FFT function in
MATLAB.
The plot of x()vs is called as magnitude spectra and angle of x()vs is
called as phase spectra

2) Discrete Time Fourier Transform

It is divided for periodic function



X (e j ) x[n].e
n
j n

Program Code:
% Experiment No: 06
% Fourier transform of a given input signal
% Rahul Kumar Gupta
% 2K11/CEEC/2011
clc
clear all
close all
x=[1,1,1,1,zeros(1,4)];
n=8;
X=fft(x,n);
magx=abs(X);
phase=angle(X)*180/pi;
subplot(2,2,1);
plot(x);
grid;
title('input signal')
xlabel('k');
ylabel('Amplitude');
subplot(2,2,2);
plot(magx);
grid;
title('Magnitude Spectra')
xlabel('k');
ylabel('X(k)');
subplot(2,2,3);
plot(phase);
grid;
title('Phase spectra')
xlabel('k');
ylabel('degrees');

Output:

16
Experiment: 07
Object:

Write a MATLAB code to generate frequency spectrum of a periodic signal.

Apparatus Required:
Personal Computer
MATLAB Software 7.6
Printer
Theory:

A continuous time signal x(t) to be periodic if there is a positive on zero value of t for
which
X(t+T) = x(t) for all t

The fundamental period T0 of x(t) is the smallest positive value of T for which above
1
equation is not satisfied and = f0 is referred as fundamental frequency.
T0

17
Two basic examples of periodic signal are the real sinusoidal signal

X(t) = cos(t+)

The complex exponential Fourier series representation of a periodic signal x(t) with T0 as
fundamental period

k
x(t) = C
k
k e j 0 t

0 = 2/ T0
Where Ck are the complex Fourier Coefficients
1
x(t )e
jt
Ck dt
T0
1
C0
T0 x(t )dt

Program Code:
% Experiment No: 07
% Frequency Spectrum a signal
% Rahul Kumar Gupta
% 2K11/CEEC/2011
clc
clear all
close all
t=0:0.01:10;
%train of pulses
x=[ones(1,201),zeros(1,99),ones(1,201),zeros(1,99),ones(1,201),zeros(1,9
9),ones(1,101)]
T=3
w=2*pi/T;
dtau=0.01;
for k=-10:10;
sum=0
i=1;
for tau=0:dtau:T
exp_part=exp(-j*w*k*tau)*dtau;
sum=sum+exp_part.*x(i);
i=i+1;
end
a(k+11)=sum

18
end
for i=1:21
mag(i)=abs(a(i));
phase(i)=angle(a(i));
end
k=-10:10
%original signal
subplot(2,2,[1:2])
plot(t,x)
title('Input signal(a pulse train)')
xlabel('Time\rightarrow')
ylabel('Amplitude of the signal')
%magnitude spectra
subplot(2,2,3)
stem(k,mag);
title('Magnitude spectra')
xlabel('k\rightarrow');
ylabel('Magnitude')
%phase spectra
subplot(2,2,4)
stem(k,phase,'Linewidth',1.2)
title('Phase spectra')
xlabel('k\rightarrow');
ylabel('phase')

19
Experiment: 08
Object:

Write a MATLAB code to generate inverse Fourier transform of a signal.

Apparatus Required:
Personal Computer
MATLAB Software 7.6
Printer
Theory:

Inverse Fourier Transform is used to convert a signal in frequency domain to a signal in


time domain.
If X() is the Fourier Transform of a signal then, the original signal x(t) or Inverse
Fourier Transform of X() is given by:-
1
x(t )e
jt
x(t ) dt
2

In MATLAB we can use a direct command to evaluate the Inverse Fourier Transform i.e.
d= ifft(x);

20
Program Code:
% Experiment No: 08
% Inverse Fourier Transform of a signal
% Rahul Kumar Gupta
% 2K11/CEEC/2011
clc
clear all
close all
w=-100:.1:100;
X=1./(1+w.^2);
dw=0.1;
m=1;
for t=0:0.01:10;
i=1;
sum=0;
for w=-100:dw:100
sum=sum+X(i)*exp(j*w*t)*dw;
i=i+1;
end
x(m)=sum;
m=m+1;
end
w=-100:.1:100;
t=0:.01:10;
% Given Signal
subplot(2,1,1)
plot(w,X)
title('Given Signal in Frequency Domain')
xlabel('Frequency\rightarrow');
ylabel('X(w)');
grid on;
% Signal in Time Domain
subplot(2,1,2);
plot(t,x);
title('Signal Obtained via Inverse FT');
xlabel('Time\rightarrow');
ylabel('X(w)');
grid on;

21
Experiment: 09
Object:

Write a MATLAB code to determine frequency response and phase response of FIR
system.

Apparatus Required:
Personal Computer
MATLAB Software 7.6

22
Printer
Theory:

A discrete LTI system can be classified by its impulse response. A system is said to be
FIR if its Impulse Response have finite number of non-zero term.
Let impulse response of discrete LTI system is h[n] then its z-transformation can be
obtained by equations:
H(z) = 1/[b(0) + b(1)z-1 + b(2)z-2 + b(3)z-3 + b(4)z-4 + +b(m)z-m]
Then in MATLAB frequency response can be obtained by using [h,p]=freqz(ba,a,w)
returns the frequency response vector h and the corresponding angular frequency vector
for the digital filter whose transfer function is determined by the (real or complex)
numerator and denominator polynomial represented by the vector b and a respectively.
The vector h and p are both of length w has values ranging from 0 to pi per sample. Freqz
generally uses an fft algorithm to compute the frequency response.

Experiment: 10
Object:

Write a MATLAB code to determine frequency response and phase response of IIR
system.

Apparatus Required:
Personal Computer
MATLAB Software 7.6

23
Printer
Theory:

A system whose impulse response or no. of non zero values of the output is infinite is
called infinite impulse response IIR system.
Let impulse response of discrete LTI system is h[n] then its z-transformation can be
obtained by equations:

b(0) + b(1)z -1 + b(2)z -2 + b(3)z -3 + b(4)z -4 + + b(m)z -m


H ( z)
a(0) + a(1)z -1 + a(2)z - 2 + a(3)z -3 + a(4)z - 4 + + b(n)z - n

Then in MATLAB frequency response can be obtained by using [h,p]=freqz(ba,a,w)


returns the frequency response vector h and the corresponding angular frequency vector
for the digital filter whose transfer function is determined by the (real or complex)
numerator and denominator polynomial represented by the vector b and a respectively.
The vector h and p are both of length w has values ranging from 0 to pi per sample. Freqz
generally uses an fft algorithm to compute the frequency response.

Problem No:01

Implement the function F(t)= 2*sin(30*pi*t) + 3*sin(40*pi*t).


% Problem No: 01
% Implement the function F(t)= 2*sin(30*pi*t) + 3*sin(40*pi*t)
% Rahul Kumar Gupta
% 2K11/CEEC/178
clc
clear all
close all
t=0:pi/2000:2*pi;
x1=2*sin(30*pi*t);

24
subplot(3,1,1)
plot(t,x1)
grid on
xlabel('Time\rightarrow')
ylabel('Amplitude\rightarrow')
title('Input Function\rightarrow x1')
x2=3*sin(40*pi*t)
subplot(3,1,2)
plot(t,x2)
grid on
xlabel('Time\rightarrow')
ylabel('Amplitude\rightarrow')
title('Input Function\rightarrow x2')
y=x1+x2;
subplot(3,1,3)
plot(t,y)
grid on
xlabel('Time\rightarrow')
ylabel('Amplitude\rightarrow')
title('Input Function\rightarrow F(t)')

Output:

Problem No: 02

Triangular Wave
% Problem No: 02
% Triangular Wave
% Rahul Kumar Gupta
% 2K11/CEEC/178
clc
clear all
close all
T=20;
x=0:5*T/1000:5*T

25
y1=min(0.5,mod(x,T)/T);
y2=min(0.5,1-mod(x,T)/T);
plot(x,y1+y2)
grid
xlabel('Time\rightarrow')
ylabel('Amplitude\rightarrow')
title('TRIANGULAR WAVE')

Output:

Problem No: 03

Find out the Fourier Transform of 2sin30t.


% Problem No.-03
% Forier Transform of a signal
% Rahul Kumar Gupta
% 2K11/CEEC/178
clc
clear all
close all
t=0:pi/50:2*pi
x=2*sin(30*pi*t)

26
t=100
y=fft(x,t)
magx=abs(y)
phase=angle(y)*180/pi
subplot(2,2,1)
plot(x)
grid
title('Input Signal')
xlabel('k')
ylabel('Amplitude')
subplot(2,2,2)
plot(magx)
grid
title('Magnitude Spectra')
xlabel('k')
ylabel('x(k)')
subplot(2,2,[3,4])
plot(phase)
grid
title('Phase Spectra')
xlabel('k')
ylabel('Degrees')

Output:

Problem No: 04

Differentiate e-at .

% Problem No.-04
% Differentiate
% Rahul Kumar Gupta
% 2K11/CEEC/178
clc
clear all
close all
syms a t

27
x=exp(-a*t)
y=diff(x,t)

Output:

Problem No: 05

Write a MATLAB code to perform signal processing operation time scaling, time shifting
and inversion of f(t)=30sin(20t)

% Problem No.-05
% Signal processing Operations
% Rahul Kumar Gupta
% 2K11/CEEC/178
clc
clear all
t=-2*pi:pi/30:2*pi;

28
x=30*sin(20*pi*t);
subplot(2,3,1)
plot(t,x)
xlabel('t')
ylabel('Amplitude')
title('Given signal')
y=30*sin(20*pi*t+4);
subplot(2,3,2)
plot(t,y)
xlabel('t')
ylabel('Amplitude')
title('Time Shifting Operation-Delay')
y=30*sin(20*pi*t-4);
subplot(2,3,3)
plot(t,y)
xlabel('t')
ylabel('Amplitude')
title('Time Shifting Operation-Advance')
y=30*sin(-20*pi*t);
subplot(2,3,4)
plot(t,y)
xlabel('t')
ylabel('Amplitude')
title('Time Inversion Operation')
y=30*sin(80*pi*t);
subplot(2,3,5)
plot(t,y)
xlabel('t')
ylabel('Amplitude')
title('Time Scaling Operation')
y=sin(-80*pi*(t+1/1000));
subplot(2,3,6)
plot(t,y)
xlabel('t')
ylabel('Amplitude')
title('Shifting+Inversion+Scaling')

Output:

29
Problem No: 08

30
Multiply of two functions: F(x)=2cos(10x) and F(y)=-5cos(50y)

% Problem No: 08
% Multiplication of Two Functions
% Rahul Kumar Gupta
% 2K11/CEEC/178
clc
clear all
close all
x=0:pi/2000:2*pi;
X=2*cos(10*pi*x);
subplot(3,1,1)
plot(x,X)
grid on
xlabel('Time\rightarrow')
ylabel('Amplitude\rightarrow')
title('First Function\rightarrow X')
y=x
Y=-5*cos(50*pi*y)
subplot(3,1,2)
plot(y,Y)
grid on
xlabel('Time\rightarrow')
ylabel('Amplitude\rightarrow')
title('Second Function\rightarrow Y')
MUL=X.*Y;
subplot(3,1,3)
plot(y,MUL)
grid on
xlabel('Time\rightarrow')
ylabel('Amplitude\rightarrow')
title('Multiplication\rightarrow F(t)')

Output:

clear all % clear the command window


close all % close the figure window
x1=1:10; % assign the descrete function using array

31
subplot(1,3,1) % breaks the figure window into mxn matrix
stem(x1) % used to get sample
title('First Function') % display title
xlabel('n') % add texts besides x axis
ylabel('x[n]') % add texts besides y axis
x2=[1 1 0 1 2 3 1 4 5 7]; % assign second function
subplot(1,3,2) % breaks the figure window into mxn matrix
stem(x2) % used to get sample
title('Second Function') % display title
xlabel('n') % add texts besides x axis
ylabel('x[n]') % add texts besides y axis
y=cconv(x1,x2); % perform convolution of functions
subplot(1,3,3) % breaks the figure window into mxn matrix
stem(y) % used to get sample
title('Circular Convolution')% display title
xlabel('n') % add texts besides x axis
ylabel('y[n]') % add texts besides y axis

32

You might also like