bpmdsp/create_filter.c

Go to the documentation of this file.
00001 
00005 #include <string.h>
00006 
00007 #include "bpm/bpm_alloc.h"
00008 #include "bpm/bpm_dsp.h"
00009 
00010 filter_t* create_filter( char name[], unsigned int options, int order, int ns,
00011                          double fs, double f1, double f2, double par ) {
00012   
00013   filterrep_t *s;
00014   int n = 0;
00015 
00016   filter_t *f = (filter_t*) calloc( 1, sizeof(filter_t) );
00017   if ( ! f ) {
00018     bpm_error( "Couldn't reserve memory for filter", __FILE__, __LINE__ );
00019     return NULL;
00020   }
00021 
00022   strncpy( f->name, name, 79 );
00023   f->options = options;
00024   f->order   = order;
00025 
00026   f->ns      = ns;
00027   f->fs      = fs;
00028   f->f1      = f1;
00029   f->f2      = f2;
00030 
00031   f->cheb_ripple = 0.;
00032   f->Q           = -1.;  // initialise to infinite Q, so pure oscillator for resonator filter
00033 
00034   if ( f->options & CHEBYSHEV ) {
00035     if ( par <  0. ) {
00036       f->cheb_ripple = par;
00037     } else {
00038       bpm_warning( "Invalid Chebyshev ripple, setting default !", __FILE__, __LINE__ );
00039     }
00040   }
00041 
00042   if ( f->options & RESONATOR ) {
00043     if ( par > 0. ) {
00044       f->Q = par;
00045     } else {
00046       bpm_warning( "Q factor <= 0, assuming pure oscillator !", __FILE__, __LINE__ );
00047     }
00048   }
00049 
00050 
00051   if ( f->fs > 0. ) {
00052     f->alpha1   = f->f1 / f->fs;
00053     f->alpha2   = f->f2 / f->fs;
00054 
00055     if ( f->options & NO_PREWARP ) {
00056       f->w_alpha1 = f->alpha1;  
00057       f->w_alpha2 = f->alpha2;  
00058     } else {
00059       f->w_alpha1 = tan( PI * f->alpha1 ) / PI;  // pre-warped alpha1
00060       f->w_alpha2 = tan( PI * f->alpha2 ) / PI;  // pre-warped alpha2
00061     }
00062   } else {
00063     bpm_error( "Invalid sampling frequency in create_filter(...)", __FILE__, __LINE__ );
00064     free(f);
00065     return NULL;
00066   }
00067 
00068   // setup the waveform buffer
00069   f->wfbuffer = alloc_simple_wave_double( ns );
00070   if ( ! f->wfbuffer ) {
00071     bpm_error( "Cannot allocate memory for waveform buffer in create_filter()",
00072                __FILE__, __LINE__ );
00073     free(f);
00074     return NULL;
00075   }
00076    
00077   // now layout the poles and zeros depending on the type of filter..
00078   if ( f->options & ( BUTTERWORTH | BESSEL | CHEBYSHEV ) ) {
00079     // calculate s plane represenation and transform to z plane for
00080     // butterworth, cheybyshev and bessel filters
00081     s = create_splane_representation( f );
00082     normalise_filter( f, s );
00083     f->cplane = zplane_transform( f, s );
00084     free(s);
00085   } 
00086 
00087   if ( f->options & RESONATOR ) {
00088     // set some values for the resonator
00089     f->alpha2   = f->alpha1;  // needed when we calculate fc_gain
00090     f->w_alpha2 = f->w_alpha1;
00091 
00092     // Directly create z pole representation for the resonator case
00093     f->cplane = create_resonator_representation( f );
00094   }
00095 
00096   // print poles and zeros
00097   // print_filter_representation( stdout, f->cplane );
00098 
00099 
00100   // calculate filter coefficients...
00101   calculate_filter_coefficients( f );
00102   
00103   // set whether filter is FIR/IIR
00104   while (n < f->cplane->npoles && f->yc[n] == 0.0) n++;
00105   if ( n >= f->cplane->npoles ) { f->IsFIR = 1; } else { f->IsFIR = 0; }
00106 
00107 
00108   return f;
00109 }

Generated on Fri Nov 9 21:17:10 2007 for libbpm by  doxygen 1.5.1