FIR Filter Design and DFT/IDFT Computation Techniques

Experiment 11: FIR Filter Design Using Frequency Sampling

11.1: Low Pass Filter Design and Frequency Response

Design a Low Pass Filter as per the given specifications and plot the Frequency Response.


1. clc;
2. close;
3. clear;
4. delta1=0.1; // Attenuation
5. delta2=0.1;
6. fl=400; // Low Cut-Off Frequency
7. fh=500; // High Cut-Off Frequency
8. fs=8000; // Sampling Frequency
9. A=-20*log10(min(delta1:delta2));
10. w1=2*%pi*fl/fs;
11. w2=2*%pi*fh/fs;
12. temp=1+((A-8)/(2.285*((2*3.14*fh/fs)-(2*3.14*fl/fs))));
13. N=ceil((temp-1)/2);
14. n=-N:N;
15. h=(((w2+w1)/2)*(sinc(((w2+w1)/2)*n)))/(3.14); //Response
// plot (n,h);
16. [xm1,fr1]=frmag(h,8000); //Frequency Response
17. figure;
18. plot(fr1,xm1);
19. title('Frequency Response','color','red','font size',4);
20. xlabel("Frequency(Normalized)","font size",2,"color","blue");
21. ylabel("Magnitude","font size",2,"color","blue");

11.2: High Pass Filter Design and Frequency Response

Design a High Pass Filter as per the given specifications and plot the Frequency Response.


1. clc;
2. close;
3. clear;
4. delta1=0.1;
5. delta2=0.1;
6. fl=2;
7. fh=2.115;
8. fs=8;
9. A=-20*log10(min(delta1:delta2));
10. w1=2*3.14*fl/fs;
11. w2=2*3.14*fh/fs;
12. temp=1+((A-8)/(2.285*((2*3.14*fh/fs)-(2*3.14*fl/fs))));
13. N=ceil((temp-1)/2);
14. n=-N:N;
15. del=[zeros(1:N) 1 zeros(N+1:2*N)];
16. h=del-(((w2+w1)/2)*(sinc(((w2+w1)/2)*n)))/(3.14);
// High Pass=1-Low Pass
// h=[-h(1:30)0-h(32:61)];
// figure;
// plot(n,h);
17. [xmh,frh]=frmag(h,8000);
18. figure;
19. plot(frh,xmh);
20. title('Frequency Response','color','red','font size',4);
21. xlabel("Frequency (Normalized)","font size",2,"color","blue");
22. ylabel("Magnitude","font size",2,"color","blue");

11.3: Band Pass Filter Design and Frequency Response

Design a Band Pass Filter as per the given specifications and plot the Frequency Response.


1. clc;
2. close;
3. clear;
4. delta1=0.1;
5. delta2=0.1;
6. fl2=600;
7. fh2=700;
8. fs=8000;
9. A=-20*log10(min(delta1:delta2));
10. w12=2*3.14*fl2/fs;
11. w22=2*3.14*fh2/fs;
12. temp2=1+((A-8)/(2.285*((2*3.14*fh2/fs)-(2*3.14*fl2/fs))));
13. N=ceil((temp2-1)/2);
14. n=-N:N;
15. h2=(((w22+w12)/2)*(sinc(((w22+w12)/2)*n)))/(3.14);
// [xm2,fr2]=frmag(h2,8000);
// figure;
// plot(fr2,xm2);
16. delta1=0.1;
17. delta2=0.1;
18. fl1=200;
19. fh1=300;
20. fs=8000;
21. A=-20*log10(min(delta1:delta2));
22. w11=2*3.14*fl1/fs;
23. w21=2*3.14*fh1/fs;
24. temp1=1+((A-8)/(2.285*((2*3.14*fh1/fs)-(2*3.14*fl1/fs))));
25. N=ceil((temp1-1)/2);
26. n=-N:N;
27. h1=(((w21+w11)/2)*(sinc(((w21+w11)/2)*n)))/(3.14);
28. h=h2-h1;
29. [xmb,frb]=frmag(h,8000);
30. figure;
31. plot(frb,xmb);
32. title('Frequency Response','color','red','font size',4);
33. xlabel("Frequency","font size",2,"color","blue");
34. ylabel("Gain","font size",2,"color","blue");

Experiment 7: DFT and IDFT Computation

