0% found this document useful (0 votes)
54 views

Experiment No:4 DFT Implementation: AIM: To Find N Point DFT of A Given Sequence

The document describes experiments to implement and test various digital filters, including discrete Fourier transforms (DFT), inverse DFT (IDFT), finite impulse response (FIR) filters, and infinite impulse response (IIR) Butterworth filters. Code examples are provided to design low pass, high pass, band pass and band stop FIR filters using the fir1 function. IIR low pass and high pass Butterworth filters are also designed using the butter function. The filters are analyzed by plotting the frequency responses and phase responses.

Uploaded by

Bijo J Francis
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Experiment No:4 DFT Implementation: AIM: To Find N Point DFT of A Given Sequence

The document describes experiments to implement and test various digital filters, including discrete Fourier transforms (DFT), inverse DFT (IDFT), finite impulse response (FIR) filters, and infinite impulse response (IIR) Butterworth filters. Code examples are provided to design low pass, high pass, band pass and band stop FIR filters using the fir1 function. IIR low pass and high pass Butterworth filters are also designed using the butter function. The filters are analyzed by plotting the frequency responses and phase responses.

Uploaded by

Bijo J Francis
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 34

EXPERIMENT NO:4

Date : 14-07-2010

DFT IMPLEMENTATION

AIM : To find N point DFT of a given sequence.

% without function
clc; close all; clear all;
x=input('enter the sequence');
N=input('enter the length of DFT');
len=length(x);
if N>len
x=[x zeros(1,N-len)];
else if N<len
x=x(1:N);
end;
end;
W=exp(-j*2*pi/N);
n=[0:1:N-1];
k=[0:1:N-1];
nk=n'*k;
W=W.^nk;
X=x*W;
subplot(3,2,1);
stem(x);
subplot(3,2,2);
stem(n,abs(X));
subplot(3,2,3);
stem(n,angle(X));

14
RESULT

enter the sequence [1 2 3];


enter the length of DFT 3

input sequence dft amplitude


4 10
amplitude

2 5

0 0
1 1.5 2 2.5 3 0 0.5 1 1.5 2
time
dft phase
5

-5
0 0.5 1 1.5 2

%using function
X=fft(x,N);
n=0:1:length(X)-1;
subplot(3,2,5);
stem(n,abs(X));
subplot(3,2,6);
stem(n,angle(X));

15
RESULT

enter the sequence [1 2 3];


enter the length of DFT 3

input sequence dft amplitude


4 10
amplitude

2 5

0 0
1 1.5 2 2.5 3 0 0.5 1 1.5 2
time
dft phase
5

-5
0 0.5 1 1.5 2

16
EXPERIMENT NO:5
Date : 21-07-2010

IDFT IMPLEMENTATION

AIM : To find inverse DFT (IDFT) of a given DFT sequence.

% without function
clc;close all;clear all;
x=input('enter the sequence');
N=input('enter the length of DFT');
len=length(x);
if N>len
x=[x zeros(1,N-len)];
else if N<len
x=x(1:N);
end;
end;
W=exp(-j*2*pi/N);
n=[0:1:N-1];
k=[0:1:N-1];
nk=n'*k;
W=W.^(-nk);
X=(x*W)/N;
subplot(2,2,1);
stem(abs(x));
title('abs(x)');
subplot(2,2,2);
stem(n,X);
title('inverse dft')
xlabel('time');

17
ylabel('amplitude');

RESULT

enter the sequence[2 1+1*i 0 1-1*i]


enter the length of DFT 4

abs(x) inverse dft


2 1

1.5

am plitude
1 0.5

0.5

0 0
1 2 3 4 0 1 2 3
time

%with function
xr=ifft(x,N);
subplot(2,2,3);
stem(abs(x));
title('abs(x)');
subplot(2,2,4);
stem(n,xr);
title('inverse dft')
xlabel('time');
ylabel('amplitude');

18
RESULT

enter the sequence[2 1+1*i 0 1-1*i]


enter the length of DFT 4

abs(x) inverse dft


2 1

1.5

amplitude
1 0.5

0.5

0 0
1 2 3 4 0 1 2 3
time

19
EXPERIMENT NO:6
Date : 28-07-2010

FIR FILTERS

AIM : To write a program in matlab for implementing FIR filters.

% FIR FILTERS
clc;
clear all;
clf;
rp=input('enter the passband ripple');
rs=input('enter the stopband ripple');
fp=input('enter the passband frequency');
fs=input('enter the stopband frequency');
f=input('enter the sampling frequency');
wp=2*fp/f;
ws=2*fs/f;
num=-20*log10(sqrt(rp*rs))-13;
den=14.6*(fs-fp)/f;
n=ceil(num/den);
n1=n+1;
if(rem(n,2)~=0)
n1=n;
n=n-1;
end;
y = boxcar(n1);

