Dsp Lab Manual
Dsp Lab Manual
(MBU-22)
B.Tech. VI Semester
(ECE & EIE)
Prepared
by
Professor
Dept Of ECE
1
DIGITAL SIGNAL PROCESSING LAB
COURSE DESCRIPTION:
Implementation of Convolution; DFT and FFT; Design of Analog, Digital FIR and IIR filters.
LIST OF EXERCISES:
1 Verify linear convolution of aperiodic sequences using CCS on DSP processors and also
verify using MATLAB.
2 Verify the circular convolution on Periodic sequences using CCS on DSP processors and
also verify using MATLAB.
3 Verify N-point DFT & IDFT using CCS on DSP processors and also verify using MATLAB.
4 Verify N-point FFT algorithm using CCS on DSP processors and also verify using MATLAB.
5 Find the frequency response of analog Butterworth prototype filters (LP/HP/BP/BR) using
MATLAB.
6 Find the frequency response of analog chebyshev prototype filters (LP/HP/BP/BR) using
MATLAB.
7 Design FIR filter (LP/HP/BP/BR) using following windowing techniques with MATLAB
A) rectangular window
B) triangular window
8 Design FIR filter (LP/HP/BP/BR) using following windowing technique with MATLAB
A) Hamming window
B) Hanning window
C) Blackman window
2
CONTENTS
EXP.
TITLE
NO.
1. Introduction To CCS
2. Linear Convolution
3. Circular Convolution
4. Discrete Fourier Transform
5. N-Point FFT Algorithm.
6. Frequency Response of Analog Butterworth prototype LP/HP/BP/BR
3
FILTERS
7. Frequency Response of Analog Chebyshev prototype LP/HP/BP/BR
FILTERS
8. FIR Filters Design(LP/HP/BP/BR) Using Windowing technique a)
Rectangular window b)Triangular window c)Kaiser window
9. FIR Filters Design(LP/HP/BP/BR) Using Windowing technique a)
Hamming window b) Hanning window c) Blackmann window
10. IIR Filter Design using bilinear transformation techniques
11. IIR Filter Design using Impulse Invariant transformation techniques
12. FIR Filters Using Frequency Sampling Method
Appendix-1
Linear to Circular Convolution
Circular to Linear Convolution
1. INTRODUCTION TO CCS
1.1 General Procedure to work on Code Composer Studio V4 for non-real time projects
Step T1:
Launch the CCS v4 icon from the Desktop or goto All Programs ->Texas Instruments ->CCSv4
Step T2:
4
Choose the location for the workspace, where your project will be saved.
Step T3:
Click the CCS icon from the welcome page to go the workbench, it is marked in the below picture.
Step T4:
From the Target menu select New target Configuration
Target -> New target Configuration.
It will open a window like given below.
Specify any arbitrary target name. For Eg., 6748config.ccxml (Extension should be .ccxml). Location
will set one default folder in current user account. It’s also allowed to change the location. Click Finish it
will open a configuration window for the created target.
5
Step T5:
Select the Connection: Texas instruments XDS100v1 USB Emulator
Select the Device: TMS320C6748.
*To make the search easy type 6748 in the space provided.
Next go to Advanced tab give the suitable gel file path as shown below.
Step T6:
Go to view option and select the Target Configuration:
View->Target Configuration.
A wizard will open in the workspace expand the User Defined folder and you can find your target, Right
click on 6748config.ccxml and select the Launch Selected Configuration.
Step T7:
Connect the target
Target -> Connect Target
6
Now our target is successfully configured and connected.
In future we no need to repeat these steps. If we are working with the same hardware we can just open the
already configured target and launch the selected configuration and connect it.
Program 1:
Procedure to create a simple non-real time project Hello world
Step P1:
Change the Perspective Debug to C/C++ from the right corner of the CCS
Step P2:
Go to File New CCS Project.
Step P3:
Specify the name of the project in the space provided
Eg., Project Name: Hello World
Click Next
Select the project type
Click Next
7
*However our target is based on C6000 family, Based on the family we need to select the Project Type.
Click finish
8
Specify the arbitrary source file name. It should be in the source folder(current project name.).
Note:
Step P5:
Go to
9
Step P6:
After successful Build, Debug your code in to the hardware by selecting the option
10
Once you click on retry ccs eill load program on to processor and then ccs will guide us to debug mode, if
it is not done automatically.
Change the Perspective C/C++ to Debug from the right corner of the CCS.
Step P7:
Now you can run the code, by selecting the option run from the
dialog box else you can
Go to Target Run
Once you run the program the output will be printed in the Console Window.
1.2 PROGRAM 1:
Procedure to create a simple non-real time project Sine wave Generation
1. Open Code Composer Studio v4.
2. Perform step T6 and T7.
3. Change the Perspective Debug to C/C++ from the right corner of the CCS (step P1).
4. Go to file New CCS Project(step P2).
5. Perform step P3 Give the Project name as sinewave.
6. Go to File New Source File (Step P4). Give the file name as sin.c
Type the given sinwave generation program.
#include <stdio.h>
#include<math.h>
#define FREQ 500
float m[127];
main()
11
{
int i=0;
for(i=0;i<127;i++)
{
m[i]=sin(2*3.14*FREQ*i/24000);
printf("%f\n",m[i]);
}
}
Start Address: m
Click : Ok
EQUIPMENTS:
Operating System – Windows XP
Constructor - Simulator
Software - CCStudio 4.0 & MATLAB 7.5
Hardware - DSK6713 kit, USB probe,5V DC supply
THEORY:
Convolution is a formal mathematical operation, just as multiplication, addition, and integration.
Addition takes two numbers and produces a third number, while convolution takes two signals
and produces a third signal. Convolution is used in the mathematics of many fields, such as
probability and statistics. 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 this equation, x1(k), x2(n-k) and y(n) represent the input to and output from the system at time
n. Here we could see that one of the input is shifted in time by a value every time it is multiplied
with the other input signal. Linear Convolution is quite often used as a method of implementing
filters of various types.
2.1 PROGRAM:
#include<stdio.h>
int x[15],h[15],y[15];
main()
{
inti,j,m,n;
printf("\n Enter value for m");
scanf("%d",&m);
printf("\n Enter value for n");
scanf("%d",&n);
printf("Enter values for input x(n)\n");
for(i=0;i<m;i++)
scanf("%d",&x[i]);
printf("Enter values for input h(n)\n");
for(i=0;i<n;i++)
scanf("%d",&h[i]);
//Padding of Zeros//
for(i=m;i<=m+n-1;i++)
x[i]=0;
for(i=n;i<=m+n-1;i++)
h[i]=0;
//Convolution Operation//
for(i=0;i<m+n-1;i++)
{
y[i]=0;
14
for(j=0;j<=i;j++)
{
y[i]=y[i]+(x[j]*h[i-j]);
}
//Displaying the Output//
}
for(i=0;i<m+n-1;i++)
printf("\n The value of Output y[%d]=%d",i,y[i]);
}
Result:
enter value for m: 4
enter value for n: 4
Enter values for input x(n):
1
2
3
4
Enter Values for input h(n):
1
2
3
4
The value of Output y[0]=1
The value of Output y[1]=4
The value of Output y[2]=10
The value of Output y[3]=20
The value of Output y[4]=25
The value of Output y[5]=24
The value of Output y[6]=16
Graph:
15
2.2 % MATLAB program for linear convolution
%linear convolution program
clc;
clear all;
close all;
disp('linear convolution program');
x=[x,zeros(1,n)];
subplot(2,2,1), stem(x);
title('i/p sequence x(n)is:');
xlabel('---->n');
ylabel('---->x(n)');grid;
h=[h,zeros(1,m)];
subplot(2,2,2), stem(h);
title('i/p sequence h(n)is:');
xlabel('---->n');
ylabel('---->h(n)');grid;
disp('convolution of x(n) & h(n) is y(n):');
y=zeros(1,m+n-1);
for i=1:m+n-1
y(i)=0;
16
for j=1:m+n-1
if(j<i+1)
y(i)=y(i)+x(j)*h(i-j+1);
end
end
end
y
subplot(2,2,[3,4]),stem(y);
title('convolution of x(n) & h(n) is :');
xlabel('---->n');
ylabel('---->y(n)');grid;
Graph:
Result : Hence linear convoluion of two sequences has been verified using CCS and MATLAB.
17
3. CIRCULAR CONVOLUTION
AIM: To verify Circular Convolution.
EQUIPMENTS:
Operating System – Windows XP
Constructor - Simulator
Software - CCStudio 3 & MATLAB 7.5
Hardware - DSK6713 kit, USB probe,5V DC supply
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 eg. 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.
3.1 Program:
18
#include <stdio.h>
int n1,n2,i,j,n,c,x[10]=0,h[10]=0,y[10]=0,a[10]=0;
main()
{
printf("Enter the length of the sequence n1: ");
scanf("%d",&n1);
printf("Enter the length of the sequence n2: ");
scanf("%d",&n2);
printf("Enter the first sequence: ");
for(i=0;i<n1;i++)
{
scanf("%d",&x[i]);
}
printf("Enter the second sequence: ");
for(i=0;i<n2;i++)
{
scanf("%d",&h[i]);
}
if(n1>n2)
n=n1;
else
n=n2;
a[0]=h[0];
for(i=1;i<n;i++)
{
a[i]=h[n-i];
}
for(j=0;j<n;j++)
{
y[j]=0;
c=x[0];
for(i=0;i<n;i++)
{
y[j]=(y[j]+(x[i]*a[i]));
x[i]=x[i+1];
}
x[n-1]=c;
printf("result is %d /n",y[j]);
}
}
Result:
Enter the length of the sequence n1: 4
Enter the length of the sequence n2: 3
Enter the first sequence: 1
2
3
4
Enter the second sequence: 1
2
3
The Circular Convolution is
19
18 16 10 16
Graph:
3.2
%Circular Convolution Program
clc;
clear all;
close all;
disp('circular convolution program');
subplot(2,2,1), stem(x);
title('i/p sequencce x(n)is:');
xlabel('---->n');
ylabel('---->x(n)');grid;
subplot(2,2,2), stem(h);
title('i/p sequencce h(n)is:');
xlabel('---->n');
ylabel('---->h(n)');grid;
20
n=m;
end
x=[x,zeros(1,n-m)];
m=n;
end
y=zeros(1,n);
y(1)=0;
a(1)=h(1);
for j=2:n
a(j)=h(n-j+2);
end
%ciruclar conv
for i=1:n
y(1)=y(1)+x(i)*a(i);
end
for k=2:n
y(k)=0;
% circular shift
for j=2:n
x2(j)=a(j-1);
end
x2(1)=a(n);
for i=1:n
if(i<n+1)
a(i)=x2(i);
y(k)=y(k)+x(i)*a(i);
end
end
end
y
subplot(2,2,[3,4]),stem(y);
title('convolution of x(n) & h(n) is:');
xlabel('---->n');
ylabel('---->y(n)');grid;
Graph :
21
Result : Hence Circular convoluion of two sequences has been verified using CCS and
MATLAB
22
4. DISCRETE FOURIER TRANSFORM
EQUIPMENTS:
Operating System – Windows XP
Constructor - Simulator
Software - CCStudio 3 & MATLAB 7.5
Hardware - DSK6713 kit, USB probe,5V DC supply
THEORY:
DFT of a discrete time signal transforms the signal in time domain into frequency domain signal.
DFT EQUTION:
The sequence of N complex numbers x0, ..., xN−1 is transformed into another sequence of N
complex numbers according to the DFT formula:
Note:
4.1 DFT
Program
#include<stdio.h>
#include<math.h>
int x[32],N,k,n,i;
float pi=3.1416,sumre=0,sumim=0,out_real[8]={0.0},out_imag[8]={0.0},m[20]={0.0},p[20]={0.0};
main()
{
printf ("enter lenght\n");
scanf ("%d",&N);
printf("enter the first sequence\n");
for(i=0;i<N;i++)
scanf ("%d",&x[i]);
for(k=0;k<N;k++)
{
sumre=0;
sumim=0;
for(n=0;n<N;n++)
{
sumre=sumre+x[n]*cos(2*pi*k*n/N);
sumim=sumim-x[n]*sin(2*pi*k*n/N);
}
out_real[k]=sumre;
out_imag[k]=sumim;
printf("x[%d]=\t%f\t+j*\t%f\n",k,out_real[k],out_imag[k]);
m[k]=sqrt(out_real[k]*out_real[k]+out_imag[k]*out_imag[k]);
printf("magnitude =%f\n",m[k]);
p[k]=atan(out_imag[k]/out_real[k]);
printf("phase value =%f\n",p[k]);
}
}
Console Window
enter length 4
enter the first sequence
1
1
0
0
x[0]= 2.000000 +j* 0.000000
magnitude =2.000000
phase value =0.000000
x[1]= 0.999996 +j* -1.000000
magnitude =1.414211
24
phase value =-0.785400
x[2]= 0.000000 +j* 0.000007
magnitude =0.000007
phase value =1.570793
x[3]= 1.000011 +j* 1.000000
magnitude =1.414221
phase value =0.785393
Output Graph
Magnitude
Phase
25
4.2 IDFT
Program
#include<stdio.h>
#include<math.h>
int x[32],N,k,n,i;
float pi=3.1416,sumre=0,sumim=0,out_real[8]={0.0},out_imag[8]={0.0},m[20]={0.0},p[20]={0.0};
main()
{
printf ("enter lenght\n");
scanf ("%d",&N);
printf("enter the first sequence\n");
for(i=0;i<N;i++)
scanf ("%d",&x[i]);
for(k=0;k<N;k++)
{
sumre=0;
sumim=0;
for(n=0;n<N;n++)
{
sumre=sumre+x[n]*cos(2*pi*k*n/N);
sumim=sumim-x[n]*sin(2*pi*k*n/N);
}
out_real[k]=sumre/N;
out_imag[k]=sumim/N;
printf("x[%d]=\t%f\t+j*\t%f\n",k,out_real[k],out_imag[k]);
m[k]=sqrt(out_real[k]*out_real[k]+out_imag[k]*out_imag[k]);
printf("magnitude =%f\n",m[k]);
p[k]=atan(out_imag[k]/out_real[k]);
printf("phase value =%f\n",p[k]);
}
}
Console Window
enter length 4
enter the first sequence
1
0
1
0
x[0]= 0.500000 +j* 0.000000
magnitude =0.500000
phase value =0.000000
x[1]= 0.000000 +j* 0.000002
magnitude =0.000002
phase value =1.570793
x[2]= 0.500000 +j* -0.000004
magnitude =0.500000
phase value =-0.000007
x[3]= 0.000000 +j* 0.000005
magnitude =0.000005
phase value =1.570785
26
Output Graph
Magnitude
Phase
RESULTS: DFT And IDFT Of The Discret Time Sequence has been verified through CCS.
27
5. N-POINT FFT ALGORITHM.
AIM: To verify N-point FFT algorithm discrete Time sequence.
EQUIPMENTS:
Operating System – Windows XP
Constructor - Simulator
Software - CCStudio 3 & MATLAB 7.5
Hardware - DSK6713 kit, USB probe,5V DC supply
THEORY:
The Fast Fourier Transform is useful to map the time-domain sequence into a continuous
function of a frequency variable. The FFT of a sequence {x(n)} of length N is given by a
complex-valued sequence X(k).
M nk
−j 2π
X ( k )=∑ x (n) e n
; 0<k < N −1
k=0
The above equation is the mathematical representation of the DFT. As the number of
computations involved in transforming a N point time domain signal into its corresponding
frequency domain signal was found to be N2 complex multiplications, an alternative algorithm
involving lesser number of computations is opted.
When the sequence x(n) is divided into 2 sequences and the DFT performed separately, the
resulting number of computations would be N2/2
(i.e.)
N
2 N2
2−1 2−1
x ( k )=∑ x (2 n ) W 2Nnk + ∑ x ( 2 n+1 ) W (2
N
n+1 )k
Dividing the sequence x(2n) into further 2 odd and even sequences would reduce the
computations.
WN is the twiddle factor
− j2 π
n
=e
nk
W N =e
( − jn2π ) nk
W
( K+ )
N
2
=W W
( K+ )
N
2
N N N
(11)
− j2 π − j 2π n
k
n n 2
=e e
−j 2π
k
=W kN e n
=W kN (cos π − j sin π )
=W
( K+ )
N
2 k
=W (−1)
N N
29
=W
( K+ )
N
2
=W k
N N (12)
Employing this equation, we deduce
N2 N
2−1 2−1
x ( k )= ∑ x ( 2 n) W 2nk
N + ∑ x( 2 n+1 ) W N
(2 nk )
FIG. 3A.1
The above shown mathematical representation forms the basis of N point FFT and is called
the Butterfly Structure.
30
STAGE – I STAGE - II
STAGE – III
FIG. 3A.2 – 8 POINT DIT
5.1 PROGRAM:
%fast fourier transform
% Matlab code for N point Fast Fourier Transform
close all; clear all;
x=input('enter the input sequence');
31
N=length(x);
%Code for FFT
disp('Fast Fourier Transformed Signal');
xk=fft(x,N); disp(xk);
%code for IFFT
disp('Inverse Fast Fourier Transformed Signal');
ixk=ifft(xk,N); disp(ixk);
%code to plot all sequences
%input sequence
n=0:N-1; subplot(2,2,1);
stem(n,x); xlabel('Time Index'); ylabel('Amplitude'); title('input sequence');
%FFT response sequence
k=0:N-1; subplot(2,2,2); stem(k,abs(xk)); xlabel('Frequency Index'); ylabel('Magnitude');
title('FFT response sequence');
%IFFT response sequence
n=0:N-1; ixk=ixk/N; subplot(2,2,3); stem(n,abs(ixk)); xlabel('Time Index'); ylabel('Magnitude');
title('IFFT response sequence');
Graph:
32
5.2 C Program to Implement FFT
#include <math.h>
#define PTS 128 //# of points for FFT
#define PI 3.14159265358979
typedef struct {float real,imag;} COMPLEX;
void FFT(COMPLEX *Y, int n); //FFT prototype
float iobuffer[PTS]; //as input and output buffer
float x1[PTS],x[PTS]; //intermediate buffer
short i; //general purpose index variable
short buffercount = 0; //number of new samples in iobuffer
short flag = 0; //set to 1 by ISR when iobuffer full
float y[128];
COMPLEX w[PTS]; //twiddle constants stored in w
COMPLEX samples[PTS]; //primary working buffer
main()
{
float j,sum=0.0 ;
int n,k,i,a;
for (i = 0 ; i<PTS ; i++) // set up twiddle constants in w
{
w[i].real = cos(2*PI*i/(PTS*2.0)); /*Re component of twiddle constants*/
w[i].imag =-sin(2*PI*i/(PTS*2.0)); /*Im component of twiddle constants*/
}
/****************Input Signal X(n) *************************/
for(i=0,j=0;i<PTS;i++)
{
x[i] = sin(2*PI*5*i/PTS); // Signal x(Fs)=sin(2*pi*f*i/Fs);
samples[i].real=0.0;
samples[i].imag=0.0;
}
/********************Auto Correlation of X(n)=R(t) ***********/
for(n=0;n<PTS;n++)
{
sum=0;
for(k=0;k<PTS-n;k++)
33
{
sum=sum+(x[k]*x[n+k]); // Auto Correlation R(t)
}
iobuffer[n] = sum;
}
/********************** FFT of R(t) ***********************/
for (i = 0 ; i < PTS ; i++) //swap buffers
{
samples[i].real=iobuffer[i]; //buffer with new data
}
for (i = 0 ; i < PTS ; i++)
samples[i].imag = 0.0; //imag components = 0
FFT(samples,PTS); //call function FFT.c
/******************** PSD ********************/
for (i = 0 ; i < PTS ; i++) //compute magnitude
{
x1[i] = sqrt(samples[i].real*samples[i].real
+ samples[i].imag*samples[i].imag);
}
} //end of main
FFT.c:
#define PTS 128 //# of points for FFT
typedef struct {float real,imag;} COMPLEX;
extern COMPLEX w[PTS]; //twiddle constants stored in w
void FFT(COMPLEX *Y, int N) //input sample array, # of points
{
COMPLEX temp1,temp2; //temporary storage variables
int i,j,k; //loop counter variables
int upper_leg, lower_leg; //indexof upper/lower butterfly leg
int leg_diff; //difference between upper/lower leg
int num_stages = 0; //number of FFT stages (iterations)
int index, step; //index/step through twiddle constant
i = 1; //log(base2) of N points= # of stages
do
{
34
num_stages +=1;
i = i*2;
}while (i!=N);
leg_diff = N/2; //difference between upper&lower legs
step = (PTS*2)/N; //step between values in twiddle.h// 512
for (i = 0;i < num_stages; i++) //for N-point FFT
{
index = 0;
for (j = 0; j < leg_diff; j++)
{
for (upper_leg = j; upper_leg < N; upper_leg += (2*leg_diff))
{
lower_leg = upper_leg+leg_diff;
temp1.real = (Y[upper_leg]).real + (Y[lower_leg]).real;
temp1.imag = (Y[upper_leg]).imag + (Y[lower_leg]).imag;
temp2.real = (Y[upper_leg]).real - (Y[lower_leg]).real;
temp2.imag = (Y[upper_leg]).imag - (Y[lower_leg]).imag;
(Y[lower_leg]).real = temp2.real*(w[index]).real
-temp2.imag*(w[index]).imag;
(Y[lower_leg]).imag = temp2.real*(w[index]).imag
+temp2.imag*(w[index]).real;
(Y[upper_leg]).real = temp1.real;
(Y[upper_leg]).imag = temp1.imag;
}
index += step;
}
leg_diff = leg_diff/2;
step *= 2;
}
j = 0;
for (i = 1; i < (N-1); i++) //bit reversal for resequencing data
{
k = N/2;
while (k <= j)
{
35
j = j - k;
k = k/2;
}
j = j + k;
if (i<j)
{
temp1.real = (Y[j]).real;
temp1.imag = (Y[j]).imag;
(Y[j]).real = (Y[i]).real;
(Y[j]).imag = (Y[i]).imag;
(Y[i]).real = temp1.real;
(Y[i]).imag = temp1.imag;
}
}
return;
}
RESULT: FFT of a Discrete Time Sequence has been verified through MATLAB.
36
6. FREQUENCY RESPONSE OF ANALOG BUTTERWORTH PROTOTYPE
LP/HP/BP/BR FILTERS
AIM: To find the frequency response of analog LP/HP/BP/BR filters using matlab.
EQUIPMENTS:
Operating System – Windows XP
Constructor - Simulator
Software - CCStudio 3 & MATLAB 7.5
Hardware - DSK6713 kit, USB probe,5V DC supply
THEORY
BUTTERWORTH FILTER
The Butterworth filter has a maximally flat response that is, no passband ripple and roll-off of
minus 20db per pole. It is “flat maximally magnitude” filters at the frequency of jω= 0, as the
first 2N - 1 derivatives of the transfer function when jω = 0 are equal to zero [5]. The phase
response of the Butterworth filter becomes more nonlinear with increasing N. This filter is
completely defined mathematically by two parameters; they are cut off frequency and number of
poles. The magnitude squared response of low pass Butterworth filter is given by,
Filter Selectivity,
Attenuation,
The frequency response of the Butterworth filter is maximally flat in the passband and rolls off
towards zero in the stopband . When observed on a logarithmic bode plot the response slopes off
linearly towards negative infinity. A first-order filter's response rolls off at −6 dB per octave
(−20 dB per decade). A second-order filter’s response rolls off at −12 dB per octave and a third-
order at −18 dB. Butterworth filters have a monotonically varying magnitude function with ω,
unlike other filter types that have non-monotonic ripple in the passband and the stopband.
Compared with a Chebyshev Type I filter or an Elliptic filter, the Butterworth filter has a slower
roll-off and therefore will require a higher order to implement a particular stopband specification.
37
Butterworth filters have a more linear phase response in the pass-band than Chebyshev Type I
and Elliptic filters .
The Butterworth filter rolls off more slowly around the cut off frequency than the Chebyshev
Type I and Elliptic filters without ripple. All of these filters are in fifth order. Fig.2:Phase
response of the various types of IIR filters
clc;
clear all;
rp = input('Enter the passband ripple = ');
rs = input('Enter the stopband ripple = ');
wp = input('Enter the passband frequency = ');
ws = input('Enter the stopband frequency = ');
fs = input('Enter the sampling frequency = ');
w1 = 2*wp/fs;
w2 = 2*ws/fs;
[n,wn] = buttord(w1,w2,rp,rs,'s');
[z,p,k] = butter(n,wn);
[b,a] = zp2tf(z,p,k);
[b,a] = butter(n,wn,'s');
w = 0:0.01:pi;
[h,om] = freqs(b,a,w);
m = 20*log10(abs(h));
an = angle(h);
subplot(2,1,1);
plot(om/pi,m);
title('Amplitude Response');
ylabel('Gain in dB ---->');
xlabel('Normalised frequency ---->');
grid on;
subplot(2,1,2);
plot(om/pi,an);
title('Phase Response');
xlabel('Normalised frequency ---->');
ylabel('Phase in radians ---->');
grid on;
Output:
Enter the passband ripple = 0.15
Enter the stopband ripple = 60
Enter the passband frequency = 1500
Enter the stopband frequency = 3000
Enter the sampling frequency = 7000
38
6.2 Butterworth Analog High Pass Filter
Program:
clc;
clear all;
rp = input('Enter the passband ripple = ');
rs = input('Enter the stopband ripple = ');
wp = input('Enter the passband frequency = ');
ws = input('Enter the stopband frequency = ');
fs = input('Enter the sampling frequency = ');
w1 = 2*wp/fs;
w2 = 2*ws/fs;
[n,wn] = buttord(w1,w2,rp,rs,'s');
[b,a] = butter(n,wn,'high','s');
w = 0:0.01:pi;
[h,om] = freqs(b,a,w);
m = 20*log10(abs(h));
an = angle(h);
subplot(2,1,1);
plot(om/pi,m);
ylabel('Gain in dB ---->');
xlabel('Normalised frequency ---->');
title('Amplitude Response');
39
grid on;
subplot(2,1,2);
plot(om/pi,an);
xlabel('Normalised frequency ---->');
ylabel('Phase in radians ---->');
title('Phase Response');
grid on;
Output:
Waveform:
40
6.3 Butterworth Analog Band Pass Filter
Program:
clc;
clear all;
rp = input('Enter the passband ripple = ');
rs = input('Enter the stopband ripple = ');
wp = input('Enter the passband frequency = ');
ws = input('Enter the stopband frequency = ');
fs = input('Enter the sampling frequency = ');
w1 = 2*wp/fs;
w2 = 2*ws/fs;
[n] = buttord(w1,w2,rp,rs);
wn = [w1 w2];
[b,a] = butter(n,wn,'bandpass','s');
w = 0:.01:pi;
[h,om] = freqs(b,a,w);
m = 20*log10(abs(h));
an = angle(h);
subplot(2,1,1);
plot(om/pi,m);
ylabel('Gain in dB ---->');
xlabel('Normalised Frequency ---->');
title('Magnitude Response');
grid on;
subplot(2,1,2);
plot(om/pi,an);
xlabel('Normalised Frequency ---->');
ylabel('Phase in Radians ---->');
title('Phase Response');
grid on;
Output:
41
Waveform:
Program:
clc;
clear all;
rp = input('Enter the passband ripple = ');
rs = input('Enter the stopband ripple = ');
wp = input('Enter the passband frequency = ');
ws = input('Enter the stopband frequency = ');
fs = input('Enter the sampling frequency = ');
w1 = 2*wp/fs;
w2 = 2*ws/fs;
[n] = buttord(w1,w2,rp,rs,'s');
wn = [w1 w2];
[b,a] = butter(n,wn,'stop','s');
w = 0:0.01:pi;
[h,om] = freqs(b,a,w);
m = 20*log10(abs(h));
an = angle(h);
subplot(2,1,1);
42
plot(om/pi,m);
title('Magnitude Response');
ylabel('Gain in dB ---->');
xlabel('Normalised Frequency ---->');
grid on;
subplot(2,1,2);
plot(om/pi,an);
title('Phase Response');
xlabel('Normalised Frequency ---->');
ylabel('Phase in radians ---->');
grid on;
Output:
Waveform:
RESULTS: Hence Butterworth analog (LP/HP/BP/BR) proto type filters are implemented and
their magnitude responses are visualized.
43
7. FREQUENCY RESPONSE OF ANALOG CHEBYSHEV LP/HP/BP/BR FILTERS
AIM: To find the frequency response of analog LP/HP filters using matlab.
EQUIPMENTS:
Operating System – Windows XP
Constructor - Simulator
Software - CCStudio 3 & MATLAB 7.5
Hardware - DSK6713 kit, USB probe,5V DC supply
THEORY
CHEBYSHEV TYPE -I FILTER The absolute difference between the ideal and actual
frequency response over the entire passband is minimized by Chebyshev Type I filter by
incorporating equal ripple in the passband. Stopband response is maximally flat. The transition
from passband to stopband is more rapid than for the Butterworth filter. The magnitude squared
Chebyshev type I response is:
Where
The magnitude squared response peaks occur in the pass band when
Program:
clc;
clear all;
rp = input('Enter the passband ripple = ');
rs = input('Enter the stopband ripple = ');
wp = input('Enter the passband frequency = ');
ws = input('Enter the stopband frequency = ');
fs = input('Enter the sampling frequency = ');
44
w1 = 2*wp/fs;
w2 = 2*ws/fs;
[n,wn] = cheb1ord(w1,w2,rp,rs,'s');
[b,a] = cheby1(n,rp,wn,'s');
w = 0:0.01:pi;
[h,om] = freqs(b,a,w);
m = 20*log10(abs(h));
an = angle(h);
subplot(2,1,1);
plot(om/pi,m);
title('Magnitude Response');
ylabel('Gain in dB ---->');
xlabel('Normalised Frequency ---->');
grid on;
subplot(2,1,2);
plot(om/pi,an);
title('Phase Response');
xlabel('Normalised Frequency ---->');
ylabel('Phase in radians ---->');
grid on;
Output:
Enter the passband ripple = 0.23
Enter the stopband ripple = 47
Enter the passband frequency = 1300
Enter the stopband frequency = 1550
Enter the sampling frequency = 7800
Waveform:
45
7.2 Chebyshev Type 1 Analog High Pass Filter
Program:
clc;
clear all;
rp = input('Enter the passband ripple = ');
rs = input('Enter the stopband ripple = ');
wp = input('Enter the passband frequency = ');
ws = input('Enter the stopband frequency = ');
fs = input('Enter the sampling frequency = ');
w1 = 2*wp/fs;
w2 = 2*ws/fs;
[n,wn] = cheb1ord(w1,w2,rp,rs,'s');
[b,a] = cheby1(n,rp,wn,'high','s');
w = 0:0.01:pi;
[h,om] = freqs(b,a,w);
m = 20*log10(abs(h));
an = angle(h);
subplot(2,1,1);
plot(om/pi,m);
subplot(2,1,1);
plot(om/pi,m);
title('Magnitude Response');
ylabel('Gain in dB ---->');
xlabel('Normalised Frequency ---->');
grid on;
subplot(2,1,2);
plot(om/pi,an);
title('Phase Response');
xlabel('Normalised Frequency ---->');
ylabel('Phase in radians ---->');
grid on;
Output:
Waveform:
46
7.3 Chebyshev Type 1 Analog Band Pass Filter
Program:
clc;
clear all;
rp = input('Enter the passband ripple = ');
rs = input('Enter the stopband ripple = ');
wp = input('Enter the passband frequency = ');
ws = input('Enter the stopband frequency = ');
fs = input('Enter the sampling frequency = ');
w1 = 2*wp/fs;
w2 = 2*ws/fs;
[n] = cheb1ord(w1,w2,rp,rs,'s');
wn = [w1 w2];
[b,a] = cheby1(n,rp,wn,'bandpass','s');
w = 0:0.01:pi;
[h,om] = freqs(b,a,w);
m = 20*log10(abs(h));
an = angle(h);
subplot(2,1,1);
plot(om/pi,m);
subplot(2,1,1);
plot(om/pi,m);
title('Magnitude Response');
ylabel('Gain in dB ---->');
xlabel('Normalised Frequency ---->');
grid on;
subplot(2,1,2);
plot(om/pi,an);
title('Phase Response');
xlabel('Normalised Frequency ---->');
ylabel('Phase in radians ---->');
grid on;
Output:
Waveform:
clc;
clear all;
rp = input('Enter the passband ripple = ');
rs = input('Enter the stopband ripple = ');
wp = input('Enter the passband frequency = ');
ws = input('Enter the stopband frequency = ');
fs = input('Enter the sampling frequency = ');
w1 = 2*wp/fs;
w2 = 2*ws/fs;
[n] = cheb1ord(w1,w2,rp,rs,'s');
wn = [w1 w2];
[b,a] = cheby1(n,rp,wn,'stop','s');
w = 0:0.01:pi;
[h,om] = freqs(b,a,w);
m = 20*log10(abs(h));
an = angle(h);
subplot(2,1,1);
plot(om/pi,m);
subplot(2,1,1);
plot(om/pi,m);
title('Magnitude Response');
ylabel('Gain in dB ---->');
xlabel('Normalised Frequency ---->');
grid on;
subplot(2,1,2);
plot(om/pi,an);
title('Phase Response');
xlabel('Normalised Frequency ---->');
ylabel('Phase in radians ---->');
grid on;
Output:
Waveform:
48
AIM: To design FIR Filter (LP/HP/BP/BR) Using Windowing technique
a. Rectangular window
b. Triangular window
c. Kaiser window
EQUIPMENTS:
THEORY:
In this method, the desired frequency response specification Hd(w), corresponding unit sample
response hd(n) is determined using the following relation
In general, unit sample response hd(n) obtained from the above relation is infinite in duration, so it must
be truncated at some point say n= M-1 to yield an FIR filter of length M (i.e. 0 to M-1). This truncation of
hd(n) to length M-1 is same as multiplying hd(n) by the rectangular window defined as
Now, the multiplication of the window function w(n) with hd(n) is equivalent to convolution of Hd(w)
with W(w), where W(w) is the frequency domain representation of the window function
Thus the convolution of Hd(w) with W(w) yields the frequency response of the truncated FIR filter
49
The frequency response can also be obtained using the following relation
But direct truncation of hd(n) to M terms to obtain h(n) leads to the Gibbs phenomenon effect which
manifests itself as a fixed percentage overshoot and ripple before and after an approximated discontinuity
in the frequency response due to the non-uniform convergence of the fourier series at a discontinuity. In
order to reduce the ripples, instead of multiplying hd(n) with a rectangular window w(n), hd(n) is
multiplied with a window function that contains a taper and decays toward zero gradually, instead of
abruptly as it occurs in a rectangular window. As multiplication of sequences h d(n) and w(n) in time
domain is equivalent to convolution of Hd(w) and W(w) in the frequency domain, it has the effect of
smoothing Hd(w).
The several effects of windowing the Fourier coefficients of the filter on the result of the frequency
response of the filter are as follows:
(i) A major effect is that discontinuities in H(w) become transition bands between values on either side of
the discontinuity.
(ii) The width of the transition bands depends on the width of the main lobe of the frequency response of
the window function, w(n) i.e. W(w).
(iii) Since the filter frequency response is obtained via a convolution relation , it is clear that the resulting
filters are never optimal in any sense.
(iv) As M (the length of the window function) increases, the mainlobe width of W(w) is reduced which
reduces the width of the transition band, but this also introduces more ripple in the frequency response.
(v) The window function eliminates the ringing effects at the bandedge and does result in lower sidelobes
at the expense of an increase in the width of the transition band of the filter.
Some of the windows commonly used are as follows:
50
The Bartlett window reduces the overshoot in the designed filter but spreads the transition region
considerably. The Hanning, Hamming and Blackman windows use progressively more complicated
cosine functions to provide a smooth truncation of the ideal impulse response and a frequency response
that looks better. The best window results probably come from using the Kaiser window, which has a
parameter ß that allows adjustment of the compromise between the overshoot reduction and transition
region width spreading.
The major advantages of using window method is their relative simplicity as compared to other methods
and ease of use. The fact that well defined equations are often available for calculating the window
coefficients has made this method successful.
51
dem = 14.6*(fs-fp)/f;
n = ceil(num/dem);
n1 = n+1;
if (rem(n,2)==50)
n1 = n;
n = n-1;
end
y = bartlett(n1);
% low-pass filter
b = fir1(n,wp,y);
[h,o] = freqz(b,1,256);
m = 20*log10(abs(h));
subplot(2,2,1);
plot(o/pi,m);
title('Magnitude Response of LPF');
ylabel('Gain in dB ---->');
xlabel('Normalised Frequency ---->');
grid on;
% high-pass filter
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);
title('Magnitude Response of HPF');
ylabel('Gain in dB ---->');
xlabel('Normalised Frequency ---->');
grid on;
% 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);
title('Magnitude Response of BPF');
ylabel('Gain in dB ---->');
xlabel('Normalised Frequency ---->');
grid on;
% band stop filter
b = fir1(n,wn,'stop',y);
[h,o] = freqz(b,1,256);
m = 20*log10(abs(h));
subplot(2,2,4);
plot(o/pi,m);
title('Magnitude Response of BSF');
ylabel('Gain in dB ---->');
xlabel('Normalised Frequency ---->');
grid on;
52
Output:
Enter the passband ripple = 0.04
Enter the stopband ripple = 0.02
Enter the passband frequency = 1500
Enter the stopband frequency = 2000
Enter the sampling frequency = 8000
Waveform:
Output:
Enter the passband ripple = 0.05
Enter the stopband ripple = 0.04
Enter the passband frequency = 1500
54
Enter the stopband frequency = 2000
Enter the sampling frequency = 9000
Waveform:
RESULT: FIR Filter (LP/HP/BP/BR) Using Windowing technique has been designed
i) Rectangular window ii) Triangular window iii) Kaiser window and their magnitude
responses are visualized.
57
9. FIR FILTER DESIGN
THEORY:
A Finite Impulse Response (FIR) filter is a discrete linear time-invariant system whose output is
based on the weighted summation of a finite number of past inputs. An FIR transversal filter structure can
be obtained directly from the equation for discrete-time convolution.
N−1
y (n )= ∑ x (k ) h(n−k ) 0<n< N −1
k=0
In this equation, x(k) and y(n) represent the input to and output from the filter at time n. h(n-k) is the
transversal filter coefficients at time n. These coefficients are generated by using FDS (Filter Design
Software or Digital filter design package).
FIR – filter is a finite impulse response filter. Order of the filter should be specified. Infinite response is
truncated to get finite impulse response. placing a window of finite length does this. Types of windows
available are Rectangular, Barlett, Hamming, Hanning, Blackmann window etc. This FIR filter is an all
zero filter.
In this method, the desired frequency response specification Hd(w), corresponding unit sample
response hd(n) is determined using the following relation
In general, unit sample response hd(n) obtained from the above relation is infinite in duration, so it must
be truncated at some point say n= M-1 to yield an FIR filter of length M (i.e. 0 to M-1). This truncation of
hd(n) to length M-1 is same as multiplying hd(n) by the rectangular window defined as
58
Thus the unit sample response of the FIR filter becomes
Now, the multiplication of the window function w(n) with hd(n) is equivalent to convolution of Hd(w)
with W(w), where W(w) is the frequency domain representation of the window function
Thus the convolution of Hd(w) with W(w) yields the frequency response of the truncated FIR filter
The frequency response can also be obtained using the following relation
But direct truncation of hd(n) to M terms to obtain h(n) leads to the Gibbs phenomenon effect which
manifests itself as a fixed percentage overshoot and ripple before and after an approximated discontinuity
in the frequency response due to the non-uniform convergence of the fourier series at a discontinuity. In
order to reduce the ripples, instead of multiplying hd(n) with a rectangular window w(n), hd(n) is
multiplied with a window function that contains a taper and decays toward zero gradually, instead of
abruptly as it occurs in a rectangular window. As multiplication of sequences h d(n) and w(n) in time
domain is equivalent to convolution of Hd(w) and W(w) in the frequency domain, it has the effect of
smoothing Hd(w).
The several effects of windowing the Fourier coefficients of the filter on the result of the frequency
response of the filter are as follows:
(i) A major effect is that discontinuities in H(w) become transition bands between values on either side of
the discontinuity.
(ii) The width of the transition bands depends on the width of the main lobe of the frequency response of
the window function, w(n) i.e. W(w).
(iii) Since the filter frequency response is obtained via a convolution relation , it is clear that the resulting
filters are never optimal in any sense.
59
(iv) As M (the length of the window function) increases, the mainlobe width of W(w) is reduced which
reduces the width of the transition band, but this also introduces more ripple in the frequency response.
(v) The window function eliminates the ringing effects at the bandedge and does result in lower sidelobes
at the expense of an increase in the width of the transition band of the filter.
Some of the windows commonly used are as follows:
The Bartlett window reduces the overshoot in the designed filter but spreads the transition region
considerably. The Hanning, Hamming and Blackman windows use progressively more complicated
cosine functions to provide a smooth truncation of the ideal impulse response and a frequency response
that looks better. The best window results probably come from using the Kaiser window, which has a
parameter ß that allows adjustment of the compromise between the overshoot reduction and transition
region width spreading.
The major advantages of using window method is their relative simplicity as compared to other methods
and ease of use. The fact that well defined equations are often available for calculating the window
coefficients has made this method successful.
Output:
Waveform:
Output:
Waveform:
Output:
Waveform:
RESULT: FIR Filter (LP/HP/BP/BR) Using Windowing technique has been designed
i) Blackmann window ii) Hamming window iii) Hanning window and their magnitude
responses are visualized.
66
10. IIR FILTER DESIGN
AIM: To design IIR (LPF/HPF) filters using bilinear transformation technique in MATLAB.
EQUIPMENTS:
Operating System – Windows XP
Constructor - Simulator
Software - CCStudio 3 & MATLAB 7.5
Hardware - DSK6713 kit, USB probe,5V DC supply
THEORY:
The IIR filter can realize both the poles and zeroes of a system because it has a rational
transfer function, described by polynomials in z in both the numerator and the denominator:
M
∑ b k z−k
H ( z ) k=0
N
∑ ak Z−k
k=1 (2)
The difference equation for such a system is described by the following:
M N
y (n )= ∑ bk x (n−k ) + ∑ ak y (n−k )
k =0 k=1 (3)
M and N are order of the two polynomials
bk and ak are the filter coefficients. These filter coefficients are generated using FDS (Filter
Design software or Digital Filter design package).
IIR filters can be expanded as infinite impulse response filters. In designing IIR filters, cutoff
frequencies of the filters should be mentioned. The order of the filter can be estimated using
butter worth polynomial. That’s why the filters are named as butter worth filters. Filter
coefficients can be found and the response can be plotted.
67
68
10.1 PROGRAM :
69
if(c==2)
disp('Frequency response of IIR HPF is:');
[b,a]=butter(n,wn,'high','s');
end
w=0:.01:pi;
[h,om]=freqs(b,a,w);
m=20*log10(abs(h));
an=angle(h);
figure,subplot(2,1,1);plot(om/pi,m);
title('magnitude response of IIR filter is:');
xlabel('(a) Normalized freq. -->');
ylabel('Gain in dB-->');
subplot(2,1,2);plot(om/pi,an);
title('phase response of IIR filter is:');
xlabel('(b) Normalized freq. -->');
ylabel('Phase in radians-->');
RESULT :
70
10.2 Program
clc;
clear;
ap=input('Enter PB attn in dB : '); as=input('Enter SB attn in dB : ');
fp=input('Enter PB freq in hz : '); fs=input('Enter SB freq in hz : ');
F=input('Enter the sampling freq : ');
wp=2*pi*fp/F;
ws=2*pi*fs/F;
wp=2*F*tan(wp/2);
ws=2*F*tan(ws/2);
[N,wc]=buttord(wp,ws,ap,as,'s')
c=input('enter choice of filter 1. LPF 2. HPF \n ');
if(c==1)
disp('Frequency response of IIR LPF is:');
[b,a]=butter(n,wn,'low','s');
end
if(c==2)
disp('Frequency response of IIR HPF is:');
[b,a]=butter(n,wn,'high','s');
end
%[b,a]=butter(N,wc,'s')
71
[bz,az]=bilinear(b,a,F)
freqz(bz,az)
command window
Enter PB attn in dB : 0.15
Enter SB attn in dB : 60
Enter PB freq in hz : 1500
Enter SB freq in hz : 3000
Enter the sampling freq : 7000
enter choice of filter 1. LPF 2. HPF : 1
-100
-200
-300
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Normalized Frequency ( rad/sample)
0
Phase (degrees)
-200
-400
-600
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Normalized Frequency ( rad/sample)
72
10.3 Program
clc;
clear;
ap=input('Enter PB attn in dB : ');
as=input('Enter SB attn in dB : ');
fp=input('Enter PB freq in hz : ');
fs=input('Enter SB freq in hz : ');
F=input('Enter the sampling freq : ');
wp=2*pi*fp/F;
ws=2*pi*fs/F;
wp=2*F*tan(wp/2);
ws=2*F*tan(ws/2);
[N,wc]=buttord(wp,ws,ap,as,'s')
c=input('enter choice of filter 1. BPF 2. BRF \n ');
if(c==1)
disp('Frequency response of IIR LPF is:');
[b,a]=butter(N,wc,'bandpass','s');
end
if(c==2)
disp('Frequency response of IIR HPF is:');
[b,a]=butter(N,wc,'stop','s');
end
%[b,a]=butter(N,wc,'s')
[bz,az]=bilinear(b,a,F)
freqz(bz,az)
Comand wind
74
RESULT: IIR Filter (LP/HP) has been implemented and their magnitude responses are
visualized through DSP processor and MATLAB.
75
11. IIR FILTER DESIGN
AIM: To design and implement IIR (BP/BR) filters using impulse invariant method in
MATLAB.
EQUIPMENTS:
Operating System – Windows XP
Constructor - Simulator
Software - CCStudio 3 & MATLAB 7.5
Hardware - DSK6713 kit, USB probe,5V DC supply
THEORY:
The IIR filter can realize both the poles and zeroes of a system because it has a rational
transfer function, described by polynomials in z in both the numerator and the denominator:
M
∑ b k z−k
H ( z ) k=0
N
∑ ak Z−k
k=1 (2)
The difference equation for such a system is described by the following:
M N
y (n )= ∑ bk x (n−k ) + ∑ ak y (n−k )
k =0 k=1 (3)
M and N are order of the two polynomials
bk and ak are the filter coefficients. These filter coefficients are generated using FDS (Filter
Design software or Digital Filter design package).
IIR filters can be expanded as infinite impulse response filters. In designing IIR filters, cutoff
frequencies of the filters should be mentioned. The order of the filter can be estimated using
butter worth polynomial. That’s why the filters are named as butter worth filters. Filter
coefficients can be found and the response can be plotted.
76
77
Program
%IIR chebyshev filter with impulse invariance transformation :
clc;
clear;
ap=input('Enter PB attn in dB : ');
as=input('Enter SB attn in dB : ');
fp=input('Enter PB freq in hz : ');
fs=input('Enter SB freq in hz : ');
F=input('Enter the sampling freq : ');
wp=2*pi*fp/F;
ws=2*pi*fs/F;
wp=wp*F;
ws=ws*F;
[N,wc]=cheb1ord(wp,ws,ap,as,'s')
c=input('Enter the type of filter : ');
if(c==1)
disp('Frequency response of IIR LPF is:');
[b,a]= cheby1(N,ap,wc,'s');
end
if(c==2)
disp('Frequency response of IIR HPF is:');
[b,a]= cheby1(N,ap,wc,'high','s');
end
if(c==3)
disp('Frequency response of IIR LPF is:');
[b,a]= cheby1(N,ap,wc,'bandpass','s');
end
if(c==4)
disp('Frequency response of IIR HPF is:');
[b,a]=cheby1(N,ap,wc,'stop','s');
end
[bz,az]=impinvar(b,a,F)
freqz(bz,az)
78
RESULT: IIR Filter has been implemented and their magnitude responses are visualized
through DSP processor and MATLAB.
79
12. FIR FILTERS USING FREQUENCY SAMPLING METHOD
EQUIPMENTS:
Operating System – Windows XP
Constructor - Simulator
Software - CCStudio 3 & MATLAB 7.5
Hardware - DSK6713 kit, USB probe,5V DC supply
THEORY:
FIR Filter Design by Frequency Sampling
The technique described below is simpler than some other approaches. Herein, it is assumed
here that the filter length, M, is odd.
The basic method is to specify the desired magnitude of the frequency response, |Hd(F)|, at a set
of frequencies Fk = k * dF = k /M. The unit sample response is then found via the inverse DFT.
The troublesome point with this method is properly setting up the desired magnitude |Hd(F)| with
even symmetry, and appropriate phase, <Hd(F), (with odd symmetry). These requirements are
needed to ensure the resulting filter is causal, has a constant delay (linear phase shift), and has
real coefficients.
If a zero phase shift was specified for each <Hd(F) then the resulting h(n) would be centered at
the origin. Hence a minimum delay of g = (M-1)/2 samples is needed to make the filter causal.
By the time-shift property of the DTFT, this corresponds to multiplication of |Hd(F)| by exp(-
j*g*2*pi*F). Note the linear phase variation given in the exponential. The derivative of Hd with
respect to w yields the group delay for the filter, which is the constant, g, (in samples). Real
coefficients are assured because of the uniform spacing of Fk and because symmetric magnitudes
are assigned to Hd(Fk) for positive and negative values of Fk.
Recall that with the method of FIR design by windowing the only inputs were the cutoff
frequency and the filter length. The resulting error |H(F)| - |Hd(F)| was not known at any given
frequency. In this technique frequency response will be exact at the specified frequencies,
F = Fk, but will be unknown at other frequencies.
General Procedure:
1) Given |Hd(F)| for 0 <= F < 1.
2) Select the filter length = M.
80
3) Find the frequency spacing, dF, for the sampling of Hd(F): dF = 1 / M (cycles / sample) Hd(F)
will be specified for F = k * dF, with k = 0 , M-1, thus giving samples at frequencies across the
range of 0 <= F < 1.
4) Find the group delay for the filter = g = (M-1) / 2.
5) Setup symmetric |Hd(k)|. Setup <Hd(k) = -g * 2 * pi * dF * k.
6) Find h(n) = IDFT{ Hd(k) }. This is an M-point IDFT.
7) Find actual frequency response, H(k) = DFT{ h(n) }. Use a large value of N,
zero padding h(n) to yield an acceptable frequency resolution.
8) Assess filter by examining plot of magnitude of frequency response.
9) Repeat as needed, going to step #2.
Program
fs=8e3;
ts=1/fs; ns=512;
t=[0:ts:ts*(ns-1)];
f1=500; f2=1800; f3=2000; f4=3200;
x1=sin(2*pi*f1*t);
x2=sin(2*pi*f2*t);
x3=sin(2*pi*f3*t);
x4=sin(2*pi*f4*t);
x=x1+x2+x3+x4;
grid on;
n=16;
w=[0.4 0.6];
w1=0.4;
a=1;
c=input('enter 1.lpf 2.hpf 3.bpf 4.brf');
if(c==1)
b=fir1(n,w1,'low');
end;
if(c==2)
w=0.5;
b=fir1(n,w,'high');
end;
if(c==3)
w=[0.4 0.6];
b=fir1(n,w,'DC-0');
end;
if(c==4)
w=[0.4 0.6];
b=fir1(n,w,'DC-1');
end;
freqz(b,a);
pause;figure;
subplot(2,1,1);
81
npts=200;
plot(t(1:npts),x(1:npts));
title('time plot of input and output');
xlabel('time');
ylabel('input signal');
y=filter(b,a,x);
subplot(2,1,2);
plot(t(1:npts),y(1:npts));
xlabel('time');
ylabel('filtered signal');
pause;figure;
subplot(2,1,1);
xfftmag=(abs(fft(x,ns)));
xfftmagh=xfftmag(1:length(xfftmag)/2);
f=(1:1:length(xfftmagh))*fs/ns;
plot(f,xfftmagh);
title('input and output spectrum');
xlabel('frequency');ylabel('input spectrum');
subplot(2,1,2);
yfftmag=(abs(fft(y,ns)));
yfftmagh=yfftmag(1:length(yfftmag)/2);
f=(1:1:length(yfftmagh))*fs/ns;
plot(f,yfftmagh);
xlabel('frequency');ylabel('filtered spectrum');
Low Pass Filter
command window display
Enter 1.lpf 2.hpf 3.bpf 4.brf 1
OUTPUT GRAPHS
82
High Pass Filter
command window display
Enter 1.lpf 2.hpf 3.bpf 4.brf 2
83
OUTPUT GRAPHS
84
Band Pass Filter
command window display
Enter 1.lpf 2.hpf 3.bpf 4.brf 3
OUTPUT GRAPHS
85
Band Reject Filter
command window display
Enter 1.lpf 2.hpf 3.bpf 4.brf 4
86
OUTPUT GRAPHS
87
RESULT: FIR Filter (LP?BP?BP/BR) using frequency sampling methed has been implemented
and their magnitude responses are visualized through DSP processor and MATLAB.
88
APPENDIX-1
Program code:
#include<stdio.h>
#include<math.h>
int x[30],h[30],y[30],x2[30],a[30],m,n,i,j,k,l;
main()
{
printf("enter length of two sequences");
scanf("%d%d",&m,&n);
printf("enter the first sequence \n");
for(i=0;i<m;i++)
scanf("%d",&x[i]);
printf("enter the second sequence \n");
for(i=0;i<n;i++)
scanf("%d",&h[i]);
l=m+n+-1;
if((m-n)!=0)
{
for(i=m;i<l;i++)
x[i]=0;
for(i=n;i<l;i++)
h[i]=0;
}
m=l;
n=l;
y[0]=0;
a[0]=h[0];
for(j=1;j<n;j++)
a[j]=h[n-j];
for(i=0;i<n;i++)
y[0]+=x[i]*a[i];
for(k=1;k<n;k++)
{
y[k]=0;
for(j=1;j<n;j++)
x2[j]=a[j-1];
x2[0]=a[n-1];
for(i=0;i<n;i++)
{
a[i]=x2[i];
y[k]+=x[i]*x2[i];
}
}
printf("the circular convolution is");
for(i=0;i<l;i++)
printf("%d\n",y[i]);
}
Output:
89
1
2
3
enter the second sequence
1
2
the circular convolution is1
4
7
6
Waveforms:
90
91
Linear to circular convolution
Program code:
#include<stdio.h>
#include<math.h>
int x[30],h[30],y[30],x2[30],a[30],m,n,i,j;
main()
{
printf("enter length of two sequences");
scanf("%d%d",&m,&n);
printf("enter the first sequence \n");
for(i=0;i<m;i++)
scanf("%d",&x[i]);
printf("enter the second sequence \n");
for(i=0;i<n;i++)
scanf("%d",&h[i]);
for(i=0;i<m+n-1;i++)
{
y[i]=0;
for(j=0;j<=i;j++)
{
y[i]=y[i]+(x[j]*h[i-j]);
}
}
for(i=0;i<n;i++)
y[i]=y[n+i]+y[i];
for(i=0;i<n;i++)
printf("Result is \n y[%d]=%d",i,y[i]);
}
Output:
Waveforms:
92
93
94