Computation of N-point DFT and IDFT of a given sequence using:

  • (a) Defining equation
  • (b) FFT method

clc;
clear all;
x=[1 2 3 4];
disp('input sequence')
disp(x);
N=length(x);
for k=0:1:N-1
    y(k+1)=0;
    for n=0:1:N-1
        y(k+1)=y(k+1)+(x(n+1)*exp(-(%i*2*%pi*n*k)/N));
    end
end
disp('DFT using formula,y')
disp(y);
mag_res=abs(y);
disp('magnitude response')
phase_res=atan(imag(y),real(y));
disp(mag_res);
disp('phase response')
disp(phase_res);
phase_res_deg=phase_res*(180/%pi);
n=0:1:N-1;
subplot(3,1,1);
plot2d3(n,x);
xlabel('samples');
ylabel('magnitude');
title('input sequence');
subplot(3,1,2);
plot2d3(n,mag_res);
xlabel('samples');
ylabel('magnitude');
title('magnitude response');
subplot(3,1,3);
plot2d3(n,phase_res_deg);
xlabel('samples');
ylabel('magnitude');
title('phase response in deg');
y1=fft(x);


disp('DFT using built in routine,y1')
disp(y1);
disp('the obtained DFT is same as that of DFT using built in routine')

Output:

“input sequence”

1. 2. 3. 4.

“DFT using formula,y”

10. + 0.i
-2. + 2.i
-2. – 9.797D-16i
-2. – 2.i

“magnitude response”

10.
2.8284271
2.
2.8284271

“phase response”

0.
2.3561945
-3.1415927
-2.3561945

“phase response in deg”

0.
135.00000
-180.00000
-135.00000

“DFT using built in routine,y1”

10. + 0.i -2. + 2.i -2. + 0.i -2. – 2.i

“the obtained DFT is same as that of DFT using built in routine”

Experiment 8: Circular Convolution Using DFT and IDFT

Evaluation of circular convolution of two sequences using the DFT and IDFT approach.


1. clc ;
2. clear ;
3. x=input('Enter the Input sequence=');
4. m=length(x);
5. xl=input('Enter the lower index of input sequence=');
6. xh=xl+m-1;
7. n=xl:1:xh;
8. subplot(3,1,1);
9. a=gca();
10. a.x_location="origin";
11. a.y_location="origin";
12. a.foreground=5;
13. a.font_color=5;
14. a.font_style=5;
15. plot2d3('gnn',n,x);
16. title('Input Sequence x[n]');
17. xlabel('Samples n');
18. ylabel('Amplitude');
19. h=input('Enter the Impulse response sequence=');
20. l=length(h);
21. hl=input('Enter the lower index of impulse response sequence=');
22. hh=hl+l-1;
23. g=hl:1:hh;
24. subplot(3,1,2);
25. a=gca();
26. a.x_location="origin";
27. a.y_location="origin";
28. a.foreground=5;
29. a.font_color=5;
30. a.font_style=5;
31. plot2d3('gnn',g,h);
32. title('Impulse Response Sequence h[n]');
33. xlabel('Samples n');
34. ylabel('Amplitude');
35. N=max(m,l);
36. p=m-l;
37. if(p>=0) then
38.     h=[h,zeros(1,p)];
39. else
40.     x=[x,zeros(1,-p)];
41. end
42. XK=fft(x,-1);
43. HK=fft(h,-1);
44. YK=XK.*HK;
45. y=ifft(YK);
46. disp('Circular convolution by DFT is y(n):');
47. disp(real(y));
48. nx=xl+hl;
49. r=nx:length(y)-1;
50. subplot(3,1,3);
51. a=gca();
52. a.x_location="origin";
53. a.y_location="origin";
54. a.foreground=5;
55. a.font_color=5;
56. a.font_style=5;
57. plot2d3('gnn',r,y);
58. title('Output Response Sequence of Circular Convolution y[n] using DFT');
59. xlabel('Samples n');
60. ylabel('Amplitude');

Output:

//INPUT:
Enter the Input sequence=[1 1 2 2]
Enter the lower index of input sequence=0
Enter the Impulse response sequence=[1 2 3 4]
Enter the lower index of impulse response sequence=0

//OUTPUT:
Circular convolution by DFT is y(n):

15 . 17 . 15 . 13 .