LAL  7.5.0.1-b72065a
ReadNoiseSpectrum.c
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2007 Jolien Creighton, Patrick Brady
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 <stdlib.h>
21 #include <math.h>
22 #include <errno.h>
23 #include <lal/LALStdlib.h>
24 #include <lal/Units.h>
25 #include <lal/LALStdio.h>
26 #include <lal/FileIO.h>
27 #include <lal/Interpolate.h>
28 #include <lal/ReadNoiseSpectrum.h>
29 
30 /*********************************************************************
31  * STATIC FUNCTION to locate the point nearest to the desired
32  * frequency for the interpolation step below
33  *********************************************************************/
34 static UINT4 mylocate(REAL8 *farray, REAL8 target, UINT4 n)
35 {
36  UINT4 i=0;
37  UINT4 jmin=0;
38 
39  for (i=0 ; i<n ; i++){
40  if ( (farray[i]-target) > 0 ){
41  break;
42  }
43  }
44 
45  jmin=i;
46 
47  if (i>0){
48  if ( (target - farray[i-1]) < (farray[i] - target) ){
49  jmin = i-1;
50  }
51  }
52 
53  return jmin;
54 }
55 
56 
57 /*********************************************************************
58  * MAIN FUNCTION to get populate the amplitude spectrum from the data
59  * file specified.
60  *********************************************************************/
61 
62 
63 /**
64  * \ingroup ReadNoiseSpectrum_h
65  * \brief Function to read in noise spectrum from a formatted ascii file and return the
66  * amplitude noise spectrum in \f$\textrm{strain}/\sqrt{\textrm{Hz}}\f$.
67  *
68  * ### Description ###
69  *
70  * <tt>LALReadNoiseSpectrum()</tt> fills the contents of the REAL4FrequencySeries
71  * \c spectrum from data read from formatted ascii file with name \c fname.
72  * The ascii file should have a header (greater than or equal to one line) which is
73  * indicated by a \f$\#\f$ at the start of the line. The first line of the file must
74  * contain the number of points at which the spectrum is sampled. If the spectrum
75  * is sampled at 500 different points, then the first line would be
76  * \code
77  * # npoints=500
78  * \endcode
79  * Replace 500 by the number of sample points in your particular data.
80  *
81  * The REAL4FrequencySeries \c spectrum should be allocated before calling the
82  * routine which uses the \c length and metadata information to determine the
83  * evenly sampled output that is reqruired. The function does nearest neighbor
84  * interpolation to get the points in the outpu frequency series.
85  *
86  */
87  void
89 {
90 
91  FILE *fp=NULL; /* where the spectrum data is stored */
93  INT4 npoints; /* number of points in the specrtum */
94  UINT4 j; /* dummy index*/
95  REAL8 *f=NULL; /* dummy variable for frequency values */
96  REAL8 *s=NULL; /* dummy variable for spectrum values */
97  REAL4 freq, myfmin, df;
98  UINT4 location;
99  DInterpolateOut intOutput;
100  DInterpolatePar intParams;
101 
102  INITSTATUS(stat);
103  ATTATCHSTATUSPTR(stat);
104 
105  /* this is the file containing the spectrum data */
106  if ( !(fp = LALFopen( fname, "r" )) )
107  {
109  }
110 
111  /* read in the first line of the file */
112  if (fgets(line,sizeof(line),fp) == NULL)
113  {
115  }
116 
117  /* check that it has the right format */
118  if ( (strstr(line,"# npoints=")) == NULL)
119  {
121  }
122  sscanf(line,"# npoints=%i", &npoints);
123 
124  /* memory for the input data */
125  f = (REAL8 *) LALMalloc( npoints * sizeof(REAL8) );
126  s = (REAL8 *) LALMalloc( npoints * sizeof(REAL8) );
127 
128  /* read data into arrays */
129  j=0;
130  while (1) {
131  if (fgets(line,sizeof(line),fp)==NULL) {
132  LALFclose(fp);
133  break;
134  }
135  if (line[0] != '#'){
136  sscanf(line,"%lf %lf\n",&f[j],&s[j]);
137  j++;
138  }
139  }
140 
141  /* populate the frequency series */
142  intParams.n=4;
143  location = 0;
144  myfmin = spectrum->f0;
145  df = spectrum->deltaF;
146  for(j=0 ; j < spectrum->data->length ; j++) {
147  freq = myfmin + ((REAL4) j)*df;
148 
149  /* if the frequency is above lowest in noise file ... */
150  if ( freq >= f[0] )
151  {
152  /* ... interpolate to desired frequency ... */
153  location=mylocate(f,(REAL8)freq,npoints);
154  if (location > (npoints-intParams.n)){
155  location = (npoints-intParams.n);
156  }
157  else if ( location < (intParams.n/2) ){
158  location = 0;
159  }
160  else {
161  location-=(intParams.n/2);
162  }
163  intParams.x = &f[location];
164  intParams.y = &s[location];
165  LALDPolynomialInterpolation(stat->statusPtr, &intOutput, freq, &intParams);
166  CHECKSTATUSPTR (stat);
167  spectrum->data->data[j] = (REAL4)(intOutput.y);
168  }
169 
170  /* ...... otherwise, just fill be lowest available value */
171  else {
172  spectrum->data->data[j] = s[0];
173  }
174  }
175 
176  /* set the units to strain / sqrt(Hz) */
177  memset( &(spectrum->sampleUnits), 0, sizeof(LALUnit) );
181 
182  /* .... and clean up */
183  LALFree( f );
184  LALFree( s );
185 
186  DETATCHSTATUSPTR(stat);
187  RETURN(stat);
188 }
#define LALMalloc(n)
Definition: LALMalloc.h:93
#define LALFree(p)
Definition: LALMalloc.h:96
#define ABORT(statusptr, code, mesg)
#define CHECKSTATUSPTR(statusptr)
#define ATTATCHSTATUSPTR(statusptr)
#define DETATCHSTATUSPTR(statusptr)
#define INITSTATUS(statusptr)
#define RETURN(statusptr)
static UINT4 mylocate(REAL8 *farray, REAL8 target, UINT4 n)
#define LALREADNOISESPECTRUMH_MSGEOPEN
#define LALREADNOISESPECTRUMH_MSGEPARS
#define LALREADNOISESPECTRUM_MAXLINELENGTH
static double f(double theta, double y, double xi)
Definition: XLALMarcumQ.c:258
void LALDPolynomialInterpolation(LALStatus *status, DInterpolateOut *output, REAL8 target, DInterpolatePar *params)
Definition: Interpolate.c:115
double REAL8
Double precision real floating-point number (8 bytes).
char CHAR
One-byte signed integer, see Headers LAL(Atomic)Datatypes.h for more details.
uint32_t UINT4
Four-byte unsigned integer.
int32_t INT4
Four-byte signed integer.
float REAL4
Single precision real floating-point number (4 bytes).
@ LALUnitIndexSecond
The second index.
Definition: LALDatatypes.h:475
@ LALUnitIndexStrain
The strain index.
Definition: LALDatatypes.h:478
#define LALFclose
Definition: LALStdio.h:51
#define LALFopen
Definition: LALStdio.h:50
void LALReadNoiseSpectrum(LALStatus *stat, REAL4FrequencySeries *spectrum, CHAR *fname)
Function to read in noise spectrum from a formatted ascii file and return the amplitude noise spectru...
#define LALREADNOISESPECTRUMH_EOPEN
Error opening file.
#define LALREADNOISESPECTRUMH_EPARS
Error parsing spectrum file.
These structures contain the output of the interpolation.
Definition: Interpolate.h:114
REAL8 y
The interpolated value.
Definition: Interpolate.h:115
These structures contain the interpolation parameters; These are the arrays of n domain values and t...
Definition: Interpolate.h:141
REAL8 * y
The array of values to interpolate.
Definition: Interpolate.h:144
REAL8 * x
The array of domain values.
Definition: Interpolate.h:143
UINT4 n
The number of points in the arrays to use in the interpolation.
Definition: Interpolate.h:142
LAL status structure, see The LALStatus structure for more details.
Definition: LALDatatypes.h:947
struct tagLALStatus * statusPtr
Pointer to the next node in the list; NULL if this function is not reporting a subroutine error.
Definition: LALDatatypes.h:954
This structure stores units in the mksA system (plus Kelvin, Strain, and ADC Count).
Definition: LALDatatypes.h:498
UINT2 unitDenominatorMinusOne[LALNumUnits]
Array of unit power denominators-minus-one.
Definition: LALDatatypes.h:501
INT2 unitNumerator[LALNumUnits]
Array of unit power numerators.
Definition: LALDatatypes.h:500
See DATATYPE-FrequencySeries types for documentation.
Definition: LALDatatypes.h:879
REAL4Sequence * data
Definition: LALDatatypes.h:885
REAL4 * data
Pointer to the data array.
Definition: LALDatatypes.h:150
UINT4 length
Number of elements in array.
Definition: LALDatatypes.h:149
FILE * fp
Definition: tconvert.c:105