%low pass filter


b=fir1(n,wp,y);

20
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,3,1);
plot(o/pi,m);
title('low pass');
ylabel('Gain in dB');
xlabel('Normalised frequency');

%high pass filter


b=fir1(n,ws,'high',y);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,3,2);
plot(o/pi,m);
title('high pass');
ylabel('Gain in dB');
xlabel('Normalised frequency');

%band pass filter


wn=[wp ws];
b=fir1(n,wn,y);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,3,3);
plot(o/pi,m);
title('band pass');
ylabel('Gain in dB');
xlabel('Normalised frequency');

%band stop filter


wn=[wp ws];

21
b=fir1(n,wn,'stop',y);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,3,4);
plot(o/pi,m);
title('stop band pass');
ylabel('Gain in dB');
xlabel('Normalised frequency');

RESULT
Low pass, high pass, band pass and band stop filters are implemented and output
waveforms obtained.
enter the passband ripple=0.05
enter the stopband ripple=0.04
enter the passband frequency=1500
enter the stopband frequency=1200
enter the sampling frequency=9000

low pass high pass band pass


50 20 0

0 -20
0
Gain in dB

Gain in dB

Gain in dB

-20 -40
-50
-40 -60

-100 -60 -80


0 0.5 1 0 0.5 1 0 0.5 1
Normalised frequency Normalised frequency Normalised frequency
stop band pass
10

0
Gain in dB

-10

-20
0 0.5 1
Normalised frequency

22
EXPERIMENT NO:7
Date : 04-08-2010

BUTTERWORTH IIR FILTERS

AIM : To write a program in matlab for implementing IIR filters

% IIR LOW PASS FILTERS


clc;
clear all;
clf;
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);
[b,a]=butter(n,wn);
w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
title('low pass IIR filter');
ylabel('Gain in dB');
xlabel('Normalised frequency');

23
subplot(2,1,2);
plot(om/pi,an);
ylabel('phase');
xlabel('Normalised frequency');

RESULT

enter the passband ripple.5


enter the stopband ripple50
enter the passband frequency200
enter the stopband frequency400
enter the sampling frequency1000

low pass IIR filter


200

0
Gain in dB

-200

-400
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Normalised frequency

2
phase

-2

-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Normalised frequency

24
%IIR HIGH PASS FILTERS
clc;
clear all;
clf;
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);
[b,a]=butter(n,wn,'high');
w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
title('high pass IIR filter');
ylabel('Gain in dB');
xlabel('Normalised frequency');
subplot(2,1,2);
plot(om/pi,an);
ylabel('phase');
xlabel('Normalised frequency');

25
RESULT

enter the passband ripple.5


enter the stopband ripple50
enter the passband frequency400
enter the stopband frequency200
enter the sampling frequency1000

high pass IIR filter


100

0
Gain in dB

-100

-200

-300
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Normalised frequency

2
phase

-2

-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Normalised frequency

26
%IIR BAND PASS FILTERS
clc;
clear all;
clf;
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);
[b,a]=butter(n,wn);
w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
title('band pass IIR filter');
ylabel('Gain in dB');
xlabel('Normalised frequency');
subplot(2,1,2);
plot(om/pi,an);
ylabel('phase');
xlabel('Normalised frequency');

27
RESULT

enter the passband ripple.4


enter the stopband ripple50
enter the passband frequency[800 1200]
enter the stopband frequency[500 1500]
enter the sampling frequency4000

band pass IIR filter


200

0
Gain in dB

-200

-400
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Normalised frequency

2
phase

-2

-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Normalised frequency

28
%IIR BANDSTOP FILTER
clc;
clear all;
clf;
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);
[b,a]=butter(n,wn,'stop');
w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
title('stop pass IIR filter');
ylabel('Gain in dB');
xlabel('Normalised frequency');
subplot(2,1,2);
plot(om/pi,an);
ylabel('phase');
xlabel('Normalised frequency');

29
RESULT
enter the passband ripple.4
enter the stopband ripple50
enter the passband frequency[800 1500]
enter the stopband frequency[1000 1200]
enter the sampling frequency4000

stop pass IIR filter


100

0
Gain in dB

-100

-200

-300
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Normalised frequency

2
phase

-2

-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Normalised frequency

30
EXPERIMENT NO:8
Date : 01-09-2010

