LALPulsar 7.1.1.1-eeff03c
CombSearch.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2009 Letizia Sammut
3 * Copyright (C) 2009 Chris Messenger
4 * Copyright (C) 2007 Reinhard Prix
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with with program; see the file COPYING. If not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301 USA
20 */
21
22/**
23 * \defgroup lalpulsar_bin_SidebandSearch Sideband Search Application
24 * \ingroup lalpulsar_bin_Apps
25 */
26
27/**
28 * \file
29 * \ingroup lalpulsar_bin_SidebandSearch
30 * \author L.Sammut, C. Messenger
31 * \brief
32 * Calculates the C-statistic for a given parameter-space of GW signals from binary sources with known sky position.
33 *
34 * Uses outputFstat file of lalpulsar_ComputeFstatistic_v2.c as input.
35 *
36 */
37
38/***********************************************************************************************/
39#include "config.h"
40
41/* System includes */
42#include <math.h>
43#include <stdio.h>
44#include <time.h>
45#include <ctype.h>
46#include <strings.h>
47#include <signal.h>
48#include <stdlib.h>
49#include <string.h>
50#include <fftw3.h>
51
52/* LAL-includes */
53#include <lal/LALConfig.h>
54#include <lal/LALMalloc.h>
55#include <lal/LALStdio.h>
56#include <lal/LALError.h>
57#include <lal/LALStdlib.h>
58#include <lal/UserInput.h>
59#include <lal/SFTfileIO.h>
60#include <lal/LogPrintf.h>
61#include <lal/AVFactories.h>
62#include <lal/ComplexFFT.h>
63#include <lal/RealFFT.h>
64#include <lal/LALPulsarVCSInfo.h>
65
66
67/***********************************************************************************************/
68/* defines */
69
70#define MAXLINELENGTH 1024 /* Maximum # of characters of in a line */
71#define TRUE (1==1)
72#define FALSE (1==0)
73
74/***********************************************************************************************/
75/* internal structures */
76
77/**
78 * A structure for frequency series
79 */
80typedef struct {
81 REAL8Vector *fvect; /**< frequency series of data, REAL8Vector has components data and length */
82 REAL8 fmin; /**< start frequency of data in Fseries */
83 REAL8 df; /**< frequency separation in Fseries */
84 INT4 dof; /**< degrees of freedom of detection statistic represented by frequency series */
85 CHAR *comment; /**< comment field */
87
88/**
89 * A structure for template parameters
90 */
91typedef struct {
92 REAL8 freq; /**< user defined start frequency */
93 REAL8 fband; /**< user defined frequency band */
94 REAL8 orbitPeriod; /**< orbital period of source */
95 REAL8 orbitasini; /**< light travel time across semi-major axis of source */
96 REAL8 f0; /**< search frequency, centre of user band, used to determine width of template */
97 INT4 unitspikes; /**< number of unit spikes in template */
99
100/**
101 * A structure that stores user input variables
102 */
103typedef struct {
104 BOOLEAN tophat; /**< tophat template flag */
105 CHAR *inputFstat; /**< filename of Fstat input data file to use */
106 CHAR *outputCstat; /**< filename to output Cstatistic */
107 REAL8 Freq; /**< user defined start frequency */
108 REAL8 FreqBand; /**< user defined frequency band */
109 /* orbital parameters */
110 REAL8 orbitPeriod; /**< binary-system orbital period in s */
111 REAL8 orbitasini; /**< amplitude of radial motion */
113
114/**
115 * A structure for information on the exclusion region needed to avoid edge effects in calculation of Cstat
116 */
117typedef struct {
118 INT4 mm; /**< number of sidebands on either side of central spike */
119 INT4 dm; /**< number of bins spanned by mm */
120 INT4 fbins; /**< number of bins in Fstat */
121 INT4 cbins; /**< number of bins in Cstat */
122 REAL8 df; /**< size of bin */
123} ExRegion;
124
125/***********************************************************************************************/
126/* Global variables */
127extern int vrbflg; /* defined in lal/lib/std/LALError.c */
128
129const char *va( const char *format, ... ); /* little var-arg string helper function */
130
131/* local prototypes */
132int main( int argc, char *argv[] );
133int initUserVars( int argc, char *argv[], UserInput_t *uvar );
134int checkUserInputConsistency( const UserInput_t *uvar );
135int ReadInput( UserInput_t *uvar, ParamStruct *userParams, VectorStruct *Fstat, ExRegion *exr );
136int createComb( ParamStruct *userParams, VectorStruct *comb, ExRegion *exr );
137int createTopHat( ParamStruct *userParams, VectorStruct *tophat, ExRegion *exr );
138int ComputeCstat( VectorStruct *template, VectorStruct *Fstat, VectorStruct *Cstat, ExRegion *exr );
139int OutputCstats( UserInput_t *uvar, ParamStruct *userParams, VectorStruct *Fstat, VectorStruct *Cstat, ExRegion *exr );
140
141/***********************************************************************************************/
142/* Function definitions */
143
144
145/*----------------------------------------------------------------------*/
146/**
147 * MAIN function of sb_search code.
148 * Calculate the C-statistic over a given portion of the parameter-space
149 * and write output into a file(default: 'Cstats').
150 */
151/*----------------------------------------------------------------------*/
152int main( int argc, char *argv[] )
153{
154 static const char *fn = __func__; /* store function name for log output */
155 UserInput_t XLAL_INIT_DECL( uvar ); /* global container for user variables */
156 ParamStruct XLAL_INIT_DECL( userParams ); /* initialise paramStruct for search parameters, set by user */
157 VectorStruct XLAL_INIT_DECL( Fstat ); /* initialise Fstat structure */
158 VectorStruct XLAL_INIT_DECL( Cstat ); /* initialise Cstat structure */
159 VectorStruct XLAL_INIT_DECL( template ); /* initialise template structure */
160 ExRegion XLAL_INIT_DECL( exr ); /* initialise the exclusion region structure */
161
162
163 vrbflg = 1; /* verbose error-messages */
164
165 /* register and read all user-variables */
166 if ( initUserVars( argc, argv, &uvar ) ) {
167 LogPrintf( LOG_CRITICAL, "%s : initUserVars failed with error = %d\n", fn, xlalErrno );
168 return XLAL_EFAULT;
169 }
170 LogPrintf( LOG_DEBUG, "registered all user variables \n" );
171
172 /* do some sanity checks on the user-input before we proceed */
173 if ( checkUserInputConsistency( &uvar ) ) {
174 LogPrintf( LOG_CRITICAL, "%s : checkUserInputConsistency failed with error = %d\n", fn, xlalErrno );
175 return XLAL_EFAULT;
176 }
177 LogPrintf( LOG_DEBUG, "checked user input consistency \n" );
178
179 /* call function to get parameters and data from user input and data file */
180 if ( ReadInput( &uvar, &userParams, &Fstat, &exr ) ) {
181 LogPrintf( LOG_CRITICAL, "%s : ReadInput failed with error = %d\n", fn, xlalErrno );
182 return XLAL_EFAULT;
183 }
184 LogPrintf( LOG_DEBUG, "userParams and Fstat retrieved from input \n" );
185
186 /* check if tophat flag was raised */
187 if ( uvar.tophat ) {
188 /* call function to create tophat template */
189 if ( createTopHat( &userParams, &template, &exr ) ) {
190 LogPrintf( LOG_CRITICAL, "%s : createTemplate failed with error = %d\n", fn, xlalErrno );
191 return XLAL_EFAULT;
192 }
193 } else {
194 /* call function to create comb template */
195 if ( createComb( &userParams, &template, &exr ) ) {
196 LogPrintf( LOG_CRITICAL, "%s : createComb failed with error = %d\n", fn, xlalErrno );
197 return XLAL_EFAULT;
198 }
199 }
200 LogPrintf( LOG_DEBUG, "template created \n" );
201
202 /* call function to compute Cstat */
203 if ( ComputeCstat( &template, &Fstat, &Cstat, &exr ) ) {
204 LogPrintf( LOG_CRITICAL, "%s : createComb failed with error = %d\n", fn, xlalErrno );
205 return XLAL_EFAULT;
206 }
207 LogPrintf( LOG_DEBUG, "ComputeCstat done \n" );
208
209
210 /* call function to output Cstat to file */
211 if ( OutputCstats( &uvar, &userParams, &Fstat, &Cstat, &exr ) ) {
212 LogPrintf( LOG_CRITICAL, "%s : OutputCstats failed with error = %d\n", fn, xlalErrno );
213 return XLAL_EFAULT;
214 }
215 LogPrintf( LOG_DEBUG, "OutputCstats done \n" );
216
217 /********************************/
218 /* Free memory */
219 /********************************/
220
221 /* free data */
222 XLALFree( Fstat.fvect->data );
223 XLALFree( Cstat.fvect->data );
224 XLALFree( template.fvect->data );
225 XLALFree( Fstat.comment );
226 XLALFree( Cstat.comment );
227
228 /* free vectors */
229 XLALFree( Fstat.fvect );
230 XLALFree( Cstat.fvect );
231 XLALFree( template.fvect );
232
233 /* Free config-Variables and userInput stuff */
235
236 /* did we forget anything ? */
238 LogPrintf( LOG_DEBUG, "%s : successfully checked memory leaks.\n", fn );
239
240 /* we're done */
241 LogPrintf( LOG_DEBUG, "'%s' successfully completed. Done. \n", fn );
242 return 0;
243
244} /* main() */
245
246/*----------------------------------------------------------------------*/
247/**
248 * initUserVars function
249 * Register "user-variables" specified from cmd-line and/or config-file.
250 * Set defaults for some user-variables and register them with the UserInput module.
251 */
252/*----------------------------------------------------------------------*/
253int initUserVars( int argc, char *argv[], UserInput_t *uvar )
254{
255 const CHAR *fn = __func__; /* store function name for log output */
256
257 /* register all user-variables */
258 XLALRegisterUvarMember( Freq, REAL8, 'f', REQUIRED, "Starting search frequency in Hz" );
259 XLALRegisterUvarMember( FreqBand, REAL8, 'b', REQUIRED, "Search frequency band in Hz" );
260 XLALRegisterUvarMember( orbitPeriod, REAL8, 'P', REQUIRED, "Orbital period in seconds" );
261 XLALRegisterUvarMember( orbitasini, REAL8, 'A', REQUIRED, "Light travel time of orbital projected semi-major axis, in seconds" );
262 XLALRegisterUvarMember( inputFstat, STRING, 'D', REQUIRED, "Filename specifying input Fstat file" );
263 XLALRegisterUvarMember( outputCstat, STRING, 'C', REQUIRED, "Output-file for C-statistic" );
264 XLALRegisterUvarMember( tophat, BOOLEAN, 't', OPTIONAL, "Perform search with tophat template" );
265
266 /* do ALL cmdline and cfgfile handling */
267 BOOLEAN should_exit = 0;
268 if ( XLALUserVarReadAllInput( &should_exit, argc, argv, lalPulsarVCSInfoList ) ) {
269 LogPrintf( LOG_CRITICAL, "%s : XLALUserVarReadAllInput failed with error = %d\n", fn, xlalErrno );
270 return XLAL_EFAULT;
271 }
272 if ( should_exit ) {
273 exit( 1 );
274 }
275
276 LogPrintf( LOG_DEBUG, "'%s' successfully completed. Leaving. \n", fn );
277 return XLAL_SUCCESS;
278
279} /* initUserVars() */
280
281
282/*----------------------------------------------------------------------*/
283/**
284 * Some general consistency-checks on user-input.
285 * Throws an error plus prints error-message if problems are found.
286 */
287/*----------------------------------------------------------------------*/
289{
290 const CHAR *fn = __func__; /* store function name for log output */
291
292 /* binary parameter checks */
293 if ( XLALUserVarWasSet( &uvar->orbitPeriod ) && ( uvar->orbitPeriod <= 0 ) ) {
294 LogPrintf( LOG_CRITICAL, "%s: Negative or zero value of orbital period not allowed! Error %d\n", fn, xlalErrno );
295 return XLAL_EDOM;
296 }
297 if ( XLALUserVarWasSet( &uvar->orbitasini ) && ( uvar->orbitasini < 0 ) ) {
298 LogPrintf( LOG_CRITICAL, "%s: Negative or zero value of projected orbital semi-major axis not allowed! Error %d\n", fn, xlalErrno );
299 return XLAL_EDOM;
300 }
301
302 LogPrintf( LOG_DEBUG, "'%s' successfully completed. Leaving. \n", fn );
303 return XLAL_SUCCESS;
304
305} /* checkUserInputConsistency() */
306
307
308/*--------------------------------------------------------------- */
309/**
310 * ReadInput function
311 * reads user input and Fstat file and assigns parameter values and Fstat array
312 */
313/*----------------------------------------------------------------*/
314int ReadInput( UserInput_t *uvar, ParamStruct *userParams, VectorStruct *Fstat, ExRegion *exr )
315{
316 const CHAR *fn = __func__; /* store function name for log output */
317
318 REAL8 *frequency = NULL, *fstats = NULL; /* temp storage for frequency and fstat vectors */
319 REAL8 dummy, a = 0, d = 0, alpha = 0, delta = 0, fstat = 0; /* temp variables to store data from file */
320 INT4 c = 0, l = 0, i = 0; /* declare and initialise counters */
321 UINT4 totlen = 0; /* recursive length counter for Fstat header */
322
323 FILE *data = NULL; /* data file */
324 CHAR *filename = uvar->inputFstat; /* Fstat input filename */
325 CHAR line[MAXLINELENGTH]; /* string to store Fstat header information */
326 Fstat->comment = XLALCalloc( 1, sizeof( CHAR ) ); /* comment field for Fstat header information - allocate initial memory */
327
328
329
330 /* Open data file - check it is good */
331 if ( ( data = fopen( filename, "r" ) ) == NULL ) {
332 LogPrintf( LOG_CRITICAL, "%s: Error opening file '%s' for reading.. Error %d\n", fn, filename, xlalErrno );
333 return XLAL_EIO;
334 }
335 LogPrintf( LOG_DEBUG, "opened inputFstat file %s for reading \n", filename );
336
337 /* go through file, store header information, count number of data lines (ignores comments), and check validity of data */
338 while ( fgets( line, MAXLINELENGTH, data ) != NULL ) {
339 /* get Fstat header information */
340 if ( strncmp( &line[0], "%%", 2 ) == 0 ) {
341 UINT4 len = strlen( line );
342 CHAR *tempstring = XLALCalloc( len + 1, sizeof( CHAR ) );
343 totlen += len;
344 Fstat->comment = XLALRealloc( Fstat->comment, ( totlen + 1 ) * sizeof( CHAR ) );
345 strcpy( tempstring, line );
346 strcat( Fstat->comment, tempstring );
347 XLALFree( tempstring );
348 }
349 /* count data lines, check fstat validity and store last sky position entry */
350 if ( strncmp( &line[0], "%", 1 ) != 0 && isdigit( line[0] ) != 0 ) {
351 sscanf( line, "%lf %lf %lf %lf %lf %lf %lf", &dummy, &alpha, &delta, &dummy, &dummy, &dummy, &fstat );
352 l++;
353 /*check if any fstats are negative, NaN or Inf*/
354 if ( fstat < 0 || isinf( fstat ) || isnan( fstat ) ) {
355 LogPrintf( LOG_CRITICAL, "%s: Fstat file %s contains negative, Inf or NaN values %d\n", fn, filename, xlalErrno );
356 return XLAL_EDOM;
357 }
358 }
359 }
360 /* close the file prior to exiting the routine */
361 fclose( data );
362
363 /* Check data has more than one Fstat entry */
364 if ( l == 1 ) {
365 LogPrintf( LOG_CRITICAL, "%s: Must be more than one Fstat entry in data file %s. Error %d\n", fn, filename, xlalErrno );
366 return XLAL_EDOM;
367 }
368 LogPrintf( LOG_DEBUG, "checked for multiple Fstats, lines l= %d \n", l );
369
370 /* Allocate some memory according to number of frequency bins in input Fstat file */
371 if ( ( frequency = XLALCalloc( l, sizeof( REAL8 ) ) ) == NULL ) {
372 LogPrintf( LOG_CRITICAL, "%s: Error allocating memory.Error %d\n", fn, xlalErrno );
373 return XLAL_ENOMEM;
374 }
375 if ( ( fstats = XLALCalloc( l, sizeof( REAL8 ) ) ) == NULL ) {
376 LogPrintf( LOG_CRITICAL, "%s: Error allocating memory. Error %d\n", fn, xlalErrno );
377 return XLAL_ENOMEM;
378 }
379
380 /* Open data file - check it is good */
381 if ( ( data = fopen( filename, "r" ) ) == NULL ) {
382 LogPrintf( LOG_CRITICAL, "%s: Error opening file '%s' for reading.. Error %d\n", fn, filename, xlalErrno );
383 return XLAL_EIO;
384 }
385
386 /* Get data and assign some parameter variables */
387 while ( ( fgets( line, MAXLINELENGTH, data ) ) != NULL ) {
388 if ( ( ( strncmp( line, "%%", 1 ) ) != 0 ) && ( ( isdigit( line[0] ) ) != 0 ) ) {
389 sscanf( line, "%lf %lf %lf %lf %lf %lf %lf", &frequency[c], &a, &d, &dummy, &dummy, &dummy, &fstats[c] );
390 c++;
391 if ( ( alpha != a ) || ( delta != d ) ) {
392 LogPrintf( LOG_CRITICAL, "%s: Sky position (alpha and delta) must be constant. Error %d\n", fn, xlalErrno );
393 return XLAL_EINVAL;
394 }
395 }
396 }
397 fclose( data ); /* close the file prior to exiting the routine */
398 LogPrintf( LOG_DEBUG, "Acquired Fstats and search parameter information. File closed. \n" );
399
400
401 /* Assign all local variables */
402 REAL8 f0, a0, df, Porb, freq, fband, fstart, fend;
403 INT4 ufreq_bin, ufreqmax_bin, ufband, mm, dm, fbins, fstart_bin, fend_bin;
404
405 /* Assign variables for easier reading */
406 df = ( frequency[c - 1] - frequency[0] ) / ( c - 1 ); /* determine frequency resolution from Fstat file */
407 Porb = userParams->orbitPeriod = uvar->orbitPeriod; /* orbital period of source */
408 a0 = userParams->orbitasini = uvar->orbitasini; /* semi-major axis */
409 freq = userParams->freq = uvar->Freq; /* user input frequency */
410 fband = userParams->fband = uvar->FreqBand; /* user input frequency search band */
411
412
413 /* get parameters - fill out userParams*/
414 f0 = freq + 0.5 * fband ; /* allocate guess frequency as centre of user input search frequency band */
415 ufreq_bin = floor( 0.5 + ( freq - frequency[0] ) / df ); /* nearest bin to user starting frequency */
416 ufreqmax_bin = floor( 0.5 + ( freq + fband - frequency[0] ) / df ); /* nearest bin to user end frequency */
417 ufband = ufreqmax_bin - ufreq_bin + 1 ; /* number of bins in user frequency band */
418 mm = floor( 0.5 + LAL_TWOPI * f0 * a0 ); /* whole number of sidebands on either side of central spike */
419 dm = floor( 0.5 + mm / ( Porb * df ) ); /* exclusion region: number of Fstat bins to include on either side of user fband for Cstat calculation */
420 fbins = ufband + 2 * dm; /* length of Fstat vector requried for search*/
421 fstart_bin = ufreq_bin - dm ; /* frequency bin of first Fstat required for search, half a comb width before user specified Freq */
422 fstart = frequency[0] + fstart_bin * df; /* start frequency of search, half a comb width before user specified Freq */
423 fend_bin = fstart_bin + ufband + 2 * dm; /* frequency bin corresponding to end of search, half a comb width after user specified Freq */
424 fend = frequency[0] + fend_bin * df; /* end frequency of search, half a comb width after user specified Freq */
425
426
427 /* check search band plus exclusion region is within data frequency band */
428 if ( ( fstart < frequency[0] ) || ( frequency[c - 1] < fend ) ) {
429 LogPrintf( LOG_CRITICAL, "%s: User input frequency range and/or exclusion range outside data limits. Error %d\n", fn, xlalErrno );
430 return XLAL_EDOM;
431 }
432 LogPrintf( LOG_DEBUG, "Calculated exlusion region - within data limits. Good. \n" );
433
434 /* fill in exRegion struct exr */
435 exr->mm = mm; /* sidebands either side of central spike - defines width of exlusion region */
436 exr->dm = dm; /* number of bins in exclusion region */
437 exr->fbins = fbins; /* number of bins required in Fstat */
438 exr->cbins = ufband; /* number of bins to output Cstat */
439 exr->df = df; /* frequency resolution */
440
441 /* fill out rest of userParam struct */
442 userParams->f0 = f0; /* guess frequency of search, taken as centre of user input search frequency band */
443 if ( uvar->tophat ) {
444 userParams->unitspikes = floor( 0.5 + ( 2 * mm + 1 ) / ( Porb * df ) ); /* number of unit amplitude spikes if tophat template flag is raised */
445 } else {
446 userParams->unitspikes = 2 * mm + 1; /* number of unit amplitude spikes in comb template (default) */
447 }
448
449
450 /* Allocate some memory according to number of Fstat frequency bins required to complete search */
451 if ( ( Fstat->fvect = XLALCreateREAL8Vector( fbins ) ) == NULL ) {
452 LogPrintf( LOG_CRITICAL, "%s: Error allocating memory to Fstat->fvect. Error %d\n", fn, xlalErrno );
453 return XLAL_ENOMEM;
454 }
455
456 /* get Fstats required for search - only need Fstats defined by user defined search band plus the exlusion region */
457 for ( i = 0; i < fbins; i++ ) {
458 Fstat->fvect->data[i] = fstats[fstart_bin + i];
459 }
460 /* fill out the rest of the Fstat struct*/
461 Fstat->fmin = fstart;
462 Fstat->df = df;
463 Fstat->dof = 4;
464
465 /* Free ReadInput function specific memory */
467 XLALFree( fstats );
468
469 LogPrintf( LOG_DEBUG, "'%s' successfully completed. Leaving. \n", fn );
470 return XLAL_SUCCESS;
471
472} /* ReadInput */
473
474
475/*--------------------------------------------------------------- */
476/**
477 * createComb function
478 * use userParams to create comb template
479 *
480 * function creates template of unit amplitude spikes at 1 zero spike +/- mm spikes on either side separated by 1/P
481 */
482/*----------------------------------------------------------------*/
483int createComb( ParamStruct *userParams, VectorStruct *comb, ExRegion *exr )
484{
485 static const char *fn = __func__; /* store function name for log output */
486
487 INT4 i, ind = 0; /* declare and initialise counters*/
488
489 /* Allocate some memory for comb template vector */
490 if ( ( comb->fvect = XLALCreateREAL8Vector( exr->fbins ) ) == NULL ) {
491 LogPrintf( LOG_CRITICAL, "%s: Error allocating memory to comb->fvect. Error %d\n", fn, xlalErrno );
492 return XLAL_ENOMEM;
493 }
494
495 /* zero all values first */
496 for ( i = 0; i < exr->fbins; i++ ) {
497 comb->fvect->data[i] = 0;
498 }
499
500 /* Assign unity at spacings determined by template type for 1 zero spike + mm positive frequency spikes */
501 for ( i = 0; i <= exr->mm; i++ ) {
502 ind = floor( 0.5 + i / ( userParams->orbitPeriod * exr->df ) );
503 comb->fvect->data[ind] = 1;
504 }
505
506 /* assign unity at spacings of 1/P for mm negative frequency spikes */
507 for ( i = 1; i <= exr->mm; i++ ) {
508 ind = ( exr->fbins ) - floor( 0.5 + i / ( userParams->orbitPeriod * exr->df ) );
509 comb->fvect->data[ind] = 1;
510 }
511
512 /* allocate rest of comb template structure */
513 comb->df = exr->df;
514 comb->dof = userParams->unitspikes;
515
516 LogPrintf( LOG_DEBUG, "%s: template with %d spikes created \n", fn, comb->dof );
517 LogPrintf( LOG_DEBUG, "'%s' successfully completed. Leaving. \n", fn );
518 return XLAL_SUCCESS;
519
520} /* createComb */
521
522
523/*--------------------------------------------------------------- */
524/**
525 * createTopHat function
526 * use userParams to create tophat template if tophat flag is raised
527 *
528 * function creates template of unit amplitude spikes for a band mm/P on either side of the central zero spike
529 */
530/*----------------------------------------------------------------*/
531int createTopHat( ParamStruct *userParams, VectorStruct *tophat, ExRegion *exr )
532{
533 static const char *fn = __func__; /* store function name for log output */
534
535 INT4 i = 0, ind = 0; /* declare and initialise counters*/
536
537 /* Allocate some memory for tophat template vector */
538 if ( ( tophat->fvect = XLALCreateREAL8Vector( exr->fbins ) ) == NULL ) {
539 LogPrintf( LOG_CRITICAL, "%s: Error allocating memory to tophat->fvect. Error %d\n", fn, xlalErrno );
540 return XLAL_ENOMEM;
541 }
542
543 /* zero all values first */
544 for ( i = 0; i < exr->fbins; i++ ) {
545 tophat->fvect->data[i] = 0;
546 }
547
548 /* Assign unity at spacings for 1 zero spike + dm = mm/P*df positive frequency spikes */
549 for ( i = 0; i <= exr->dm; i++ ) {
550 tophat->fvect->data[i] = 1;
551 }
552
553 /* assign unity for dm = mm/P*df negative frequency spikes */
554 for ( i = 1; i <= exr->dm; i++ ) {
555 ind = ( exr->fbins - i );
556 tophat->fvect->data[ind] = 1;
557 }
558
559 /* allocate rest of tophat template structure */
560 tophat->df = exr->df;
561 tophat->dof = userParams->unitspikes;
562
563
564 LogPrintf( LOG_DEBUG, "%s: template with %d spikes created. halfwidth(dm) = %d bins\n", fn, tophat->dof, exr->dm );
565 LogPrintf( LOG_DEBUG, "'%s' successfully completed. Leaving. \n", fn );
566 return XLAL_SUCCESS;
567
568} /* createTopHat */
569
570
571
572
573/*--------------------------------------------------------------- */
574/**
575 * ComputeCstat function
576 * uses comb and Fstat structures to calculate the Cstat
577 */
578/*----------------------------------------------------------------*/
579int ComputeCstat( VectorStruct *template, VectorStruct *Fstat, VectorStruct *Cstat, ExRegion *exr )
580{
581 static const char *fn = __func__; /* store function name for log output */
582
583 INT4 i; /* initialise counters */
584 INT4 N = exr->fbins; /* number of frequency bins in Fstat */
585
586 /* Allocate memory for FFT plans and vectors */
587 REAL8FFTPlan *pfwd = NULL;
588 REAL8FFTPlan *prev = NULL;
589 COMPLEX16Vector *f_out = NULL;
590 COMPLEX16Vector *t_out = NULL;
591 COMPLEX16Vector *c_out = NULL;
592 REAL8Vector *cstats = NULL;
593
594
595 /* Create FFT plans and vectors */
596 pfwd = XLALCreateREAL8FFTPlan( N, 1, 0 );
597 prev = XLALCreateREAL8FFTPlan( N, 0, 0 );
598 cstats = XLALCreateREAL8Vector( N );
599 f_out = XLALCreateCOMPLEX16Vector( N / 2 + 1 );
600 t_out = XLALCreateCOMPLEX16Vector( N / 2 + 1 );
601 c_out = XLALCreateCOMPLEX16Vector( N / 2 + 1 );
602
603 /* Fourier transform fstat array */
604 XLALREAL8ForwardFFT( f_out, Fstat->fvect, pfwd );
605
606 /* Fourier transform template for convolution with fstat Fourier transform */
607 XLALREAL8ForwardFFT( t_out, template->fvect, pfwd );
608
609 /* Perform convolution of fstat with template by multiplication in Fourier time domain */
610 for ( i = 0; i < ( N / 2 + 1 ); i++ ) {
611 c_out->data[i] = crect( ( creal( f_out->data[i] ) * creal( t_out->data[i] ) ) - ( cimag( f_out->data[i] ) * cimag( t_out->data[i] ) ), ( creal( f_out->data[i] ) * cimag( t_out->data[i] ) ) + ( cimag( f_out->data[i] ) * creal( t_out->data[i] ) ) );
612 }
613
614 /* Inverse FFT back to frequency domain to retrieve Cstat */
615 XLALREAL8ReverseFFT( cstats, c_out, prev );
616
617 /* Allocate some memory for Cstat vector */
618 if ( ( Cstat->fvect = XLALCreateREAL8Vector( exr->cbins ) ) == NULL ) {
619 LogPrintf( LOG_CRITICAL, "%s: Error allocating memory to Cstat->fvect. Error %d\n", fn, xlalErrno );
620 return XLAL_ENOMEM;
621 }
622
623 /* Assign Cstat arrary values of Cstatistic */
624 for ( i = 0; i < exr->cbins; i++ ) {
625 Cstat->fvect->data[i] = cstats->data[i + exr->dm];
626 }
627
628 /* fill in the rest of the Cstat struct */
629 Cstat->dof = Fstat->dof * template->dof;
630 Cstat->fmin = Fstat->fmin + Fstat->df * exr->dm;
631 Cstat->df = Fstat->df;
632
633 /* Destroy FFT plans and free ComputeCstat function specific memory */
636 XLALDestroyREAL8Vector( cstats );
640
641 LogPrintf( LOG_DEBUG, "%s: Cstat dof = %d \n", fn, Cstat->dof );
642 LogPrintf( LOG_DEBUG, "'%s' successfully completed. Leaving. \n", fn );
643 return XLAL_SUCCESS;
644
645} /* ComputeCstat */
646
647
648int OutputCstats( UserInput_t *uvar, ParamStruct *userParams, VectorStruct *Fstat, VectorStruct *Cstat, ExRegion *exr )
649{
650 static const char *fn = __func__; /* store function name for log output */
651
652 FILE *Cstat_out = NULL; /* output filename */
653 INT4 i; /* initialise counters */
654
655 /* open output file - also check it is good */
656 if ( XLALUserVarWasSet( &uvar->outputCstat ) ) {
657 if ( ( Cstat_out = fopen( uvar->outputCstat, "wb" ) ) == NULL ) {
658 LogPrintf( LOG_CRITICAL, "%s: Error opening file '%s' for reading.. Error %d\n", fn, uvar->outputCstat, xlalErrno );
659 return XLAL_EIO;
660 }
661
662 /* get full command line describing search */
663 if ( ( Cstat->comment = XLALUserVarGetLog( UVAR_LOGFMT_CMDLINE ) ) == NULL ) {
664 LogPrintf( LOG_CRITICAL, "%s : XLALUserVarGetLog failed with error = %d\n", fn, xlalErrno );
665 return XLAL_EFAULT;
666 }
667 /* print Fstat header information and Cstat command line to output file */
668 fprintf( Cstat_out, "%%%% *********************************************************************** \n" );
669 fprintf( Cstat_out, "%%%% *************** Fstat Header ****************** \n %s", Fstat->comment );
670 fprintf( Cstat_out, "%%%% *********************************************************************** \n" );
671 fprintf( Cstat_out, "%%%% *************** Cstat Header ****************** \n" );
672 /** \deprecated FIXME: the following code uses obsolete CVS ID tags.
673 * It should be modified to use git version information. */
674 fprintf( Cstat_out, "%%%% %s\n%%%% cmdline: %s\n", "$Id$", Cstat->comment );
675
676 /* Cstat header - output user input to file */
677 fprintf( Cstat_out, "%%%% input: f0 = %f,\t asini = %lf, Porb = %lf,\t user fmin = %f,\n", userParams->f0, userParams->orbitasini, userParams->orbitPeriod, userParams->freq );
678 fprintf( Cstat_out, "%%%% Fstat params: fmin = %lf \t df = %2.16e \t fbins = %d \t dof = %d \n", Fstat->fmin, Fstat->df, Fstat->fvect->length, Fstat->dof );
679 fprintf( Cstat_out, "%%%% Cstat params: fmin = %lf \t df = %2.16e \t cbins = %d \t dof = %d \n", Cstat->fmin, Cstat->df, Cstat->fvect->length, Cstat->dof );
680 fprintf( Cstat_out, "%%%% \n%%%% i \t frequency \t\t Fstat \t\t Cstat \n " );
681
682 /* output fstat inputs and cstat output to file */
683 for ( i = 0; i < exr->cbins; i++ ) {
684 fprintf( Cstat_out, "%d\t%6.12f\t%lf\t%lf\n", i, Cstat->fmin + i * Cstat->df, Fstat->fvect->data[i + exr->dm], ( Cstat->fvect->data[i] / exr->fbins ) );
685 }
686 fprintf( Cstat_out, "%% Done" );
687 }
688 /* close file */
689 fclose( Cstat_out );
690
691 LogPrintf( LOG_DEBUG, "'%s' successfully completed. Leaving. \n", fn );
692 return XLAL_SUCCESS;
693
694} /* OutputCstats */
#define MAXLINELENGTH
Definition: CombSearch.c:70
int main(int argc, char *argv[])
MAIN function of sb_search code.
Definition: CombSearch.c:152
int ComputeCstat(VectorStruct *template, VectorStruct *Fstat, VectorStruct *Cstat, ExRegion *exr)
ComputeCstat function uses comb and Fstat structures to calculate the Cstat.
Definition: CombSearch.c:579
int createTopHat(ParamStruct *userParams, VectorStruct *tophat, ExRegion *exr)
createTopHat function use userParams to create tophat template if tophat flag is raised
Definition: CombSearch.c:531
int OutputCstats(UserInput_t *uvar, ParamStruct *userParams, VectorStruct *Fstat, VectorStruct *Cstat, ExRegion *exr)
Definition: CombSearch.c:648
int createComb(ParamStruct *userParams, VectorStruct *comb, ExRegion *exr)
createComb function use userParams to create comb template
Definition: CombSearch.c:483
int vrbflg
defined in lal/lib/std/LALError.c
int ReadInput(UserInput_t *uvar, ParamStruct *userParams, VectorStruct *Fstat, ExRegion *exr)
ReadInput function reads user input and Fstat file and assigns parameter values and Fstat array.
Definition: CombSearch.c:314
int initUserVars(int argc, char *argv[], UserInput_t *uvar)
initUserVars function Register "user-variables" specified from cmd-line and/or config-file.
Definition: CombSearch.c:253
int checkUserInputConsistency(const UserInput_t *uvar)
Some general consistency-checks on user-input.
Definition: CombSearch.c:288
const char * va(const char *format,...)
#define __func__
log an I/O error, i.e.
void LALCheckMemoryLeaks(void)
const LALVCSInfoList lalPulsarVCSInfoList
NULL-terminated list of VCS and build information for LALPulsar and its dependencies
static double double delta
#define c
#define STRING(a)
INT4 dummy
Definition: SinCosLUT.i:123
#define fprintf
int l
#define LAL_TWOPI
#define crect(re, im)
unsigned char BOOLEAN
double REAL8
#define XLAL_INIT_DECL(var,...)
char CHAR
uint32_t UINT4
int32_t INT4
void * XLALCalloc(size_t m, size_t n)
void * XLALRealloc(void *p, size_t n)
void XLALFree(void *p)
void LogPrintf(LogLevel_t, const char *format,...) _LAL_GCC_PRINTF_FORMAT_(2
LOG_CRITICAL
LOG_DEBUG
static const INT4 a
int XLALREAL8ForwardFFT(COMPLEX16Vector *output, const REAL8Vector *input, const REAL8FFTPlan *plan)
int XLALREAL8ReverseFFT(REAL8Vector *output, const COMPLEX16Vector *input, const REAL8FFTPlan *plan)
void XLALDestroyREAL8FFTPlan(REAL8FFTPlan *plan)
REAL8FFTPlan * XLALCreateREAL8FFTPlan(UINT4 size, int fwdflg, int measurelvl)
int XLALUserVarReadAllInput(BOOLEAN *should_exit, int argc, char *argv[], const LALVCSInfoList vcs_list)
void XLALDestroyUserVars(void)
#define XLALRegisterUvarMember(name, type, option, category,...)
void CHAR * XLALUserVarGetLog(UserVarLogFormat format)
int XLALUserVarWasSet(const void *cvar)
UVAR_LOGFMT_CMDLINE
REAL8Vector * XLALCreateREAL8Vector(UINT4 length)
COMPLEX16Vector * XLALCreateCOMPLEX16Vector(UINT4 length)
void XLALDestroyREAL8Vector(REAL8Vector *vector)
void XLALDestroyCOMPLEX16Vector(COMPLEX16Vector *vector)
#define xlalErrno
XLAL_ENOMEM
XLAL_SUCCESS
XLAL_EFAULT
XLAL_EDOM
XLAL_EIO
XLAL_EINVAL
float data[BLOCKSIZE]
Definition: hwinject.c:360
int N
double alpha
COMPLEX16 * data
A structure for information on the exclusion region needed to avoid edge effects in calculation of Cs...
Definition: CombSearch.c:117
INT4 dm
number of bins spanned by mm
Definition: CombSearch.c:119
INT4 mm
number of sidebands on either side of central spike
Definition: CombSearch.c:118
REAL8 df
size of bin
Definition: CombSearch.c:122
INT4 fbins
number of bins in Fstat
Definition: CombSearch.c:120
INT4 cbins
number of bins in Cstat
Definition: CombSearch.c:121
A structure for template parameters.
Definition: CombSearch.c:91
REAL8 fband
user defined frequency band
Definition: CombSearch.c:93
REAL8 orbitasini
light travel time across semi-major axis of source
Definition: CombSearch.c:95
REAL8 freq
user defined start frequency
Definition: CombSearch.c:92
REAL8 f0
search frequency, centre of user band, used to determine width of template
Definition: CombSearch.c:96
REAL8 orbitPeriod
orbital period of source
Definition: CombSearch.c:94
INT4 unitspikes
number of unit spikes in template
Definition: CombSearch.c:97
REAL8 * data
A structure for frequency series.
Definition: CombSearch.c:80
CHAR * comment
comment field
Definition: CombSearch.c:85
REAL8Vector * fvect
frequency series of data, REAL8Vector has components data and length
Definition: CombSearch.c:81
REAL8 df
frequency separation in Fseries
Definition: CombSearch.c:83
INT4 dof
degrees of freedom of detection statistic represented by frequency series
Definition: CombSearch.c:84
REAL8 fmin
start frequency of data in Fseries
Definition: CombSearch.c:82
double df