home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 308_01 / coeff.c < prev    next >
Text File  |  1990-06-17  |  2KB  |  84 lines

  1. /* File coeff.c
  2. /* Estimates required number of FIR filter coefficients
  3. /* Based on formula in Rabiner & Gold, p. 156
  4. /* Passband ripple entered in dB instead of %
  5. /* where dB = 20*log10(1+%)
  6. /* Adapted by Bob Briggs
  7. /* 1 December 1989
  8.  */
  9.  
  10.  #include "stdio.h"
  11.  #include "math.h"
  12.  
  13.  main(){
  14.    char c;
  15.    int r;
  16.    double a1,a2,a3,a4,a5,a6,d0,d1,d2,d3,d4,f,f1,n;
  17.    double fpass,fsamp,fstop;
  18.  
  19.    a1 = 0.005309;
  20.    a2 = 0.07114;
  21.    a3 = -0.4761;
  22.    a4 = -0.00266;
  23.    a5 = -0.5941;
  24.    a6 = -0.4278;
  25.  
  26.    putchar('\n');
  27.    printf("******* FIR filter coefficient number estimator *******\n");
  28.    while(1){
  29.       putchar('\n');
  30.       printf("Enter passband ripple (in dB)\n");
  31.       scanf("%lf",&d1);
  32.       if(d1 <= 0.0){printf("Error: ripple must be > zero dB\n"); continue;}
  33.       printf("Enter stopband attenuation (in dB)\n");
  34.       scanf("%lf",&d2);
  35.       printf("Enter transition width (normalized Hz, Nyquist = 0 to 0.5 Hz)\n");
  36.         putchar('\n');
  37.         printf("     [To enter frequencies instead, enter a negative number.\n");
  38.         printf("         Program will prompt for frequencies and\n");
  39.         printf("         calculate transition width as:\n");
  40.         printf("         tw = (fstop - fpass)/fsamp ]\n");
  41.       scanf("%lf",&f);
  42.  
  43.       if(f<0){
  44.           printf("Enter stopband edge frequency, fstop\n");
  45.           scanf("%lf",&fstop);
  46.           printf("Enter passband edge frequency, fpass\n");
  47.           scanf("%lf",&fpass);
  48.           printf("Enter sampling frequency, fsamp\n");
  49.           scanf("%lf",&fsamp);
  50.       }
  51.  
  52.       putchar('\n');
  53.       printf("Ripple = %g dB\n",d1);
  54.       printf("Stopband attenuation = %g dB\n",d2);
  55.       if(f<0){
  56.          printf("fstop = %g\n",fstop);
  57.           printf("fpass = %g\n",fpass);
  58.           printf("fsamp = %g\n",fsamp);
  59.             printf("Transition width = %g\n",(fstop-fpass)/fsamp);
  60.       }
  61.       else printf("Transition width = %g (normalized Hz)\n",f);
  62.  
  63.       printf("Enter N or n to change data, RETURN if OK\n");
  64.       while((c = getchar()) != '\n');  /* flush buffer */
  65.       c = getchar();
  66.       if(c != 'n' && c != 'N') break;
  67.    }
  68.  
  69.    if(f<0) f = (fstop - fpass)/fsamp;
  70.    d1 /= 20;
  71.    d1 = pow(10,d1);
  72.    d1 = (d1-1)/(d1+1);
  73.    d3 = log10(d1);
  74.    d2 /= -20;
  75.    f1 = 11.01217 + 0.51244*(d3 - d2);
  76.    d4 = d3*d3;
  77.    d0 = a1*d4 + a2*d3 +a3;
  78.    d0 *= d2;
  79.    d0 += a4*d4 + a5*d3 + a6;
  80.    n = d0/f - f1*f + 1;
  81.    r = (int)(n + 0.51);
  82.    printf("The required number of coefficients is %d\n",r);
  83. }
  84.