CHEBYSHEV FIRST ORDER FILTERS

AIM : To write a program in matlab for implementing CHEBYSHEV


FIRST ORDER filters.

%CHEBYSHEV FIRST ORDER LPF


clc;close all;clear all;
rp=input('enter pass band ripple');
rs=input('enter stop band ripple');
wp=input('enter pass band freq');
ws=input('enter stop band freq');
fs=input('enter sampling freq');
w1=2*wp/fs;
w2=2*ws/fs;
[n wn]=cheb1ord(w1,w2,rp,rs);
[b a]=cheby1(n,rp,wn);
w=0:0.01:pi;
[h om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
title('magnitude response of chebyshev first order lpf');

31
xlabel('normalised freq');
ylabel('gain');
subplot(2,1,2);
plot(om/pi,an);
title('phase response of chebyshev first order lpf');
xlabel('normalised freq');
ylabel('phase');

RESULT

enter pass band ripple.5


enter stop band ripple50
enter pass band freq1000
enter stop band freq1500
enter sampling freq5000

magnitude response of chebyshev first order lpf


0

-100
gain

-200

-300

-400
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
normalised freq
phase response of chebyshev first order lpf
4

2
phase

-2

-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
normalised freq

32
%CHEBYSHEV FIRST ORDER HPF
clc;close all;clear all;
rp=input('enter pass band ripple');
rs=input('enter stop band ripple');
wp=input('enter pass band freq');
ws=input('enter stop band freq');
fs=input('enter sampling freq');
w1=2*wp/fs;
w2=2*ws/fs;
[n wn]=cheb1ord(w1,w2,rp,rs);
[b a]=cheby1(n,rp,wn,'high');
w=0:0.01:pi;
[h om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
title('magnitude response of chebyshev first order hpf');
xlabel('normalised freq');
ylabel('gain');
subplot(2,1,2);
plot(om/pi,an);
title('phase response of chebyshev first order hpf');
xlabel('normalised freq');
ylabel('phase');

RESULT

33
enter pass band ripple.5
enter stop band ripple50
enter pass band freq1500
enter stop band freq1000
enter sampling freq5000

magnitude response of chebyshev first order hpf


0

-100
gain

-200

-300

-400
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
normalised freq
phase response of chebyshev first order hpf
4

2
phase

-2

-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
normalised freq

34
%CHEBYSHEV FIRST ORDER BPF
clc;close all;clear all;
rp=input('enter pass band ripple');
rs=input('enter stop band ripple');
wp=input('enter pass band freq');
ws=input('enter stop band freq');
fs=input('enter sampling freq');
w1=2*wp/fs;
w2=2*ws/fs;
[n wn]=cheb1ord(w1,w2,rp,rs);
[b a]=cheby1(n,rp,wn,'bandpass');
w=0:0.01:pi;
[h om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
title('magnitude response of chebyshev first order bpf');
xlabel('normalised freq');
ylabel('gain');
subplot(2,1,2);
plot(om/pi,an);
title('phase response of chebyshev first order bpf');
xlabel('normalised freq');
ylabel('phase');

RESULT

35
enter pass band ripple.5
enter stop band ripple50
enter pass band freq[1000 1200]
enter stop band freq[800 1500]
enter sampling freq4500

magnitude response of chebyshev first order bpf


0

-200
gain

-400

-600
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
normalised freq
phase response of chebyshev first order bpf
4

2
phase

-2

-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
normalised freq

36
%CHEBYSHEV FIRST ORDER BSF
clc;close all;clear all;
rp=input('enter pass band ripple');
rs=input('enter stop band ripple');
wp=input('enter pass band freq');
ws=input('enter stop band freq');
fs=input('enter sampling freq');
w1=2*wp/fs;
w2=2*ws/fs;
[n wn]=cheb1ord(w1,w2,rp,rs);
[b a]=cheby1(n,rp,wn,'stop');
w=0:0.01:pi;
[h om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
title('magnitude response of chebyshev first order bsf');
xlabel('normalised freq');
ylabel('gain');
subplot(2,1,2);
plot(om/pi,an);
title('phase response of chebyshev first order bsf');
xlabel('normalised freq');
ylabel('phase');

37
RESULT

enter pass band ripple.5


enter stop band ripple50
enter pass band freq[1000 1200]
enter stop band freq[800 1500]
enter sampling freq4500

magnitude response of chebyshev first order bsf


100

0
gain

-100

-200
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
normalised freq
phase response of chebyshev first order bsf
4

2
phase

-2

-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
normalised freq

38
EXPERIMENT NO:9
Date : 08-09-2010

CHEBYSHEV SECOND ORDER FILTERS

AIM : To write a program in matlab for implementing CHEBYSHEV


SECOND ORDER filters.

%CHEBYSHEV SECOND ORDER LPF


clc;close all;clear all;
rp=input('enter pass band ripple');
rs=input('enter stop band ripple');
wp=input('enter pass band freq');
ws=input('enter stop band freq');
fs=input('enter sampling freq');
w1=2*wp/fs;
w2=2*ws/fs;
[n wn]=cheb2ord(w1,w2,rp,rs);
[b a]=cheby2(n,rp,wn);
w=0:0.01:pi;
[h om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);

39
title('magnitude response of chebyshev second order lpf');
xlabel('normalised freq');
ylabel('gain');
subplot(2,1,2);
plot(om/pi,an);
title('phase response of chebyshev second order lpf');
xlabel('normalised freq');
ylabel('phase');

RESULT

enter pass band ripple.5


enter stop band ripple50
enter pass band freq800
enter stop band freq1000
enter sampling freq4500

40
magnitude response of chebyshev second order lpf
0

-20
gain

-40

-60
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
normalised freq
phase response of chebyshev second order lpf
2

1
phase

-1

-2
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
normalised freq

%CHEBYSHEV SECOND ORDER HPF


clc;close all;clear all;
rp=input('enter pass band ripple');
rs=input('enter stop band ripple');
wp=input('enter pass band freq');
ws=input('enter stop band freq');
fs=input('enter sampling freq');
w1=2*wp/fs;
w2=2*ws/fs;
[n wn]=cheb2ord(w1,w2,rp,rs);

41
[b a]=cheby2(n,rp,wn,'high');
w=0:0.01:pi;
[h om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
title('magnitude response of chebyshev second order hpf');
xlabel('normalised freq');
ylabel('gain');
subplot(2,1,2);
plot(om/pi,an);
title('phase response of chebyshev second order hpf');
xlabel('normalised freq');
ylabel('phase');

RESULT

enter pass band ripple.5


enter stop band ripple50
enter pass band freq1000
enter stop band freq800
enter sampling freq4500

42
magnitude response of chebyshev second order hpf
0

-10
gain

-20

-30
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
normalised freq
phase response of chebyshev second order hpf
2

1
phase

-1

-2
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
normalised freq

%CHEBYSHEV SECOND ORDER BPF


clc;close all;clear all;

43
rp=input('enter pass band ripple');
rs=input('enter stop band ripple');
wp=input('enter pass band freq');
ws=input('enter stop band freq');
fs=input('enter sampling freq');
w1=2*wp/fs;
w2=2*ws/fs;
[n wn]=cheb2ord(w1,w2,rp,rs);
[b a]=cheby2(n,rp,wn,'bandpass');
w=0:0.01:pi;
[h om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
title('magnitude response of chebyshev second order bpf');
xlabel('normalised freq');
ylabel('gain');
subplot(2,1,2);
plot(om/pi,an);
title('phase response of chebyshev second order bpf');
xlabel('normalised freq');
ylabel('phase');

RESULT

enter pass band ripple.5


enter stop band ripple50
enter pass band freq[800 1500]
enter stop band freq[1000 1200]
enter sampling freq4500

44
magnitude response of chebyshev second order bpf
200

0
gain

-200

-400
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
normalised freq
phase response of chebyshev second order bpf
4

2
phase

-2
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
normalised freq

%CHEBYSHEV SECOND ORDER BSF


clc;close all;clear all;

45
rp=input('enter pass band ripple');
rs=input('enter stop band ripple');
wp=input('enter pass band freq');
ws=input('enter stop band freq');
fs=input('enter sampling freq');
w1=2*wp/fs;
w2=2*ws/fs;
[n wn]=cheb2ord(w1,w2,rp,rs);
[b a]=cheby2(n,rp,wn,'stop');
w=0:0.01:pi;
[h om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
title('magnitude response of chebyshev second order bsf');
xlabel('normalised freq');
ylabel('gain');
subplot(2,1,2);
plot(om/pi,an);
title('phase response of chebyshev second order bsf');
xlabel('normalised freq');
ylabel('phase');

RESULT

enter pass band ripple.5


enter stop band ripple50
enter pass band freq[1000 1200]
enter stop band freq[800 1500]
enter sampling freq4500

46
magnitude response of chebyshev second order bsf
20

0
gain

-20

-40

-60
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
normalised freq
phase response of chebyshev second order bsf
2

1
phase

-1

-2
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
normalised freq

47

You might also like