DSPLAST
DSPLAST
2022-2025
Digital Signal Processing Lab
FAMILIARIZATION OF MATLAB
MATLAB BASICS
MATLAB is an interactive system whose basic data element is an array that does not
require dimensioning. It allows you to solve many technical computing problems, especially
those with matrix and vector formulations, in a fraction of the time it would take to write a
program in a scalar noninteractive language such as C or Fortran.
The name MATLAB stands for matrix laboratory. MATLAB was originally written
to provide easy access to matrix software developed by the LINPACK and EISPACK projects.
Today MATLAB engines incorporate the LAPACK and BLAS libraries, embedding the state of
the art in software for matrix computation.
Operators
Arithmatic operators perform numeric computations. The following table provides a
summary.
+ addition
- subtraction
* multiplication
/ division
^ power
.* element by element multiplication
./ element by element division
.^ element by element power
Double click on the icon for MATLAB. Within about 30 seconds MATLAB will
open, a screen like the picture below appears.
Command Window: This is where you can type commands and usually the answers (or
error messages) appear here too. You will see the cursor flickering after the >> prompt.
This means that MATLAB is waiting for further instructions.
Workspace: If you define new quantities (called variables) their names should be listed
here.
Command History: This is the space where past commands are remembered. If you want
to re-run a previous command or to edit it, you can drag it from this window to the
command window.
To begin MATLAB, click File>> New>> M-file. This opens a blank window as shown
below.
The M-file is executed using the run command under the Tools menu. The output
signal appears in Figure Window.
MATLAB FUNCTIONS
A function is a group of statements that together perform a task. Functions are small
programs that accept inputs and return outputs. Functions provide more flexibility in
programming. Some of the frequently used buit-in functions in MATLAB are listed below.
[h,t] = impz(b,a) Returns the impulse response of the filter with numerator
coefficients b and denominator coefficients a. impz chooses
the number of samples and returns the response in the column
vector h and the sample times in the column vector t.
filter(b,a,x) Filters the data in vector x with the filter described by
numerator coefficients b and denominator coefficients a.
fft(x,n) Returns the n-point DFT of the sequence x. If the length of x
is less than n, x is padded with trailing zeros to length n. If the
length of x is greater than n, x is truncated.
ifft(x,n) Returns the n-point inverse DFT of the sequence x.
abs(x) Returns the absolute value of the elements of vector x. If x is
complex, returns the magnitude.
angle(z) Returns the phase angles, in radians, for each element of
vector z. The angles lie between ± π.
PROGRAMS
Prog.No:01
18-01-2024
AIM
Generate unit Impulse, step and ramp functions.
PROGRAM
clc
clear all
close all
n=-10:10
y=[zeros(1,10) 1 zeros(1,10)]
subplot(3,1,1)
stem(n,y)
axis([-10,10,0,2])
xlabel('n')
ylabel('del(n)')
title('Unit Impulse')
y=[zeros(1,10) ones(1,11)]
subplot(3,1,2)
stem(n,y)
axis([-10,10,0,2])
xlabel('n')
ylabel('u(n)')
title('Unit Step')
y=[zeros(1,11) (1:10)]
subplot(3,1,3)
stem(n,y)
axis([-10,10,0,10])
xlabel('n')
ylabel('r(n)')
title('Unit Ramp')
OUTPUT
RESULT
Unit impulse, step and ramp functions are plotted.
Prog.No:02
18-01-2024
AIM
Generate shifted Impulse and step functions.
PROGRAM
clc
clear all
close all
n=-10:10
y=[zeros(1,12) 1 zeros(1,8)]
subplot(3,1,1)
stem(n,y)
axis([-10,10,0,2])
xlabel('n')
ylabel('del(n-2)')
title('Shifted Impulse Function')
y=[zeros(1,12) ones(1,9)]
subplot(3,1,2)
stem(n,y)
axis([-10,10,0,2])
xlabel('n')
ylabel('u(n-2)')
title('Shifted Step Function 1')
y=[zeros(1,7) ones(1,14)]
subplot(3,1,3)
stem(n,y)
axis([-10,10,0,2])
xlabel('n')
ylabel('u(n+3)')
title('Shifted Step Function 2')
OUTPUT
RESULT
Shifted impulse and step functions are plotted.
Prog.No:03
25-01-2024
AIM
Generate exponential functions.
PROGRAM
clc
clear all
close all
n=-10:10
a=1.1
y=a.^n
subplot(2,1,1)
stem(n,y)
axis([-10,10,0,3])
xlabel('n')
ylabel('x(n)')
title('Growing Exponential')
a=0.9
y=a.^n
subplot(2,1,2)
stem(n,y)
axis([-10,10,0,3])
xlabel('n')
ylabel('x(n)')
title('Decaying Exponential')
OUTPUT
RESULT
Exponential functions are plotted.
Prog.No:04
25-01-2024
AIM
Generate sinusoidal functions.
PROGRAM
clc
clear all
close all
n=-40:40
A=4
N=20
y=A*sin(2*pi*n/N)
subplot(2,1,1)
stem(n,y)
axis([-40,40,-5,5])
xlabel('n')
ylabel('x(n)')
title('Sine Wave')
A=4
N=20
y=A*cos(2*pi*n/N)
subplot(2,1,2)
stem(n,y)
axis([-40,40,-5,5])
xlabel('n')
ylabel('x(n)')
title('Cosine Wave')
OUTPUT
RESULT
Sinusoidal functions are plotted.
Prog.No:05
01-02-2024
AIM
Generate square and sawtooth waveforms.
PROGRAM
clc
clear all
close all
n=-20:20
A=5
N=10
y=A*square(2*pi*n/N)
subplot(2,1,1)
stem(n,y)
axis([-20,20,-7,7])
xlabel('n')
ylabel('x(n)')
title('Square Wave')
A=5
N=10
y=A*sawtooth(2*pi*n/N)
subplot(2,1,2)
stem(n,y)
axis([-20,20,-7,7])
xlabel('n')
ylabel('x(n)')
title('Sawtooth Wave')
OUTPUT
RESULT
Square and sawtooth waveforms are plotted.
Prog.No:06
01-02-2024
AIM
Generate unit step and ramp functions.
PROGRAM
clc
clear all
close all
t=-10:.5:10
y=[zeros(1,21) ones(1,20)]
subplot(2,1,1)
plot(t,y)
axis([-10,10,0,2])
xlabel('t')
ylabel('u(t)')
title('Unit Step Function')
t=-10:10
y=[zeros(1,11) (1:10)]
subplot(2,1,2)
plot(t,y)
axis([-10,10,0,10])
xlabel('t')
ylabel('r(t)')
title('Unit Ramp Function')
OUTPUT
RESULT
Unit step and ramp functions are plotted.
Prog.No:07
08-02-2024
AIM
Generate sinusoidal functions.
PROGRAM
clc
clear all
close all
t=-20:.01:20
A=2
T=10
y=A*sin(2*pi*t/T)
subplot(2,1,1)
plot(t,y)
axis([-20,20,-3,3])
xlabel('t')
ylabel('x(t)')
title('Sine Wave')
A=2
T=10
y=A*cos(2*pi*t/T)
subplot(2,1,2)
plot(t,y)
axis([-20,20,-3,3])
xlabel('t')
ylabel('x(t)')
title('Cosine Wave')
OUTPUT
RESULT
Sinusoidal functions are plotted.
Prog.No:08
08-02-2024
AIM
Generate square, sawtooth and triangular waveforms.
PROGRAM
clc
clear all
close all
t=-20:.01:20
A=2
T=5
y= A*square(2*pi*t/T)
subplot(3,1,1)
plot(t,y)
axis([-20,20,-3,3])
xlabel('t')
ylabel('x(t)')
title('Square Wave')
A=2
T=5
y= A*sawtooth(2*pi*t/T)
subplot(3,1,2)
plot(t,y)
axis([-20,20,-3,3])
xlabel('t')
ylabel('x(t)')
title('Sawtooth Wave')
A=2
T=5
dc=0.5
y= A*sawtooth(2*pi*t/T,dc)
subplot(3,1,3)
plot(t,y)
axis([-20,20,-3,3])
xlabel('t')
ylabel('x(t)')
title('Triangular Wave')
OUTPUT
RESULT
Square, sawtooth and triangular waveforms are plotted.
Prog.No:09
15-02-2024
AMPLITUDE MODULATION
AIM
Generate an amplitude modulating signal for a given carrier signal, modulating signal and
modulation index.
THEORY
In amplitude modulation, the instantaneous amplitude of the carrier signal is varied
in accordance with the amplitude of the modulating (or message) signal. If the carrier signal is
Vc sinωct and message signal is Vm sinωmt , then modulated signal is given by
VAM = (1+m sinωmt) Vc sinωct
where m is the modulation index which is equal to Vm / Vc
PROGRAM
clc
clear all
close all
t=0:0.1:500
T1=10
cs=4*sin(2*pi*t/T1)
subplot(5,1,1)
plot(t,cs)
xlabel('t')
ylabel('c(t)')
title('Carrier Signal')
T2=100
ms=2*sin(2*pi*t/T2)
subplot(5,1,2)
plot(t,ms)
xlabel('t')
ylabel('m(t)')
title('Message Signal')
m=0.5
am=(1+m*sin(2*pi*t/T2)).*cs
subplot(5,1,3)
plot(t,am)
xlabel('t')
ylabel('x(t)')
title('AM Wave with 0< m <1 ')
m=1
am=(1+m*sin(2*pi*t/T2)).*cs
subplot(5,1,4)
plot(t,am)
xlabel('t')
ylabel('x(t)')
title('AM Wave with m =1')
m=3
am=(1+m*sin(2*pi*t/T2)).*cs
subplot(5,1,5)
plot(t,am)
xlabel('t')
ylabel('x(t)')
title('AM Wave with m >1')
OUTPUT
RESULT
AM wave is generated and plotted.
Prog.No:10
22-02-2024
LINEAR CONVOLUTION 1
AIM
Perform linear convolution of two given sequences starting from origin.
THEORY
Linear convolution is the operation to find out the output y(n) of an LTI system if its
input x(n) and impulse response h(n) are known.
y(n) = x(n) * h(n)
∞
PROGRAM
clc
clear all
close all
x=input('enter the first sequence')
x1=input('enter the starting of n value')
h=input('enter the second sequence:')
h1=input('enter the starting n value')
y=conv(x,h)
% INPUT SIGNAL
subplot(3,1,1)
n=x1:length(x)+x1-1
stem(n,x)
xlabel('n')
ylabel('x(n)')
title('input signal')
% IMPULSE RESPONSE
subplot(3,1,2)
n=h1:length(h)+h1-1
stem(n,h)
xlabel('n')
ylabel('h(n)')
title('impulse responce')
subplot(3,1,3)
n=x1+h1:length(y)+x1+h1-1
stem(n,y)
xlabel('n')
ylabel('y(n)')
title('output signal')
OBSERVATION 1:
1 3 5 5 2
OBSERVATION 2:
2 -7 10 -3 -9 -2
OUTPUT 1
OUTPUT 2
RESULT
Linear convolution is performed and signals are plotted.
Prog.No:11
01-03-2024
CIRCULAR CONVOLUTION
AIM
Perform circular convolution of two given sequences.
THEORY
A convolution operation that contains a circular shift is called circular convolution.
Circular convolution of two sequences x1(n) and x2(n) each having a length of N is given by
The result of circular convolution is also an N point sequence. If the length of the given sequences
is not equal, sufficient number of zeros must be padded to each.
PROGRAM
clc
clear all
close all
%INPUTTING DATA
x=input('enter the sequence x(n)=')
h=input('enter the sequence h(n)=')
%ZERO PADDING
xl=length(x)
hl=length(h)
yl=max(xl,hl)
x=[x,zeros(1,yl-xl)]
h=[h,zeros(1,yl-hl)]
c=cconv(x,h,yl)
n=0:yl-1
subplot(3,1,1)
stem(n,x)
title('first seq')
%GENERATION OF BY(n)
subplot(3,1,2)
stem(n,h)
title('seond seq')
subplot(3,1,3)
stem(n,c)
title('circular convalution')
OBSERVATION 1:
y =
11 9 10 12
OBSERVATION 2:
y = 11 9 10
8 -2 -1 -4 -1
OUTPUT 1
OUTPUT 2
RESULT
Circular convolution is performed and signals are plotted.
Prog.No:12
08-03-2024
AIM
Obtain the impulse response of LTI systems described by the difference equations,
(a) y(n)+0.7y(n-1)-0.45y(n-2)-0.6y(n-3) = 0.8x(n)-0.44x(n-1)+0.36x(n-2)+0.02x(n-3)
(b) y(n)-0.5y(n-1)+0.25y(n-2) = x(n)+0.4x(n-1)
THEORY
The impulse response of an LTI system is its response to an impulse. In MATLAB,
the function filter (b, a, x) can be used to find the impulse response from transfer function.
b and a are the coefficients of x(n) and y(n) respectively. x is a unit impulse sequence.
PROGRAM
clc
clear all
close all
OBSERVATION 1:
y =
OBSERVATION 2:
y =
0.0000 0.0000
OUTPUT 1
OUTPUT 2
RESULT
Impulse responses are obtained for the given systems and plotted.
Prog.No:13
08-03-2024
AIM
Obtain the impulse response of the following LTI system from transfer function and also
plot output of the system for step input.
y(n)-0.5y(n-1)+0.25y(n-2) = x(n)+0.4x(n-1)
THEORY
The impulse response of an LTI system is its response to an impulse. In MATLAB, the
function [h,t] = impz (b, a) can be used to find the impulse response from transfer function.
b and a are the coefficients of x(n) and y(n) respectively. Also output of the system for step input
can be obtained from convolution of input and impulse response.
Y(Z)-0.5Z-1Y(Z)+0.25Z-2Y(Z) = X(Z)+0.4Z-1X(Z)
Y(Z) [1-0.5Z-1+0.25Z-2] = X(Z) [1+0.4Z-1]
Y(Z) / X(Z) = [1+0.4Z-1] / [ 1-0.5Z-1+0.25Z-2]
PROGRAM
clc
clear all
close all
x=ones(size(t))
subplot(3,1,2)
stem(t,x)
xlabel('n')
ylabel('x(n)')
title('Step Input')
y=conv(x,h)
n=0:length(y)-1
subplot(3,1,3)
stem(n,y)
xlabel('n')
ylabel('x(n)*h(n)')
title('Output of the System for Step Input')
OBSERVATION:
h = y =
1.0000 1.0000
0.9000 1.9000
0.2000 2.1000
-0.1250 1.9750
-0.1125 1.8625
-0.0250 1.8375
0.0156 1.8531
0.0141 1.8672
0.0031 1.8703
-0.0020 1.8684
-0.0018 1.8666
-0.0004 1.8662
0.0002 1.8665
0.0002 1.8667
0.8667
-0.0333
-0.2333
-0.1083
0.0042
0.0292
0.0135
-0.0005
-0.0036
-0.0017
0.0001
0.0005
0.0002
OUTPUT
RESULT
Impulse response and output of the system are obtained and plotted.
Prog.No:14
15-03-2024
AIM
Compute the N point DFT for a given sequence x(n), plot the amplitude and phase spectrum and
obtain the IDFT.
THEORY
DFT stands for Discrete Fourier Transform. It has been developed to convert a continuous function
of ω into a discrete function of ω, so that frequency analysis of discrete time signals can be performed on a
digital system. Generally DFT is defined along with number of samples and is called N point DFT.
N point DFT of x(n) is defined as
𝑵−𝟏
X(k) = ∑𝒏=𝟎 𝐱(𝐧)e-j2πkn/N for k=0,1,2,------,N-1
The plot of |X(k)| versus k is called magnitude spectrum and plot of ∠X(k) versus k is called phase
spectrum.
In MATLAB functions fft (x,N) and ifft (X,N) are used for finding DFT and IDFT of a sequence x(n).
Magnitude and phase spectrum of the DFT sequence are obtained using functions abs and angle
respectively.
PROGRAM
clc
clear all
close all
%INPUT DATA
x=input('enter the sequence:')
N=input('enter the N point value:')
n=0:N-1
%GENERATION OF DFT
X=fft(x,N)
mag=abs(X)
subplot(3,1,1)
stem(n,mag)
xlabel('k')
ylabel('|X(k)|')
title('magnitude spectrum of X(k)')
pha=angle(X)
subplot (3,1,2)
stem(n,pha)
xlabel('k')
ylabel('<X(k)')
title('phase spectrum of X(k)')
%GENERATION OF IDFT
x1=ifft(X,N)
OBSERVATION 1:
X =
0.9900 0 - 0.3300i 0.3300 0 + 0.3300i
mag =
0.9900 0.3300 0.3300 0.3300
00 0.330.3300
phase =
0 -1.5708 0 1.5708
x1 =
OBSERVATION 2:
X =
mag =
2.6131
phase =
1.1781
x1 =
2 2 2 2 1 1 1 1
OUTPUT 1
OUTPUT 2
RESULT
DFT and IDFT of the given sequences are computed and plotted the graphs.