bpmrf/rf_butterworthlowpass.c

Go to the documentation of this file.
00001 
00005 #include <bpm/bpm_interface.h>
00006 #include <bpm/bpm_rf.h>
00007 #include <bpm/bpm_alloc.h>
00008 #include <bpm/bpm_nr.h>
00009 
00024 int rf_butterworthlowpass( double **RF, int order, double fc ) {
00025 
00026 
00027   int i = 0;
00028   double f;
00029 
00030   if ( ! RF ) {
00031     bpm_error( "Invalid RF pointer in rf_butterworthlowpass(...)",
00032                __FILE__, __LINE__ );
00033     return BPM_FAILURE;
00034   }
00035 
00036   if ( fc <= 0 ) {
00037     bpm_error( "Cut off frequency =< 0 in rf_butterworthlowpass(...)",
00038                __FILE__, __LINE__);
00039     return BPM_FAILURE;
00040   }
00041 
00042   if ( order <= 0 ) {
00043     bpm_error( "Order of filter =< 0 in rf_butterworthlowpass(...)",
00044                __FILE__, __LINE__);
00045     return BPM_FAILURE;
00046   }
00047 
00048   // Allocate space for the FFT
00049   double **FFT = alloc_complex_wave_double( rf_nsamples );
00050 
00051   // perform FFT
00052   rf_complexFFT( RF, FFT, NR_FFTFORWARD );
00053   
00054   // apply filter
00055   for( i = 0; i < rf_nsamples; i++ ) {
00056 
00057     // calculate the frequency for the sample...
00058     // translate the 2nd nyquist band to zero frequency to get
00059     // mirror image, so automatically filtering twice...
00060     f = rf_samplefreq * 1.e6 / rf_nsamples * ( i + 0.5 );
00061     if ( f > rf_samplefreq * 1.e6 / 2. ) f -= rf_samplefreq * 1.e6; 
00062     FFT[i][Re] *= 1. / (  1. + pow( f / ( fc * 1.e6 ), 2.* order ) );
00063     FFT[i][Im] *= 1. / (  1. + pow( f / ( fc * 1.e6 ), 2.* order ) );
00064   }  
00065 
00066   // backward FFT
00067   rf_complexFFT( FFT, RF, NR_FFTBACKWARD );
00068 
00069   // Apply weighting factor after fft
00070   for( i = 0; i < rf_nsamples; i++ ) {
00071     RF[i][Re] *= 2. / (double) rf_nsamples;
00072     RF[i][Im] *= 2. / (double) rf_nsamples;
00073   }
00074 
00075   // Clean up
00076   free_complex_wave_double( FFT, rf_nsamples );
00077 
00078   return BPM_SUCCESS;
00079 }
00080 // end of file

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