AvgSpecTest.c

Go to the documentation of this file.
00001 /*
00002 *  Copyright (C) 2007 Bernd Machenschalk, 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 <lal/LALStdlib.h>
00023 #include <lal/AVFactories.h>
00024 #include <lal/Units.h>
00025 #include <lal/TimeFreqFFT.h>
00026 #include <lal/Window.h>
00027 #include <lal/Random.h>
00028 #include <lal/ResampleTimeSeries.h>
00029 
00030 #include <lal/LALRCSID.h>
00031 NRCSID (AVGSPECTESTC,"$Id: AvgSpecTest.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 ((void)0)
00035 
00036 int main( void )
00037 {
00038   UINT4 resamplefac = 4;
00039   UINT4 seglen = 64 * 1024; /* after resampling */
00040   UINT4 numovrlpseg = 10; /* after resampling */
00041   UINT4 stride = seglen / 2; /* after resampling */
00042   UINT4 reclen = numovrlpseg * stride + stride; /* after resampling */
00043   /* UINT4 numovrlpseg = 8; */ /* after resampling */
00044   /* UINT4 stride = seglen; */ /* after resampling */
00045   /* UINT4 reclen = numovrlpseg * stride; */ /* after resampling */
00046 
00047   static LALStatus status;
00048 
00049   static AverageSpectrumParams  specpar;
00050   static REAL4FrequencySeries   fseries;
00051   static REAL4TimeSeries        tseries;
00052   static RandomParams          *randpar;
00053   static ResampleTSParams       resamplepar;
00054   static PassBandParamStruc     highpasspar;
00055   REAL8 avg;
00056   UINT4 j;
00057   UINT4 k;
00058   FILE *fp;
00059 
00060   lalDebugLevel = 3;
00061 
00062   /* allocate memory for time and frequency series */
00063   tseries.deltaT = 0.1;
00064   LALCreateVector( &status, &tseries.data, reclen * resamplefac );
00065   TESTSTATUS( &status );
00066   LALCreateVector( &status, &fseries.data, seglen / 2 + 1 );
00067   TESTSTATUS( &status );
00068 
00069   for ( j = 0; j < tseries.data->length; ++j )
00070     tseries.data->data[j] = sin( 0.1 * j );
00071   /*
00072   memset( tseries.data->data, 0,
00073       tseries.data->length * sizeof( *tseries.data->data ) );
00074   tseries.data->data[seglen/2-1] = 1;
00075   tseries.data->data[seglen/2] = 1;
00076   tseries.data->data[seglen/2+1] = 1;
00077   */
00078   /* create white Gaussian noise */
00079   LALCreateRandomParams( &status, &randpar, 0 );
00080   TESTSTATUS( &status );
00081   LALNormalDeviates( &status, tseries.data, randpar );
00082   TESTSTATUS( &status );
00083   LALDestroyRandomParams( &status, &randpar );
00084   TESTSTATUS( &status );
00085 
00086   /* resample */
00087   resamplepar.deltaT = resamplefac * tseries.deltaT;
00088   resamplepar.filterType = defaultButterworth;
00089   LALResampleREAL4TimeSeries( &status, &tseries, &resamplepar );
00090   TESTSTATUS( &status );
00091 
00092   /* do some simple colouring: high-pass filter */
00093   highpasspar.nMax = 4;
00094   highpasspar.f1   = -1;
00095   highpasspar.a1   = -1;
00096   highpasspar.f2   = 0.1 / tseries.deltaT; /* ~20% of Nyquist */
00097   highpasspar.a2   = 0.9; /* this means 10% attenuation at f2 */
00098   LALDButterworthREAL4TimeSeries( &status, &tseries, &highpasspar );
00099   TESTSTATUS( &status );
00100 
00101   specpar.method  = useMean;
00102   specpar.overlap = seglen - stride;
00103 
00104   LALCreateForwardRealFFTPlan( &status, &specpar.plan, seglen, 0 );
00105   TESTSTATUS( &status );
00106   specpar.window = XLALCreateRectangularREAL4Window(seglen);
00107 
00108   /* compute spectrum */
00109   LALREAL4AverageSpectrum( &status, &fseries, &tseries, &specpar );
00110   TESTSTATUS( &status );
00111 
00112   /* output results */
00113   fp = fopen( "out1.dat", "w" );
00114   for ( k = 0; k < fseries.data->length; ++k )
00115     fprintf( fp, "%e\t%e\n", k * fseries.deltaF, fseries.data->data[k] );
00116   fclose( fp );
00117 
00118   /* compute average */
00119   avg = 0;
00120   for ( k = 1; k < fseries.data->length - 1; ++k )
00121     avg += fseries.data->data[k];
00122   avg /= ( fseries.data->length - 2 );
00123   printf( "lal mean:\t%g\n", avg );
00124 
00125   /* use the xlal function */
00126   XLALREAL4AverageSpectrumWelch( &fseries, &tseries, seglen, stride,
00127       specpar.window, specpar.plan );
00128   if ( xlalErrno )
00129   {
00130     XLAL_PERROR( "main" );
00131     exit( 1 );
00132   }
00133 
00134   /* output results */
00135   fp = fopen( "out2.dat", "w" );
00136   for ( k = 0; k < fseries.data->length; ++k )
00137     fprintf( fp, "%e\t%e\n", k * fseries.deltaF, fseries.data->data[k] );
00138   fclose( fp );
00139 
00140   /* compute average */
00141   avg = 0;
00142   for ( k = 1; k < fseries.data->length - 1; ++k )
00143     avg += fseries.data->data[k];
00144   avg /= ( fseries.data->length - 2 );
00145   printf( "xlal mean:\t%g\n", avg );
00146 
00147   /* median-mean method */
00148   XLALREAL4AverageSpectrumMedianMean( &fseries, &tseries, seglen, stride,
00149       specpar.window, specpar.plan );
00150   if ( xlalErrno )
00151   {
00152     XLAL_PERROR( "main" );
00153     exit( 1 );
00154   }
00155 
00156   /* output results */
00157   fp = fopen( "out3.dat", "w" );
00158   for ( k = 0; k < fseries.data->length; ++k )
00159     fprintf( fp, "%e\t%e\n", k * fseries.deltaF, fseries.data->data[k] );
00160   fclose( fp );
00161 
00162   /* compute average */
00163   avg = 0;
00164   for ( k = 1; k < fseries.data->length - 1; ++k )
00165     avg += fseries.data->data[k];
00166   avg /= ( fseries.data->length - 2 );
00167   printf( "med mean:\t%g\n", avg );
00168 
00169   /* median method */
00170   XLALREAL4AverageSpectrumMedian( &fseries, &tseries, seglen, stride,
00171       specpar.window, specpar.plan );
00172   if ( xlalErrno )
00173   {
00174     XLAL_PERROR( "main" );
00175     exit( 1 );
00176   }
00177 
00178   /* output results */
00179   fp = fopen( "out4.dat", "w" );
00180   for ( k = 0; k < fseries.data->length; ++k )
00181     fprintf( fp, "%e\t%e\n", k * fseries.deltaF, fseries.data->data[k] );
00182   fclose( fp );
00183 
00184   /* compute average */
00185   avg = 0;
00186   for ( k = 1; k < fseries.data->length - 1; ++k )
00187     avg += fseries.data->data[k];
00188   avg /= ( fseries.data->length - 2 );
00189   printf( "median:\t\t%g\n", avg );
00190 
00191   /* cleanup */
00192   XLALDestroyREAL4Window( specpar.window );
00193   LALDestroyRealFFTPlan( &status, &specpar.plan );
00194   TESTSTATUS( &status );
00195   LALDestroyVector( &status, &fseries.data );
00196   TESTSTATUS( &status );
00197   LALDestroyVector( &status, &tseries.data );
00198   TESTSTATUS( &status );
00199 
00200   /* exit */
00201   LALCheckMemoryLeaks();
00202   return 0;
00203 }

Generated on Thu Aug 28 03:11:56 2008 for LAL by  doxygen 1.5.2