Loading [MathJax]/extensions/TeX/AMSsymbols.js
LALPulsar 7.1.1.1-b246709
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
compareTS.c
Go to the documentation of this file.
1/*
2* Copyright (C) 2009 Reinhard Prix
3*
4* This program is free software; you can redistribute it and/or modify
5* it under the terms of the GNU General Public License as published by
6* the Free Software Foundation; either version 2 of the License, or
7* (at your option) any later version.
8*
9* This program is distributed in the hope that it will be useful,
10* but WITHOUT ANY WARRANTY; without even the implied warranty of
11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12* GNU General Public License for more details.
13*
14* You should have received a copy of the GNU General Public License
15* along with with program; see the file COPYING. If not, write to the
16* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17* MA 02110-1301 USA
18*/
19
20/*
21 * Author: R. Prix
22 *
23 * Compare two binary strain files, written from hardware-injection
24 * streams, return maximal relative error.
25 *
26 * Note: the strain file need to start with magic REAL4 1234.5,
27 * followed by an INT4 argument giving the number of elements.
28 */
29
30/* ---------- includes ---------- */
31#include "config.h"
32
33#include <stdio.h>
34
35#include <lal/LALDatatypes.h>
36#include <lal/AVFactories.h>
37#include <lal/UserInput.h>
38#include <lal/LogPrintf.h>
39#include <lal/SFTfileIO.h>
40#include <lal/LALPulsarVCSInfo.h>
41
42/* User variables */
43typedef struct {
49} UserVar;
50
51
52/* ----- local prototypes ----- */
53REAL4 getMaxErrSFT( const SFTtype *sft1, const SFTtype *sft2 );
54REAL4 getMaxErrSFTVector( const SFTVector *sftvect1, const SFTVector *sftvect2 );
55int initUserVars( UserVar *uvar );
58
59extern int vrbflg;
60
61/*----------------------------------------------------------------------
62 * main function
63 *----------------------------------------------------------------------*/
64int
65main( int argc, char *argv[] )
66{
67 /* register all user-variables */
68 UserVar XLAL_INIT_DECL( uvar );
70
71 /* read cmdline & cfgfile */
72 BOOLEAN should_exit = 0;
74 if ( should_exit ) {
75 exit( 1 );
76 }
77
78 /* now read in the two timeseries */
79 REAL4Vector *ts1, *ts2;
80 XLAL_CHECK_MAIN( ( ts1 = XLALREAL4VectorFromFile( uvar.infile1 ) ) != NULL, XLAL_EFUNC, "Failed to load timeseries file '%s'.\n", uvar.infile1 );
81 XLAL_CHECK_MAIN( ( ts2 = XLALREAL4VectorFromFile( uvar.infile2 ) ) != NULL, XLAL_EFUNC, "Failed to load timeseries file '%s'.\n", uvar.infile2 );
82
85
86 XLAL_CHECK_MAIN( maxd <= uvar.relErrorMax, XLAL_ETOL, "FAILED. Maximal relative error %e exceeds tolerance %e.\n", maxd, uvar.relErrorMax );
87
88 XLALPrintInfo( "OK. Maximal relative error %e is within tolerance of %e.\n", maxd, uvar.relErrorMax );
89
90 /* free memory */
93
95
97
98 return XLAL_SUCCESS;
99
100} /* main */
101
102
103/*----------------------------------------------------------------------*/
104/* register all our "user-variables" */
105int
107{
108 /* set some defaults */
109 uvar->debug = lalDebugLevel;
110 uvar->verbose = 0;
111 uvar->relErrorMax = 1e-4;
112
113 /* now register all our user-variable */
114 XLALRegisterUvarMember( infile1, STRING, '1', REQUIRED, "First timeseries input file" );
115 XLALRegisterUvarMember( infile2, STRING, '2', REQUIRED, "Second timeseries input file" );
116 XLALRegisterUvarMember( verbose, BOOLEAN, 'V', OPTIONAL, "Verbose output of differences" );
117 XLALRegisterUvarMember( relErrorMax, REAL8, 'e', OPTIONAL, "Maximal relative error acceptable to 'pass' comparison" );
118
119 return XLAL_SUCCESS;
120} /* initUserVars() */
121
122
123/**
124 * Compare two REAL8 vectors, returns a measure of the difference.
125 */
126REAL4
128{
129 UINT4 i, numSteps;
130 REAL8 total_power, maxdiff, sumdiff, maxpower;
131 REAL8 reldiff, reldiff_max, reldiff_avg;
132
133 if ( !ts1 || !ts2 || !ts1->data || !ts2->data ) {
134 XLALPrintError( "%s: illegal NULL input.\n", __func__ );
136 }
137
138 numSteps = ts1->length;
139 if ( ts2->length != numSteps ) {
140 XLALPrintError( "%s: number of timesteps of ts1 (%d) differs from ts2 (%d).\n", __func__, numSteps, ts2->length );
142 }
143
144 sumdiff = 0;
145 maxdiff = 0;
146 total_power = 0;
147 maxpower = 0;
148 for ( i = 0; i < numSteps; i ++ ) {
149 REAL8 power, diff;
150
151 diff = fabs( ts1->data[i] - ts2->data[i] );
152
153 if ( diff > maxdiff ) {
154 maxdiff = diff;
155 }
156
157 sumdiff += diff;
158
159 power = fabs( ts1->data[i] );
160 if ( power > maxpower ) {
161 maxpower = power;
162 }
163
164 total_power += power;
165
166 } /* for i < numSteps */
167
168 reldiff_max = maxdiff / maxpower;
169 reldiff_avg = sumdiff / total_power;
170
171 LogPrintf( LOG_DEBUG, "%s: maximal difference = %g, maximal amplitude = %g ==> relative error %g\n", __func__, maxdiff, maxpower, reldiff_max );
172 LogPrintf( LOG_DEBUG, "%s: total difference = %g, total summed amplitude = %g ==> relative avg error %g\n", __func__, sumdiff, total_power, reldiff_avg );
173
174 reldiff = fmax( reldiff_max, reldiff_avg );
175
176 return ( REAL4 )reldiff;
177
178} /* XLALcompareREAL4Vectors() */
179
180
181/**
182 * Load timeseries from binary input file into REAL4Vector
183 */
186{
187 REAL4Vector *vect;
188 FILE *fp;
189 REAL4 magic;
190 UINT4 len;
191
192 if ( !fname ) {
193 XLALPrintError( "%s: filename is NULL.\n", __func__ );
195 }
196
197 if ( ( fp = fopen( fname, "rb" ) ) == NULL ) {
198 XLALPrintError( "%s: failed to open file '%s' for reading.\n", __func__, fname );
200 }
201
202 if ( ( fread( &magic, sizeof( magic ), 1, fp ) != 1 ) || ( magic != 1234.5 ) ) {
203 XLALPrintError( "%s: file '%s' has wrong magic byte (%f) != 1234.5.\n", __func__, fname, magic );
204 fclose( fp );
206 }
207
208 if ( fread( &len, sizeof( len ), 1, fp ) != 1 ) {
209 XLALPrintError( "%s: failed to read UINT4 length from file '%s'\n", __func__, fname );
210 fclose( fp );
212 }
213
214 if ( ( vect = XLALCreateREAL4Vector( len ) ) == NULL ) {
215 XLALPrintError( "%s: XLALCreateREAL4Vector(%d) failed.\n", __func__, len );
216 fclose( fp );
218 }
219
220 if ( len != fread( vect->data, sizeof( *vect->data ), len, fp ) ) {
221 XLALPrintError( "%s: failed to read %d REAL4s from input file '%s'.\n", __func__, len, fname );
222 fclose( fp );
225 }
226
227 fclose( fp );
228
229 return vect;
230
231} /* XLALREAL4VectorFromFile() */
#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
#define STRING(a)
double e
int main(int argc, char *argv[])
Definition: compareTS.c:65
REAL4Vector * XLALREAL4VectorFromFile(const CHAR *fname)
Load timeseries from binary input file into REAL4Vector.
Definition: compareTS.c:185
REAL4 XLALcompareREAL4Vectors(REAL4Vector *ts1, REAL4Vector *ts2)
Compare two REAL8 vectors, returns a measure of the difference.
Definition: compareTS.c:127
REAL4 getMaxErrSFT(const SFTtype *sft1, const SFTtype *sft2)
Definition: compareSFTs.c:207
int vrbflg
defined in lal/lib/std/LALError.c
int initUserVars(UserVar *uvar)
Definition: compareTS.c:106
REAL4 getMaxErrSFTVector(const SFTVector *sftvect1, const SFTVector *sftvect2)
Definition: compareSFTs.c:240
unsigned char BOOLEAN
double REAL8
#define XLAL_INIT_DECL(var,...)
char CHAR
uint32_t UINT4
int32_t INT4
float REAL4
#define lalDebugLevel
void LogPrintf(LogLevel_t, const char *format,...) _LAL_GCC_PRINTF_FORMAT_(2
LOG_DEBUG
int XLALUserVarReadAllInput(BOOLEAN *should_exit, int argc, char *argv[], const LALVCSInfoList vcs_list)
void XLALDestroyUserVars(void)
#define XLALRegisterUvarMember(name, type, option, category,...)
REAL4Vector * XLALCreateREAL4Vector(UINT4 length)
void XLALDestroyREAL4Vector(REAL4Vector *vector)
#define XLAL_ERROR_REAL8(...)
#define XLAL_ERROR_NULL(...)
int int int XLALPrintInfo(const char *fmt,...) _LAL_GCC_PRINTF_FORMAT_(1
#define XLAL_CHECK(assertion,...)
#define XLAL_CHECK_MAIN(assertion,...)
int XLALPrintError(const char *fmt,...) _LAL_GCC_PRINTF_FORMAT_(1
#define XLAL_IS_REAL8_FAIL_NAN(val)
XLAL_EBADLEN
XLAL_SUCCESS
XLAL_EFUNC
XLAL_ETOL
XLAL_EDOM
XLAL_ESYS
XLAL_EINVAL
A vector of COMPLEX8FrequencySeries.
REAL4 * data
CHAR * infile2
Definition: compareTS.c:45
BOOLEAN verbose
Definition: compareTS.c:47
INT4 debug
Definition: compareTS.c:46
CHAR * infile1
Definition: compareTS.c:44
REAL8 relErrorMax
Definition: compareTS.c:48