int main( int argc, char *argv[] ) { const UINT4 n = 65536; const REAL4 dt = 1.0 / 16384.0; static LALStatus status; static REAL4TimeSeries x; static COMPLEX8FrequencySeries X; static REAL4TimeSeries y; static REAL4FrequencySeries Y; static COMPLEX8TimeSeries z; static COMPLEX8FrequencySeries Z; RealFFTPlan *fwdRealPlan = NULL; RealFFTPlan *revRealPlan = NULL; ComplexFFTPlan *fwdComplexPlan = NULL; ComplexFFTPlan *revComplexPlan = NULL; RandomParams *randpar = NULL; AverageSpectrumParams avgSpecParams; UINT4 srate[] = { 4096, 9000 }; UINT4 npts[] = { 262144, 1048576 }; REAL4 var[] = { 5, 16 }; UINT4 j, sr, np, vr; /*CHAR fname[2048];*/ ParseOptions( argc, argv ); LALSCreateVector( &status, &x.data, n ); TestStatus( &status, CODES( 0 ), 1 ); LALCCreateVector( &status, &X.data, n / 2 + 1 ); TestStatus( &status, CODES( 0 ), 1 ); LALCCreateVector( &status, &z.data, n ); TestStatus( &status, CODES( 0 ), 1 ); LALCCreateVector( &status, &Z.data, n ); TestStatus( &status, CODES( 0 ), 1 ); LALCreateForwardRealFFTPlan( &status, &fwdRealPlan, n, 0 ); TestStatus( &status, CODES( 0 ), 1 ); LALCreateReverseRealFFTPlan( &status, &revRealPlan, n, 0 ); TestStatus( &status, CODES( 0 ), 1 ); LALCreateForwardComplexFFTPlan( &status, &fwdComplexPlan, n, 0 ); TestStatus( &status, CODES( 0 ), 1 ); LALCreateReverseComplexFFTPlan( &status, &revComplexPlan, n, 0 ); TestStatus( &status, CODES( 0 ), 1 ); randpar = XLALCreateRandomParams( 100 ); /* * * Try the real transform. * */ x.f0 = 0; x.deltaT = dt; x.sampleUnits = lalMeterUnit; snprintf( x.name, sizeof( x.name ), "x" ); XLALNormalDeviates( x.data, randpar ); for ( j = 0; j < n; ++j ) /* add a 60 Hz line */ { REAL4 t = j * dt; x.data->data[j] += 0.1 * cos( LAL_TWOPI * 60.0 * t ); } LALSPrintTimeSeries( &x, "x.out" ); snprintf( X.name, sizeof( X.name ), "X" ); LALTimeFreqRealFFT( &status, &X, &x, fwdRealPlan ); TestStatus( &status, CODES( 0 ), 1 ); LALCPrintFrequencySeries( &X, "X.out" ); LALFreqTimeRealFFT( &status, &x, &X, revRealPlan ); TestStatus( &status, CODES( 0 ), 1 ); LALSPrintTimeSeries( &x, "xx.out" ); /* * * Try the average power spectum. * */ avgSpecParams.method = useMean; for ( np = 0; np < XLAL_NUM_ELEM(npts) ; ++np ) { /* length of time series for 7 segments, overlapped by 1/2 segment */ UINT4 tsLength = npts[np] * 7 - 6 * npts[np] / 2; LALCreateVector( &status, &y.data, tsLength ); TestStatus( &status, CODES( 0 ), 1 ); LALCreateVector( &status, &Y.data, npts[np] / 2 + 1 ); TestStatus( &status, CODES( 0 ), 1 ); avgSpecParams.overlap = npts[np] / 2; /* create the window */ avgSpecParams.window = XLALCreateHannREAL4Window(npts[np]); avgSpecParams.plan = NULL; LALCreateForwardRealFFTPlan( &status, &avgSpecParams.plan, npts[np], 0 ); TestStatus( &status, CODES( 0 ), 1 ); for ( sr = 0; sr < XLAL_NUM_ELEM(srate) ; ++sr ) { /* set the sample rate of the time series */ y.deltaT = 1.0 / (REAL8) srate[sr]; for ( vr = 0; vr < XLAL_NUM_ELEM(var) ; ++vr ) { REAL4 eps = 1e-6; /* very conservative fp precision */ REAL4 Sfk = 2.0 * var[vr] * var[vr] * y.deltaT; REAL4 sfk = 0; REAL4 lbn; REAL4 sig; REAL4 ssq; REAL4 tol; /* create the data */ XLALNormalDeviates( y.data, randpar ); ssq = 0; for ( j = 0; j < y.data->length; ++j ) { y.data->data[j] *= var[vr]; ssq += y.data->data[j] * y.data->data[j]; } /* compute tolerance for comparison */ lbn = log( y.data->length ) / log( 2 ); sig = sqrt( 2.5 * lbn * eps * eps * ssq / y.data->length ); tol = 5 * sig; /* compute the psd and find the average */ LALREAL4AverageSpectrum( &status, &Y, &y, &avgSpecParams ); TestStatus( &status, CODES( 0 ), 1 ); LALSMoment( &status, &sfk, Y.data, 1 ); TestStatus( &status, CODES( 0 ), 1 ); /* check the result */ if ( fabs(Sfk-sfk) > tol ) { fprintf( stderr, "FAIL: PSD estimate appears incorrect\n"); fprintf( stderr, "expected %e, got %e ", Sfk, sfk ); fprintf( stderr, "(difference = %e, tolerance = %e)\n", fabs(Sfk-sfk), tol ); exit(2); } } } /* destroy structures that need to be resized */ LALDestroyRealFFTPlan( &status, &avgSpecParams.plan ); TestStatus( &status, CODES( 0 ), 1 ); XLALDestroyREAL4Window( avgSpecParams.window ); LALDestroyVector( &status, &y.data ); TestStatus( &status, CODES( 0 ), 1 ); LALDestroyVector( &status, &Y.data ); TestStatus( &status, CODES( 0 ), 1 ); } /* * * Try the complex transform. * */ z.f0 = 0; z.deltaT = dt; z.sampleUnits = lalVoltUnit; snprintf( z.name, sizeof( z.name ), "z" ); { /* dirty hack */ REAL4Vector tmp; tmp.length = 2 * z.data->length; tmp.data = (REAL4 *)z.data->data; XLALNormalDeviates( &tmp, randpar ); } for ( j = 0; j < n; ++j ) /* add a 50 Hz line and a 500 Hz ringdown */ { REAL4 t = j * dt; z.data->data[j] += 0.2 * cos( LAL_TWOPI * 50.0 * t ); z.data->data[j] += I * exp( -t ) * sin( LAL_TWOPI * 500.0 * t ); } LALCPrintTimeSeries( &z, "z.out" ); TestStatus( &status, CODES( 0 ), 1 ); snprintf( Z.name, sizeof( Z.name ), "Z" ); LALTimeFreqComplexFFT( &status, &Z, &z, fwdComplexPlan ); TestStatus( &status, CODES( 0 ), 1 ); LALCPrintFrequencySeries( &Z, "Z.out" ); LALFreqTimeComplexFFT( &status, &z, &Z, revComplexPlan ); TestStatus( &status, CODES( 0 ), 1 ); LALCPrintTimeSeries( &z, "zz.out" ); XLALDestroyRandomParams( randpar ); LALDestroyRealFFTPlan( &status, &fwdRealPlan ); TestStatus( &status, CODES( 0 ), 1 ); LALDestroyRealFFTPlan( &status, &revRealPlan ); TestStatus( &status, CODES( 0 ), 1 ); LALDestroyComplexFFTPlan( &status, &fwdComplexPlan ); TestStatus( &status, CODES( 0 ), 1 ); LALDestroyComplexFFTPlan( &status, &revComplexPlan ); TestStatus( &status, CODES( 0 ), 1 ); LALCDestroyVector( &status, &Z.data ); TestStatus( &status, CODES( 0 ), 1 ); LALCDestroyVector( &status, &z.data ); TestStatus( &status, CODES( 0 ), 1 ); LALCDestroyVector( &status, &X.data ); TestStatus( &status, CODES( 0 ), 1 ); LALSDestroyVector( &status, &x.data ); TestStatus( &status, CODES( 0 ), 1 ); LALCheckMemoryLeaks(); return 0; }
int main( void ) { const UINT4 n = 65536; const UINT4 m = 8; static AverageSpectrumParams specpar; static REAL4FrequencySeries fseries; static REAL4TimeSeries tseries; static LALStatus status; static RandomParams *randpar; REAL8 ave; UINT4 i; /* allocate memory for time and frequency series */ tseries.deltaT = 1; LALCreateVector( &status, &tseries.data, n * m ); TESTSTATUS( &status ); LALCreateVector( &status, &fseries.data, n / 2 + 1 ); TESTSTATUS( &status ); /* set time series data to be a unit impulse */ /* memset( tseries.data->data, 0, tseries.data->length * sizeof( *tseries.data->data ) ); tseries.data->data[0] = 1; */ randpar = XLALCreateRandomParams( 1 ); XLALNormalDeviates( tseries.data, randpar ); XLALDestroyRandomParams( randpar ); /* prepare average spectrum parameters */ specpar.method = useMedian; specpar.overlap = n / 2; /* specpar.overlap = 0; */ LALCreateForwardRealFFTPlan( &status, &specpar.plan, n, 0 ); TESTSTATUS( &status ); specpar.window = XLALCreateWelchREAL4Window(n); /* compute spectrum */ LALREAL4AverageSpectrum( &status, &fseries, &tseries, &specpar ); TESTSTATUS( &status ); /* output results -- omit DC & Nyquist */ /* for ( i = 1; i < fseries.data->length - 1; ++i ) fprintf( stdout, "%e\t%e\n", i * fseries.deltaF, fseries.data->data[i] ); */ /* average values of power spectrum (omit DC & Nyquist ) */ ave = 0; for ( i = 1; i < fseries.data->length - 1; ++i ) ave += fseries.data->data[i]; ave /= fseries.data->length - 2; fprintf( stdout, "median:\t%e\terror:\t%f%%\n", ave, fabs( ave - 2.0 ) / 0.02 ); /* now do the same for mean */ specpar.method = useMean; LALREAL4AverageSpectrum( &status, &fseries, &tseries, &specpar ); TESTSTATUS( &status ); /* average values of power spectrum (omit DC & Nyquist ) */ ave = 0; for ( i = 1; i < fseries.data->length - 1; ++i ) ave += fseries.data->data[i]; ave /= fseries.data->length - 2; fprintf( stdout, "mean:\t%e\terror:\t%f%%\n", ave, fabs( ave - 2.0 ) / 0.02 ); /* cleanup */ XLALDestroyREAL4Window( specpar.window ); LALDestroyRealFFTPlan( &status, &specpar.plan ); TESTSTATUS( &status ); LALDestroyVector( &status, &fseries.data ); TESTSTATUS( &status ); LALDestroyVector( &status, &tseries.data ); TESTSTATUS( &status ); /* exit */ LALCheckMemoryLeaks(); return 0; }
int main( void ) { UINT4 resamplefac = 4; UINT4 seglen = 64 * 1024; /* after resampling */ UINT4 numovrlpseg = 10; /* after resampling */ UINT4 stride = seglen / 2; /* after resampling */ UINT4 reclen = numovrlpseg * stride + stride; /* after resampling */ /* UINT4 numovrlpseg = 8; */ /* after resampling */ /* UINT4 stride = seglen; */ /* after resampling */ /* UINT4 reclen = numovrlpseg * stride; */ /* after resampling */ static LALStatus status; static AverageSpectrumParams specpar; static REAL4FrequencySeries fseries; static REAL4TimeSeries tseries; static RandomParams *randpar; static ResampleTSParams resamplepar; static PassBandParamStruc highpasspar; REAL8 avg; UINT4 j; UINT4 k; FILE *fp; /* allocate memory for time and frequency series */ tseries.deltaT = 0.1; LALCreateVector( &status, &tseries.data, reclen * resamplefac ); TESTSTATUS( &status ); LALCreateVector( &status, &fseries.data, seglen / 2 + 1 ); TESTSTATUS( &status ); for ( j = 0; j < tseries.data->length; ++j ) tseries.data->data[j] = sin( 0.1 * j ); /* memset( tseries.data->data, 0, tseries.data->length * sizeof( *tseries.data->data ) ); tseries.data->data[seglen/2-1] = 1; tseries.data->data[seglen/2] = 1; tseries.data->data[seglen/2+1] = 1; */ /* create white Gaussian noise */ LALCreateRandomParams( &status, &randpar, 0 ); TESTSTATUS( &status ); LALNormalDeviates( &status, tseries.data, randpar ); TESTSTATUS( &status ); LALDestroyRandomParams( &status, &randpar ); TESTSTATUS( &status ); /* resample */ resamplepar.deltaT = resamplefac * tseries.deltaT; resamplepar.filterType = defaultButterworth; LALResampleREAL4TimeSeries( &status, &tseries, &resamplepar ); TESTSTATUS( &status ); /* do some simple colouring: high-pass filter */ highpasspar.nMax = 4; highpasspar.f1 = -1; highpasspar.a1 = -1; highpasspar.f2 = 0.1 / tseries.deltaT; /* ~20% of Nyquist */ highpasspar.a2 = 0.9; /* this means 10% attenuation at f2 */ LALDButterworthREAL4TimeSeries( &status, &tseries, &highpasspar ); TESTSTATUS( &status ); specpar.method = useMean; specpar.overlap = seglen - stride; LALCreateForwardRealFFTPlan( &status, &specpar.plan, seglen, 0 ); TESTSTATUS( &status ); specpar.window = XLALCreateRectangularREAL4Window(seglen); /* compute spectrum */ LALREAL4AverageSpectrum( &status, &fseries, &tseries, &specpar ); TESTSTATUS( &status ); /* output results */ fp = fopen( "out1.dat", "w" ); for ( k = 0; k < fseries.data->length; ++k ) fprintf( fp, "%e\t%e\n", k * fseries.deltaF, fseries.data->data[k] ); fclose( fp ); /* compute average */ avg = 0; for ( k = 1; k < fseries.data->length - 1; ++k ) avg += fseries.data->data[k]; avg /= ( fseries.data->length - 2 ); printf( "lal mean:\t%g\n", avg ); /* use the xlal function */ XLALREAL4AverageSpectrumWelch( &fseries, &tseries, seglen, stride, specpar.window, specpar.plan ); if ( xlalErrno ) { XLAL_PERROR(); exit( 1 ); } /* output results */ fp = fopen( "out2.dat", "w" ); for ( k = 0; k < fseries.data->length; ++k ) fprintf( fp, "%e\t%e\n", k * fseries.deltaF, fseries.data->data[k] ); fclose( fp ); /* compute average */ avg = 0; for ( k = 1; k < fseries.data->length - 1; ++k ) avg += fseries.data->data[k]; avg /= ( fseries.data->length - 2 ); printf( "xlal mean:\t%g\n", avg ); /* median-mean method */ XLALREAL4AverageSpectrumMedianMean( &fseries, &tseries, seglen, stride, specpar.window, specpar.plan ); if ( xlalErrno ) { XLAL_PERROR(); exit( 1 ); } /* output results */ fp = fopen( "out3.dat", "w" ); for ( k = 0; k < fseries.data->length; ++k ) fprintf( fp, "%e\t%e\n", k * fseries.deltaF, fseries.data->data[k] ); fclose( fp ); /* compute average */ avg = 0; for ( k = 1; k < fseries.data->length - 1; ++k ) avg += fseries.data->data[k]; avg /= ( fseries.data->length - 2 ); printf( "med mean:\t%g\n", avg ); /* median method */ XLALREAL4AverageSpectrumMedian( &fseries, &tseries, seglen, stride, specpar.window, specpar.plan ); if ( xlalErrno ) { XLAL_PERROR(); exit( 1 ); } /* output results */ fp = fopen( "out4.dat", "w" ); for ( k = 0; k < fseries.data->length; ++k ) fprintf( fp, "%e\t%e\n", k * fseries.deltaF, fseries.data->data[k] ); fclose( fp ); /* compute average */ avg = 0; for ( k = 1; k < fseries.data->length - 1; ++k ) avg += fseries.data->data[k]; avg /= ( fseries.data->length - 2 ); printf( "median:\t\t%g\n", avg ); /* cleanup */ XLALDestroyREAL4Window( specpar.window ); LALDestroyRealFFTPlan( &status, &specpar.plan ); TESTSTATUS( &status ); LALDestroyVector( &status, &fseries.data ); TESTSTATUS( &status ); LALDestroyVector( &status, &tseries.data ); TESTSTATUS( &status ); /* exit */ LALCheckMemoryLeaks(); return 0; }