bpmdsp/ddc.c

Go to the documentation of this file.
00001 
00007 #include "bpm/bpm_dsp.h"
00008 
00009 // the ddc buffers
00010 static doublewf_t *_ddc_buffer_real = NULL;
00011 static doublewf_t *_ddc_buffer_imag = NULL;
00012 
00013 int _check_ddc_buffers( int ns, double fs ) {
00014 
00015   if ( _ddc_buffer_real ) {
00016     if ( ( _ddc_buffer_real->ns != ns ) || ( ( _ddc_buffer_real->fs - fs ) > 1.0e-10 ) ) {
00017       bpm_warning( "Reallocating _ddc_buffer_real with different number of samples & fs!",
00018                    __FILE__, __LINE__ );
00019       doublewf_delete ( _ddc_buffer_real );
00020       _ddc_buffer_real = doublewf( ns, fs );
00021     }
00022   } else {
00023     bpm_warning( "Allocating DDC-Re buffer, no ddc_initialise() found", __FILE__, __LINE__ );
00024     _ddc_buffer_real = doublewf( ns, fs );
00025   }
00026 
00027   if ( _ddc_buffer_imag ) {
00028     if ( ( _ddc_buffer_imag->ns != ns ) || ( ( _ddc_buffer_imag->fs - fs ) > 1.0e-10 ) ) {
00029       bpm_warning( "Reallocating _ddc_buffer_imag with different number of samples & fs!",
00030                    __FILE__, __LINE__ );
00031       doublewf_delete ( _ddc_buffer_imag );
00032       _ddc_buffer_imag = doublewf( ns, fs );
00033     }
00034   } else {
00035     bpm_warning( "Allocating DDC-Im  buffer, no ddc_initialise() found", __FILE__, __LINE__ );
00036     _ddc_buffer_imag = doublewf( ns, fs );
00037   }
00038 
00039   if ( ! _ddc_buffer_real || ! _ddc_buffer_imag ) {
00040     bpm_error( "Cannot (re-)allocate memory for DDC buffers :(!", 
00041                __FILE__, __LINE__ );
00042     return BPM_FAILURE;
00043   }
00044 
00045   return BPM_SUCCESS;
00046 }
00047 
00048 // ----------------------------------------------------------------------------
00049 
00050 int ddc_initialise( int ns, double fs ) {
00051 
00052   if ( _ddc_buffer_real || _ddc_buffer_imag ) {
00053     bpm_error( "DDC buffers already existing, please cleanup first with ddc_cleanup() !", 
00054                __FILE__, __LINE__ );
00055     return BPM_FAILURE;
00056   }
00057 
00058   _ddc_buffer_real = doublewf( ns, fs );
00059   _ddc_buffer_imag = doublewf( ns, fs );
00060   if( ! _ddc_buffer_real || ! _ddc_buffer_imag ) {
00061     bpm_error( "Failed to allocate memory for DDC buffers", __FILE__, __LINE__ );
00062     return BPM_FAILURE;
00063   }
00064   
00065   return BPM_SUCCESS;
00066 }
00067 
00068 // ----------------------------------------------------------------------------
00069 
00070 void ddc_cleanup( void ) {
00071   if ( _ddc_buffer_real ) doublewf_delete( _ddc_buffer_real );
00072   if ( _ddc_buffer_imag ) doublewf_delete( _ddc_buffer_imag );
00073   return;
00074 }
00075 
00076 // ----------------------------------------------------------------------------
00077 
00078 int ddc( doublewf_t *w, double f, filter_t *filter, complexwf_t *dcw ) {
00079   
00080   int i = 0;
00081 
00082   if ( _check_ddc_buffers( dcw->ns, dcw->fs ) ) return BPM_FAILURE;
00083 
00084   // downmix entire waveform...
00085   for (i=0;i<w->ns;i++){
00086     _ddc_buffer_real->wf[i] = w->wf[i] * cos( 2.*PI*f * (double) i / w->fs );
00087     _ddc_buffer_imag->wf[i] = w->wf[i] * sin( 2.*PI*f * (double) i / w->fs );
00088   }
00089   
00090   // filter 2 omega component
00091   if( apply_filter( filter, _ddc_buffer_real->wf ) == BPM_FAILURE ) return BPM_FAILURE;
00092   if( apply_filter( filter, _ddc_buffer_imag->wf ) == BPM_FAILURE ) return BPM_FAILURE;
00093   
00094   // return complex waveform
00095   complexwf_setreal( dcw, _ddc_buffer_real );
00096   complexwf_setimag( dcw, _ddc_buffer_imag );
00097   
00098   return BPM_SUCCESS;
00099 }

Generated on Thu Jan 10 10:18:04 2008 for libbpm by  doxygen 1.5.1