LALInference  4.1.6.1-89842e6
LALInferenceRemoveLines.c
Go to the documentation of this file.
1 /*
2  * LALInferenceRemoveLines.c: Line removal functions for LALInference
3  *
4  * Copyright (C) 2013 Michael Coughlin
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 #include <stdio.h>
24 #include <stdlib.h>
25 
26 #include <lal/LALStdio.h>
27 #include <lal/LALStdlib.h>
28 
29 #include <lal/LALInspiral.h>
30 #include <lal/LALCache.h>
31 #include <lal/LALFrStream.h>
32 #include <lal/TimeFreqFFT.h>
33 #include <lal/LALDetectors.h>
34 #include <lal/AVFactories.h>
35 #include <lal/ResampleTimeSeries.h>
36 #include <lal/TimeSeries.h>
37 #include <lal/FrequencySeries.h>
38 #include <lal/Units.h>
39 #include <lal/Date.h>
40 #include <lal/StringInput.h>
41 #include <lal/VectorOps.h>
42 #include <lal/Random.h>
43 #include <lal/LALNoiseModels.h>
44 #include <lal/XLALError.h>
45 #include <lal/GenerateInspiral.h>
46 
47 #include <lal/SeqFactories.h>
48 #include <lal/DetectorSite.h>
49 #include <lal/GenerateInspiral.h>
50 #include <lal/GeneratePPNInspiral.h>
51 #include <lal/SimulateCoherentGW.h>
52 #include <lal/LIGOMetadataTables.h>
53 #include <lal/LIGOMetadataUtils.h>
54 #include <lal/LIGOMetadataInspiralUtils.h>
55 #include <lal/LIGOMetadataRingdownUtils.h>
56 #include <lal/LALInspiralBank.h>
57 #include <lal/FindChirp.h>
58 #include <lal/LALInspiralBank.h>
59 #include <lal/GenerateInspiral.h>
60 #include <lal/NRWaveInject.h>
61 #include <lal/GenerateInspRing.h>
62 #include <math.h>
63 #include <lal/LALInspiral.h>
64 #include <lal/LALSimulation.h>
65 
66 #include <lal/LALInference.h>
67 #include <lal/LALInferenceReadData.h>
68 #include <lal/LALInferenceLikelihood.h>
69 #include <lal/LALInferenceTemplate.h>
71 
72 #include <gsl/gsl_errno.h>
73 #include <gsl/gsl_rng.h>
74 #include <gsl/gsl_randist.h>
75 #include <gsl/gsl_fft_complex.h>
76 #include <gsl/gsl_fft_halfcomplex.h>
77 #include <gsl/gsl_spline.h>
78 #include <gsl/gsl_statistics.h>
79 
80 #define max(a,b) (((a)>(b))?(a):(b))
81 
82 static void median_cleanup_REAL8( REAL8FrequencySeries *work, UINT4 n );
83 static double chisqr(int Dof, double Cv);
84 static double igf(double S, double Z);
85 
87  REAL8FrequencySeries *spectrum,
88  const REAL8TimeSeries *tseries,
89  UINT4 seglen,
90  UINT4 stride,
91  const REAL8Window *window,
92  const REAL8FFTPlan *plan,
93  REAL8 *pvalues
94  )
95 {
96  REAL8FrequencySeries *work; /* array of frequency series */
97  REAL8 *bin; /* array of bin values */
98  UINT4 reclen; /* length of entire data record */
99  UINT4 numseg;
100  UINT4 seg;
101  UINT4 k,l;
102 
103  if ( ! spectrum || ! tseries || ! plan )
105  if ( ! spectrum->data || ! tseries->data )
107  if ( tseries->deltaT <= 0.0 )
109 
110  reclen = tseries->data->length;
111  numseg = 1 + (reclen - seglen)/stride;
112 
113  /* consistency check for lengths: make sure that the segments cover the
114  * * data record completely */
115  if ( (numseg - 1)*stride + seglen != reclen )
117  if ( spectrum->data->length != seglen/2 + 1 )
119 
120  /* create frequency series data workspaces */
121  work = XLALCalloc( numseg, sizeof( *work ) );
122  if ( ! work )
124  for ( seg = 0; seg < numseg; ++seg )
125  {
126  work[seg].data = XLALCreateREAL8Vector( spectrum->data->length );
127  if ( ! work[seg].data )
128  {
129  median_cleanup_REAL8( work, numseg ); /* cleanup */
131  }
132  }
133 
134  for ( seg = 0; seg < numseg; ++seg )
135  {
136  REAL8Vector savevec; /* save the time series data vector */
137  int code;
138 
139  /* save the time series data vector */
140  savevec = *tseries->data;
141 
142  /* set the data vector to be appropriate for the even segment */
143  tseries->data->length = seglen;
144  tseries->data->data += seg * stride;
145 
146  /* compute the modified periodogram for the even segment */
147  code = XLALREAL8ModifiedPeriodogram( work + seg, tseries, window, plan );
148 
149  /* restore the time series data vector to its original state */
150  *tseries->data = savevec;
151 
152  /* now check for failure of the XLAL routine */
153  if ( code == XLAL_FAILURE )
154  {
155  median_cleanup_REAL8( work, numseg ); /* cleanup */
157  }
158  }
159 
160  /* create array to hold a particular frequency bin data */
161  bin = XLALMalloc( numseg * sizeof( *bin ) );
162  if ( ! bin )
163  {
164  median_cleanup_REAL8( work, numseg ); /* cleanup */
166  }
167 
168  double meanVal = 0, normVal = 0;
169 
170  UINT4 numBins = 100;
171  int binIndex;
172  float Observed[numBins];
173  float Expected[numBins];
174  int min = 0; int max = 100;
175  int count;
176  float interval = (float)(max - min ) / numBins;
177  double CriticalValue = 0.0;
178  double XSqr, XSqrTerm;
179  double bin_val, expected_val;
180 
181  for ( k = 0; k < spectrum->data->length; ++k ) {
182  pvalues[k] = 0.0;
183  }
184 
185 
186  /* now loop over frequency bins and compute the median-mean */
187  for ( k = 0; k < spectrum->data->length; ++k )
188  {
189  /* assign array of even segment values to bin array for this freq bin */
190  for ( seg = 0; seg < numseg; ++seg ) {
191  bin[seg] = work[seg].data->data[k];
192  meanVal = meanVal + work[seg].data->data[k];
193  }
194 
195  meanVal = meanVal / (double) numseg;
196 
197  for ( l = 0; l < numBins; ++l ) {
198  Observed[l] = 0.0;
199  Expected[l] = 0.0;
200  }
201 
202  count = 0.0;
203  for ( seg = 0; seg < numseg; ++seg ) {
204  normVal = 2 * bin[seg] / meanVal;
205  binIndex = (int)((normVal- min)/interval);
206 
207  Observed[binIndex] = Observed[binIndex] + 1;
208  count = count + 1;
209  }
210 
211  CriticalValue = 0.0;
212 
213  for ( l = 0; l < numBins; ++l ) {
214 
215  bin_val = l*interval + min;
216  expected_val = count * chisqr(2,bin_val);
217 
218  Expected[l] = expected_val;
219 
220  XSqr = Observed[l] - Expected[l];
221 
222  XSqrTerm = (float) ((XSqr * XSqr) / Expected[l]);
223 
224  CriticalValue = CriticalValue + XSqrTerm;
225 
226  }
227 
228  pvalues[k] = 1.0/CriticalValue;
229 
230  }
231 
232  /* free the workspace data */
233  XLALFree( bin );
234  median_cleanup_REAL8( work, numseg );
235 
236  return 0;
237 }
238 
239 static double chisqr(int Dof, double Cv)
240 {
241  if(Cv < 0 || Dof < 1)
242  {
243  return 0.0;
244  }
245  double K = ((double)Dof) * 0.5;
246  double X = Cv * 0.5;
247  if(Dof == 2)
248  {
249  return exp(-1.0 * X);
250  }
251 
252  double PValue = igf(K, X);
253  if(isnan(PValue) || isinf(PValue))
254  {
255  return 1e-14;
256  }
257 
258  PValue /= tgamma(K);
259 
260  return (1.0 - PValue);
261 }
262 
263 static double igf(double S, double Z)
264 {
265  if(Z < 0.0)
266  {
267  return 0.0;
268  }
269  double Sc = (1.0 / S);
270  Sc *= pow(Z, S);
271  Sc *= exp(-Z);
272 
273  double Sum = 1.0;
274  double Nom = 1.0;
275  double Denom = 1.0;
276 
277  for(int k = 0; k < 200; k++)
278  {
279  Nom *= Z;
280  S++;
281  Denom *= S;
282  Sum += (Nom / Denom);
283  }
284 
285  return Sum * Sc;
286 }
287 
289 {
290  int saveErrno = xlalErrno;
291  UINT4 i;
292  for ( i = 0; i < n; ++i )
293  if ( work[i].data )
294  XLALDestroyREAL8Vector( work[i].data );
295  XLALFree( work );
296  xlalErrno = saveErrno;
297  return;
298 }
299 
301  REAL8FrequencySeries *spectrum,
302  const REAL8TimeSeries *tseries,
303  UINT4 seglen,
304  UINT4 stride,
305  const REAL8Window *window,
306  const REAL8FFTPlan *plan,
307  REAL8 *pvalues
308  )
309 {
310  REAL8FrequencySeries *work; /* array of frequency series */
311  REAL8 *bin; /* array of bin values */
312  UINT4 reclen; /* length of entire data record */
313  UINT4 numseg;
314  UINT4 seg;
315  UINT4 k,l;
316 
317  if ( ! spectrum || ! tseries || ! plan )
319  if ( ! spectrum->data || ! tseries->data )
321  if ( tseries->deltaT <= 0.0 )
323 
324  reclen = tseries->data->length;
325  numseg = 1 + (reclen - seglen)/stride;
326 
327  /* consistency check for lengths: make sure that the segments cover the
328  * * * * data record completely */
329  if ( (numseg - 1)*stride + seglen != reclen )
331  if ( spectrum->data->length != seglen/2 + 1 )
333  /* create frequency series data workspaces */
334  work = XLALCalloc( numseg, sizeof( *work ) );
335  if ( ! work )
337  for ( seg = 0; seg < numseg; ++seg )
338  {
339  work[seg].data = XLALCreateREAL8Vector( spectrum->data->length );
340  if ( ! work[seg].data )
341  {
342  median_cleanup_REAL8( work, numseg ); /* cleanup */
344  }
345  }
346 
347  for ( seg = 0; seg < numseg; ++seg )
348  {
349  REAL8Vector savevec; /* save the time series data vector */
350  int code;
351 
352  /* save the time series data vector */
353  savevec = *tseries->data;
354 
355  /* set the data vector to be appropriate for the even segment */
356  tseries->data->length = seglen;
357  tseries->data->data += seg * stride;
358 
359  /* compute the modified periodogram for the even segment */
360  code = XLALREAL8ModifiedPeriodogram( work + seg, tseries, window, plan );
361 
362  /* restore the time series data vector to its original state */
363  *tseries->data = savevec;
364 
365  /* now check for failure of the XLAL routine */
366  if ( code == XLAL_FAILURE )
367  {
368  median_cleanup_REAL8( work, numseg ); /* cleanup */
370  }
371  }
372 
373  /* create array to hold a particular frequency bin data */
374  bin = XLALMalloc( numseg * sizeof( *bin ) );
375  if ( ! bin )
376  {
377  median_cleanup_REAL8( work, numseg ); /* cleanup */
379  }
380 
381  double meanVal = 0, normVal = 0;
382 
383  UINT4 numBins = 100;
384  int binIndex;
385  float Observed[numBins];
386  float Expected[numBins];
387  float ObservedCDF[numBins];
388  float ExpectedCDF[numBins];
389  int min = 0; int max = 100;
390  int count;
391  float interval = (float)(max - min ) / numBins;
392  float ObservedSum, ExpectedSum;
393  double KS, KSVal, nKSsquared;
394  double bin_val, expected_val;
395 
396  for ( k = 0; k < spectrum->data->length; ++k ) {
397  pvalues[k] = 0.0;
398  }
399 
400  /* now loop over frequency bins and compute the median-mean */
401  for ( k = 0; k < spectrum->data->length; ++k )
402  {
403  /* assign array of even segment values to bin array for this freq bin */
404  for ( seg = 0; seg < numseg; ++seg ) {
405  bin[seg] = work[seg].data->data[k];
406  meanVal = meanVal + work[seg].data->data[k];
407  }
408 
409  meanVal = meanVal / (double) numseg;
410 
411  for ( l = 0; l < numBins; ++l ) {
412  Observed[l] = 0.0;
413  Expected[l] = 0.0;
414  ObservedCDF[l] = 0.0;
415  ExpectedCDF[l] = 0.0;
416  }
417 
418  count = 0.0;
419  for ( seg = 0; seg < numseg; ++seg ) {
420  normVal = 2 * bin[seg] / meanVal;
421  binIndex = (int)((normVal- min)/interval);
422 
423  Observed[binIndex] = Observed[binIndex] + 1;
424  count = count + 1;
425  }
426 
427  for ( l = 0; l < numBins; ++l ) {
428 
429  bin_val = l*interval + min;
430  expected_val = count * chisqr(2,bin_val);
431 
432  Expected[l] = expected_val;
433 
434  }
435 
436  ObservedSum = 0.0;
437  ExpectedSum = 0.0;
438 
439  for ( l = 0; l < numBins; ++l ) {
440 
441  if (l > 0) {
442  ObservedCDF[l] = ObservedCDF[l-1] + Observed[l];
443  ExpectedCDF[l] = ExpectedCDF[l-1] + Expected[l];
444 
445  }
446  else {
447  ObservedCDF[l] = Observed[l];
448  ExpectedCDF[l] = Expected[l];
449  }
450 
451  ObservedSum = ObservedSum + Observed[l];
452  ExpectedSum = ExpectedSum + Expected[l];
453 
454  }
455 
456  KS = 0.0;
457 
458  for ( l = 0; l < numBins; ++l ) {
459 
460  ObservedCDF[l] = ObservedCDF[l]/ObservedSum;
461  ExpectedCDF[l] = ExpectedCDF[l]/ExpectedSum;
462 
463  KSVal = ObservedCDF[l] - ExpectedCDF[l];
464 
465  if (KSVal < 0) {
466  KSVal = -KSVal;
467  }
468 
469  if (KSVal > KS) {
470  KS = KSVal;
471  }
472 
473  }
474 
475  nKSsquared = count * KS * KS;
476  pvalues[k] = 2*exp(-(2.000071+.331/sqrt(count)+1.409/count)*nKSsquared);
477 
478  }
479 
480  /* free the workspace data */
481  XLALFree( bin );
482  median_cleanup_REAL8( work, numseg );
483 
484  return 0;
485 }
486 
488  REAL8FrequencySeries *spectrum,
489  const REAL8TimeSeries *tseries,
490  UINT4 seglen,
491  UINT4 stride,
492  const REAL8Window *window,
493  const REAL8FFTPlan *plan,
494  char* filename,
495  LIGOTimeGPS GPStime
496  )
497 {
498  REAL8FrequencySeries *work; /* array of frequency series */
499  REAL8 *bin; /* array of bin values */
500  UINT4 reclen; /* length of entire data record */
501  UINT4 numseg;
502  UINT4 seg;
503  UINT4 k;
504 
505  double trigtime = GPStime.gpsSeconds+1e-9*GPStime.gpsNanoSeconds;
506  double PSDtime;
507  double *PSDtimes;
508  int *PSDtimesIndex, segindex;
509 
510  if ( ! spectrum || ! tseries || ! plan )
512  if ( ! spectrum->data || ! tseries->data )
514  if ( tseries->deltaT <= 0.0 )
516 
517  reclen = tseries->data->length;
518  numseg = 1 + (reclen - seglen)/stride;
519 
520  /* consistency check for lengths: make sure that the segments cover the
521  * * data record completely */
522  if ( (numseg - 1)*stride + seglen != reclen )
524  if ( spectrum->data->length != seglen/2 + 1 )
526 
527  /* create frequency series data workspaces */
528  work = XLALCalloc( numseg, sizeof( *work ) );
529  if ( ! work )
531  for ( seg = 0; seg < numseg; ++seg )
532  {
533  work[seg].data = XLALCreateREAL8Vector( spectrum->data->length );
534  if ( ! work[seg].data )
535  {
536  median_cleanup_REAL8( work, numseg ); /* cleanup */
538  }
539  }
540 
541  /* create array to hold a particular frequency bin data */
542  PSDtimes = XLALMalloc( (numseg-1) * sizeof( *PSDtimes ) );
543  PSDtimesIndex = XLALMalloc( (numseg-1) * sizeof( *PSDtimesIndex ) );
544 
545  int count = 0;
546 
547  for ( seg = 0; seg < numseg; ++seg )
548  {
549  REAL8Vector savevec; /* save the time series data vector */
550  int code;
551 
552  /* save the time series data vector */
553  savevec = *tseries->data;
554 
555  PSDtime = tseries->epoch.gpsSeconds + 1e-9*tseries->epoch.gpsNanoSeconds + seg*tseries->deltaT*seglen;
556  if ((PSDtime > trigtime - 0.5) & (PSDtime < trigtime + 0.5))
557  {
558  }
559  else {
560  PSDtimes[count] = PSDtime;
561  PSDtimesIndex[count] = seg;
562 
563  count = count + 1;
564  }
565 
566  /* set the data vector to be appropriate for the even segment */
567  tseries->data->length = seglen;
568  tseries->data->data += seg * stride;
569 
570  /* compute the modified periodogram for the even segment */
571  code = XLALREAL8ModifiedPeriodogram( work + seg, tseries, window, plan );
572 
573  /* restore the time series data vector to its original state */
574  *tseries->data = savevec;
575 
576  /* now check for failure of the XLAL routine */
577  if ( code == XLAL_FAILURE )
578  {
579  median_cleanup_REAL8( work, numseg ); /* cleanup */
581  }
582  }
583 
584  /* create array to hold a particular frequency bin data */
585  bin = XLALMalloc( (numseg) * sizeof( *bin ) );
586 
587  if ( ! bin )
588  {
589  median_cleanup_REAL8( work, numseg ); /* cleanup */
591  }
592 
593  double SUMx = 0, SUMy = 0, SUMxy = 0, SUMxx = 0;
594  double f, t, datavallog;
595  double slope, y_intercept, y_estimate, y_estimate_log;
596  double deltaF = work->deltaF;
597 
598  FILE *out;
599 
600  out = fopen(filename, "w");
601 
602  /* now loop over frequency bins and compute the median-mean */
603  for ( k = 0; k < spectrum->data->length; ++k )
604  {
605  f = ((double) k) * deltaF;
606 
607  fprintf(out,"%e",f);
608  /* assign array of even segment values to bin array for this freq bin */
609  for ( seg = 0; seg < numseg; ++seg ) {
610  bin[seg] = work[seg].data->data[k];
611  fprintf(out," %e ",bin[seg]);
612  }
613  fprintf(out,"\n");
614 
615  count = 0; SUMx = 0, SUMy = 0, SUMxy = 0, SUMxx = 0;
616  for ( seg = 0; seg < numseg-1; ++seg ) {
617  segindex = PSDtimesIndex[seg];
618 
619  t = PSDtimes[segindex];
620  datavallog = log10(bin[segindex]);
621 
622  SUMx = SUMx + t;
623  SUMy = SUMy + datavallog;
624  SUMxy = SUMxy + t*datavallog;
625  SUMxx = SUMxx + t*t;
626 
627  count = count + 1;
628 
629  }
630 
631  slope = ( SUMx*SUMy - count*SUMxy ) / ( SUMx*SUMx - count*SUMxx );
632  y_intercept = ( SUMy - slope*SUMx ) / count;
633 
634  y_estimate_log = slope*trigtime + y_intercept;
635  y_estimate = pow(10.0,y_estimate_log);
636 
637  spectrum->data->data[k] = y_estimate;
638 
639  }
640 
641  fclose(out);
642 
643  /* set metadata */
644  spectrum->epoch = work->epoch;
645  spectrum->f0 = work->f0;
646  spectrum->deltaF = work->deltaF;
647  spectrum->sampleUnits = work->sampleUnits;
648 
649  /* free the workspace data */
650  XLALFree( bin );
651  median_cleanup_REAL8( work, numseg );
652 
653  return 0;
654 
655 }
656 
658  REAL8FrequencySeries *spectrum,
659  const REAL8TimeSeries *tseries,
660  UINT4 seglen,
661  UINT4 stride,
662  const REAL8Window *window,
663  const REAL8FFTPlan *plan,
664  REAL8 *pvalues
665  )
666 {
667  REAL8FrequencySeries *work; /* array of frequency series */
668  REAL8 *bin; /* array of bin values */
669  UINT4 reclen; /* length of entire data record */
670  UINT4 numseg;
671  UINT4 seg;
672  UINT4 k,l;
673 
674  if ( ! spectrum || ! tseries || ! plan )
676  if ( ! spectrum->data || ! tseries->data )
678  if ( tseries->deltaT <= 0.0 )
680 
681  reclen = tseries->data->length;
682  numseg = 1 + (reclen - seglen)/stride;
683 
684  /* consistency check for lengths: make sure that the segments cover the
685  * * * data record completely */
686  if ( (numseg - 1)*stride + seglen != reclen )
688  if ( spectrum->data->length != seglen/2 + 1 )
690 
691  /* create frequency series data workspaces */
692  work = XLALCalloc( numseg, sizeof( *work ) );
693  if ( ! work )
695  for ( seg = 0; seg < numseg; ++seg )
696  {
697  work[seg].data = XLALCreateREAL8Vector( spectrum->data->length );
698  if ( ! work[seg].data )
699  {
700  median_cleanup_REAL8( work, numseg ); /* cleanup */
702  }
703  }
704 
705  for ( seg = 0; seg < numseg; ++seg )
706  {
707  REAL8Vector savevec; /* save the time series data vector */
708  int code;
709 
710  /* save the time series data vector */
711  savevec = *tseries->data;
712 
713  /* set the data vector to be appropriate for the even segment */
714  tseries->data->length = seglen;
715  tseries->data->data += seg * stride;
716 
717  /* compute the modified periodogram for the even segment */
718  code = XLALREAL8ModifiedPeriodogram( work + seg, tseries, window, plan );
719 
720  /* restore the time series data vector to its original state */
721  *tseries->data = savevec;
722 
723  /* now check for failure of the XLAL routine */
724  if ( code == XLAL_FAILURE )
725  {
726  median_cleanup_REAL8( work, numseg ); /* cleanup */
728  }
729  }
730 
731  /* create array to hold a particular frequency bin data */
732  bin = XLALMalloc( numseg * sizeof( *bin ) );
733  if ( ! bin )
734  {
735  median_cleanup_REAL8( work, numseg ); /* cleanup */
737  }
738 
739  int count;
740  double SUMx = 0, SUMy = 0, SUMxy = 0, SUMxx = 0;
741  double res, slope, y_intercept, y_estimate_log;
742 
743  double f, dataval, flog, datavallog;
744 
745  double deltaF = spectrum->deltaF;
746 
747  for ( k = 0; k < spectrum->data->length; ++k ) {
748  pvalues[k] = 0.0;
749  }
750 
751  UINT4 numBands = 4;
752  float frequencyBands[4];
753  frequencyBands[0] = 0, frequencyBands[1] = 10, frequencyBands[2] = 100, frequencyBands[3] = 2048;
754 
755  for ( l = 0; l < numBands-1; ++l)
756  {
757 
758  count = 0, SUMx = 0, SUMy = 0, SUMxy = 0, SUMxx = 0;
759  /* now loop over frequency bins and compute the median-mean */
760  for ( k = 0; k < spectrum->data->length; ++k )
761  {
762 
763  f = ((double) k) * deltaF;
764  dataval = spectrum->data->data[k];
765  flog = log10(f);
766  datavallog = log10(dataval);
767 
768  if ((f>=frequencyBands[l]) & (f<=frequencyBands[l+1])) {
769 
770  SUMx = SUMx + flog;
771  SUMy = SUMy + datavallog;
772  SUMxy = SUMxy + flog*datavallog;
773  SUMxx = SUMxx + flog*flog;
774 
775  count = count + 1;
776 
777  }
778  }
779 
780  slope = ( SUMx*SUMy - count*SUMxy ) / ( SUMx*SUMx - count*SUMxx );
781  y_intercept = ( SUMy - slope*SUMx ) / count;
782 
783  for (k=0; k<spectrum->data->length; ++k) {
784 
785  f = ((double) k) * deltaF;
786  dataval = spectrum->data->data[k];
787  flog = log10(f);
788  datavallog = log10(dataval);
789 
790  if ((f>=frequencyBands[l]) & (f<=frequencyBands[l+1])) {
791 
792  y_estimate_log = slope*flog + y_intercept;
793 
794  res = fabs(datavallog - y_estimate_log);
795  pvalues[k] = 1.0/res;
796  }
797  }
798  }
799 
800 
801  /* free the workspace data */
802  XLALFree( bin );
803  median_cleanup_REAL8( work, numseg );
804 
805  return 0;
806 }
807 
809  REAL8FrequencySeries *spectrum,
810  const REAL8TimeSeries *tseries,
811  UINT4 seglen,
812  UINT4 stride,
813  const REAL8Window *window,
814  const REAL8FFTPlan *plan,
815  REAL8 *pvalues,
816  char* filename
817  )
818 {
819  REAL8FrequencySeries *work; /* array of frequency series */
820  REAL8 *bin; /* array of bin values */
821  UINT4 reclen; /* length of entire data record */
822  UINT4 numseg;
823  UINT4 seg;
824  UINT4 k,l;
825 
826  if ( ! spectrum || ! tseries || ! plan )
828  if ( ! spectrum->data || ! tseries->data )
830  if ( tseries->deltaT <= 0.0 )
832 
833  reclen = tseries->data->length;
834  numseg = 1 + (reclen - seglen)/stride;
835 
836  /* consistency check for lengths: make sure that the segments cover the
837  * * * * * data record completely */
838  if ( (numseg - 1)*stride + seglen != reclen )
840  if ( spectrum->data->length != seglen/2 + 1 )
842 
843  /* create frequency series data workspaces */
844  work = XLALCalloc( numseg, sizeof( *work ) );
845  if ( ! work )
847  for ( seg = 0; seg < numseg; ++seg )
848  {
849  work[seg].data = XLALCreateREAL8Vector( spectrum->data->length );
850  if ( ! work[seg].data )
851  {
852  median_cleanup_REAL8( work, numseg ); /* cleanup */
854  }
855  }
856 
857  for ( seg = 0; seg < numseg; ++seg )
858  {
859  REAL8Vector savevec; /* save the time series data vector */
860  int code;
861 
862  /* save the time series data vector */
863  savevec = *tseries->data;
864 
865  /* set the data vector to be appropriate for the even segment */
866  tseries->data->length = seglen;
867  tseries->data->data += seg * stride;
868 
869  /* compute the modified periodogram for the even segment */
870  code = XLALREAL8ModifiedPeriodogram( work + seg, tseries, window, plan );
871 
872  /* restore the time series data vector to its original state */
873  *tseries->data = savevec;
874 
875  /* now check for failure of the XLAL routine */
876  if ( code == XLAL_FAILURE )
877  {
878  median_cleanup_REAL8( work, numseg ); /* cleanup */
880  }
881  }
882 
883  /* create array to hold a particular frequency bin data */
884  bin = XLALMalloc( numseg * sizeof( *bin ) );
885  if ( ! bin )
886  {
887  median_cleanup_REAL8( work, numseg ); /* cleanup */
889  }
890 
891  double mx,my,sx,sy,sxy,denom,r;
892 
893  for ( k = 0; k < spectrum->data->length; ++k ) {
894  pvalues[k] = 0.0;
895  }
896 
897  FILE *out;
898 
899  out = fopen(filename, "w");
900 
901  for ( k = 0; k < spectrum->data->length; ++k )
902  {
903  fprintf(out,"%e",((double) k) * work->deltaF);
904 
905  /* now loop over frequency bins and compute the median-mean */
906  for ( l = 0; l < spectrum->data->length; ++l )
907  {
908 
909  /* Calculate the mean of the two series x[], y[] */
910  mx = 0;
911  my = 0;
912  for ( seg = 0; seg < numseg; ++seg ) {
913  mx += work[seg].data->data[k];
914  my += work[seg].data->data[l];
915  }
916  mx /= numseg;
917  my /= numseg;
918 
919  /* Calculate the denominator */
920  sx = 0;
921  sy = 0;
922  for ( seg = 0; seg < numseg; ++seg ) {
923  sx += (work[seg].data->data[k] - mx) * (work[seg].data->data[k] - mx);
924  sy += (work[seg].data->data[l] - my) * (work[seg].data->data[l] - my);
925  }
926  denom = sqrt(sx*sy);
927 
928  sxy = 0;
929  for ( seg = 0; seg < numseg; ++seg ) {
930  sxy += (work[seg].data->data[k] - mx) * (work[seg].data->data[l] - my);
931  }
932  r = fabs(sxy / denom);
933 
934  fprintf(out," %e ",r);
935 
936  }
937 
938  fprintf(out,"\n");
939 
940  }
941 
942  fclose(out);
943 
944  /* free the workspace data */
945  XLALFree( bin );
946  median_cleanup_REAL8( work, numseg );
947 
948  return 0;
949 }
static void median_cleanup_REAL8(REAL8FrequencySeries *work, UINT4 n)
static double chisqr(int Dof, double Cv)
static double igf(double S, double Z)
#define max(a, b)
int k
#define fprintf
int l
double e
const double sx
const double sy
sigmaKerr data[0]
double REAL8
uint32_t UINT4
int LALInferenceRemoveLinesChiSquared(REAL8FrequencySeries *spectrum, const REAL8TimeSeries *tseries, UINT4 seglen, UINT4 stride, const REAL8Window *window, const REAL8FFTPlan *plan, REAL8 *pvalues)
Determine non-Gaussian frequency bins using a chi-squared test.
int LALInferenceXCorrBands(REAL8FrequencySeries *spectrum, const REAL8TimeSeries *tseries, UINT4 seglen, UINT4 stride, const REAL8Window *window, const REAL8FFTPlan *plan, REAL8 *pvalues, char *filename)
Determine correlated frequency bands using cross correlation.
int LALInferenceRemoveLinesPowerLaw(REAL8FrequencySeries *spectrum, const REAL8TimeSeries *tseries, UINT4 seglen, UINT4 stride, const REAL8Window *window, const REAL8FFTPlan *plan, REAL8 *pvalues)
Determine large amplitude frequency bins using power law fit.
int LALInferenceAverageSpectrumBinFit(REAL8FrequencySeries *spectrum, const REAL8TimeSeries *tseries, UINT4 seglen, UINT4 stride, const REAL8Window *window, const REAL8FFTPlan *plan, char *filename, LIGOTimeGPS GPStime)
Calculate PSD by fitting bins to lines.
int LALInferenceRemoveLinesKS(REAL8FrequencySeries *spectrum, const REAL8TimeSeries *tseries, UINT4 seglen, UINT4 stride, const REAL8Window *window, const REAL8FFTPlan *plan, REAL8 *pvalues)
Determine non-Gaussian frequency bins using a K-S test.
void * XLALCalloc(size_t m, size_t n)
void * XLALMalloc(size_t n)
void XLALFree(void *p)
static const INT4 r
int XLALREAL8ModifiedPeriodogram(REAL8FrequencySeries *periodogram, const REAL8TimeSeries *tseries, const REAL8Window *window, const REAL8FFTPlan *plan)
REAL8Vector * XLALCreateREAL8Vector(UINT4 length)
void XLALDestroyREAL8Vector(REAL8Vector *vector)
#define xlalErrno
#define XLAL_ERROR(...)
XLAL_EBADLEN
XLAL_ENOMEM
XLAL_EFAULT
XLAL_EFUNC
XLAL_EINVAL
XLAL_FAILURE
INT4 gpsNanoSeconds
REAL8Sequence * data
REAL8Sequence * data
LIGOTimeGPS epoch
REAL8 * data