DSP Lab Manual
DSP Lab Manual
Contents
A. Introduction........................................................................................................................................................2
B. Introduction to MATLAB tutorial.....................................................................................................................3
1
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
A. Introduction
MATLAB stands for MATrix LABoratory. It is a matrix processing language that is used for scientific
and engineering data processing. It handles both numeric and symbolic variables and can be used either
interactively (in the Command Window) or through input (‘m’) files. It is sold by MathWorks, Inc.
If you are familiar with MATLAB: this lab will be a review. Take this opportunity to refresh your
memory, fill in any gaps you missed the first time, and improve your knowledge by helping those
students who have not had experience with MATLAB before.
For those new to MATLAB: this lab presents some of the basic capabilities of MATLAB, but it is not a
program that you learn in one lab. I expect you will continue to practice and learn more about MATLAB
throughout other courses. As you work, you can find help in many textbooks, User’s Guides, the
MathWorks web site, by accessing the help window, or type help command or just help. If your lab
partner is familiar with MATLAB already they can assist you, but make sure you understand and do the
exercises yourself.
In addition, look through the tutorial that follows below. This provides a summary of commands that
should be useful as you work through the exercises that follow. Look it over quickly before the lab, then
refer to it as needed in the lab – its not going to make a lot of sense until you try it!
2
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
B. Introduction to MATLAB tutorial
Using interactive mode
In the Command Window, type in an equation and hit enter, just like using a calculator. The
order of operation is: exponentiation (^) first, then multiplication and division (* /), then addition and
subtraction (+ -). You can always use parentheses. For example, typing 2 + 3 * 5 – 4 * 2 + 2^3 gives the
same as 2 + (3*5) – (4*2) + 23 = 17 (try it!)
Variable Names
Names must start with a letter, then you can use any letters, numbers, or underscore ( _ ) up to 31
characters long. Variables are case sensitive and cannot contain any punctuation marks. Avoid using
reserved names (sin, pi, i, j, etc.) to avoid confusion. The command clear will clear the memory of any
previous assignments.
Some special symbols:
% comments – anything on the same line after this is not an operation
… Continue on next line
; suppresses printing to the screen (when used at the end of a line), or starts a new line in an array
: creates an array (a:b:c creates an array from a to c in steps of b)
Built-in functions
There are many built in functions. Most are obvious, such as sin(x), cos(x), etc. Some other useful
ones are: sqrt(x), exp(x) and mean(x). Sometimes they are not so obvious, such as log(x) = natural log,
log10(x) = common log (base 10). There are many more! A useful command is lookfor. For example,
typing look for plot will return all the functions associated with plots. Then you can use help to get the
exact syntax.
Arrays
Variables can be scalars, vectors or matrices without any declaration. Vectors and matrices are entered in
square brackets []. Some examples:
A = 2 (scalar),
B = [1 2 3] (row vector),
C = [1 2 3]’ or C = [1; 2; 3] (column vector),
D = [1 2 3; 4 5 6; 7 8 9] (3x3 matrix)
Arrays can be created with the colon ( : ) operator or with the linspace or logspace command. For
example, x = 0:0.1:1 (from 0 to 1 in 0.1 steps) or linspace(0,1,11) (from 0 to 1 with 11 elements) both
create x = [0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1].
MATLAB’s strongest feature is its ability to manipulate vectors and matrices. Here are some useful
matrix operations:
* multiplication – scalar, vector or matrix, depending on the variable type
3
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
‘ transpose
det(A) determinant
inv(A) inverse
length(A) length of vector A
size(A) gives (m,n) for an m x n matrix
“. “Element-by-element operation For example,.* causes each element of a matrix to be multiplied by the
corresponding element of another matrix, rather than performing matrix multiplication.
A typical problem is to solve for a vector X in the equation AX = B. This can be done one of two ways:
X = inv (A)*B or X = A\B. In most cases, either operation is fine. In the case of an ill-conditioned
matrix, the second method will be more efficient and accurate.
Some special matrices that may come in handy are zeros (n), ones (n), [], rand (n) and eye (n).
To address a matrix element a ij, the notation is A(i,j). The colon symbol ( : ) indicates all of a row or
column. Saying a: b indicates rows or columns from a to b. For example, A(:,1:2) addresses all rows,
columns 1 – 2, as shown:
» A = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]
A=
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
» A(:,1:2)
ans =
1 2
5 6
9 10
13 14
M-Files (scripts & functions)
Usually you will want to save programs in a file to debug and to use again. Choose New from
File menu to open the Editor Window. Type all commands in the editor and save the file as filename.m.
Run the file from the Debug menu, or use the Run button on the toolbar, or type filename in the
Command Window.
Ordinary script files are simply a self-contained list of commands, while functions are used to
pass variables in and out. The first line of the function file must have the format:
function output = filename(inputs) or function [outputs] = filename(inputs)
Run the file by typing filename(input values)
4
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
The MathWorks tutorials “Automating Analysis with Scripts” and “Writing Functions” have more
information.
Polynomials
Polynomials are represented by a vector of coefficients. For example, the polynomial
x2 + 2x + 3 would be represented by p = [1 2 3]. Some useful polynomial commands:
polyval(p,x) - evaluate the polynomial at the given value of x
roots(p) - find the roots of the polynomial
p = poly(r) - construct the polynomial from roots r = [r1, r2, …]
conv, deconv - multiply or divide polynomials
Plots
The command plot(x,y) (where x and y are arrays) creates a 2-dimensional plot of y versus x.
The arrays need to be the same size.
The commands to add a plot title, axes labels and legend are title, xlabel, ylabel and legend.
They all use the format command(‘…’), for example, title(‘My Plot’). These, as well as other
additions like text boxes and arrows, can also be added in the graphics window after the plot is
drawn.
5
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
There are many other plotting features, more than will be listed in this tutorial. A few useful ones
are given here. Type help plot for more information.
plot multiple lines on one graph: plot(x1, y1, x2, y2, …)
use different line styles, colors and data point symbols: plot(x,y,‘…’)
turn grid on or off: grid on or grid off
keep the plot window open to add another plot: hold on (hold off)
change axes range: axis([xmin xmax ymin ymax])
make multiple plots on one page: subplot(a,b,c), plot(x,y) makes a rows and b columns of
subplots, and puts this plot in position c
3-d plots include surf, mesh and contour
Try this example:
x = [1 2 3]
y1 = [4 6 10]
y2 = [5 7 11]
plot(x, y1, ‘r’, x, y2, ‘k--’)
6
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
>= greater than or equal to
~= not equal to
== equal to
The format of an if loop is given below. The commands in the if loop will only be executed if the
condition is true, otherwise they will be ignored.
if logical conditions
:
commands
:
end
Some variations: if logical conditions
:
commands
:
else
:
other commands
:
end
This process can be continued with elseif. The command break jumps to the next statement after the
loop.
Procedure:
I. This first section consists of exercises to try on your own for practice, as much as you need depending
on your experience. Take your time and make sure you understand everything in this section before
going on.
7
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
inv(B)
What does A.*B do?
3. Write for, while and if loops. Examples:
a) for k = 1:10
y = 2^k
end
b) k=0
while k<=10
y = 2^k
k = k+1
end
c) k=1
if k < 10
y = 2^k
else
disp(‘ERROR!’)
end
Be careful of “endless” while loops! For example, what would happen in b) if you did not increment k to
k+1?
Draw the two lines in different line styles and colors (see help plot.) Add axes labels, a title and a legend
to your plot.
8
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
6. Write, save and run an m-file. Use one of the above examples, or make up your own.
9
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
Experiment No: - 01 Signal Generation
H = subplot(m,n,p) or subplot(mnp), breaks the Figure window into an m-by-n matrix of small axes, selects
the p-th axes for the current plot, and returns the axis handle. The axes are counted along the top row of the
Figure window, then the second row, etc. For example,
subplot(2,1,1), PLOT(income)
subplot(2,1,2), PLOT(outgo)
Plots income on the top half of the window and outgo on the bottom half.
exp Exponential.
exp(X) is the exponential of the elements of X, e to the X. For complex Z=X+i*Y, exp(Z) = exp(X)*(COS(Y)
+i*SIN(Y)).
cos Cosine.
cos(X) is the cosine of the elements of X.
sin Sine.
sin(X) is the sine of the elements of X.
11
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
AIM: - (a) To write a MATLAB program for the generation of ‘unit impulse’ signal.
PROCEDURE:-
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window
PROGRAM:-
>> clear all
>> t = -2:1:2;
>> y = [zeros(1,2),ones(1,1),zeros(1,2)];
>> subplot(2,2,1);
>> stem(t,y);
>> ylabel('Amplitude -->');
>> xlabel('(a) n -->');
RESULTS:-
Thus the MATLAB program for unit-impulse signal was performed and the output was
verified.
OUTPUT:
AIM: - (b) To write a MATLAB program for the generation of ‘Unit step’ sequence [u(n)-u(n-
N)].
PROCEDURE:-
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window
12
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
PROGRAM:-
>> clear all
>> n = input('enter the N values');
enter the N values 7
>> t = 0:1:n-1;
>> y1 = ones(1,n);
>> subplot(2,2,2);
>> stem(t,y1);
>> ylabel('Amplitude -->');
>> xlabel('(b) n -->');
RESULTS: - Thus the MATLAB program for unit step sequence was performed and the output was
verified.
OUTPUT:
AIM: - (c) To write a MATLAB program for the generation of ‘Ramp’ sequence
PROCEDURE:-
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window
PROGRAM:-
>> clear all
>> n1 = input('enter the length of ramp sequence');
enter the length of ramp sequence 7
>> t = 0:n1;
>> subplot(2,2,3);
>> stem(t,t);
>> ylabel('Amplitude -->');
>> xlabel('(c) n -->');
13
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
RESULTS:-
Thus the MATLAB program for ramp sequence was performed and the output was verified.
OUTPUT:
AIM: - (d) To write a MATLAB program for the generation of ‘Exponential’ sequence
PROCEDURE:-
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window
PROGRAM:-
>> clear all
>> n2 = input('enter the length of exponential sequence');
enter the length of exponential sequence 7
>> t = 0:n2;
>> a = input('enter the a value');
enter the a value -2
>> y2 = exp(a*t);
>> subplot(2,2,4);
>> stem(t,y2);
>> ylabel('Amplitude -->');
>> xlabel('(d) n -->');
RESULTS:-
Thus the MATLAB program for exponential sequence was performed and the output was
verified.
OUTPUT:
14
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
AIM: - (e) To write a MATLAB program for the generation of ‘sine’ wave.
PROCEDURE:-
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window
PROGRAM:-
RESULTS:-
Thus the MATLAB program for sine wave was performed and the output was verified.
OUTPUT:
AIM: - (f) To write a MATLAB program for the generation of ‘cosine’ wave.
PROCEDURE:-
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window
PROGRAM:-
>> clear all
>> t = 0:0.01:pi;
>> y = cos(2*pi*t);
>> subplot(2,1,2);
>> plot(t,y);
>> ylabel('Amplitude -->');
>> xlabel('(b) n -->');
RESULTS:-
Thus the MATLAB program for cosine wave was performed and the output was verified.
OUTPUT:
16
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
PROGRAM:-
clear all
a=input('Enter the range of the cycle or Enter X-Axis Range for the Waveform \n a = ');
17
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
i='y';
while(i=='y')
b=input('Press ....\n 1.Sine Waveform \n 2.Cosine Waveform \n 3.Exp. Waveform \n 4.Unit Step
Signal \n 5.Ramp Signal \n 6.Exit \n Enter Your Choice.......');
t=-a*pi:0.1:a*pi;
t1=0:1:a-1;
switch(b)
case 1
f=sin(t);
a1=stem(t,f)
xlabel('Time')
ylabel('sin(t)')
grid
title('Sine Waveform')
case 2
f=cos(t);
a2=stem(t,f)
xlabel('Time')
ylabel('cos(t)')
grid
title('Cosine Waveform')
case 3
b=input('enter the value of b for e^bt \n b = ');
f=exp(b*t);
a3=stem(t,f)
xlabel('Time')
ylabel('exp(b*t)')
grid
title('Exp. Waveform')
case 4
f=ones(1,a);
a4=stem(t1,f)
xlabel('Time')
ylabel('u(t)')
grid
title('Unit step signal')
case 5
a5=stem(t1,t1)
xlabel('Time')
ylabel('r(t)')
grid
title('Ramp Signal')
18
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
case 6
break;
otherwise
display('Wrong Choice')
end
i=input('Do you want to cont...... ? \n Press (y/n)', 's');
end
OUTPUT:
Enter the range of the cycle or Enter X-Axis Range for the Waveform
a= 2
Press....
1. Sine Waveform
2. Cosine Waveform
3. Exp. Waveform
4. Unit step Signal
5. Ramp Signal
6. Exit
R = INPUT('What is your name','s') gives the prompt in the text string and waits for
character string input. The typed input is not evaluated; the characters are simply
returned as a MATLAB string.
The text string for the prompt may contain one or more '\n'. The '\n' means skip to the
beginning of the next line. This allows the prompt string to span several lines. To output
just a '\' use '\\'.
Experiment No: - 02
AIM: - To write a MATLAB program for Signal Operation like Shifitng, Scaling and Folding
20
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
PROCEDURE:-
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window
PROGRAM:-
subplot (3,1,1);
stem(n,x);
title('Signal x(n)');
m=n+n1;
y=x;
subplot(3,1,2);
stem(m,y);
title('Delayed signal x(n-n1)');
t=n-n2;
z=x;
subplot(3,1,3);
stem(t,z);
title('Advanced signal x(n+n2)');
OUTPUT
For vectors, max(X) is the largest element in X. For matrices, max(X) is a row vector containing the
maximum element from each column. For N-D arrays, max(X) operates along the first non-singleton
dimension.
max(X,Y) returns an array the same size as X and Y with the largest elements taken from X or Y. Either one
can be a scalar.
Example: If X = [2 8 4 then max(X,[],1) is [7 8 9],7 3 9]
max(X,[],2) is [8 and max(X,5) is [5 8 59],7 5 9].
for Repeat statements a specific number of times.
The general form of a for statement is:
for variable = expr, statement, ..., statement END
The columns of the expression are stored one at a time in the variable and then the following
statements, up to theEND, are executed. The expression is often of the form X:Y, in which case its
columns are simply scalars. Some examples (assume N has already been assigned a value).
for I = 1:N,
for J = 1:N,
A(I,J) = 1/(I+J-1);
END
END
for S = 1.0: -0.1: 0.0, END steps S with increments of -0.1
for E = EYE(N), ... END sets E to the unit N-vectors.
Long loops are more memory efficient when the colon expression appears in the for statement since the
index vector is never created. The BREAK statement can be used to terminate the loop prematurely.
' Complex conjugate transpose.
X' is the complex conjugate transpose of X.
B = ctranspose(A) is called for the syntax A' (complex conjugate transpose) when A is an object.
AIM: - (a) Program for linear convolution of the sequence x=[1,2] and h=[1,2,4].
22
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
PROCEDURE:-
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window
PROGRAM:-
>> clear all;
>> x = input('enter the 1st sequence');
enter the 1st sequence [1 2]
>> h = input('enter the 2nd sequence');
enter the 2nd sequence [1 2 4]
>> y = conv(x,h);
>> figure;
>> subplot(3,1,1);
>> stem(x);
>> ylabel('Amplitude -->');
>> xlabel('(a) n-->');
>> subplot(3,1,2);
>> stem(h);
>> ylabel('Amplitude -->');
>> xlabel('(b) n -->');
>> subplot(3,1,3);
>> stem(y);
>> ylabel('Amplitude -->');
>> xlabel('(c) n -->');
>> disp('The resultant signal is');
The resultant signal is
>> y
y=
1 4 8 8
RESULTS:-
Thus the MATLAB program for computing the linear convolution of two sequences was
performed and the output was verified.
OUTPUT:
Figure below shows the discrete input signals x(n) and h(n) and the convolved output y(n).
23
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
PROCEDURE:-
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window
PROGRAM:-
>> clear;
>> a = input('enter the sequence x(n) =');
enter the sequence x(n) = [1 2 4]
>> b = input('enter the sequence h(n) =');
enter the sequence h(n) = [1 2]
>> n1 = length(a);
>> n2 = length(b);
>> N = max(n1,n2);
>> x = [a zeros(1,(N-n1))];
>> for i = 1:N
k = i;
for j =1:n2
H(i,j) = x(k)*b(j);
k = k-1;
24
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
if(k == 0)
k = N;
end
end
end
>> y = zeros(1,N);
>> M = H';
>> for j = 1:N
for i = 1:n2
y(j) = M(i,j)+y(j);
end
end
>> disp('The output sequence is y(n)=');
The output sequence is y(n)=
>> disp(y);
9 4 8
>> stem(y);
>> title('Circular Convolution');
>> xlabel('n');
>> ylabel('y(n)');
RESULTS:- Thus the MATLAB program for computing the circular convolution of two sequences was
performed and the output was verified.
OUTPUT:
25
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
Experiment No: - 4 Z Transform
Vector DEN specifies the coefficients of the denominator in descending powers of s. Matrix NUM indicates
the numerator coefficients with as many rows as there are outputs. The zero locations are returned in the
columns of matrix Z, with as many columns as there are rows in NUM. The pole locations are returned in
column vector P, and the gains for each numerator transfer function in vector K.
zplane Z-plane zero-pole plot.
zplane(Z,P) plots the zeros Z and poles P (in column vectors) with the unit circle for reference. Each zero is
represented with a 'o' and each pole with a 'x' on the plot. Multiple zeros and poles are indicated by the
multiplicity number shown to the upper right of the zero or pole. zplane(Z,P) where Z and/or P is a matrix,
plots the zeros or poles in different columns using the colors specified by the axes ColorOrder property.
Creation:
SYS = tf(NUM,DEN) creates a continuous-time transfer function SYS with numerator(s) NUM and
denominator(s) DEN. The output SYS is a tf object.
SYS = tf(NUM,DEN,TS) creates a discrete-time transfer function with sample time TS (set TS=-1 if the
sample time is undetermined).
26
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
Experiment No: - 4
PROCEDURE:-
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window
PROGRAM:-
>> clear all;
>> num = input('enter the numerator polynomial:');
enter the numerator polynomial:[2 3 1]
>> den = input('enter the denominator polynomial:');
enter the denominator polynomial:[1 2 0 2 3 4]
>> trans = tf(num,den);
>> [zeros poles gain] = tf2zp(num,den);
>> disp('Poles of transfer function are:');
Poles of transfer function are:
>> disp(poles);
-2.2797
0.7957 + 1.0242i
0.7957 - 1.0242i
-0.6559 + 0.7829i
-0.6559 - 0.7829i
>> disp('zeros of transfer function are:');
zeros of transfer function are:
>> disp(zeros);
-1.0000
-0.5000
>> disp('gain of transfer function is:');
gain of transfer function is:
>> disp(gain);
2
>> zplane(num,den);
RESULTS:-
Thus the MATLAB program for Z-transform (Computation and plot) was performed and the
output was verified.
OUTPUT:
The gain responses of low-pass, high-pass, bandpass and bandstop filters using Rectangular window are shown in
figure.
27
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
28
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
Experiment No: - 05 Discrete Fourier Transform
fft(X,N) is the N-point FFT, padded with zeros if X has less than N points and truncated if it has
more.
fft(X,[],DIM) or fft(X,N,DIM) applies the fft operation across the dimension DIM.
For length N input vector x, the DFT is a length N vector X, with elements N
.* Array multiply.
X.*Y denotes element-by-element multiplication. X and Y must have the same dimensions unless
one is a scalar.A scalar can be multiplied into anything.
C = times(A,B) is called for the syntax 'A .* B' when A or B is an object.
29
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
Experiment No: - 05(a)
AIM: - (a) To write a MATLAB program for computing ‘DFT’ of input sequence.
PROCEDURE:-
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window
PROGRAM:-
close all;
clear all;
N=input('How many point DFT do you want?');
x2=input('Enter the sequence=');
n2=length(x2);
x2=[x2 zeros(1,N-n2)];
for k=1:N
for n=1:N
w=exp((-2*pi*i*(k-1)*(n-1))/N);
%prev.step=>evaluating w-matrix
x(n)=w;
end
c(k,:)=x
end
r=[c]*[x2']
%plotting magnitude and angle
subplot(211)
stem(abs(r));
title('DFT-absolute value');
subplot(212)
stem(angle(r));
title('DFT-angle');
RESULTS:-
Thus the MATLAB program for computing the DFT of input sequence was performed and the
output was verified.
OUTPUT:
How many point DFT do you want?10
AIM: - (b) To write a MATLAB program for compute the IDFT of input sequence.
PROCEDURE:-
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window
PROGRAM:-
close all;
clear all;
N=input('How many point IDFT do you want?');
x2=input('Enter the sequence=');
n2=length(x2);
x2=[x2 zeros(1,N-n2)];
31
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
for k=1:N
for n=1:N
w=exp((2*pi*i*(k-1)*(n-1))/N);
%prev.step=>evaluating w-matrix
x(n)=w;
end
c(k,:)=x;
end
r=[c]*[x2']
r=r/n2;
%plotting magnitude and angle
subplot(211)
stem(abs(r));
title('IDFT-absolute value');
subplot(212)
stem(angle(r));
title('IDFT-angle');
RESULTS:-
Thus the MATLAB program for computing the IDFT of input sequence was performed and the
output was verified.
OUTPUT:
Enter the sequence=[33.0000 -12.5172 + 6.2941i -3.5451 - 4.0287i 2.0172 - 2.5757i 2.0451 + 0.1388i 1.0000 +
0.0000i 2.0451 - 0.1388i 2.0172 + 2.5757i -3.5451 + 4.0287i -12.5172 - 6.2941i]
r=
10.0000
0.0000 + 0.0000i
40.0001 + 0.0000i
70.0000 - 0.0000i
59.9997 - 0.0000i
50.0000 - 0.0000i
40.0002 - 0.0000i
29.9999 - 0.0000i
20.0001 + 0.0000i
10.0000 + 0.0000i
32
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
33
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
Experiment No: - 6 Fast Fourier Transform
AIM: - MATLAB program for computing 8-point DIT-FFT of any input sequence.
PROCEDURE:-
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window
PROGRAM:-
>> clear all
>> x = input('enter the sequence');
enter the sequence [0 1 2 3 4 5 6 7]
>> n = input('enter the length of fft');
enter the length of fft 8
>> k = 0:n-1;
>> X = fft(x,n);
>> stem(X);
>> ylabel('Imaginary axis -->');
>> xlabel('Real axis -->');
>> X
X=
Columns 1 through 6
28.0000 -4.0000 + 9.6569i -4.0000 + 4.0000i -4.0000 + 1.6569i -4.0000 -4.0000 - 1.6569i
Columns 7 through 8
-4.0000 - 4.0000i -4.0000 - 9.6569i
RESULTS:-
Thus the MATLAB program for computing the FFT of input sequence was performed and the
output was verified.
OUTPUT:
34
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
Syntax used in program:
rem(x,y) is x - n.*y where n = fix(x./y) if y ~= 0. If y is not an integer and the quotient x./y is within roundoff
error of an integer, then n is that integer. By convention, rem(x,0) is NaN. The input x and y must be real
arrays of the same size, or real scalars.
[H,W] = freqz(B,A,N) returns the N-point complex frequency response vector H and the N-point frequency
vector W in radians/sample of the filter. The frequency response is evaluated at N points equally spaced around
the upper half of the unit circle. If N isn't specified, it defaults to 512.
B = fir1(N,Wn) designs an N'th order lowpass FIR digital filter and returns the filter coefficients in length N+1
vector B. The cut-off frequency Wn must be between 0 < Wn < 1.0, with 1.0 corresponding to half the sample
rate. The filter B is real and has linear phase. The normalized gain of the filter at Wn is -6 dB.
abs(X) is the absolute value of the elements of X. When X is complex, abs(X) is the complex modulus
(magnitude) of the elements of X.
Experiment No: - 7
35
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
AIM: - To plot the magnitude & phase response of first order low pass filter.
PROCEDURE:-
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window
PROGRAM:-
clear all;
b1= [0.2 0.2]
a1= [1 -0.6]
w= 0:0.01:pi;
h1=freqz(b1,a1,w)
subplot(2,2,1)
plot(w/pi, abs(h1))
xlabel('normalised frequency');
ylabel('Magnitude');
Hold on;
subplot(2,2,2)
plot(w/pi, 20*log10(abs(h1)))
xlabel('normalised frequency');
ylabel('Magnitude in db');
Hold on;
subplot (2,1, 2)
plot(w/pi, angle(h1))
xlabel('normalised frequency');
ylabel('phase');
Hold on;
RESULTS:-
Thus the MATLAB program for designing of First order low pass filter was performed and the
output was verified.
OUTPUT:
The Magnitude and phase responses of First Order low pass filter are shown in figure.
36
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
37
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
Experiment No: - 8 FIR filter design using window techniques
AIM: - Design the digital FIR Low pass, High pass, Band pass, and Bandstop filters using
Rectangular window.
PROCEDURE:-
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window
PROGRAM:-
>> b = fir1(n,wp,y);
>> [h,o] = freqz(b,1,256);
>> m = 20*log10(abs(h));
>> subplot(2,2,1);
>> plot(o/pi,m);
>> ylabel('Gain in dB -->');
38
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
>> xlabel('(a) Normalized frequency -->');
>> b = fir1(n,wp,'high',y);
>> [h,o] = freqz(b,1,256);
>> m = 20*log10(abs(h));
>> subplot(2,2,2);
>> plot(o/pi,m);
>> ylabel('Gain in dB -->');
>> xlabel('(b) Normalized frequency -->');
Band-pass filter:
>> wn = [wp,ws];
>> b = fir1(n,wn,y);
>> [h,o] = freqz(b,1,256);
>> m = 20*log10(abs(h));
>> subplot(2,2,3);
>> plot(o/pi,m);
>> ylabel('Gain in dB -->');
>> xlabel('(c) Normalized frequency -->');
Band-stop filter:
>> b = fir1(n,wp,'stop',y);
>> [h,o] = freqz(b,1,256);
>> m = 20*log10(abs(h));
>> subplot(2,2,4);
>> plot(o/pi,m);
>> ylabel('Gain in dB -->');
>> xlabel('(d) Normalized frequency -->');
RESULTS:-
Thus the MATLAB program for designing of FIR Low pass, High pass, Band pass, and
Bandstop filters using Rectangular window was performed and the output was verified.
OUTPUT:
The gain responses of low-pass, high-pass, bandpass and bandstop filters using Rectangular window are shown in
figure.
39
Name of Student
Roll No.
INDORE INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
40
Name of Student
Roll No.