AverageSpectrumTest.c

Go to the documentation of this file.
00001 /*
00002 *  Copyright (C) 2007 Bernd Machenschalk, Duncan Brown, Jolien Creighton
00003 *
00004 *  This program is free software; you can redistribute it and/or modify
00005 *  it under the terms of the GNU General Public License as published by
00006 *  the Free Software Foundation; either version 2 of the License, or
00007 *  (at your option) any later version.
00008 *
00009 *  This program is distributed in the hope that it will be useful,
00010 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 *  GNU General Public License for more details.
00013 *
00014 *  You should have received a copy of the GNU General Public License
00015 *  along with with program; see the file COPYING. If not, write to the
00016 *  Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00017 *  MA  02111-1307  USA
00018 */
00019 
00020 #include <math.h>
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <lal/LALStdlib.h>
00024 #include <lal/AVFactories.h>
00025 #include <lal/TimeFreqFFT.h>
00026 #include <lal/RealFFT.h>
00027 #include <lal/Window.h>
00028 #include <lal/Random.h>
00029 
00030 #include <lal/LALRCSID.h>
00031 NRCSID (AVERAGESPECTRUMTESTC,"$Id: AverageSpectrumTest.c,v 1.6 2008/07/08 22:13:24 kipp Exp $");
00032 
00033 #define TESTSTATUS( s ) \
00034   if ( (s)->statusCode ) { REPORTSTATUS( s ); exit( 1 ); } else \
00035 ((void)0)
00036 
00037 int main( void )
00038 {
00039   const UINT4 n = 65536;
00040   const UINT4 m = 8;
00041   static AverageSpectrumParams specpar;
00042   static REAL4FrequencySeries fseries;
00043   static REAL4TimeSeries tseries;
00044   static LALStatus status;
00045   static RandomParams *randpar;
00046   REAL8 ave;
00047   UINT4 i;
00048 
00049   lalDebugLevel = 3;
00050 
00051   /* allocate memory for time and frequency series */
00052   tseries.deltaT = 1;
00053   LALCreateVector( &status, &tseries.data, n * m );
00054   TESTSTATUS( &status );
00055   LALCreateVector( &status, &fseries.data, n / 2 + 1 );
00056   TESTSTATUS( &status );
00057 
00058   /* set time series data to be a unit impulse */
00059   /*
00060   memset( tseries.data->data, 0, tseries.data->length * sizeof( 
00061         *tseries.data->data ) );
00062   tseries.data->data[0] = 1;
00063   */
00064   LALCreateRandomParams( &status, &randpar, 1 );
00065   TESTSTATUS( &status );
00066   LALNormalDeviates( &status, tseries.data, randpar );
00067   TESTSTATUS( &status );
00068   LALDestroyRandomParams( &status, &randpar );
00069   TESTSTATUS( &status );
00070 
00071   /* prepare average spectrum parameters */
00072   specpar.method  = useMedian;
00073   specpar.overlap = n / 2;
00074   /* specpar.overlap = 0; */
00075   LALCreateForwardRealFFTPlan( &status, &specpar.plan, n, 0 );
00076   TESTSTATUS( &status );
00077   specpar.window = XLALCreateWelchREAL4Window(n);
00078 
00079   /* compute spectrum */
00080   LALREAL4AverageSpectrum( &status, &fseries, &tseries, &specpar );
00081   TESTSTATUS( &status );
00082 
00083   /* output results -- omit DC & Nyquist */
00084   /*
00085   for ( i = 1; i < fseries.data->length - 1; ++i )
00086     fprintf( stdout, "%e\t%e\n", i * fseries.deltaF, 
00087         fseries.data->data[i] );
00088    */
00089 
00090   /* average values of power spectrum (omit DC & Nyquist ) */
00091   ave = 0;
00092   for ( i = 1; i < fseries.data->length - 1; ++i )
00093     ave += fseries.data->data[i];
00094   ave /= fseries.data->length - 2;
00095   fprintf( stdout, "median:\t%e\terror:\t%f%%\n", ave, fabs( ave - 2.0 ) / 0.02 );
00096 
00097   /* now do the same for mean */
00098   specpar.method  = useMean;
00099   LALREAL4AverageSpectrum( &status, &fseries, &tseries, &specpar );
00100   TESTSTATUS( &status );
00101   /* average values of power spectrum (omit DC & Nyquist ) */
00102   ave = 0;
00103   for ( i = 1; i < fseries.data->length - 1; ++i )
00104     ave += fseries.data->data[i];
00105   ave /= fseries.data->length - 2;
00106   fprintf( stdout, "mean:\t%e\terror:\t%f%%\n", ave, fabs( ave - 2.0 ) / 0.02 );
00107 
00108 
00109   /* cleanup */
00110   XLALDestroyREAL4Window( specpar.window );
00111   LALDestroyRealFFTPlan( &status, &specpar.plan );
00112   TESTSTATUS( &status );
00113   LALDestroyVector( &status, &fseries.data );
00114   TESTSTATUS( &status );
00115   LALDestroyVector( &status, &tseries.data );
00116   TESTSTATUS( &status );
00117 
00118   /* exit */
00119   LALCheckMemoryLeaks();
00120   return 0;
00121 }

Generated on Wed Jul 23 03:16:43 2008 for LAL by  doxygen 1.5.2