Monday, 13 January 2014

dsp.Write a c program for FIR filter design using windows

/* Write a c program for FIR filter design using windows
 
   Input :1.Number of coefficients of the filter (M),
          2.Cutoff frequency of digital filter(wc),
          3.Choice : high pass or low pass.
          4.Choice of the window.
   
  Output :Coefficients of the corresponding filter

  Roll No :-20
  Batch :-A          */

 
 
#include<conio.h>
#include<stdio.h>
#include<math.h>

void main()
 {
   float wc,tou,M,hd[50],h[50],wn,pi,n;
   int ch,p;
   char HPF;
   clrscr();
   printf("\t\tFIR filter design using windows\n\n");
   printf("n\nEnter the length(M) of the filter(coefficient):");
   scanf("%f",&M);
   p=(int)M;
   printf("\n\nEnter the cutoff Frequency(Discrete Frequency) Wc:");
   scanf("%f"&wc);
   printf("\n\nChoice for the filter:\n");
   printf("\n\nEnter 'Y' for High Pass Filter=");
   HPF=toupper(getch());
   printf("%c"HPF);
   tou=(M-1)/2;
   pi=22.0/7.0;
   for(n=0;n<=M-1;n++)
     {
       hd[n]=(sin(wc*(n-tou)))/(pi*(n-tou));
       if(n==tou)&&((p/2)*2!=p))hd[n]=wc/pi;
         {
           if(HPF=='Y')
            {
              for(n=0;n<=M;n++)
               {
                 hd[n]=(sin(pi*(n-tou))-sin(wc*(n-tou)))/(pi*(n-tou));
                 if(n==tou)&&((p/2)*2!=p))hd[n]=1(wc/pi);
               }
            }
          printf("\n\nEnter the windows you want to use");
          printf("\n1.Rectangular window \n2.Triangular(bartlett) window\n3.Hamming Window\n4.Hanning window");
          printf("\nEnter your choice");
          scanf("%d",&ch);
          switch(ch)
           {
             case 1:for(n=0;n<=M;n++)
                     {
                       wn=1;
                       h[n]=hd[n]*wn;
                     }
                     break;
            case 2:for(n=0;n<=M-1;n++)
                     {
                       wn=1-(2*abs(n-tou)/(M-1));
                       h[n]=hd[n]*wn;
                     }
                     break;
            case 3:for(n=0;n<=M-1;n++)
                    {
                      wn=0.54=0.46*cos((2*pi*n)/(M-1));
                      h[n]=hd[n]*wn;
                    }
                     break;
           case 4:for(n=0;n<=M-1;n++)
                   {
                     wn=(1-cos((2*pi*n)/(M-1)));
                     h[n]=hd[n]*wn;
                   }
                   break;
         }
        if(HPF=='Y')
          printf("\n\nCoefficient of High Pass FIR Filter are as follows");
        else
          printf("\n\nCoefficient of Low Pass FIR Filter are as follows");
          for(n=0;n<=M-1;n++)
           {
             printf("\n\nh[%1.0f]=%f",n,h[n]);
           }
          getch();
 }

***********************************************************
                         FIR filter design using windows

 Enter the length(M) of the filter (coefficient):4

 Enter the cutoff frequency(descrete frequency) wc =50

 Choice for the filter
 Enter 'y' for High Pass Filter
 Press key 'l' for Low Pass Filter = Y

 Enter the Window you want to use
 rectangle window (Enter 1)
 triangular(bartlett) window (Enter 2)
 hamming window (Enter 3)
 hanning window (Enter 4)
 choice =1

 coefficient of lowpass FIR filter are as follows...
 h0=-0.082257
 h1=-0.084224
 h2=-0.084224
 h3=-0.082257 

No comments:

Post a Comment