Experiment No:4 DFT Implementation: AIM: To Find N Point DFT of A Given Sequence
Experiment No:4 DFT Implementation: AIM: To Find N Point DFT of A Given Sequence
Date : 14-07-2010
DFT IMPLEMENTATION
% 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
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
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
% 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
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
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
% 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);
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');
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
0 -20
0
Gain in dB
Gain in dB
Gain in dB
-20 -40
-50
-40 -60
0
Gain in dB
-10
-20
0 0.5 1
Normalised frequency
22
EXPERIMENT NO:7
Date : 04-08-2010
23
subplot(2,1,2);
plot(om/pi,an);
ylabel('phase');
xlabel('Normalised frequency');
RESULT
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
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
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
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
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
-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
-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
-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
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
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
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
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
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
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
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
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
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