18EEL67_Digital SignalProcessing LabManual.pdf (1)
18EEL67_Digital SignalProcessing LabManual.pdf (1)
Digital Signal
Processing Laboratory
Manual 18EEL67
Faculty Incharge
Mr Harshavardhan V K
Lab Manual / Semester 6th
Electrical & Electronics Engineering Department, Acharya Institute of Technology
Overview
Year / Semester 3rd Year /6th Semester Academic Year 2022 - 2023
Laboratory Title Digital Signal Processing Laboratory Laboratory Code 18EEL67
Total Contact Hours 42 Exam Hours 3 Hours
CIE Marks 40 SEE Marks 60
Objectives
To explain the use of MATLAB software in evaluating the DFT and IDFT of given sequence
To verify the convolution property of the DFT
To design and implementation of IIR and FIR filters for given frequency specifications.
To realize IIR and FIR filters.
To help the students in developing software skills
Course Outcomes
Prerequisites
• MATLAB programming language.
• Basic operation such as creating file, delete, copy, rename etc should be known.
• DSP algorithm operation should be understood.
Base Course
• Signals and Systems
• Digital Signal Processing
Resource Required
• MATLAB software.
Page 1
Lab Manual / Semester 6th
Electrical & Electronics Engineering Department, Acharya Institute of Technology
Introduction
MATLAB Windows:
MATLAB works with through these basic windows
Command Window
This is the main window .it is characterized by MATLAB command prompt >> when you launch
the application program MATLAB puts you in this window all commands including those for
user-written programs ,are typed in this window at the MATLAB prompt
Page 2
Lab Manual / Semester 6th
Electrical & Electronics Engineering Department, Acharya Institute of Technology
Graphics Window
The output of all graphics commands typed in the command window are flushed to the graphics or
figure window, a separate gray window with white background color the user can create as many
windows as the system memory will allow.
Edit Window
This is where you write edit, create and save your own programs in files called M files.
Input-output
MATLAB supports interactive computation taking the input from the screen and flushing, the
output to the screen. In addition it can read input files and write output files.
Data Type
The fundamental data distinct data objects- integers, real numbers, matrices, character strings,
structures and cells. There is no need to declare variables as real or complex, MATLAB
automatically sets the variable to be real.
Dimensioning
Dimensioning is automatic in MAT required for vectors or arrays .we can find the dimensions of
an existing matrix or a vector with the size and length commands.
Page 3
Lab Manual / Semester 6th
Electrical & Electronics Engineering Department, Acharya Institute of Technology
Page 4
Lab Manual / Semester 6th
Electrical & Electronics Engineering Department, Acharya Institute of Technology
Page 5
Lab Manual / Semester 6th
Electrical & Electronics Engineering Department, Acharya Institute of Technology
Page 6
Lab Manual / Semester 6th
Electrical & Electronics Engineering Department, Acharya Institute of Technology
Contents
Expt.
Experiment
No.
1 Verification of Sampling Theorem both in time and frequency domains.
2 Evaluation of impulse response of a system.
3 To perform linear convolution of given sequences.
To perform circular convolution of given sequences using (a) the convolution summation
4 formula (b) the matrix method and (c) Linear convolution from circular convolution with
zero padding.
5 Computation of N – point DFT and to plot the magnitude and phase spectrum.
6 Linear and circular convolution by DFT and IDFT method.
7 Solution of a given difference equation.
8 Calculation of DFT and IDFT by FFT
9 Design and implementation of IIR filters to meet given specification (Low pass, high pass,
band pass and band reject Filters)
10 Design and implementation of FIR filters to meet given specification (Low pass, high pass,
band pass and band reject filters) using different window functions
11 Design and implementation of FIR filters to meet given specification (Low pass, high pass,
band pass and band reject filters) using frequency sampling technique.
12 Realization of IIR and FIR filters.
Reference
• Proakis & Monalakis, Digital Signal processing Principles Algorithms & Applications,
Pearson Education, Edition, New Delhi, 2007.
• Oppenheim & Schaffer, Discrete Time signal Processing, PHI. 2003.
• S. K. Mitra, Digital Signal processing, Tata Mc-Graw Hill, 2nd Edition, 2004.
• Lee Tan: Digital signal processing, Elsevier publications, 2007.
• Practical C++ programming by Oreilly.
• Introduction to programming with MATLAB for scientists and Engineers, 2e Broenkow,ML
2007.
• MATLAB and introduction with applications 2nd edition- Amos Gillat.
Evaluation Scheme
CIE Marks: 40
Internal Assessment: 16 Marks
Write-up: 03 Marks
Conduction & result: 10 Marks
Viva-voce: 03 Marks
Continues Assessment: 24
Scheme of External Examination
External Exam will be conducted for 100 Marks and obtained marks will be scaled down for
60 Marks by university
Page 7
Lab Manual / Semester 6th
Electrical & Electronics Engineering Department, Acharya Institute of Technology
Experiment 1
1.0 Verification of Sampling Theorem both in time and frequency domains.
1.1 Aim
To verify Sampling theorem for a signal of given frequency.
1.2 Theory
Sampling is a process of converting a continuous time signal (analog signal) x (t) into a discrete time
signal x[n], which is represented as a sequence of numbers. (A/D converter). Converting back x[n]
into analog (resulting in x(t)) is the process of reconstruction. (D/A converter). For x(t) to be exactly
the same as x (t), sampling theorem in the generation of x(n) from x(t) is used. The sampling
frequency fs determine the spacing between samples. Aliasing-A high frequency signal is converted
to a lower frequency, results due to under sampling. Though it is undesirable in ADCs, it finds
practical applications in stroboscope and sampling oscilloscopes.
Sampling theorem: Sampling theorem includes two definitions.
i. A band limit of the signal which is having finite energy, whose maximum frequency component
is W Hz can be completely representing into its samples at the rate of 2W samples/sec.
ii. A band limit of the signal which is having finite energy, whose maximum frequency component
is W Hz, can be completely recovered from its samples at the rate of 2W samples/sec.
Nyquist Rate Sampling: The Nyquist rate is the minimum sampling rate required to avoid aliasing,
equal to the highest modulating frequency contained within the signal. In other words, Nyquist rate is
equal to two sided bandwidth of the signal (Upper and lower sidebands) i.e. fs = 2W.To avoid
aliasing, the sampling rate must exceed the Nyquist rate. i. e. fs>fN, where fN =2W.
1.2 Algorithm
Page 8
Lab Manual / Semester 6th
Electrical & Electronics Engineering Department, Acharya Institute of Technology
Step 1: MATLAB can generate only discrete time signals. For an approximate analog signal xt,
choose the spacing between the samples to be very small (≈0), say 50µs = 0.00005. Next choose the
time duration, say xt exists for 0.05seconds. (tfinal in program) (For low frequency say <1000 Hz
choose 0.05 secs, for higher choose 0.01 secs or lesser as appropriate). Now begin with the vector that
represents the time base-
t = 0:0.00005:0.05;
The colon (:) operator in MATLAB creates a vector, in the above case a time vector running from 0 to
0.05 in steps of 0.00005. The semicolon (;) tells MATLAB not to display the result. Given t, the
analog signal xt of frequency f is generated as (cos (ωt) =cos (2πft)):-
xt=cos(2*pi*f*t);
Where pi is recognized as 3.14 by MATLAB.
Step 2: To illustrate oversampling condition, choose sampling frequency fs 0=2.2*f. For this sampling
rate T0=1/fs0, generate the time vector as n1 = 0:T0:0.05; & over sampled discrete time signal x1=cos
(2*pi*f*n1);
Step 3: Repeat step 2 for different sampling frequencies, i.e., fs=1.3*f & fs=2*f for under sampling
and Nyquist sampling conditions.
Page 9
Lab Manual / Semester 6th
Electrical & Electronics Engineering Department, Acharya Institute of Technology
clc; figure(2);
close all; plot(f,abs(xk));
clear all; xlabel('frequency');
f1 = input('Enter the first sine wave frequency = '); ylabel('amplitude');
f2= input('Enter the second sine wave frequency = title('Nyquist Rate Sampling');
'); fn = 2*max(f1,f2); grid;
fs = fn/2; fs = 2*fn;
t = [0:1/fs:0.1]; t = [0:1/fs:0.1];
x = cos(2*pi*f1*t)+cos(2*pi*f2*t); x = cos(2*pi*f1*t)+cos(2*pi*f2*t);
xk = fft(x); xk = fft(x);
f = [0:length(xk)-1]*fs/length(xk); f = [0:length(xk)-1]*fs/length(xk);
figure(1);
plot(f,abs(xk)); figure(3);
xlabel('frequency'); plot(f,abs(xk));
ylabel('amplitude'); xlabel('freq');
title('Under Sampling'); ylabel('amplitude');
grid; title('Over Sampling');
fs = fn; grid;
t = [0:1/fs:0.1];
x = cos(2*pi*f1*t)+cos(2*pi*f2*t);
xk = fft(x);
f = [0:length(xk)-1]*fs/length(xk);
1.6 Inference
1. From the under sampling plot observe the aliasing effect. The analog signal is of 200Hz
(T=0.005s). The reconstructed (from under sampled plot) is of a lower frequency. The alias
frequency is computed as fd-fs1 = 200-1.3*200 = 200-260= -60Hz. This is verified from the plot.
The minus sign results in an 180˚ phase shift.
2. Sampling at the Nyquist rate results in samples sin(πn) which are identically zero, i.e., we are
sampling at the zero crossing points and hence the signal component is completely missed. This
can be avoided by adding a small phase shift to the sinusoid. The above problem is not seen in
cosine waveforms (except cos (90n)). A simple remedy is to sample the analog signal at a rate
higher than the Nyquist rate. The Fig1.2 shows the result due to a cosine signal
(x1=cos(2*pi*fd*n1);
3. The over sampled plot shows a reconstructed signal almost similar to that of the analog signal.
Using low pass filtering the wave form can be further smoothened.
Page
Lab Manual / Semester 6th
Electrical & Electronics Engineering Department, Acharya Institute of Technology
Frequency Domain
1.8 Results
1.9 Conclusion
Page
Lab Manual / Semester 6th
Electrical & Electronics Engineering Department, Acharya Institute of Technology
Experiment 2
2.0 Evaluation of impulse response of a system
2.1 Aim
To write a MATLAB program to evaluate the impulse response of the system.
2.2 Theory
LTI Discrete time system is completely specified by its impulse response i.e. knowing the impulse
response we can compute the output of the system to any arbitrary input. Let h[n] denotes the impulse
response of the LTI discrete time systems. Since discrete time system is time invariant, its response to
[n-1] will be h[n-1] .Likewise the response to [n+2] , [n-4] and [n-6] will be h[n+2], h[n-4] and h[n-
6] .
From the above result arbitrary input sequence x[n] can be expressed as a weighted linear combination
of delayed and advanced unit sample in the form k=+ and k=-
x[n] = x[k] h[n-k]
where weight x[k] on the right hand side denotes specifically the k th sample value of the sequence.
The response of the LTI discrete time system to the sequence
x[k] [n-k] will be x[k] h [n-k].
As a result, the response y[n] of the discrete time system to x[n] will be given by
y[n] = x[k] h [n-k]..................(1)
This can be alternately written as
y[n] = x[n-k] h [k]...............(2)
The above equation (1) and (2) is called the convolution sum of the sequences x[n] and h[n] and
represented compactly as y[n] =x[n] * h[n] Where the notation * denotes the convolution sum.
Structure for Realization of Linear Time Invariant systems: Let us consider the first order system
Y(n)=-a 1y(n-1)+b0 x(n) +b1 x(n-1).This realization uses separate delays(memory) for both the input
and output samples and it is called as Direct form one structure. A close approximation reveals that
the two delay elements contain the same input w (n) and hence the same output w(n-1).consequently
these two elements can be merged into one delay. In contrast to the direct form I structure, this new
realization requires only one delay for auxiliary quantity w (n), and it is more efficient in terms of
memory requirements. It is called the direct form II structure and it is used extensively.
2.3 Algorithm
Page
Lab Manual / Semester 6th
Electrical & Electronics Engineering Department, Acharya Institute of Technology
2.5 Calculation
clc;
Let the Difference equation is given as y(n) = x(n)+0.5x(n-1)+0.85x(n-2)+y(n-1)+y(n-2)
close
y(n) = x(n)+0.5x(n-1)+0.85x(n-2)+y(n-1)+y(n-2)
all;
y(n) - y(n-1) - y(n-2) = x(n) + 0.5x(n-1) + 0.85x(n-2) Taking Z transform on both
clear
sides, y(z) - z-1 y(z)- z-2 y(z) = x(z) + 0.5 z-1 x(z) + 0.85 z-2 x(z)
all;
y(z)[1 - z-1 - z-2] = x(z)[1 + 0.5 z-1 + 0.85 z-2 ]
But, h(z) = y(z)/x(z) %difference equation of second order system
= [1 + 0.5 z-1 + 0.85 %y(n)=x(n)+.5x(n-1)+.85x(n-2)+y(n-1)+y(n-2)
z-2 ]/ [1 - z-1 - z-2] By dividing we
get H(z) = 1 + 1.5 z-1 b=input('enter
+ 3.35 z-2 + 4.85 thezcoefficient
-3
of x(n),x(n-1)..= ');
h(n) = [1 1.5 3.35 4.85] a=input('enter the coefficient of y(n),y(n-1)..= ');
N=input('enter the no of samples of imp
2.6 MATLAB Program: response='); [h,t]=impz(b,a,N);
subplot(2,1,1);
%figure(
1)
plot(t,h);
title('plot of impulse
response');
ylabel('amplitude');
xlabel('time index --- >N');
subplot(2,1,2);
%figure(
2)
stem(t,h)
Page
Lab Manual / Semester 6th
Electrical & Electronics Engineering Department, Acharya Institute of Technology
2.8 Results
2.9 Conclusion
Page
Lab Manual / Semester 6th
Electrical & Electronics Engineering Department, Acharya Institute of Technology
Experiment 3
3.1 Aim
To obtain Linear convolution of two finite duration sequences.
3.2 Theory
Convolution is an integral concatenation of two signals. It has many applications in numerous areas of
signal processing. The most popular application is the determination of the output signal of a linear
time-invariant system by convolving the input signal with the impulse response of the system. Note
that convolving two signals is equivalent to multiplying the Fourier transform of the two signals. In
linear systems, convolution is used to describe the relationship between three signals of interest: the
input signal, the impulse response, and the output signal. In linear convolution length of output
sequence is, length(y (n)) = length(x (n)) + length (h (n)) – 1.
Mathematical Formula:
The linear convolution of two continuous time signals x(t) and h(t) is defined by
Where x (n) is the input signal and h (n) is the impulse response of the system.
3.3 Algorithm
The timing information for a sequence is provided by another vector, say n=-3:5; creates a vector with
values from -3 to 5 with an increment of 1. During plotting, the time vector size and the sequence size
should be the same, i.e., the number of elements in the sequence x 1 and in its time vector n1 should be
the same. Similarly for x2 and y.
Page
Lab Manual / Semester 6th
Electrical & Electronics Engineering Department, Acharya Institute of Technology
3.5 Calculation
On simplification we get,
Z= X1 * X2
On Simplification, we get
Page
Lab Manual / Semester 6th
Electrical & Electronics Engineering Department, Acharya Institute of Technology
Page
Lab Manual / Semester 6th
Electrical & Electronics Engineering Department, Acharya Institute of Technology
3.9 Results
3.10 Conclusion
Page
Lab Manual / Semester 6th
Electrical & Electronics Engineering Department, Acharya Institute of Technology
Experiment 4
4.0 (a) Circular Convolution of two given sequences using summation formula.
(b) Aim
To write MATLAB program to find circular convolution by matrix method.
(c) Aim
To find linear convolution from circular convolution using MATLAB program with zero padding.
4.2 Theory
a) Circular Convolution of two given sequences using summation formula.
As seen in the last experiment, the output y[n] of a LTI (linear time invariant) system can be
obtained by convolving the input x[n] with the system‟s impulse response h[n]. The above linear
convolution is generally applied to aperiodic sequences. Whereas the Circular Convolution is used
to study the interaction of two signals that are periodic.
The linear convolution sum is y[n] x[n] h[n]
x[k ]h[n k] x[n k ]h[k]
k
. To compute
k
x[k ]h[ n k
k
N
] where the index
nk implies circular shifting operation and kimplies folding the sequence circularly.
N N
Steps for circular convolution are the same as the usual convolution, except all index calculations
are done "mod N" = "on the wheel".
Plot f [m] and h [−m] as shown in Fig. 4.1. (use f(m) instead of x(k))
Multiply the two sequences
Add to get y[m]
"Spin" h [−m] n times Anti Clock Wise (counter-clockwise) to get h[n-m].
Where x[n] and h[n] can be both finite and infinite duration sequences. If infinite sequences, they
should be periodic, and the N is chosen to be at least equal to the period. If they are finite
Page
Lab Manual / Semester 6th
Electrical & Electronics Engineering Department, Acharya Institute of Technology
sequences N is chosen as >= to max (xlength, hlength). Whereas in linear convolution N>=
xlength+hlength-1.
Let x1(n) and x2(n) are finite duration sequences both of length N with DFT‟s X1(k) and X2(k).
Convolution of two given sequences x1(n) and x2(n) is given by the equation,
x3(n) = IDFT[X3(k)]where X3(k) = X1(k) X2(k)
Matrix Method for Convolution: Due to the importance of Discrete Fourier Transform (DFT) in signal
processing application, it is critical to have an efficient method to compute this algorithm. DFT operates on a N
-point sequence of numbers, referred to as x(n) . The value x(n) is presented in time domain data and usually
can be taught as a uniformly sampled version of a finite period of a continuous function f (x) . The DFT of x(n)
sequence is transformed to X(k) in frequency domain representation employing by using Discrete Fourier
Transform. The functions x(n) and X(k) is generally represented in complex signal form, given by
Where x(n) is the input time domain representation and N is the number of input to the DFT. The value
n represents the discrete time-domain index and k is the normalized frequency domain index. The description of
efficient computation is discussed on DFT methods since the IDFT and DFT consumes the same type of
computational algorithm. From the computation of each value of k , it is observed that direct computation of
X(k) involves N complex multiplications ( 4N real multiplications) and N −1 complex additions ( 4N − 2 real
additions). Eventually, to compute all N values of the DFT requires N2 complex multiplications and N2 − N
complex additions. The multiplication of two discrete time signals in discrete Fourier transform is equivalent to
the circular convolution of their sequences in time domain. For x(n) and h(n) signal convolution is express as:
Here the term h(m-n)N indicates the circular convolution. The convolution in time domain of two
signal x and h is perform by multiplying its discrete fourier transform and the converting it in time domain by
inverse discrete fourier transform. The equation of DFT is the summation of discrete signal multiplied by
Page
Lab Manual / Semester 6th
Electrical & Electronics Engineering Department, Acharya Institute of Technology
4.3 Algorithm
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
MATLAB recognizes index 1 to be positive maximum. Index 0 is not recognized. Hence in the below
program wherever y, x and h sequences are accessed, the index is added with +1. the modulo index
calculation for circular convolution is carried out using the function - MOD Modulus (signed
remainder after division). MOD(x, y) is x - y.*floor(x./y) if y ~= 0. By convention, MOD(x, 0) is x.
The input x and y must be real arrays of the same size, or real scalars. MOD(x, y) has the same sign as
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
y while REM(x, y) has the same sign as x. MOD(x, y) and REM(x, y) are equal if x and y have the
same sign, but differ by y if x and y have different signs.
MATLAB program to find Circular Convolution by matrix multiplication using circshift command or
we can use convmtx command.
A = convmtx (h,n) returns the convolution matrix, A, such that the product of A and a vector, x, is the
convolution of h and x. If h is a column vector of length m, A is (m+n-1)-by-n and the product
of A and a column vector, x, of length n is the convolution of h and x. If h is a row vector of
length m, A is n-by-(m+n-1) and the product of a row vector, x, of length n with A is the convolution
of h and x. Convmtx handles edge conditions by zero padding.
Y = circshift(A,K) circularly shifts the elements in array A by K positions. If K is an integer,
then circshift shifts along the first dimension of A whose size does not equal 1. If K is a vector of
integers, then each element of K indicates the shift amount in the corresponding dimension of A.
Y = circshift(A,K,dim) circularly shifts the values in array A by K positions along dimension dim.
Inputs K and dim must be scalars.
4.5 Calculation
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
x(n)= [ 121212]
h(n)= [1234]
M= length(x) + length(h)-1
= 6 +4 -1
=9
x(n) = [ 1 2 12 12 0 0 0 ]
h(n)= [ 1 2 34 0 0 0 0 0]
y(n)= x(n).h(n)
1 0 0 0 2 1 1 2 1 1
2 1 0 0 0 2 2 1 2 2
1 2 1 0 0 0 1 2 1 3
21210021 2 4
y(n)= 12121002 1 . 0
21212100 2 0
02121200 0 0
00212110 0 0
00021221 0 0
y(n)= [ 14814161415108]
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
y(n+1)=y(n+1)+hn(k+1)*xn(i+1
); end;
end;
disp('circular convolution output‟);
disp(y);
subplot(2,2,1
); stem(xn);
xlabel('n');
ylabel('x(n)');
title('plot of h(n)');
subplot(2,2,2
); stem(hn);
xlabel('n');
ylabel('h(n)');
title('plot of h(n)');
subplot(2,1,2
); stem(y);
xlabel('n');
ylabel('y(n)');
title('circular convolution output');
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
x=transpose(x1); h=transpose(h1);
temp=h; for i=1:N-1;
temp=circshift(temp,
1);
h=horzcat(h,temp);
end;
h
x
y=h*x ;
disp('Circular convolved output y[n] =
'); y
subplot(3,1,1);
stem(x1); xlabel('N--
>');
ylabel('Amplitude--
>');
title('1st input sequence x[n]
'); subplot(3,1,2);
stem(h1); xlabel('N-->');
ylabel('Amplitude-->');
title('2nd input sequence
clc;
close
all;
clear
all;
x1=input('enter the first sequence=');
x2=input('enter the second sequence=');
n=input('enter the no of points of the
dft='); subplot(3,1,1);
stem(x1,'filled');
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
stem(x2,'filled');
title('plot of second
sequence'); n1=length(x1);
n2=length(x
2);
m=n1+n2-1;
x=[x1 zeros(1,n2-1)];
y=[x2 zeros(1,n1-
1)]; x_fft=fft(x,m);
y_fft=fft(y,m);
dft_xy=x_fft.*y_fft
y=ifft(dft_xy,m);
disp('the circular convolution result
is'); disp(y);
subplot(3,1,3);
stem(y,'filled');
Input:
enter the first sequence x(n)=[1 1 2 1]
enter the second sequence h(n)=[1 2 3 4]
Output:
circular convolution output 13141112
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
4.8 Results
4.9 Conclusion
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
Experiment 5
5.0 Computation of N point DFT of a given sequence and to plot magnitude and phase spectrum.
5.1 Aim
To compute N-point DFT of a given sequence and to plot magnitude and phase spectrum.
5.2 Theory
Discrete Fourier Transform is a powerful computation tool which allows us to evaluate the Fourier
Transform X (ejw) on a digital computer or specially designed digital hardware. Since X(ej ) is
continuous and periodic, the DFT is obtained by sampling one period of the Fourier Transform at a
finite number of frequency points. Apart from determining the frequency content of a signal, DFT is
used to perform linear filtering operations in the frequency domain.
The sequence of N complex numbersx0... xN−1 is transformed into the sequence of N complex numbers
X0... XN−1 by the DFT according to the formula:
k = 0,1, …. N-1
5.3 Algorithm
MATLAB has an inbuilt function „FFT‟ which computes the Discrete Fourier transform. FFT(X) is
the discrete Fourier transform (DFT) of vector X. For length N input vector x, the DFT is a length N
vector X, with elements N. FFT(X,N) is the N-point FFT, padded with zeros if X has less than N
points and truncated if it has more. The magnitude spectrum is computed using the function ABS
Absolute value. 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. The phase spectrum is computed using the
function ANGLE Phase angle. ANGLE (H) returns the phase angles, in radians, of a matrix with
complex elements.
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
5.5 MATLAB Program: Computation of N point DFT of a given sequence and to plot magnitude and
clc;
close
all;
clear
all;
x=input('enter the sequence= ');
N=input('enter the no of dft points=
'); xk=fft(x,N);
disp('N point DFT of the sequence
is=') disp(xk);
n=0:1:N-1;
figure(1);
stem(n,abs(xk)
);
disp('magnitude of the sequence
is=') disp(abs(xk));
xlabel('k');
ylabel('|xk|');
title('magnitude
spectrum'); figure(2);
stem(angle(xk));
disp('phase value of the sequence
is=') disp(angle(xk));
xlabel('k');
ylabel('angle (xk)');
title('phase
spectrum');
figure(3);
n1=0:1:length(x)-1;
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
5.6 Calculation
EXAMPLE:
Let us assume the input sequence x[n] = [1 1 0 0]
We have,
k = 0,1, …. N-1
For k = 0,
X(1) = 1 – j
For k = 2
For k = 3,
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
5.8 Results
5.9 Conclusion
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
Experiment 6
6.1 Aim
To write MATLAB program to verify linear and circular convolution by DFT and IDFT method.
6.2 Theory
Circular convolution is another way of finding the convolution sum of two input signals. It resembles
the linear convolution, except that the sample values of one of the input signals is folded and right
shifted before the convolution sum is found. Also note that circular convolution could also be found
by taking the DFT of the two input signals and finding the product of the two frequency domain
signals. The Inverse DFT of the product would give the output of the signal in the time domain which
is the circular convolution output. The two input signals could have been of varying sample lengths.
But we take the DFT of higher point, which ever signals levels to. For e.g. If one of the signal is of
length 256 and the other spans 51 samples, then we could only take 256 point DFT. So the output of
IDFT would be containing 256 samples instead of 306 samples, which follows N1+N2 – 1 where N1
& N2 are the lengths 256 and 51 respectively of the two inputs. Thus the output which should have
been 306 samples long is fitted into 256 samples. The 256 points end up being a distorted version of
the correct signal. This process is called circular convolution. Circular convolution is explained using
the following example.
6.3 Algorithm
1. Give input sequence x[n].
2. Give impulse response sequence h[n].
3. Find the Circular Convolution and linear convolution y[n] using the DFT method.
4. Plot x[n],h[n],y[n].
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
6.4 Calculation
Circular Convolution
x[n]= [1 2 3 4] y[n]= [3 5 2 1]
x(k)=
h(k)=
1
y(n)=
4
y(n)=
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
clc; clc;
close all; close all;
clear all; clear all;
x1=input('enter the first sequence='); x=input('enter input x(n)= ');
x2=input('enter the second sequence='); m=length(x)
n=input('enter the no of points of the dft='); h=input('enter input h(n)= ');
subplot(3,1,1); n=length(h)
stem(x1,'filled'); subplot(3,2,1);
title('plot of first sequence'); stem(x);
subplot(3,1,2); title('input sequence x(n)');
stem(x2,'filled'); xlabel('-- - ->n');
title('plot of second sequence'); ylabel('-- - ->amplitude');
n1=length(x1); grid;
n2=length(x2); subplot(3,1,2);
m=n1+n2-1; stem(h);
x=[x1 zeros(1,n2-1)]; title('input sequence h(n)');
y=[x2 zeros(1,n1-1)]; xlabel('-- - ->n');
x_fft=fft(x,m); ylabel('-- - ->amplitude');
y_fft=fft(y,m); grid;
dft_xy=x_fft.*y_fft disp('circular convolution of x(n) &h(n) ');
y=ifft(dft_xy,m); y1=fft(x,n);
disp('the linear convolution result is'); y2=fft(h,n);
disp(y); y3=y1.*y2;
subplot(3,1,3); y=ifft(y3,n);
stem(y,'filled'); disp(y);
title('plot of linearly convoluted sequence'); subplot(3,1,3);
stem(y);
title('circular convoluted output');
xlabel('-- - ->n');
ylabel('-- - ->amplitude');
grid;
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
6.8 Results
6.9 Conclusion
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
Experiment 7
7.1 Aim
To obtain the impulse response/step response of a system described by the given difference equation.
7.2 Theory
A difference equation with constant coefficients describes a LTI system. For example the
difference equation y[n] + 0.8y[n-2] + 0.6y[n-3] = x[n] + 0.7x[n-1] + 0.5x[n-2] describes a LTI
system of order 3. The coefficients 0.8, 0.7, etc are all constant i.e., they are not functions of time
(n). The difference equation y[n]+0.3ny[n-1]=x[n] describes a time varying system as the
coefficient 0.3n is not constant.
The difference equation can be solved to obtain y[n], the output for a given input x[n] by
rearranging as y[n] = x[n] + 0.7x[n-1]+0.5x[n-2]- 0.8y[n-2]- 0.6y[n-3] and solving.
The output depends on the input x[n]
With x[n]= δ[n], an impulse, the computed output y[n] is the impulse response.
If x[n]=u[n], a step response is obtained.
If x[n] = cos(wn) is a sinusoidal sequence, a steady state response is obtained (wherein y[n] is of
the same frequency as x[n], with only an amplitude gain and phase shift-refer Fig.7.3).
Similarly for any arbitrary sequence of x[n], the corresponding output response y[n] is computed.
The difference equation containing past samples of output, i.e., y[n-1], y[n-2], etc leads to a
recursive system, whose impulse response is of infinite duration (IIR). For such systems the
impulse response is computed for a large value of n, say n=100 (to approximate n=∞). The
MATLAB function filter is used to compute the impulse response/ step response/ response to any
given x[n]. Note: The filter function evaluates the convolution of an infinite sequence (IIR) and
x[n], which is not possible with conv function (remember conv requires both the sequences to be
finite).
The difference equation having only y[n] and present and past samples of input (x[n], x[n-k]),
represents a system whose impulse response is of finite duration (FIR). The response of FIR
systems can be obtained by both the „conv‟ and „filter‟ functions. The filter function results
in a response whose length is equal to that of the input x[n], whereas the output sequence from
conv function is of a longer length (xlength + hlength-1).
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
1.7.4Input the
Algorithm
two sequences as a and b representing the coefficients of y and x.
2. If IIR response, then input the length of the response required (say 100, which can be made
constant).
3. Compute the output response using the „filter‟ command.
4. Plot the input sequence & impulse response (and also step response, etc if required).
7.5 Calculation
The difference equation is given by
y(n) = x(n)+0.7x(n-1)+0. 5x(n-2)-0.8y(n-2)-0.6y(n-3)
y(z)+0.8z-2+0.6z-3 y(z)=x(z)+0.7z-1 x(z) + 0.5 z-2 x(z)
y(z) [1+0.8z-2+0.6z-3=x(z)+(1+0.7z-1+0.5z-2)]
H(z)= y(z)
x(z)
= (1+0.7z-1+0.5z-2)
(1+0.8z-1+0.6z-3)
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
7.8 Result
7.9 Conclusion
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
Experiment 8
8.1 Aim
To write a MATLAB program for computation of DFT and IDFT using Direct and FFT method
8.2 Theory
DFT:
Discrete Fourier Transform (DFT) is used for performing frequency analysis of discrete time signals.
DFT gives a discrete frequency domain representation whereas the other transforms are continuous in
frequency domain. The N point DFT of discrete time signal x[n] is given by the equation.
The inverse DFT allows us to recover the sequence x[n] from the frequency samples
FFT:
A fast Fourier transform (FFT) is an efficient algorithm to compute the discrete Fourier transform
(DFT) and its inverse. FFTs are of great importance to a wide variety of applications, from digital
signal processing and solving partial differential equations to algorithms for quick multiplication of
large integers. Evaluating the sums of DFT directly would take O(N 2) arithmetical operations. An
FFT is an algorithm to compute the same result in only O(N log N) operations. In general, such
algorithms depend upon the factorization of N, but there are FFTs with O(N log N) complexity for all
N, even for prime N. Since the inverse DFT is the same as the DFT, but with the opposite sign in the
exponent and a 1/N factor, any FFT algorithm can easily be adapted for it as well.
8.3 Algorithm
1. Get the input sequence
2. Find the DFT of the input sequence using direct equation of DFT.
3. Find the IDFT using the direct equation.
4. Find the FFT of the input sequence using MATLAB function.
5. Find the IFFT of the input sequence using MATLAB function.
6. Display the above outputs using stem function
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
8.4 Calculation
x(n)=[1 1 1 1 1 1 0 0 ]
this equation is formulated in the matrix form by equation
XN= [Wn] xN
with N=8 x8=[W8]x8
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
Output:
xf= 6.0000-0.7071 - 1.7071i1.0000 - 1.0000i0.7071 + 0.2929i0 - 0.0000i
0.7071 - 0.2929i 1.0000 + 1.0000i-0.7071 + 1.7071i
8.7 Results
8.8 Conclusion
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
Experiment 9
9.0 Design and implementation of IIR filter to meet given specifications.
9.1 Aim
Design and implementation of IIR filter to meet given specifications.
9.2 Theory
Infinite Impulse Response (IIR) filter: IIR filters are of recursive type, whereby the present output
sample depends on the present input, past input samples and output samples. The impulse response
h(n) for a realizable filter is,h(n) = 0 for n 0. And for stability, it must satisfy the condition,
There are two methods of stating the specifications as illustrated in previous program. In the first
program, the given specifications are directly converted to digital form and the designed filter is also
implemented. In the last two programs the Butterworth and chebyshev filters are designed using
bilinear transformation (for theory verification).
Method I: Given the order N, cutoff frequency fc, sampling frequency fs and the IIR filter type
(Butterworth, cheby1, cheby2).
Step 1: Compute the digital cut-off frequency Wc (in the range -π < Wc < π, with π corresponding
to fs/2) for fc and fs in Hz. For example let fc=400Hz, fs=8000Hz
Wc = 2*π* fc / fs = 2* π * 400/8000 = 0.1* π radians
For MATLAB the Normalized cut-off frequency is in the range 0 and 1, where 1 corresponds to
fs/2 (i.e., fmax)). Hence to use the MATLAB commands
wc = fc / (fs/2) = 400/(8000/2) = 0.1
Note: if the cut off frequency is in radians then the normalized frequency is computed as wc = Wc
/π
Step 2: Compute the Impulse Response [b,a] coefficients of the required IIR filter and the response
type (low pass, band pass, etc) using the appropriate butter, cheby1, cheby2 command. For
example given a Butterworth filter, order N=2, and a high pass response, the coefficients [b,a] of
the filter are computed using the MATLAB inbuilt command „butter‟ as [b,a] =butter(N, wc ,
'high');
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
Method 2:
Given the pass band (Wp in radians) and Stop band edge (Ws in radians) frequencies, Pass band ripple
Rp and stop band attenuation As.
Step 1: Since the frequencies are in radians divide by π to obtain normalized frequencies to get
Wp=Wp/pi and ws=Ws/pi
If the frequencies are in Hz (note: in this case the sampling frequency should be given), then
obtain normalized frequencies as wp=fp/(fs/2), ws=fstop/(fs/2), where fp, fstop and fs are the
passband, stop band and sampling frequencies in Hz
Step 2: Compute the order and cut off frequency as
[N, wc] = BUTTORD(wp, ws, Rp, Rs)
Step 3: Compute the Impulse Response [b,a] coefficients of the required IIR filter and the response
type as [b,a] =butter(N, wc , 'high');
BUTTORD Butterworth filter order selection. [N, Wn] = BUTTORD(Wp, Ws, Rp, Rs) returns the
order N of the lowest order digital Butterworth filter that loses no more than Rp dB in the pass
band and has at least Rs dB of attenuation in the stop band. Wp and Ws are the pass band and stop
band edge frequencies, normalized from 0 to 1 (where 1 corresponds to pi radians/sample). For
example,
Low pass: Wp = .1, Ws = .2 High pass: Wp = .2, Ws = .1
Band pass: Wp = [.2 .7], Ws = [.1 .8] Band stop: Wp = [.1 .8], Ws = [.2 .7]
BUTTORD also returns Wn, the Butterworth natural frequency (or, the "3 dB frequency") to use
with BUTTER to achieve the specifications. [N, Wn] = BUTTORD (Wp, Ws, Rp, Rs,‟s‟) does the
computation for an analog filter, in which case Wp and Ws are in radians/second. When Rp is
chosen as 3 dB, the Wn in BUTTER is equal to Wp in BUTTORD.
BUTTER Butterworth digital and analog filter design. [B,A] = BUTTER (N,Wn) designs an Nth
order low pass digital Butterworth filter and returns the filter coefficients in length N+1 vectors B
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
(numerator) and A (denominator). The coefficients are listed in descending powers of z. The cutoff
frequency Wn must be 0.0 < Wn < 1.0, with 1.0 corresponding to half the sample rate. If Wn is a
two-element vector, Wn = [W1 W2], BUTTER returns an order 2N band pass filter with pass band
W1 < W < W2. [B,A] = BUTTER (N, Wn, 'high') designs a high pass filter. [B,A] = BUTTER
(N, Wn, 'stop') is a band stop filter if Wn = [W1 W2]. BUTTER (N, Wn, 's'), BUTTER(N, Wn,
'high','s') and BUTTER(N, Wn, 'stop', 's') design analog Butterworth filters. In this case, Wn is in
[rad/s] and it can be greater than 1.0.
9.4 Algorithm
Step 2: T=1
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
Step 5: poles
filter. Step 1:
rad
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
Step 2: T=1
Step 5: poles
Therefore
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
1.9.6
Butterworth
MATLABLPF Program: Design and Implementation of IIR filter to meet
given specifications. (Butterworth Filter)
%the specifications %system function of the filter
clc; [b,a]=butter(N,Wn)
clear all; w=0:0.01:pi;
close all; [h,om]=freqz(b,a,w,'whole');
alphap=3; m=abs(h);
alphas=15; an=angle(h);
fp=500; subplot(2,1,1);
fs=750; plot(om/pi,20*log(m));
f=2000; grid;
omp=2*fp/f; ylabel('gain in dB');
oms=2*fs/f; xlabel('normalized frequency');
%to find cut off frequency & order of the filter subplot(2,1,2);
[N,Wn]=buttord(omp,oms,alphap,alphas) plot(om/pi,an);
disp('order of the filter n ='); grid;
disp(N); ylabel('phase in radian');
disp('cut off frequency Wn= '); xlabel('normalized frequency');
disp(Wn); % to convert analog filter to digital filter
% using bilinear transformation
[bz,az]=bilinear(b,a,f);
Output:
N=2
Wn = 0.5083
order of the filter n = 2
cut off frequency Wn= 0.5083
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
b = 0.30050.60110.3005
a = 1.00000.03040.1717
2. Butterworth HPF
%the specifications %system function of the filter
clc; [b,a]=butter(N,Wn,'high')
clear all; w=0:0.01:pi;
close all; [h,om]=freqz(b,a,w,'whole');
alphap=3; m=abs(h);
alphas=15; an=angle(h);
fp=500; subplot(2,1,1);
fs=750; plot(om/pi,20*log(m));
f=2000; grid;
omp=2*fp/f; ylabel('gain in dB');
oms=2*fs/f; xlabel('normalized frequency');
%to find cut off frequency & order of the filter subplot(2,1,2);
[N,Wn]=buttord(omp,oms,alphap,alphas) plot(om/pi,an);
disp('order of the filter n ='); grid;
disp(N); ylabel('phase in radian');
disp('cut off frequency Wn= '); xlabel('normalized frequency');
disp(Wn); % to convert analog filter to digital filter
% using bilinear transformation
[bz,az]=bilinear(b,a,f);
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
Output:
N= 2
Wn = 0.5083
order of the filter n = 2
cut off frequency Wn= 0.5083 b = 0.2853 -0.57070.2853
a = 1.00000.03040.1717
3. Butterworth BPF
clear all; subplot(2,1,1);
alphap=2; plot(ph/pi,m);
alphas=20; grid;
wp=[0.2*pi,0.4*pi]; ylabel(„gain in dB‟);
ws=[0.1*pi,0.5*pi]; xlabel(„normalized frequency‟);
[n,wn]=buttord(wp/pi,ws/pi,alphap,alphas) subplot(2,1,2);
[b,a]=butter(n,wn) plot(ph/pi,an);
w=0:0.01:pi; grid;
[h,ph]=freqz(b,a,w); ylabel(„phase in radian‟);
m=20*log(abs(h)); xlabel(„normalized frequency‟);
an=angle(h);
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
Output:
n =4
wn =0.19500.4082
4. Butterworth BSF
clear all; subplot(2,1,1);
alphap=2; plot(ph/pi,m);
alphas=20; grid;
wp=[0.2*pi,0.4*pi]; ylabel(„gain in dB‟);
ws=[0.1*pi,0.5*pi]; xlabel(„normalized frequency‟);
[n,wn]=buttord(wp/pi,ws/pi,alphap,alphas) subplot(2,1,2);
[b,a]=butter(n,wn,‟stop‟) plot(ph/pi,an);
w=0:0.01:pi; grid;
[h,ph]=freqz(b,a,w); ylabel(„phase in radian‟);
m=20*log(abs(h)); xlabel(„normalized frequency‟);
an=angle(h);
Output:
n= 4
wn = 0.1950 0.4082
b= 0.4086 -2.0201 5.3798 -9.1468 10.8962 -9.1468 5.3798 -2.0201 0.4086
a = 1.0000 -3.8710 7.9699 -10.6417 10.0781 -6.8167 3.2579 -1.0044 0.1670
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
1.9.7 MATLAB
Chebyshev Program: Design and Implementation
type-I LPF of IIR filter to meet
an=angle(h);
given specifications. (Chebyshev Filter)
clear all; subplot(2,1,1);
alphap=2; plot(ph/pi,m);
alphas=20; grid;
wp=0.05*pi; ylabel('gain in dB');
ws=0.25*pi; xlabel('normalized frequency');
[n,wn]=cheb1ord(wp/pi,ws/pi,alphap,alphas) subplot(2,1,2);
[b,a]=cheby1(n,alphap,wn) plot(ph/pi,an);
w=0:0.01:pi; grid;
[h,ph]=freqz(b,a,w); ylabel('phase in radian');
m=20*log10(abs(h)); xlabel('normalized frequency');
Output:
N=2
Wn = 0.5083
order of the filter n = 2
cut off frequency Wn=
0.5083 b = 0.2853 -0.5707
0.2853
a = 1.0000 0.0304 0.1717
n=2
wn = 0.0500
b = 0.0038 0.0076 0.0038
a = 1.0000 -1.8625 0.8816
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
Output:
N=2
Wn = 0.5083
order of the filter n = 2
cut off frequency Wn= 0.5083
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
Output:
n= 4
wn = 0.1950 0.4082
b= 0.0017 0 -0.0067 0 0.0101
0 -0.0067 0 0.0017
a =1.0000 -4.3974 10.5070 -16.3549
18.1728 -14.51218.2671 -3.06360.6200
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
Output:
n= 3
wn = 0.1000 0.5000
b= 0.2537 -0.8969 1.6246 -1.9332 1.6246
-0.8969 0.2537
a= 1.0000 -2.1790 1.8039 -1.1676 0.9185
-0.3803 0.0343
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
Experiment 10
10.0 Design and implementation of FIR filter to meet given specifications (using different window techn
Design and implementation of FIR filter to meet given specifications (low pass filter using
10.1 Aim
hamming window).
10.2 Theory
There are two types of systems – Digital filters (perform signal filtering in time domain) and spectrum
analyzers (provide signal representation in the frequency domain). The design of a digital filter is
carried out in 3 steps- specifications, approximations and implementation.
DESIGNING AN FIR FILTER (using window method):
Method I: Given the order N, cutoff frequency fc, sampling frequency fs and the window.
Step 1: Compute the digital cut-off frequency Wc (in the range -π < Wc < π, with π
corresponding to fs/2) for fc and fs in Hz. For example let fc=400Hz, fs=8000Hz Wc = 2*π* fc
/ fs = 2* π * 400/8000 = 0.1* π radians. For MATLAB the Normalized cut-off frequency is in
the range 0 and 1, where 1 corresponds to fs/2 (i.e., fmax)). Hence to use the MATLAB
commands. wc = fc / (fs/2) = 400/(8000/2) = 0.1
Note: if the cut off frequency is in radians then the normalized frequency is computed as wc = Wc / π
Step 2: Compute the Impulse Response h(n) of the required FIR filter using the given Window
type and the response type (low pass, band pass, etc). For example given a rectangular
window, order N=20, and a high pass response, the coefficients (i.e., h[n] samples) of the filter
are computed using the MATLAB inbuilt command „fir1‟ as h =fir1(N, wc , 'high',
boxcar(N+1)); Note: In theory we would have calculated h[n]=hd[n]×w[n], where hd[n] is the
desired impulse response (low pass/ high pass, etc given by the sinc function) and w[n] is the
window coefficients. We can also plot the window shape as stem(boxcar(N)). Plot the
frequency response of the designed filter h(n) using the freqz function and observe the type of
response (low pass / high pass /band pass).
Method 2:
Given the pass band (wp in radians) and Stop band edge (ws in radians) frequencies, Pass band
ripple Rp and stopband attenuation As.
Step 1: Select the window depending on the stopband attenuation required. Generally if As>40
dB, choose Hamming window. (Refer table )
Step 2: Compute the order N based on the edge frequencies as
Transition bandwidth = tb=ws-wp;
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
N=ceil (6.6*pi/tb);
Step 3: Compute the digital cut-off frequency Wc as
Wc=(wp+ws)/2
Now compute the normalized frequency in the range 0 to 1 for MATLAB as
wc=Wc/pi;
Note: In step 2 if frequencies are in Hz, then obtain radian frequencies (for computation of tb and N)
as wp=2*pi*fp/fs, ws=2*pi*fstop/fs, where fp, fstop and fs are the passband, stop band and sampling
frequencies in Hz
Step 4: Compute the Impulse Response h(n) of the required FIR filter using N, selected
window, type of response(low/high,etc) using „fir1‟ as in step 2 of method 1.
IMPLEMENTATION OF THE FIR FILTER
1. Once the coefficients of the FIR filter h[n] are obtained, the next step is to simulate an input
sequence x[n], say input of 100, 200 & 400 Hz (with sampling frequency of fs), each of 20/30
points. Choose the frequencies such that they are >, < and = to fc.
2. Convolve input sequence x[n] with Impulse Response, i.e., x (n)*h (n) to obtain the output of
the filter y[n]. We can also use the „filter‟ command.
3. Infer the working of the filter (low pass/ high pass, etc).
10.3 Algorithm
1. Get the sampling frequency
2. Get the pass band frequency
3. Get the stop band frequency
4. Get the pass band ripple and stop band attenuation
5. Select the window suitable for stop band attenuation
6. Calculate the order N based on transition width
7. Find the N window coefficients
8. Find the impulse response of h[n]
9. Verify the frequency response of h[n]
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
If Wn is a two-element vector, Wn = [W1 W2], FIR1 returns an order N band pass filter with pass
band W1 < W < W2. B = FIR1 (N, Wn, 'high') designs a high pass filter. B = FIR1 (N, Wn, 'stop') is a
band stop filter if Wn = [W1 W2]. If Wn is a multi-element vector,
Wn = [W1 W2 W3 W4 W5 ... WN], FIR1 returns an order N multiband filter with bands
FREQZ Digital filters frequency response. [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 whose
numerator and denominator coefficients are in vectors B and A. 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. For FIR filter enter A=1 and B = h[n] coefficients. Appropriately choose N as 128, 256, etc
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
10.6 MATLAB Program: Design and Implementation of FIR filter using windows Techniques.
k=1 : 1 : ((N-1)/2);
n=k-1-((N-1)/2);
w_han(k)=.5-.5*cos(2*pi*(k-1)/(N-1));
hd(k)=(sin(wc2*n)-sin(wc1*n))./(pi*n);
for m=1:1:a
Hw3= hn(m)*((exp(j*w*(1-m)))+ (exp(-j*w*(1-
m+2*a)))); Hw2=Hw2+Hw3;
end
Hw=Hw2+Hw
1;
H_mag=abs(Hw)
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
Output:
1. Frequency response of band pass filter using Hamming window
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
Hw3=hn(m)*((exp(j*w*(1-m)))+(exp(-j*w*(1-
m+2*a)))); Hw2=Hw2+Hw3;
end
Hw=Hw2+Hw
1
H_mag=abs(H
w)
plot(w/pi,H_mag,'k');
grid;
title('magnitude Response','fontweight','b');
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
Output:
3. To plot frequency response of high pass filter using Hamming window
hn = -0.00810.0469 -0.14410.2000
H_mag = 0.0104 0.0093 0.00600.00050.00770.01980.03830.06610.1062
0.1605
0.2290 0.3083 0.39230.4723 0.53870.58270.5981
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
for m=1:1:a
Hw3= hn(m)*((exp(j*w*(1-m)))+ (exp(-j*w*(1-
m+2*a)))); Hw2=Hw2+Hw3;
end
Hw=Hw2+Hw
1;
H_mag=abs(Hw)
plot(w/pi,H_mag,'k');
grid;
title('Magnitude Response','fontweight','b');
xlabel('Normalised frequency,\omega/\
Output:
4. To plot frequency response of band stop filter using rectangular window
hn = -0.04580.22230.01910.7500
H_mag = 1.1413 1.1222 1.0647 0.9698 0.8418 0.6909 0.5348 0.3975 0.3054
0.2809 0.3364 0.4688 0.6582 0.8705 1.0641 1.1994 1.2479
10.8 Results
10.9 Conclusion
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
Experiment 11
11.0 Design and implement FIR filter using frequency sampling method.
11.1 Aim
To Design and implement FIR filter using frequency sampling method.
11.2 Theory
A finite impulse response (FIR) filter is discrete linear time invariant system whose output is based on
the weighted summation of a finite number of input. An FIR transversal filter structure can be
obtained directly from the equation for discrete time convolution
y(n)= −1
=
ℎ(−) 0<n<n-1
In this equation x(k) and y(n) represents the input to & output from the filter at time n h(n-k) is the
transversal filter coefficient at time n these coefficients are generated by using FDS.
FIR filter is a finite impulse response filter order of the filter should be specified Infinite response in
truncated to get finite impulse response placing a window of finite length. This type of available are
rectangular barter, Hamming, Hanning Blackman window etc. This FIR filter is an zero filter.
11.3 Algorithm
1. Get the sampling frequency
2. Get the passband & stopband frequency
3. Get the passband ripple & stopband attenuation
4. Calculate the order N based on transition width
5. Find the N window coefficient
6. Verify the frequency response of h(n)
11.5 Calculation
Design an ideal bandpass filter with a frequency response
Hd=(ejw)= 1 for π/4 <= |w| <= 3π/4
0 otherwise
Find the values of h(n) for N=11 and plot the frequency response .
The ideal frequency response of the filter shown in fig
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
w in
0 20 30 45 60 75 90 105 120 135 150 160 180
degree
(ejw) in -0.1366 0.012 -0.1817 0.5 0.818 1.05 1.1366 1.05 0.818 0.5 0.1817 0.012 -0.1366
db -17.3 -38.17 -14.8 -6.02 -1.74 0.4346 1.11 0.4346 -1.74 -6.05 -14.8 -38.17 -17.3
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
11.6 MATLAB Program: To design and implement FIR filter using frequency sampling method.
11.7 Output
Input:
enter the order of the filter N=7
Output:
hn = -0.11460.07930.32100.42860.32100.0793 -0.1146
11.8 Results
11.9 Conclusion
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
Experiment 12
12.1 Aim
To obtain realization of IIR & FIR filters.
12.2 Theory
A digital filter transfer can be realized in a variety of ways. There are two types of realization
1. Recursive 2. Non Recursive.
1. For recursive realization the current output y(n) is a function of past outputs, past & present inputs.
This form corresponds to an Infinite Impulse Response (IIR) digital filter. In this section we discuss
this type of realization.
2. For non-recursive realization current output sample y(n) is a function of only past and
present inputs. This form corresponds to a Finite Impulse Response (FIR) digital filter.
The structure are implemented using MATLAB inbuilt functions residuez and tf.
12.4 Calculation
1. Realize the system with difference equation
y(n)= 3y(n-1)- 1y(n-2)+x(n)+ 1x(n-1) is cascade form
4 8 3
y(n)= 3y(n-1)- 1y(n-2)+x(n)+ 1x(n-1)
4 8 3
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
Here
&
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
Page
Lab Manual / Semester
6th Electrical & Electronics Engineering Department, Acharya Institute of
2.%realization
Realization of
of FIR cascade structure
IIR cascade structure
num=input('enter the coefficients of
Input:
enter the coefficients
numerator='); of numerator=[2
den=input('enter 10 23 34 31
the coefficients of 16 4]
enter the coefficients
denominator='); of denominator=[1]den);
[b,a]=eqtflength(num,
Output:
[z, p,
sos =
k]=tf2zp(b,a);
2.0000 6.0000 4.0000 1.0000 0 0
1.0000 1.0000 2.0000 1.0000 0 0
12.7
1.0000 Output
1.0000 0.5000 1.0000 0 0
12.8 Result
12.9 Conclusion
Page