FIR filter LPF, HPF, BPF using Windowing Method
Problem Statement : Design an FIR filter from given specifications using windowing method ( Program should work for different types of filter specifications i.e LPF, HPF, BPF etc and all window sequences. Plot the frequency response for different frequency terms i.e. analog and DT frequency).
1: // FIR
2: #include<stdio.h>
3: #include<conio.h>
4: #include<math.h>
5: void main()
6: {
7: float wc,tou,M,hd[50],h[50],wn,pi,n;
8: int choice,p;
9: char HPF;
10: clrscr();
11: printf("\t\tFIR filter design using windows\n\n");
12: printf("Enter the length (number of coefficients) of the filter M = ");
13: scanf("%f",&M); // length(M) of the filter
14: p = (int)M;
15: printf("\nEnter the discrete time cutoff frequency wc = ");
16: scanf("%f",&wc); // cutoff frequency of the filter i.e. wc
17: printf("\nif you want highpass filter, enter HPF = y, otherwise "
18: "perss any key\ndefault is lowpass filter\nHPF = ");
19: HPF = getch(); printf("%c",HPF); // enter for HPF or LPF
20: tou = (M-1)/2; // delay in hd(n)
21: pi = 22.0/7.0; // value of pi
22: // Next part of the program calculates hd(n)
23: for(n = 0; n <= M-1; n++) // this loop calculates hd(n) for LPF
24: { // (by default)
25: hd[n] = (sin(wc*(n-tou)))/(pi*(n-tou)); // hd(n) for LPF
26: if((n==tou)&&((p/2)*2 != p)) hd[n] = wc/pi; // hd(n) at n = tou
27: }
28: if(HPF =='y') // this loop recalculates hd(n) for HPF if user
29: { // wants to design HPF
30: for(n = 0; n <= M-1; n++)
31: { // hd(n) for HPF
32: hd[n] = (sin(pi*(n-tou)) - sin(wc*(n-tou)))/(pi*(n-tou));
33: if((n==tou)&&((p/2)*2 != p)) hd[n] = 1-(wc/pi);
34: } // hd(n) at n = tou
35: }
36: // Next part of the program performs windowing depending upon
37: // choice of window
38: printf("\n\nEnter the window you want to use");// choice of window
39: printf("\nrectangular window (Enter 1)"); //rectangular
40: printf("\ntriangular(bartlett) window (Enter 2)"); //triangular
41: printf("\nhamming window (Enter 3)"); //hamming
42: printf("\nhanning window (Enter 4)\nchoice = "); //hanning
43: scanf("%d",&choice); // enter choice
44: switch(choice)
45: { // h(n) = hd(n) * w(n) depending upon type of window
46: case 1 : for(n = 0; n <= M-1; n++) // rectangular
47: {
48: wn = 1; //wn for rectangular window
49: h[n] = hd[n] * wn; // windowing operation
50: } break;
51: case 2 : for(n = 0; n <= M-1; n++) // triangular
52: {
53: wn = 1 - (2*abs(n-tou)/(M-1)); // calculation of wn
54: h[n] = hd[n] * wn; // windowing operation
55: } break;
56: case 3 : for(n = 0; n <= M-1; n++) // hamming
57: {
58: wn = 0.54 - 0.46*cos((2*pi*n)/(M-1)); // wn
59: h[n] = hd[n] * wn; // windowing operation
60: } break;
61: case 4 : for(n = 0; n <= M-1; n++) // hanning
62: {
63: wn = (1-cos((2*pi*n)/(M-1)))/2; // calculation of wn
64: h[n] = hd[n] * wn; // windowing operation
65: } break;
66: }
67: // Next part of the program displays coefficients on the screen
68: if(HPF == 'y') printf("\ncoefficients of highpass FIR filter"
69: " are as follows...");
70: else printf("\ncoefficients of lowpass FIR filter"
71: " are as follows...");
72: for(n = 0; n <= M-1; n++)
73: {
74: printf("\nh%1.0f = %f",n,h[n]);
75: }
76: getch();
77: }
{ 0 comments... read them below or add one }
Post a Comment