bpmprocess/freq_to_sample.c

Go to the documentation of this file.
00001 
00005 #include <bpm/bpm_messages.h>
00006 #include <bpm/bpm_process.h>
00007 
00008 
00009 /*
00010   This routine returns the sample number corresponding to the frequency, note
00011   that this routine is not aware of the nyquist bands, and just keeps on 
00012   counting from 0 -> fs. 
00013   
00014   @param  fs       sampling frequency
00015   @param  ns       number of samples
00016   @param  tsample  the queried frequency of the sample
00017   @param  isample  the returned sample number
00018   
00019   @return BPM_SUCCESS upon success, BPM_FAILURE upon failure
00020 */
00021 int freq_to_sample( double fs, int ns, double f, int *iS ) {
00022 
00023   double fi;
00024   int i;
00025 
00026   if ( f < 0. ) {
00027     *iS = 0;
00028     return BPM_SUCCESS;
00029   }
00030   
00031   for ( i=0; i<ns; i++ ) {
00032     // get the frequency corrsponding to sample i
00033     fi = ( (double) i + 0.5 ) / ( (double) ns ) * fs;
00034     if ( ABS( f - fi ) <  ( fs / (double) ns ) ) break;
00035   }
00036 
00037   *iS = i;
00038 
00039   
00040   return BPM_SUCCESS;
00041 }

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