00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #include <math.h>
00056
00057
00058
00059
00060 #include <lal/LALStdlib.h>
00061 #include <lal/FoldAmplitudes.h>
00062 #include <lal/LALConstants.h>
00063 #include <lal/AVFactories.h>
00064
00065
00066
00067 NRCSID( FOLDAMPLITUDESTESTC, "$Id: FoldAmplitudesTest.c,v 1.2 2007/06/08 14:41:52 bema Exp $" );
00068
00069
00070
00071
00072 #define FOLDAMPLITUDESTESTC_ENOM 0
00073 #define FOLDAMPLITUDESTESTC_ECHK 1
00074 #define FOLDAMPLITUDESTESTC_EFLS 2
00075
00076 #define FOLDAMPLITUDESTESTC_MSGENOM "Nominal exit"
00077 #define FOLDAMPLITUDESTESTC_MSGECHK "Error checking failed to catch bad data"
00078 #define FOLDAMPLITUDESTESTC_MSGEFLS "Incorrect answer for valid data"
00079
00080
00081
00082 #define FOLDAMPLITUDESTESTC_TOL 1.0e-3
00083 #define FOLDAMPLITUDESTESTC_LENGTH 10
00084 #define FOLDAMPLITUDESTESTC_BADLENGTH 1
00085 #define FOLDAMPLITUDESTESTC_TIMESTEP 0.01
00086 #define FOLDAMPLITUDESTESTC_NUMBINS 10
00087 #define FOLDAMPLITUDESTESTC_FREQ 33.2
00088 #define FOLDAMPLITUDESTESTC_FREQDOT 2.5
00089
00090
00091 int lalDebugLevel = LALMSGLVL3;
00092
00093
00094
00095
00096 int main( void )
00097 {
00098
00099
00100 static LALStatus status;
00101 REAL4Vector *output;
00102 FoldAmplitudesInput input;
00103 FoldAmplitudesInput badinput;
00104 FoldAmplitudesParams param;
00105
00106
00107 REAL4 f = FOLDAMPLITUDESTESTC_FREQ;
00108 REAL4 fDot = FOLDAMPLITUDESTESTC_FREQDOT;
00109 REAL4 delT = FOLDAMPLITUDESTESTC_TIMESTEP;
00110 REAL4 twoPi = (REAL4) LAL_TWOPI;
00111 REAL4 binRange;
00112 INT4 lengthAmpVec = FOLDAMPLITUDESTESTC_LENGTH;
00113 INT4 i;
00114 INT4 j;
00115 INT4 k;
00116 INT2 gotError = 0;
00117
00118
00119 input.phaseVec = NULL;
00120 input.amplitudeVec = NULL;
00121 badinput.phaseVec = NULL;
00122 badinput.amplitudeVec = NULL;
00123 output = NULL;
00124 LALSCreateVector( &status, &input.phaseVec, FOLDAMPLITUDESTESTC_LENGTH );
00125 LALSCreateVector( &status, &input.amplitudeVec, FOLDAMPLITUDESTESTC_LENGTH );
00126 LALSCreateVector( &status, &badinput.phaseVec, FOLDAMPLITUDESTESTC_BADLENGTH );
00127 LALSCreateVector( &status, &badinput.amplitudeVec, FOLDAMPLITUDESTESTC_LENGTH );
00128 LALSCreateVector( &status, &output, FOLDAMPLITUDESTESTC_NUMBINS );
00129
00130
00131
00132
00133
00134 #ifndef LAL_NDEBUG
00135 if ( ! lalNoDebug )
00136 {
00137
00138
00139 param.numBins = FOLDAMPLITUDESTESTC_NUMBINS;
00140 param.binMin = 0.0;
00141 param.binMax = twoPi;
00142
00143 LALFoldAmplitudes( &status, NULL, &input, ¶m );
00144
00145 if ( status.statusCode != FOLDAMPLITUDESH_ENULLP
00146 || strcmp(status.statusDescription, FOLDAMPLITUDESH_MSGENULLP) )
00147 {
00148 printf( "Got error code %d and message %s\n",
00149 status.statusCode, status.statusDescription );
00150 printf( "Expected error code %d and message %s\n",
00151 FOLDAMPLITUDESH_ENULLP, FOLDAMPLITUDESH_MSGENULLP );
00152 return FOLDAMPLITUDESTESTC_ECHK;
00153 }
00154
00155
00156 param.numBins = FOLDAMPLITUDESTESTC_NUMBINS;
00157 param.binMin = 0.0;
00158 param.binMax = twoPi;
00159
00160 LALFoldAmplitudes( &status, output, &badinput, ¶m );
00161
00162 if ( status.statusCode != FOLDAMPLITUDESH_EVECSIZE
00163 || strcmp(status.statusDescription, FOLDAMPLITUDESH_MSGEVECSIZE) )
00164 {
00165 printf( "Got error code %d and message %s\n",
00166 status.statusCode, status.statusDescription );
00167 printf( "Expected error code %d and message %s\n",
00168 FOLDAMPLITUDESH_EVECSIZE, FOLDAMPLITUDESH_MSGEVECSIZE );
00169 return FOLDAMPLITUDESTESTC_ECHK;
00170 }
00171
00172
00173 param.numBins = 0;
00174 param.binMin = 0.0;
00175 param.binMax = twoPi;
00176
00177 LALFoldAmplitudes( &status, output, &input, ¶m );
00178
00179 if ( status.statusCode != FOLDAMPLITUDESH_ENUMBINS
00180 || strcmp(status.statusDescription, FOLDAMPLITUDESH_MSGENUMBINS) )
00181 {
00182 printf( "Got error code %d and message %s\n",
00183 status.statusCode, status.statusDescription );
00184 printf( "Expected error code %d and message %s\n",
00185 FOLDAMPLITUDESH_ENUMBINS, FOLDAMPLITUDESH_MSGENUMBINS );
00186 return FOLDAMPLITUDESTESTC_ECHK;
00187 }
00188
00189
00190 param.numBins = FOLDAMPLITUDESTESTC_NUMBINS;
00191 param.binMin = 0.0;
00192 param.binMax = 0.0;
00193
00194 LALFoldAmplitudes( &status, output, &input, ¶m );
00195
00196 if ( status.statusCode != FOLDAMPLITUDESH_EBINSIZE
00197 || strcmp(status.statusDescription, FOLDAMPLITUDESH_MSGEBINSIZE) )
00198 {
00199 printf( "Got error code %d and message %s\n",
00200 status.statusCode, status.statusDescription );
00201 printf( "Expected error code %d and message %s\n",
00202 FOLDAMPLITUDESH_EBINSIZE, FOLDAMPLITUDESH_MSGEBINSIZE );
00203 return FOLDAMPLITUDESTESTC_ECHK;
00204 }
00205
00206
00207 param.numBins = FOLDAMPLITUDESTESTC_NUMBINS;
00208 param.binMin = -0.5;
00209 param.binMax = 0.5;
00210
00211 LALFoldAmplitudes( &status, output, &input, ¶m );
00212
00213 if ( status.statusCode != FOLDAMPLITUDESH_EBINMIN
00214 || strcmp(status.statusDescription, FOLDAMPLITUDESH_MSGEBINMIN) )
00215 {
00216 printf( "Got error code %d and message %s\n",
00217 status.statusCode, status.statusDescription );
00218 printf( "Expected error code %d and message %s\n",
00219 FOLDAMPLITUDESH_EBINMIN, FOLDAMPLITUDESH_MSGEBINMIN );
00220 return FOLDAMPLITUDESTESTC_ECHK;
00221 }
00222
00223 }
00224 #endif
00225
00226
00227
00228
00229
00230
00231
00232
00233 for (k = 0; k < 2; ++k) {
00234 param.numBins = 4;
00235 param.binMin = 0.0;
00236 if (k == 0) {
00237 param.binMax = 1.0;
00238 } else {
00239 param.binMax = twoPi;
00240 }
00241 binRange = param.binMax - param.binMin;
00242
00243 for (j = -8; j <= 8; ++j) {
00244
00245 for ( i = 0 ; i < (INT4)input.amplitudeVec->length ; ++i )
00246 {
00247 input.amplitudeVec->data[i] = 1.0;
00248
00249 input.phaseVec->data[i] = j*(binRange/8.0) + j*j*j*binRange + .001;
00250 }
00251
00252
00253 for ( i = 0 ; i < (INT4)output->length ; ++i )
00254 {
00255 output->data[i] = 0.0;
00256 }
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272 LALFoldAmplitudes( &status, output, &input, ¶m );
00273
00274 if ( status.statusCode )
00275 {
00276 printf( "Unexpectedly got error code %d and message %s\n",
00277 status.statusCode, status.statusDescription );
00278 return FOLDAMPLITUDESTESTC_EFLS;
00279 }
00280
00281 printf("\n");
00282 for ( i = 0 ; i < param.numBins ; ++i )
00283 {
00284 printf("Constant phase test binRange %g, index %i, Bin %i, Output Amplitude %f \n",binRange,j,i,output->data[i]);
00285 if (i == (j + 8)/2 % param.numBins) {
00286 if (output->data[i] != lengthAmpVec*1.0) {
00287 printf ("For binRange %g expected all the amplitudes to fold into bin %i but instead got %g in this bin. \n",binRange, i,output->data[i]);
00288 gotError = 1;
00289 }
00290 } else {
00291 if (output->data[i] != 0.0) {
00292 printf ("For binRange %g expected no amplitudes to fold into bin %i but instead got %g in this bin. \n",binRange, i,output->data[i]);
00293 gotError = 1;
00294 }
00295 }
00296 }
00297
00298 }
00299
00300 }
00301
00302 printf("\n");
00303 if (gotError > 0) {
00304 printf("Test 1 in FoldAmplitudesTest.c Failed. \n");
00305 return FOLDAMPLITUDESTESTC_EFLS;
00306 } else {
00307 printf("Test 1 in FoldAmplitudesTest.c Passed. \n");
00308 }
00309
00310
00311
00312
00313 param.numBins = 10;
00314 param.binMin = 0.0;
00315 param.binMax = twoPi;
00316 binRange = param.binMax - param.binMin;
00317
00318 for ( i = 0 ; i < (INT4)input.amplitudeVec->length ; ++i )
00319 {
00320 input.amplitudeVec->data[i] = 1;
00321 input.phaseVec->data[i] = twoPi*i/10.0 + .0001;
00322 }
00323
00324
00325 for ( i = 0 ; i < (INT4)output->length ; ++i )
00326 {
00327 output->data[i] = 0.0;
00328 }
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344 LALFoldAmplitudes( &status, output, &input, ¶m );
00345
00346 if ( status.statusCode )
00347 {
00348 printf( "Unexpectedly got error code %d and message %s\n",
00349 status.statusCode, status.statusDescription );
00350 return FOLDAMPLITUDESTESTC_EFLS;
00351 }
00352
00353 printf("\n");
00354 for ( i = 0 ; i < param.numBins ; ++i )
00355 {
00356 printf("Bin %i Output Amplitude %f \n",i,output->data[i]);
00357 if (output->data[i] != 1.0) {
00358 printf ("In bin %i expected 1 but instead got %g in this bin. \n",i,output->data[i]);
00359 gotError = 1;
00360 }
00361
00362 }
00363
00364 printf("\n");
00365 if (gotError > 0) {
00366 printf("Test 2 in FoldAmplitudesTest.c Failed. \n");
00367 return FOLDAMPLITUDESTESTC_EFLS;
00368 } else {
00369 printf("Test 2 in FoldAmplitudesTest.c Passed. \n");
00370 }
00371
00372
00373
00374
00375 param.numBins = 10;
00376 param.binMin = 0.0;
00377 param.binMax = twoPi;
00378 binRange = param.binMax - param.binMin;
00379
00380 for ( i = 0 ; i < (INT4)input.amplitudeVec->length ; ++i )
00381 {
00382 input.amplitudeVec->data[i] = sin(twoPi*i/10.0 + .0001);
00383 input.phaseVec->data[i] = twoPi*i/10.0 + .0001;
00384 }
00385
00386
00387 for ( i = 0 ; i < (INT4)output->length ; ++i )
00388 {
00389 output->data[i] = 0.0;
00390 }
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406 LALFoldAmplitudes( &status, output, &input, ¶m );
00407
00408 if ( status.statusCode )
00409 {
00410 printf( "Unexpectedly got error code %d and message %s\n",
00411 status.statusCode, status.statusDescription );
00412 return FOLDAMPLITUDESTESTC_EFLS;
00413 }
00414
00415 printf("\n");
00416 for ( i = 0 ; i < param.numBins ; ++i )
00417 {
00418 printf("Bin %i Output Amplitude %f \n",i,output->data[i]);
00419 if (fabs(output->data[i] - sin(twoPi*i/10.0 + .0001)) > fabs(output->data[i]*FOLDAMPLITUDESTESTC_TOL)) {
00420 printf ("In bin %i expected %g but instead got %g in this bin. \n",i,sin(twoPi*i/10.0 + .0001),output->data[i]);
00421 gotError = 1;
00422 }
00423
00424 }
00425
00426 printf("\n");
00427 if (gotError > 0) {
00428 printf("Test 3 in FoldAmplitudesTest.c Failed. \n");
00429 return FOLDAMPLITUDESTESTC_EFLS;
00430 } else {
00431 printf("Test 3 in FoldAmplitudesTest.c Passed. \n");
00432 }
00433
00434
00435
00436
00437
00438 param.numBins = 5;
00439 param.binMin = 0.0;
00440 param.binMax = 1.0;
00441 binRange = param.binMax - param.binMin;
00442
00443 for ( i = 0 ; i < (INT4)input.amplitudeVec->length ; ++i )
00444 {
00445 input.amplitudeVec->data[i] = sin(2.0*twoPi*i/5.0 + .001);
00446 input.phaseVec->data[i] = i/5.0 + .001;
00447 }
00448
00449
00450 for ( i = 0 ; i < (INT4)output->length ; ++i )
00451 {
00452 output->data[i] = 0.0;
00453 }
00454
00455 printf("\n");
00456 for ( i = 0 ; i < (INT4)input.amplitudeVec->length ; ++i )
00457 {
00458 printf("Index %i Input Amplitude %f \n",i, input.amplitudeVec->data[i]);
00459 }
00460
00461 printf("\n");
00462 for ( i = 0 ; i < (INT4)input.phaseVec->length ; ++i )
00463 {
00464 printf("Index %i Input Phase in Cycles %f \n",i, input.phaseVec->data[i]);
00465 }
00466
00467 LALFoldAmplitudes( &status, output, &input, ¶m );
00468
00469 if ( status.statusCode )
00470 {
00471 printf( "Unexpectedly got error code %d and message %s\n",
00472 status.statusCode, status.statusDescription );
00473 return FOLDAMPLITUDESTESTC_EFLS;
00474 }
00475
00476 printf("\n");
00477 for ( i = 0 ; i < param.numBins ; ++i )
00478 {
00479 printf("Bin %i Output Amplitude %f \n",i,output->data[i]);
00480 if (fabs(output->data[i] - 2.0*sin(2.0*twoPi*i/5.0 + .001)) > fabs(output->data[i]*FOLDAMPLITUDESTESTC_TOL)) {
00481 printf ("In bin %i expected %g but instead got %g in this bin. \n",i,2.0*sin(2.0*twoPi*i/5.0 + .001),output->data[i]);
00482 gotError = 1;
00483 }
00484
00485 }
00486
00487 printf("\n");
00488 if (gotError > 0) {
00489 printf("Test 4 in FoldAmplitudesTest.c Failed. \n");
00490 return FOLDAMPLITUDESTESTC_EFLS;
00491 } else {
00492 printf("Test 4 in FoldAmplitudesTest.c Passed. \n");
00493 }
00494
00495
00496
00497
00498 param.numBins = FOLDAMPLITUDESTESTC_NUMBINS;
00499 param.binMin = 0.0;
00500 param.binMax = twoPi;
00501
00502 printf("\n");
00503 printf("Test 5 check by hand: numBins, binMax = %i, %g \n",param.numBins,param.binMax);
00504
00505 for ( i = 0 ; i < (INT4)input.amplitudeVec->length ; ++i )
00506 {
00507 input.amplitudeVec->data[i] = sin(twoPi*f*i*delT + 0.5*fDot*i*delT*i*delT - 10.001);
00508 input.phaseVec->data[i] = twoPi*f*i*delT + 0.5*fDot*i*delT*i*delT - 10.001;
00509 }
00510
00511 for ( i = 0 ; i < (INT4)output->length ; ++i )
00512 {
00513 output->data[i] = 0.0;
00514 }
00515
00516 printf("\n");
00517 for ( i = 0 ; i < (INT4)input.amplitudeVec->length ; ++i )
00518 {
00519 printf("Index %i Input Amplitude %f \n",i, input.amplitudeVec->data[i]);
00520 }
00521
00522 printf("\n");
00523 for ( i = 0 ; i < (INT4)input.phaseVec->length ; ++i )
00524 {
00525 printf("Index %i Input Phase %f \n",i, input.phaseVec->data[i]);
00526 }
00527
00528 LALFoldAmplitudes( &status, output, &input, ¶m );
00529
00530 if ( status.statusCode )
00531 {
00532 printf( "Unexpectedly got error code %d and message %s\n",
00533 status.statusCode, status.statusDescription );
00534 return FOLDAMPLITUDESTESTC_EFLS;
00535 }
00536
00537 printf("\n");
00538 for ( i = 0 ; i < param.numBins ; ++i )
00539 {
00540 printf("Bin %i Output Amplitude %f \n",i,output->data[i]);
00541 }
00542
00543 printf("\n");
00544 if (gotError > 0) {
00545 printf("Test 5in FoldAmplitudesTest.c Failed. \n");
00546 return FOLDAMPLITUDESTESTC_EFLS;
00547 } else {
00548 printf("Test 5 in FoldAmplitudesTest.c Passed. \n");
00549 }
00550
00551
00552
00553
00554
00555 LALSDestroyVector( &status, &input.phaseVec );
00556 LALSDestroyVector( &status, &input.amplitudeVec );
00557 LALSDestroyVector( &status, &badinput.phaseVec );
00558 LALSDestroyVector( &status, &badinput.amplitudeVec );
00559 LALSDestroyVector( &status, &output );
00560
00561 LALCheckMemoryLeaks();
00562
00563 printf("\n");
00564 printf("PASS: All tests \n");
00565 printf("\n");
00566
00567 return FOLDAMPLITUDESTESTC_ENOM;
00568 }