LALApps 10.1.0.1-eeff03c
InjectTimeSeries.c
Go to the documentation of this file.
1/*
2* Copyright (C) 2007 Jolien Creighton, Teviet Creighton, John Whelan
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#include <math.h>
21#include <lal/LALStdio.h>
22#include <lal/LALStdlib.h>
23#include <lal/LALError.h>
24#include <lal/Units.h>
25#include <lal/Random.h>
26#include <Inject.h>
27
28/**
29 * \author Creighton, T. D.
30 * \addtogroup InjectTimeSeries_c
31 *
32 * \brief Injects a time series of floating-point numbers into a time
33 * series of floating-point numbers using nearest-neighbour interpolation.
34 *
35 * ### Algorithm ###
36 *
37 * Samples are added to the target time series. If the timestamps of
38 * samples in the two time series are not identical, the offsets in the
39 * target time series to which samples are added are rounded to the nearest
40 * integer --- no sub-sample interpolation is performed.
41 */
42/** @{ */
43
44
45/** \see See documentation in \ref InjectTimeSeries_c */
46void
49 REAL4TimeSeries *signalvec )
50{
51 INT4 n; /* 1 + highest index of output touched by the injection */
52 INT4 i; /* index over output data */
53 REAL4 *outData; /* pointer to output->data->data */
54 REAL8 dt; /* output->deltaT in units of signalvec->deltaT */
55 REAL8 offset; /* the time from the start of *signalvec to the start
56 of *output, in units of signalvec->deltaT. */
57
60
61 /* Make sure parameter structures and their fields exist. */
68 outData = output->data->data;
69
70 /* Make sure we never divide by zero. */
71 ASSERT( signalvec->deltaT != 0.0, stat, INJECTH_EBAD, INJECTH_MSGEBAD );
72 dt = output->deltaT / signalvec->deltaT;
74
75 /* Check dimensions. */
76 {
77 CHAR newName[LALNameLength];
78
81 if(snprintf( newName, LALNameLength, "%s plus %s", output->name,
82 signalvec->name ) >= LALNameLength)
84 memcpy( output->name, newName, LALNameLength*sizeof(CHAR) );
85 }
86
87 /* Compute offset. */
88 offset = ( output->epoch.gpsSeconds - signalvec->epoch.gpsSeconds ) /
89 signalvec->deltaT;
90 offset += ( output->epoch.gpsNanoSeconds -
91 signalvec->epoch.gpsNanoSeconds ) * 1.0e-9 /
92 signalvec->deltaT;
93
94 /* Compute initial value of i, and correct to ensure we will never
95 index either array out of its bounds. */
96 i = (INT4)( -offset / dt );
97 if ( i < 0 )
98 i = 0;
99 while ( offset + i*dt < 0.0 )
100 i++;
101 if ( i >= (INT4)( output->data->length ) )
102 LALWarning( stat, "Signal starts after the end of the output"
103 " time series." );
104
105 /* Compute final value of i+1, and correct to ensure we will never
106 index either array out of its bounds. */
107 n = (INT4)( ( signalvec->data->length - offset ) / dt );
108 if ( n > (INT4)( output->data->length ) )
109 n = output->data->length;
110 while ( offset + n*dt > signalvec->data->length )
111 n--;
112 if ( n <= 0 )
113 LALWarning( stat, "Signal ends before the start of the output"
114 " time series." );
115
116 /* Start injecting... */
117 for ( ; i < n; i++ ) {
118
119 /* Interpolate the signal. ***REMOVED***
120 REAL8 t = offset + i*dt; interpolated signal index
121 INT4 j = (INT4)floor( t ); signal index preceding t
122 REAL4 frac = (REAL4)( t - j ); interpolation fraction
123 REAL4 y = frac*(signalvec->data->data[j+1]) + interpolated signal
124 ( 1.0 - frac )*(signalvec->data->data[j]); value */
125
126 /* Extract the nearest signal sample. */
127 INT4 j = (INT4)floor( offset + i*dt + 0.5 );
128 REAL4 y = signalvec->data->data[j];
129
130 /* Add the signal to the output. */
131 outData[i] += y;
132 }
133
134 /* Exit. */
136 RETURN( stat );
137}
138
139/** @} */
#define INJECTH_MSGENUL
Definition: Inject.h:68
#define INJECTH_MSGEBAD
Definition: Inject.h:69
#define INJECTH_MSGEUNIT
Definition: Inject.h:70
int j
#define ABORT(statusptr, code, mesg)
#define ATTATCHSTATUSPTR(statusptr)
#define ASSERT(assertion, statusptr, code, mesg)
#define DETATCHSTATUSPTR(statusptr)
#define INITSTATUS(statusptr)
#define RETURN(statusptr)
#define INJECTH_EBAD
A sampling interval is (effectively) zero.
Definition: Inject.h:63
#define INJECTH_EUNIT
Input or output is not in units of ADC counts.
Definition: Inject.h:64
#define INJECTH_ENUL
Unexpected null pointer in arguments.
Definition: Inject.h:62
void LALSSInjectTimeSeries(LALStatus *stat, REAL4TimeSeries *output, REAL4TimeSeries *signalvec)
double REAL8
char CHAR
int32_t INT4
float REAL4
LALNameLength
int LALWarning(LALStatus *status, const char *warning)
int XLALUnitCompare(const LALUnit *unit1, const LALUnit *unit2)
const LALUnit lalADCCountUnit
int i
Definition: inspinj.c:596
stat
INT4 gpsNanoSeconds
CHAR name[LALNameLength]
REAL4Sequence * data
LALUnit sampleUnits
LIGOTimeGPS epoch
REAL4 * data
int output(const char *outfile, int outtype, REAL4TimeSeries *series)
Definition: view.c:603