Logo Coherent WaveBurst  
Reference Guide
Logo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
wavelinefilter.cc
Go to the documentation of this file.
1 /************************************************************************
2  * Sergey Klimenko, University of Florida
3  *
4  * v-1.63: 08/07/01
5  * - addded flag reFine (force to do fScan if !reFine)
6  * set true by default, set false if SNR<0
7  * - corrected situation when getOmega fails to find frequency
8  * getOmega always uses FilterID=1
9  * - correct frequency band fBand if start not from the first harmonic
10  * v-2.0: 11/08/01
11  * - svitched to new class of wavelet transforms
12  * replace wavearray<double> class with wavearray class
13  ************************************************************************/
14 
15 #include "wavelinefilter.hh"
16 //#include "DVector.hh"
17 #include "Biorthogonal.hh"
18 #include <iostream>
19 
20 using namespace std;
21 
23  FilterID(1), Frequency(60.), Window(0.0), Stride(1.), nFirst(1), nLast(0),
24  nStep(1), nScan(20), nBand(5), nSubs(1), fBand(0.45), nLPF(-1), nWave(16),
25  clean(false), badData(false), noScan(false), nRIF(6), SNR(2.), reFine(true),
26  dumpStart(0), FilterState(0), SeedFrequency(60.), CurrentTime(0.),
27  StartTime(0.), Sample(0.0)
28 {
29  reset();
30 }
31 
32 /* linefilter constructor
33  * f - approximate line frequency
34  * w - time interval to remove lines.
35  *fid - select filter ID (-1/0/1), -1 by default
36  * nT - number of subdivisions of time interval w
37  */
38 linefilter::linefilter(double f, double w, int fid, int nT) :
39  FilterID(1), Frequency(60.), Window(0.0), Stride(1.), nFirst(1), nLast(0),
40  nStep(1), nScan(20), nBand(5), nSubs(1), fBand(0.45), nLPF(-1), nWave(16),
41  clean(false), badData(false), noScan(false), nRIF(6), SNR(2.), reFine(true),
42  dumpStart(0), FilterState(0), SeedFrequency(60.), CurrentTime(0.),
43  StartTime(0.), Sample(0.0)
44 {
45  reset();
46  Frequency = fabs(f);
48  FilterID = fid;
49  Window = w;
50  if(f < 0.) clean = true;
51  nSubs = (nT > 0) ? nT : 1;
52 }
53 
55 
57  : FilterID(x.FilterID), Frequency(x.Frequency), Window(x.Window),
58  Stride(x.Stride), nFirst(x.nFirst), nLast(x.nLast), nStep(x.nStep),
59  nScan(x.nScan), nBand(x.nBand), nSubs(x.nSubs), fBand(x.fBand),
60  nLPF(x.nLPF), nWave(x.nWave), clean(x.clean), badData(false),
61  noScan(x.noScan), nRIF(x.nRIF), SNR(x.SNR), reFine(x.reFine),
62  dumpStart(0), FilterState(0), SeedFrequency(60.), CurrentTime(0.),
63  StartTime(0.), Sample(0.)
64 {}
65 
67 linefilter::clone(void) const {
68  return new linefilter(*this);
69 }
70 
71 unsigned int linefilter::maxLine(int L)
72 {
73  unsigned int imax = (nLPF>0) ? int(L/2)+1 : int(L/4)+1;
74  if(nFirst > imax) std::cout<<"linefilter: Invalid harmonic number.\n";
75  if(imax>nLast && nLast>0) imax = nLast+1;
76  if(imax <= nFirst) imax = nFirst+1;
77  if(imax > (unsigned)L/2) imax = L/2;
78  return imax;
79 }
80 
81 
82 /***setFilter*********************************************************
83  * nF - first harmonic
84  * nL - last harmonic
85  * nS - skip harmonics
86  * nD - decimation depth (resampling)
87  * nB - number of frequency bins to estimate noise spectral density
88  * nR - order of interpolating filter for resample
89  * nW - lifting wavelet order
90  *********************************************************************/
91  void linefilter::setFilter(int nF,
92  int nL,
93  int nS,
94  int nD,
95  int nB,
96  int nR,
97  int nW){
98 
99  reset();
100  if(nS == 0) nS++;
101  nStep = nS;
102  nFirst = nF;
103  nLast = nL;
104 
105  if(nS<0){
106  nFirst *= -nStep;
107  nLast *= -nStep;
108  Frequency /= -nStep;
110  }
111 
112  nBand = (nB>=2) ? nB : 2;
113  nWave = nW;
114  nLPF = nD;
115  nRIF = nR;
116  return;
117 }
118 
120  FilterState = 0;
121  CurrentTime = 0.;
122  StartTime = 0.;
123  Sample = 0.0;
124  badData = false;
125  lineList.clear();
126  dumpStart = 0;
128 }
129 
130 void linefilter::resize(size_t dS) {
131 
132  if(dS == 0){ // clear the list
133  lineList.clear();
134  dumpStart = 0;
135  }
136 
137  else{ // pop entries from the beginnig of the list
138 
139  if(lineList.size() <= dS)
140  dumpStart = lineList.size();
141 
142  else
143  do { lineList.pop_front(); }
144  while(lineList.size() > dumpStart);
145 
146  }
147 
148  return;
149 }
150 
151 
152 void linefilter::setFScan(double f,
153  double sN,
154  double fB,
155  int nS)
156 {
157  noScan = true;
158  fBand = fabs(fB);
159  nScan = nS;
160  SNR = fabs(sN);
161  reFine = (sN>0) ? true : false;
162  if(f != 0.) {
163  Frequency = nStep>0 ? fabs(f) : fabs(f/nStep);
164  noScan = (f < 0.) ? true : false;
165  }
166  return;
167 }
168 
169 /****************************************************************************
170  * Function estimates Power Spectral Density for input TD time series.
171  * Return WaveData object with average Spectral Density near harmonics
172  * (f,2*f,3*f,..imax*f).
173  * Spectral Density is estimated using nb Fourier bins around harmonics.
174  ****************************************************************************/
175 
177 {
178  int i,j,k;
179  double a, b, c;
180  int L = int(TD.rate()/Frequency + 0.5); // # of samples in one line cycle
181  int m = int(TD.size()/nSubs); // length of 1/nSubs TD
182  int n = int(m/(nb*L))*L; // length of 1/nb/nSubs TD
183 
184  WaveData td2(2*L); // S.K. wavefft 9/10/20002
185  WaveData tds(L);
186  WaveData tdn(L);
187  WaveData tdx(n);
188  WaveData psd(L/2);
189 
190  if(nb < 1) nb = 1;
191 
192  psd = 0.;
193  if (n/L == 0) {
194  cout <<" linefilter::getPSD error: time series is too short to contain\n"
195  <<" one cycle of fundamental harmonic " << Frequency << "\n";
196  return psd;
197  }
198 
199  c = (nb>1) ? 1./(nb-1) : 1.;
200  c *= Window/nSubs/nb/nSubs; // before 03/24/01 was c *= double(m)/nSubs/TD.rate()/nb;
201  psd.rate(TD.rate());
202 
203 // cout<<"c="<<c<<" n="<<n<<" m="<<m<<" L="<<L<<"\n";
204 
205  for(k = 0; k < nSubs; k++){
206  psd.data[0] += tdn.Stack(TD, m, m*k);
207 
208  for(j = 0; j < nb; j++){
209  if(nb > 1) {
210  psd.data[0] -= tds.Stack(TD, n, n*j+m*k);
211  tds -= tdn;
212  }
213  else
214  tds = tdn;
215 
216  tds.hann();
217 // tds.FFTW(1); // calculate FFT for stacked signal
218 
219 // SK wavefft fix 9/10/2002
220  td2.rate(tds.rate());
221  td2.cpf(tds); td2.cpf(tds,L,0,L);
222  td2.FFTW(1);
223  tds[slice(0,L/2,2)] << td2[slice(0,L/2,4)];
224  tds[slice(1,L/2,2)] << td2[slice(1,L/2,4)];
225 // SK wavefft fix 9/10/2002: end
226 
227  for (i = 2; i < L-1; i+=2){ // always neglect last harmonic
228  a = tds.data[i]; b = tds.data[i+1];
229  psd.data[i/2] += c*(a*a+b*b); // save spectral density
230  // cout<<"nb="<<nb<<" i="<<i<<" PSD="<<psd.data[i/2]<<endl;
231  }
232  }
233  }
234 
235  return psd;
236 }
237 
238 /******************************************************************
239  * makeFilter calculates comb filter.
240  * Returns the line intensity at the filter output
241  * nFirst - "number" of the first harmonic
242  * nLast - "number" of the last harmonic
243  * nStep - skip harmonics
244  * FilterID - select the filter #, 1 by default
245  * nBand - number of frequency bins used to estimate the noise SD
246  *****************************************************************/
247 double linefilter::makeFilter(const WaveData &TD, int FID)
248 {
249  if (badData) {
250  cout <<" linefilter::MakeFilter() error: badData flag is on\n";
251  return 0.;
252  }
253 
254  double S, N;
255  unsigned int i;
256  int L = int(TD.rate()/Frequency+0.5); // number of samples per one cycle
257  int k = int(TD.size()/L);
258 
259  if (k == 0) {
260  cout <<" linefilter::MakeFilter() error: data length too short to contain\n"
261  <<" at least one cycle of target frequency = " << Frequency << " Hz\n";
262  badData = true;
263  return 0.;
264  }
265 
266  unsigned int imax = maxLine(L); // max number of harmonics
267 
268  if((int)Filter.size() < L/2) Filter.resize(L/2);
269  Filter = 0.;
270  for (i = nFirst; i < imax; i += abs(nStep)) Filter.data[i] = 1.;
271 
272  LineSD = getPSD(TD); // Line energy + NSD
273 
274  if (FID == 1) {
275  NoiseSD = getPSD(TD,nBand); // Line energy + NSD*nBand
276  for (i = nFirst; i < imax; i += abs(nStep)) {
277  S = LineSD.data[i];
278  N = NoiseSD.data[i];
279  Filter.data[i] = (S>N && S>0) ? (1-N/S) : 0.;
280  // cout<<"S="<<S<<" N="<<N<<" F="<<Filter.data[i]<<endl;
281  }
282  }
283 
284  S = 0.;
285  N = 0.;
286  for (i = nFirst; i < imax; i += abs(nStep)) {
287  S += LineSD.data[i]*Filter.data[i]*Filter.data[i];
288  N += (FID == 1) ? NoiseSD.data[i]*Filter.data[i] : 0.;
289  }
290 
291  if(S < SNR*N || S <= 0.) badData = true;
292  // if(badData) cout << S << " FID=" << FID << endl;
293  return S;
294 }
295 
296 /*******************************************************************
297  * getLine() replaces the original data TD with calculated
298  * interference signal. Filter F is applied. Returns linedata object
299  * with the line summary information. Works with resampled data
300  *******************************************************************/
301 
303 {
304  double a,b;
305  int iF;
306  double F;
307  linedata v;
308 
309  v.frequency = 0.;
310  v.intensity = 0.;
312 
313  if (Frequency <= 0) {
314  cout << " getLine() error: invalid interference frequency"
315  << " : " << Frequency << " Hz\n";
316  return v;
317  }
318 
319  int L = int(TD.rate()/Frequency + 0.5); // number of samples per cycle
320  int n = int(TD.size()/nSubs); // data section length
321 
322  unsigned int imax = maxLine(L); // max number of harmonics
323 
324  if (n/L == 0 || L < 4) {
325  cout << " getLine() error: input data length too short to contain\n"
326  << " one cycle of target frequency = " << Frequency << " Hz\n";
327  return v;
328  }
329 
330  WaveData td2(2*L); // SK wavefft fix 9/10/2002
331  WaveData tds(L);
332  WaveData amp(L);
333  amp *= 0.;
334 
335  double p = axb(Frequency, n/TD.rate());
336  double phase = 0.;
337 
338  v.frequency = Frequency;
339  v.intensity = 0.;
340 
341  for (int k = 0; k < nSubs; k++) {
342  tds.Stack(TD, n, n*k); // stack signal
343 
344  if(!clean) tds.hann();
345 
346 // SK wavefft fix 9/10/2002
347  td2.rate(tds.rate());
348  td2.cpf(tds); td2.cpf(tds,L,0,L);
349  td2.FFTW(1);
350  tds[slice(0,L/2,2)]<<td2[slice(0,L/2,4)];
351  tds[slice(1,L/2,2)]<<td2[slice(1,L/2,4)];
352 // SK wavefft fix 9/10/2002: end
353 
354 //*************************************************************
355 // reconstruction of filtered interference signal
356 //*************************************************************
357  // cout<<"tds.size="<<tds.size()<<" L="<<L-1<<" imax="<<imax
358  // <<" nFirst="<<nFirst<<" nLast="<<nLast<<" Filter.size="<<Filter.size()<<endl;
359 
360 // apply the filter
361  for (unsigned int i = 0; i < (unsigned)L-1; i+=2) {
362  F = Filter.data[i/2];
363  tds.data[i] *= F;
364  tds.data[i+1] *= F;
365  if(F > 0.){
366  a = tds.data[i]; b = tds.data[i+1];
367  amp.data[i] += (a*a + b*b)/nSubs;
368 
369  if(k==0){
370  phase = arg(f_complex(a,b));
371  amp.data[i+1] = phase;
372  }
373  else{
374  b = arg(f_complex(a,b));
375  a = (b-phase)/2./PI - axb(p,double(k*(i/2)));
376  a -= (a>0.) ? long(a+0.5) : long(a-0.5);
377  amp.data[i+1] += 2*PI*a/nSubs;
378  phase = b;
379  }
380  }
381  }
382  if((L&1) == 1) tds.data[L-1] = 0.; // take care of odd L
383 
384 // for(int jj=0; jj<tds.size(); jj++) cout<<"i="<<jj<<" tds="<<tds.data[jj]<<endl;
385 
386 // SK wavefft fix 9/10/2002
387  td2=0.;
388  td2[slice(0,L/2,4)]<<tds[slice(0,L/2,2)];
389  td2[slice(1,L/2,4)]<<tds[slice(1,L/2,2)];
390  td2.FFTW(-1);
391  tds.cpf(td2,L);
392 // SK wavefft fix 9/10/2002: end
393 
394  tds.getStatistics(a,b);
395  v.intensity += b*b;
396 
397  iF = (k == nSubs-1) ? TD.size() : n*k+n;
398  if(clean) // reconstruct interference to clean data
399  for (int i = 0; i < L; i++)
400  for(int j = n*k+i; j < iF; j += L)
401  TD.data[j] = tds.data[i];
402  }
403 
404 // add amplitudes and noise spectral density into the linedata
405  iF = imax-nFirst;
406  v.amplitude.resize(iF);
407  v.line.resize(iF);
408  v.noise.resize(iF);
409  v.filter.resize(iF);
410 
411  for (unsigned int i = nFirst; i < imax; i += abs(nStep)) {
412  iF = i-nFirst;
413  v.line[iF] = LineSD.data[i];
414  v.noise[iF] = (FilterID == 0) ? 0. : NoiseSD.data[i];
415  v.filter[iF] = Filter.data[i];
416  a = amp.data[2*i];
417  b = amp.data[2*i+1];
418  v.amplitude[iF] = float(2.*float(sqrt(a)))*exp(f_complex(0.,b));
419  if(!clean) v.amplitude[iF] *= sqrt(1.5); // correct for Hann (9/3/02)
420  }
421 
422 // calculate inverse FFT to reproduce one cycle of filtered signal
423  v.intensity /= nSubs;
424  if(!clean) v.intensity *= 1.5; // correct for Hann (9/3/02)
425  v.first = nFirst;
426 
427  return v;
428 }
429 
430 /*******************************************************************
431  * heterodyne() estimates single line amplitude and phase by using
432  * straight heterodyning. It replaces the original data TD with calculated
433  * interference signal. Returns linedata object
434  * with the line summary information. Works with original data
435  *******************************************************************/
436 
438 {
439  long i,k,m;
440  double a,b,u,v;
441  register double C,S, c,s, ch,sh, x;
442  double omega;
443  long N = TD.size();
444  register double* p = TD.data;
445  size_t mode;
446  linedata Q;
447 
448  Q.frequency = 0.;
449  Q.intensity = 0.;
451 
452  if (Frequency <= 0) {
453  cout << " getLine() error: invalid interference frequency"
454  << " : " << Frequency << " Hz\n";
455  return Q;
456  }
457 
458  int L = int(TD.rate()/Frequency); // number of samples per cycle
459  int n = int(TD.size()/nSubs); // data sub-section length
460  size_t imax = L; // max number of harmonics
461  size_t M = imax-nFirst; // number of modes
462 
463  omega = 2*PI*nFirst*Frequency/TD.rate();
464 
465  // tabulate sin and cos
466 
467  if(!lineList.size()) {
468  ct.resize(n);
469  st.resize(n);
470  wt.resize(n);
471  for(i=0; i<n; i++){
472  ct.data[i] = cos(i*omega);
473  st.data[i] = sin(i*omega);
474  wt.data[i] = 1.-cos(i*2.*PI/double(n));
475  }
476  }
477 
478  WaveData amp(nSubs*M);
479  WaveData phi(nSubs*M);
480 
481  amp = 0.;
482  phi = 0.;
483 
484  Q.frequency = Frequency;
485  Q.intensity = 0.;
486 
487  for (mode = nFirst; mode < imax; mode += abs(nStep)) {
488  m = nSubs*(mode-nFirst)/abs(nStep);
489  omega = 2*PI*mode*Frequency/TD.rate();
490  c = cos(omega);
491  s = sin(omega);
492  p = TD.data;
493 
494  ch = cos(2*PI/double(n));
495  sh = sin(2*PI/double(n));
496 
497  for (k = 0; k < nSubs; k++) {
498  a = b = 0.;
499  x = omega*k*n;
500  C = cos(x);
501  S = sin(x);
502 
503  if(FilterID==-1){
504  for (i = 0; i < n; i++) {
505  a += C * *p;
506  b += S * *(p++);
507  x = c*C-s*S;
508  S = c*S+s*C;
509  C = x;
510  }
511  }
512 
513  else if(FilterID==-2 || mode>nFirst){
514  u = 1.;
515  v = 0.;
516  for (i = 0; i < n; i++) {
517  x = (1-u) * *(p++);
518  a += C*x;
519  b += S*x;
520  x = c*C-s*S;
521  S = c*S+s*C;
522  C = x;
523  x = ch*u-sh*v;
524  v = ch*v+sh*u;
525  u = x;
526  }
527  }
528 
529  else if(FilterID==-3){
530  for (i = 0; i < n; i++) {
531  x = wt.data[i] * *(p++);
532  a += ct.data[i]*x;
533  b += st.data[i]*x;
534  }
535  }
536 
537  amp.data[k+m] = 2.*sqrt(a*a + b*b)/double(n);
538  phi.data[k+m] = atan2(b,a);
539  // cout<<m<<" a="<<a<<" b="<<b<<endl;
540  // cout<<m<<" "<<amp.data[k+m]<<" "<<phi.data[k+m]<<endl;
541  }
542  }
543 
544 //*************************************************************
545 // reconstruction of filtered interference signal
546 //*************************************************************
547  Q.amplitude.resize(M);
548  Q.line.resize(M);
549  Q.noise.resize(M);
550  Q.filter.resize(M);
551 
552  if(clean) TD = 0.;
553 
554  for (mode = nFirst; mode < imax; mode += abs(nStep)) {
555  m = nSubs*(mode-nFirst)/abs(nStep);
556  omega = 2*PI*mode*Frequency/TD.rate();
557  c = cos(omega);
558  s = sin(omega);
559 
560  a = b = 0.;
561  for (k = 0; k < nSubs; k++) {
562  a += amp.data[k+m]*amp.data[k+m];
563  b += phi.data[k+m];
564  }
565 
566  b /=nSubs;
567  Q.line[mode-nFirst] = 10.;
568  Q.noise[mode-nFirst] = 1.;
569  Q.filter[mode-nFirst] = 1;
570  Q.amplitude[mode-nFirst] = float(sqrt(float(a)/nSubs))*exp(f_complex(0.,b));
571  Q.intensity += a/nSubs/2.;
572 
573  if(clean){
574  p = TD.data;
575 
576  a = amp.data[m];
577  x = phi.data[m];
578  C = cos(x);
579  S = sin(x);
580  for (i = 0; i < n/2; i++){
581  *(p++) -= a*C;
582  x = c*C-s*S;
583  S = c*S+s*C;
584  C = x;
585  }
586 
587  for (k = 0; k < nSubs-1; k++) {
588  // cout<<"k="<<k<<" m="<<m<<" "<<amp.data[k+m]<<endl;
589  x = phi.data[k+m] +omega*(k*n+n/2);
590  C = cos(x);
591  S = sin(x);
592  x = phi.data[k+m+1]+omega*(k*n+n/2);
593  u = cos(x);
594  v = sin(x);
595  for (i = 0; i < n; i++) {
596  a = amp.data[k+m] * C;
597  b = amp.data[k+1+m]* u;
598  x = c*C-s*S;
599  S = c*S+s*C;
600  C = x;
601  x = c*u-s*v;
602  v = c*v+s*u;
603  u = x;
604  *(p++) -= (a*(n-i) + b*i)/n;
605  }
606  }
607 
608  a = amp.data[nSubs-1+m];
609  x = phi.data[nSubs-1+m]+omega*(TD.size()-n/2);
610  C = cos(x);
611  S = sin(x);
612  for (i = TD.size()-n/2; i<N; i++){
613  *(p++) -= a*C;
614  x = c*C-s*S;
615  S = c*S+s*C;
616  C = x;
617  }
618 
619  }
620  }
621 
622  if(clean){
623  b = TD.rms();
624  Q.intensity = b*b;
625  }
626  Q.first = nFirst;
627 
628  // cout<<"hetero: freq="<<Frequency<<" intensity="<<Q.intensity<<endl;
629 
630  return Q;
631 }
632 
633 
634 /*******************************************************************
635  * getOmega(WaveData &TD) refines the fundamental frequency using
636  * phase difference (p2+w*s/2) - (p1+w*s/2), where pi+w*s/2 is a
637  * phase in the middle of time interval s.
638  * It works with resampled data
639  *******************************************************************/
640 
641 double linefilter::getOmega(const WaveData &TD, int nsub)
642 {
643  double a,b;
644  double F;
645 
646  if(noScan) return Frequency;
647  if(!reFine) return -Frequency;
648 
649  if(nsub<2) nsub = 2;
650 
651  if (Frequency <= 0) {
652  cout << " getOmega() error: invalid interference frequency"
653  << " : " << Frequency << " Hz\n";
654  return 0.;
655  }
656 
657 // use Wiener filter if FID = -1 or 1;
658 // int FID = (FilterID == 0) ? 0 : 1;
659 // starting 08/07/01 getOmega always works with FilterID=1
660  int FID = 1;
661 
662  WaveData TDR(1);
663  TDR.resample(TD, newRate(TD.rate()), 6);
664  makeFilter(TDR, FID);
665  if(badData) return -Frequency;
666 
667  int L = int(TDR.rate()/Frequency + 0.5); // number of samples per cycle
668  int n = int(TDR.size()/nsub); // data section length
669 
670  unsigned int imax = maxLine(L); // max number of harmonics
671 
672  if (n/L == 0 || L < 4) {
673  cout << " getOmega() error: input data length too short to contain\n"
674  << " one cycle of target frequency = " << Frequency << " Hz\n";
675  return 0.;
676  }
677 
678  WaveData td2(2*L); // SK wavefft fix 9/10/2002
679  WaveData tds(L);
680  WaveData amp(L);
681  WaveData phi(L);
682  amp *= 0.;
683  phi *= 0.;
684 
685  double step = n/TDR.rate();
686  double wt = Frequency * step;
687  double phase = 0.;
688  double FSNR = SNR/(1.+SNR); // Filter threshold
689 
690  for (int k = 0; k < nsub; k++) {
691  tds.Stack(TDR, n, n*k); // stack signal
692 
693  tds.hann();
694 // SK wavefft fix 9/10/2002
695  td2.rate(tds.rate());
696  td2.cpf(tds); td2.cpf(tds,L,0,L);
697  td2.FFTW(1);
698  tds[slice(0,L/2,2)]<<td2[slice(0,L/2,4)];
699  tds[slice(1,L/2,2)]<<td2[slice(1,L/2,4)];
700 // SK wavefft fix 9/10/2002: end
701 
702 //*************************************************************
703 // estimate frequency from the phase difference
704 // nsub always > 1 - calculate frequency from the data TD
705 //*************************************************************
706 
707  for (unsigned int i = 2; i < (unsigned)L-1; i+=2) {
708  F = Filter.data[i/2];
709  a = tds.data[i]*F;
710  b = tds.data[i+1]*F;
711 
712  if(F > FSNR){
713  amp.data[i] += a*a+b*b;
714  phase = arg(f_complex(a,b))/2./PI;
715  phase += axb(wt/2.,double(i/2));
716  phase -= intw(phase); // phase(t)
717 
718  if(k>0){
719  F = phase - phi.data[i+1];
720  F -= intw(F); // phase(t2) - phase(t1)
721  phi.data[i] += (long(wt*(i/2)+0.5) + F)/step/(i/2);
722  }
723  else
724  phi.data[i] = 0.;
725 
726  phi.data[i+1] = phase;
727  }
728  }
729  }
730 
731 // average frequency
732 
733  double omega = 0.;
734  double weight = 0.;
735 
736  for (unsigned int i = nFirst; i < imax; i += abs(nStep)) {
737  F = Filter.data[i];
738  if(F>FSNR){
739  a = 1 - F;
740  if(a < 0.0001) a = 0.0001;
741  a = 1./a;
742  omega += a*phi.data[2*i];
743  weight += a;
744  }
745  }
746 
747 // was before 08/08/01
748 // omega += amp.data[2*i]*phi.data[2*i];
749 // weight += amp.data[2*i];
750 
751  omega = (weight>1.) ? omega/weight/(nsub-1) : -Frequency;
752  return omega;
753 }
754 
755 
756 /***********************************************************************
757  * fScan(WaveData &) finds the line fundamental frequency
758  * (saves in Frequency) and the interference amplitude.
759  * Uses:
760  * Frequency - seed value for line frequency
761  * fBand - defines frequency band (f-df,f+df), where to scan for peak
762  ***********************************************************************/
763 double linefilter::fScan(const WaveData &TD)
764 {
765 #define np 6 // number of points in interpolation scheme in resample()
766 
767  badData = false;
768 
769  if(noScan) return Frequency;
770 
771  WaveData td2(1);
772 
773  int n = TD.size();
774 // int FID = (FilterID<0) ? abs(FilterID) : 0; // set filter ID for makeFilter()
775  int FID = 0; // starting on 03/24/01 for fScan always use FID=0
776 
777  double d, s, sp, fc;
778  double fwhm = 1.;
779  double delta = 0.;
780  double am = 1., ac = 0.;
781  double dfft = nSubs*TD.rate() / n;
782  double e1 = 1.e-3;
783 
784 // double fw = fBand*dfft; before 08/03/01
785  double fw = fBand*dfft/nFirst;
786 
787  double ff = Frequency; // seed frequency before tunning
788  double fp = Frequency; // central frequency.
789 
790 // ff - seed interference frequency;
791 // fc - central frequency
792 // fp - peak's frequency estimated via scan
793 // fnew - new sampling rate which is multiple of target frequency f
794 // ac - calculated frequency correction (relative to fc) in uints of fw
795 // am - maximum allowed deviation of frequebcy in units of fw
796 // aw - am/FWHM of the peak
797 // ic = +1, -1 stores the sign of ac at previous step
798 // dfft - Fourier frequency resolution for given sample
799 // fw - frequency band width for 3 sample points to build parabola
800 // e1 - limit iteration accuracy of the frequency deviation
801 // e2 - limit iteration accuracy for frequency window width
802 
803  if (TD.rate() <= 0.) {
804  cout << " fScan() error: invalid sampling rate = "
805  << TD.rate() << " Aborting calculation.\n";
806  badData = true;
807  return ff;
808  }
809 
810  if ( ff <= 0.) {
811  cout << " fScan() error: invalid interference frequency = "
812  << ff << " Aborting calculation.\n";
813  badData = true;
814  return ff;
815  }
816 
817 
818 //*******************************************************
819 // detailed scan of the region (ff-fband, ff+fband)
820 //*******************************************************
821 
822  int mScan = -nScan;
823  if ( mScan > 0 ) {
824  WaveData sw(mScan);
825  sp = 0.;
826 
827  cout <<" Scanning frequency from "<< ff-mScan*fw/2. << " Hz to "
828  << ff+mScan*fw/2. <<" Hz\n";
829 
830  int ip = 0;
831  for (int i=0; i < mScan && !badData; i++) {
832  Frequency = ff + (i - mScan/2)*fw;
833 
834  td2.resample(TD, newRate(TD.rate()), np);
835  s = makeFilter(td2,FID);
836 
837  sw.data[i] = s;
838  if(s > sp) {sp = s; fp = Frequency; ip = i;}
839  printf(" Frequency = %f Hz, sqrt(<E>) = %f \n", Frequency, s);
840  }
841 
842 // improve peak approximation by taking 3 points close to peak
843  if (ip > 0 && ip < (mScan-1) && !badData) {
844  d = 2.*sw.data[ip] - sw.data[ip + 1] - sw.data[ip - 1];
845  fp += (d > 0.) ? 0.5*fw*(sw.data[ip + 1] - sw.data[ip - 1])/d : 0.;
846  }
847  }
848 
849 //*******************************************************
850 // start tuning frequency
851 //*******************************************************
852  int k = 3;
853  int mode;
854  double ss[3];
855  int ks[3] = {1,1,1};
856 
857  fc = fp;
858  while (!badData) {
859 
860 // calculate energies for 3 values of frequency : fp-fw, fp, fp+fw
861  for (int m = 0; m < 3; m++) {
862  if(ks[m]){
863  Frequency = fc + fw*(m - 1);
864  td2.resample(TD, newRate(TD.rate()), np);
865  ss[m] = makeFilter(td2,FID);
866  ks[m] = 0;
867  }
868  if(badData) break;
869  }
870 
871  if(k++ > nScan){
872  badData = true; // limit number of iterations
873 // cout << "fScan hits a limit on number of iterations\n";
874  }
875  if(badData) break;
876 
877 // find minimum of parabola using three values ss[i]
878 // if d > 0 - parabola has maximum, if d < 0 - minimum
879 
880  d=2.*ss[1]-(ss[2]+ss[0]);
881 
882  if (d > 0.) {
883  ac = 0.5 * (ss[2] - ss[0]); // d * (f-fc)/fw
884  fwhm = sqrt(ac*ac+2.*ss[1]*d)/d; // FWHM/fw
885  fwhm *= fw/dfft; // FWHM/dfft
886  ac /= d; // (f-fc)/fw
887  }
888  else {
889  ac = (ss[2] > ss[0])? am : -am;
890  fwhm = 1.;
891  }
892 
893 // cout<<" fp="<<fp<<" fc="<<fc<<" fw="<<fw<<" ac="<<ac<< "\n";
894 
895  mode = 0; // fw shift
896  if(fabs(ac) < am) mode = 1; // fw/2 shift
897  if(fabs(ac)<am/4. && fw/dfft>0.1) mode = 2; // no shift
898 
899  delta = 1.;
900  if(mode){
901  delta = (fc-fp)/fw + ac; // deviation from the peak frequency
902  fp += delta*fw; // new peak frequency
903  }
904 
905  delta *= fw/dfft; // deviation in units of dfft
906 
907 // cout<<k-1<<" "<<fp<<" "<<delta<<" "<<fwhm<<" "<<ac*fw/dfft<< "\n";
908 
909  if(fabs(delta) < e1) break; // limit in units of dfft
910  if(fabs(delta*fwhm) < e1 && fw/dfft<0.1) break; // limits ac in units of FWHM
911 
912  switch (mode) {
913  case 0: // one step shift right or left
914  if(ac > 0.){ // move one fw step right
915  ss[0] = ss[1]; ss[1] = ss[2]; ks[2] = 1;
916  }
917  else{ // move one fw step left
918  ss[2] = ss[1]; ss[1] = ss[0]; ks[0] = 1;
919  }
920  fc += (ac>0.) ? fw : -fw; // new central frequency
921  fp = fc;
922  break;
923 
924  case 1: // half step shift right or left
925  if(ac > 0.){
926  ss[0] = ss[1]; ks[1] = 1;
927  }
928  else{
929  ss[2] = ss[1]; ks[1] = 1;
930  }
931  fw *= 0.5; // reduce fw by 2 if d>0
932  fc += (ac>0.) ? fw : -fw; // new central frequency
933  break;
934 
935  case 2: // no shift
936  ks[0] = 1; ks[2] = 1;
937  fw = 4*fw*fabs(ac); // reduce fw
938  if(fw/dfft<0.01) fw = 0.01*dfft; // reduce fw
939  k++;
940  break;
941 
942  }
943 
944  }
945 
946  if(badData)
947  fp = ff;
948 // else if(reFine){
949 // Frequency = fp; // use value of frequency found by fScan
950 // fp = fabs(getOmega(TD, nSubs)); // return refined frequency
951 // }
952  Frequency = ff;
953  return (badData) ? Frequency : fp; // return refined frequency
954 
955 }
956 
957 
958 /***********************************************************************
959  * Function returns the interference signal for harmonics from nFirst
960  * to nLast with fundamental frequency F
961  * Update lineList trend data
962  * Filter = 0 - corresponds to pure "comb" filter
963  * Filter = 1 - corresponds to optimal Wiener filter with 1-st method of
964  * noise estimation (default)
965  ***********************************************************************/
967 {
968  WaveData td2(1);
969  double s = 0.;
970  linedata v;
971  double seedF = Frequency;
972 
973 // use Wiener filter if FID = -1 or 1;
974  int FID = (FilterID == 0) ? 0 : 1;
975 
976  if ( TD.rate() <= 0. || omega <= 0. ) {
977  cout << " Interference() error: invalid interference frequency = "
978  << omega << "\n Aborting calculation.\n";
979 
980  }
981 
983  v.intensity = 0.;
984  v.frequency = Frequency;
985  v.first = nFirst;
986 
987  if (badData) { // skip bad data
988 // lineList.insert(lineList.end(),v); // update lineList trend data
989 // cout << "Interference(): skip bad data\n";
990  return 0.;
991  }
992 
993  if(FilterID < 0){
994  v = getHeteroLine(TD);
995  s = v.intensity;
996  }
997  else {
998 
999 // resample data at new sample rate "fnew" which is multiple of base
1000 // frequency of interference fLine and approximately is two times as high
1001 // as original frequency "Rate"
1002 
1003  Frequency = omega;
1004  td2.resample(TD, newRate(TD.rate()), nRIF);
1005  s = makeFilter(td2, FID);
1006  v = getLine(td2); // get interference and linedata
1007 
1008  if(clean){ // return interference
1009  if(badData){
1010  TD *= 0.;
1011  }
1012  else
1013  TD.resample(td2, TD.rate(), nRIF); // resample at original sampling rate
1014  }
1015 
1016  }
1017 
1018  if(badData){
1019  v.frequency *= -1.;
1020  Frequency = seedF; // do not update frequency;
1021  }
1022  if(v.intensity > 0.)
1023  lineList.push_back(v); // update lineList trend data
1024 
1025  // cout<<"intensity="<<sqrt(v.intensity)<<" s="<<s
1026  // <<" amplitude="<<abs(v.amplitude[0])<<endl;
1027 
1028  return s;
1029 }
1030 
1031 
1032 
1033 /************************************************************************
1034  * apply(ts) estimate interference and save trend data
1035  * if clean=true returns time series with the interference signal removed
1036  ***********************************************************************/
1038 //---------------------------------- Check the input data.
1039  if (!ts.size()) return;
1040  if (!ts.rate()) return;
1041 
1042  StartTime = ts.start();
1044 
1045  Stride = ts.size()/ts.rate();
1046  double s = Window > 0. ? Window : Stride;
1047  double delta = s;
1048 
1049  double rate = ts.rate();
1050  int n = ts.size();
1051 
1052  int LPF = (nLPF>0) ? nLPF : 0; // depth of the wavelet LP filter
1053 
1054  Biorthogonal<wavereal> w(nWave); // needed if LPF is applied
1055  WSeries<wavereal> *pw = 0;
1056 
1057  WaveData tw; // needed if LPF is applied
1058  double omega = Frequency;
1059 
1060  int NN = 0;
1061  int nTS = ts.size();
1062 
1063  if(LPF){ // apply wavelet low path filter
1064  pw = new WSeries<wavereal>(ts, w);
1065  NN = (nTS >> LPF) << LPF; // new number of samples
1066  if(NN != nTS){ // adjust the number of samples
1067  NN += 1<<LPF;
1068  pw->resample(NN*rate/nTS,nRIF);
1069  rate = pw->rate();
1070  }
1071 
1072  pw->Forward(LPF);
1073  pw->getLayer(tw,0);
1074  rate /= (1<<LPF); // rate of decimated data
1075  tw.rate(rate);
1076  n = tw.size();
1077  }
1078 
1079  int nn = Window > 0. ? int(Window*rate) : n; // if window = 0, take whole ts
1080 
1081 // cout<<"Window="<<Window<<" rate="<<rate<<" n="<<n<<" nn="<<nn<<endl;
1082 
1083  if (nn < int(rate/Frequency)) {
1084  cout <<" linefilter::apply() error: invalid time window "<<Window<<" sec.\n";
1085  return;
1086  }
1087 
1088  WaveData *tx = new WaveData(nn);
1089 
1090 // printf(" Time interval (sec) | Base frequency (Hz) | Sqrt(E_int)\n");
1091 
1092  int i = 0;
1093  while(i <= n-nn && nn > 0) {
1094 
1095  if((n-i-nn) < nn) { // the last data interval is too short
1096  delta *= double(n - i)/nn; // add leftover to the last interval
1097  nn = n - i;
1098  }
1099 
1100  tx->rate(rate);
1101  if((int)tx->size() != nn) tx->resize(nn);
1102 
1103  if(LPF) tx->cpf(tw,nn,i);
1104  else tx->cpf(ts,nn,i);
1105 
1106  if(FilterID>=0){
1107  if (!reFine || badData || (lineList.size()<3))
1108  omega = fScan(*tx);
1109  else{
1110  omega = getOmega(*tx, nSubs);
1111  if(omega < 0.) omega = fScan(*tx);
1112  }
1113  }
1114 
1115  s = Interference(*tx, omega);
1116  CurrentTime += delta;
1117 
1118  if(clean && !badData){
1119  if(LPF) tw.sub(*tx,nn,0,i);
1120  else ts.cpf(*tx,nn,0,i);
1121  }
1122 
1123  // if(!badData)
1124 // printf(" %8.3f - %8.3f %12.6f %20.6f\n" ,
1125 // double(i)/rate, double(i + nn)/rate, Frequency*nFirst, sqrt(s));
1126 
1127  i += nn;
1128  }
1129 
1130  if(clean && LPF){
1131  pw->putLayer(tw,0);
1132  pw->Inverse();
1133  if(NN != nTS)
1134  ts.resample((WaveData &) *pw,ts.rate(),nRIF);
1135  else
1136  ts = *pw;
1137 
1138  if(nTS != (int)ts.size()) // check if data has the same length as input data
1139  cout << "linefilter::apply(): is "<<ts.size()<<", should be: "<<nTS<<"\n";
1140  }
1141 
1142  delete tx;
1143  if(pw) delete pw;
1144  return;
1145 }
1146 
1147 /*********************************************************************
1148  * get Trend Data from lineList object
1149  *
1150  *********************************************************************/
1152 {
1153  int l_size = lineList.size();
1154  int v_size;
1155  int n = 0;
1156  wavearray<float> out(l_size);
1157  wavearray<float> www(l_size);
1158  list<linedata>::iterator it;
1159  linedata* v;
1160  double a=0., F=0.;
1161  double step = (Window>0.) ? Window : Stride;
1162  double time = 0.;
1163  double phase = 0.;
1164  double averF = 0.;
1165 
1166  out = 0;
1167  www = 0;
1168 
1169  out.rate((step>0.) ? 1./step : 1.);
1170 
1171  if(l_size < 2) { return out; }
1172 
1173  if(m < 0) m=0;
1174  it = lineList.begin(); // set the list at the beginning
1175 
1176  v = &(*(it++));
1177  v_size = v->amplitude.size();
1178 
1179  if(m > 0 && m <= v_size)
1180  phase = arg(v->amplitude[m-1]);
1181 
1182  out.start(v->T_current);
1183 
1184  if(c=='p') // average frequency
1185  for(int i=0; i<l_size; i++) {
1186  v = &(*(it++));
1187  F = double(v->frequency);
1188  if(F<=0.) continue;
1189  F *= (m == 0) ? 1. : (v->first+m-1);
1190  averF += F; n++;
1191  }
1192 
1193  if(n) averF /= double(n);
1194 
1195  it = lineList.begin(); // set the list at the beginning
1196 
1197  for(int i=0; i<l_size; i++) {
1198  v = &(*(it++));
1199  v_size = v->amplitude.size();
1200  switch (c) {
1201 
1202  case 't': // get start time
1203  out.data[i] = v->T_current - out.start();
1204  break;
1205 
1206  case 'a': // get harmonic's amplitude
1207  if(m == 0 || m > v_size)
1208  out.data[i] = (m==0) ? sqrt(v->intensity) : 0.;
1209  else
1210  out.data[i] = abs(v->amplitude[m-1]);
1211  break;
1212 
1213  case 'p': // get harmonic's phase
1214  case 'f': // get harmonic frequency
1215  if(m == 0 || m > v_size){
1216  if(c == 'p')
1217  out.data[i] = v_size;
1218  else
1219  out.data[i] = v->frequency;
1220  }
1221  else {
1222  F = fabs(v->frequency * (v->first+m-1));
1223  a = (arg(v->amplitude[m-1])-phase)/2./PI;
1224  a += axb(F, Stride/2.);
1225  a -= axb(averF, v->T_current-out.start()+Stride/2.);
1226  a -= (a>0.) ? long(a+0.5) : long(a-0.5);
1227  out.data[i] = 2.*PI*a;
1228  www.data[i] = 2.*PI*a;
1229  step = v->T_current - time;
1230  time = v->T_current;
1231  if(c == 'f') out.data[i] = F;
1232  }
1233  if(c == 'f' && step<1.1*Stride){ // calculate frequency
1234  a = (i>0) ? (www.data[i]-www.data[i-1])/2./PI : F*step;
1235  a -= (a>0.) ? long(a+0.5) : long(a-0.5);
1236  out.data[i] = (long(F*step+0.5) + a)/step;
1237  }
1238  break;
1239 
1240  case 'F': // get harmonic frequency
1241  a = double(v->frequency);
1242  out.data[i] = (m == 0) ? a : a*(v->first+m-1);
1243  break;
1244 
1245  case 'P': // get harmonic's phase
1246  if(m == 0 || m > v_size)
1247  out.data[i] = v_size;
1248  else
1249  out.data[i] = arg(v->amplitude[m-1]);
1250  break;
1251 
1252  case 's': // get signal power
1253  out.data[i] = 0.;
1254  if(m > v_size || v_size < 1) break;
1255  if(m == 0)
1256  for(int j=0; j<v_size; j++){
1257  F = v->filter[j];
1258  out.data[i] += v->line[j] * F*F;
1259  }
1260  else{
1261  F = v->filter[m-1];
1262  out.data[i] = v->line[m-1] * F*F;
1263  }
1264  out.data[i] *= out.rate();
1265  break;
1266 
1267  case 'S': // get signal spectral density
1268  out.data[i] = 0.;
1269  if(m > v_size || v_size < 1) break;
1270  if(m == 0)
1271  for(int j=0; j<v_size; j++)
1272  out.data[i] += v->line[j];
1273  else
1274  out.data[i] = v->line[m-1];
1275  break;
1276 
1277  case 'n': // get noise power
1278  out.data[i] = 0.;
1279  if(m > v_size || v_size < 1) break;
1280  if(m == 0)
1281  for(int j=0; j<v_size; j++)
1282  out.data[i] += v->noise[j] * v->filter[j];
1283  else
1284  out.data[i] = v->noise[m-1] * v->filter[m-1];
1285  out.data[i] *= out.rate();
1286  break;
1287 
1288  case 'N': // get noise spectral density
1289  out.data[i] = 0.;
1290  if(m > v_size || v_size < 1) break;
1291  if(m == 0)
1292  for(int j=0; j<v_size; j++)
1293  out.data[i] += v->noise[j];
1294  else
1295  out.data[i] = v->noise[m-1];
1296  break;
1297 
1298  case 'K': // get SNR
1299  a = 0.;
1300  out.data[i] = 0.;
1301  if(m > v_size || v_size < 1) break;
1302  if(m == 0)
1303  for(int j=0; j<v_size; j++){
1304  F = v->filter[j];
1305  a += v->noise[j]*F;
1306  out.data[i] += v->line[j] * F*F;
1307  }
1308  else{
1309  F = v->filter[m-1];
1310  a = v->noise[m-1]*F;
1311  out.data[i] = v->line[m-1] * F*F;
1312  }
1313  out.data[i] /= (a>0.) ? a : 1.;
1314  break;
1315 
1316  case 'W': //get filter (m>0)
1317  out.data[i] = 0.;
1318  if(m > v_size || v_size < 1) break;
1319  if(m > 0)
1320  out.data[i] = v->filter[m-1];
1321  break;
1322 
1323  default: // get first harmonic
1324  out.data[i] = v->first;
1325  break;
1326  }
1327  }
1328  return out;
1329 }
1330 
1331 
1332 /*********************************************************************
1333  * Dumps lineList data into file *fname in binary format and type
1334  * "double".
1335  *********************************************************************/
1336 bool linefilter::DumpTrend(const char *fname, int app)
1337 {
1338  size_t l_size;
1339  size_t v_size;
1340  linedata* v;
1341 
1342  list<linedata>::iterator it = lineList.begin();
1343 
1344  if(dumpStart >= lineList.size()) return false;
1345  for(size_t i=0; i<dumpStart; i++) it++;
1346  l_size = lineList.size() - dumpStart;
1347 
1348 // calculate the data length
1349 
1350  size_t max_size = 0;
1351  for(size_t i=0; i<l_size; i++) {
1352  v = &(*(it++));
1353  v_size = v->amplitude.size();
1354  if(v_size > max_size) max_size = v_size;
1355  }
1356 
1357  int m = (5*max_size+4);
1358  size_t n = m*(l_size+1);
1359  if(n < 4) return false;
1360 
1361 // pack lineList into WaveData out
1362 
1364  out->data[0] = max_size; // length of the linedata amplitude vector
1365  out->data[1] = l_size; // length of the lineList
1366  out->data[2] = m; // total length of the linedata
1367  out->data[3] = n; // total lenght
1368 
1369  it = lineList.begin();
1370  for(size_t i=0; i<dumpStart; i++) it++;
1371 
1372 // cout<<"dumpStart = "<<dumpStart;
1373 // cout<<", listSize = "<<lineList.size();
1374 // cout<<", Stride = "<<Stride<<endl;
1375 
1376  double gpsTime = it->T_current;
1377 
1378  int gps = int(gpsTime)/1000;
1379  out->data[4] = float(gps); // int(gps time/1000)
1380  out->data[5] = gpsTime - 1000.*double(gps); // rest of gps time
1381  out->data[6] = (Window>0.) ? Window : Stride; // filter stride
1382 
1383  for(size_t i=1; i<=l_size; i++) {
1384  v = &(*(it++));
1385  v_size = v->amplitude.size();
1386 
1387  out->data[i*m+0]= v->T_current - gpsTime;
1388  out->data[i*m+1]= v->frequency;
1389  out->data[i*m+2]= v->intensity;
1390  out->data[i*m+3]= v->first;
1391 
1392  for(unsigned int j=0; j<max_size; j++){
1393  if(j < v_size) {
1394  out->data[i*m+4+j*5] = abs(v->amplitude[j]);
1395  out->data[i*m+5+j*5] = arg(v->amplitude[j]);
1396  out->data[i*m+6+j*5] = v->line[j];
1397  out->data[i*m+7+j*5] = v->noise[j];
1398  out->data[i*m+8+j*5] = v->filter[j];
1399  }
1400  else {
1401  out->data[i*m+4+j*5] = 0.;
1402  out->data[i*m+5+j*5] = 0.;
1403  out->data[i*m+6+j*5] = 0.;
1404  out->data[i*m+7+j*5] = 0.;
1405  out->data[i*m+8+j*5] = 0.;
1406  }
1407  }
1408  }
1409 
1410  out->DumpBinary(fname, app);
1411 
1412  delete out;
1413  return true;
1414 }
1415 
1416 
1417 /*********************************************************************
1418  * Read trend data from file into the lineList data structure.
1419  *********************************************************************/
1420 bool linefilter::LoadTrend(const char *fname)
1421 {
1422  linedata v;
1424  float *p = 0;
1425  double last = 0.;
1426  double step = 0.;
1427  double time = 0.;
1428  double gpsTime = 0.;
1429  unsigned int count = 0;
1430 
1431  in.ReadBinary(fname); // read file
1432  if(in.size() < 6) return false;
1433 
1434  while(count < in.size()){
1435  p = &(in.data[count]);
1436  int m = int(*(p+2) + 0.5); // total length of the linedata
1437  if(m <= 1) return false;
1438 
1439  int l_size = int(*(p+1) + 0.5); // length of the lineList
1440  if(l_size < 1) return false;
1441 
1442  int v_size = int(*p + 0.5); // length of the linedata amplitude vector
1443  count += int(*(p+3)+0.5); // add total length of the block
1444 
1445  gpsTime = double(int(*(p+4)+0.5))*1000. + *(p+5); // block start time
1446  StartTime = 0.;
1447  CurrentTime = *(p+6);
1448  Stride = *(p+6);
1449  Window = *(p+6);
1450 
1451  v.amplitude.resize(v_size);
1452  v.line.resize(v_size);
1453  v.noise.resize(v_size);
1454  v.filter.resize(v_size);
1455 
1456  time = *(p+m);
1457  last = time;
1458  step = 0.;
1459 
1460 // pack lineList from WaveData inv->T_current;
1461 
1462  for(int i=1; i<=l_size; i++) {
1463  p += m;
1464  if(*p != 0.) step = *p - last;
1465  time += step;
1466  last = *p;
1467  v.T_current = time+gpsTime;
1468  v.frequency = *(p+1);
1469  v.intensity = *(p+2);
1470  v.first = int(*(p+3)+0.5);
1471 
1472  for(int j=0; j<v_size; j++){
1473  v.amplitude[j] = *(p+4+j*5);
1474  v.amplitude[j]*= exp(f_complex(0,*(p+5+j*5)));
1475  v.line[j] = *(p+6+j*5);
1476  v.noise[j] = *(p+7+j*5);
1477  v.filter[j] = *(p+8+j*5);
1478  }
1479 
1480  lineList.insert(lineList.end(),v);
1481  }
1482  }
1483  return true;
1484 }
1485 
1486 
1487 
1488 
wavearray< double > ct
unsigned int nBand
std::list< linedata > lineList
virtual size_t size() const
Definition: wavearray.hh:127
static const double C
Definition: GNGen.cc:10
tuple f
Definition: cwb_online.py:91
printf("total live time: non-zero lags = %10.1f \n", liveTot)
double newRate(double)
double delta
virtual void rate(double r)
Definition: wavearray.hh:123
The linefilter class containes methods to track and remove quasi- monochromatic lines.
wavearray< double > a(hp.size())
double StartTime
WaveData Filter
WSeries< float > v[nIFO]
Definition: cwb_net.C:62
unsigned int nLast
int n
Definition: cwb_net.C:10
void setFilter(int nF=1, int nL=0, int nS=1, int nD=-1, int nB=5, int nR=6, int nW=8)
void apply(WaveData &ts)
Operate on wavearray object.
wavearray< double > wt
std::vector< float > line
int count
Definition: compare_bkg.C:373
cout<< endl;cout<< "ts size = "<< ts.size()<< " ts rate = "<< ts.rate()<< endl;tf.Forward(ts, wdm);int levels=tf.getLevel();cout<< "tf size = "<< tf.size()<< endl;double dF=tf.resolution();double dT=1./(2 *dF);cout<< "rate(hz) : "<< RATE<< "\t layers : "<< nLAYERS<< "\t dF(hz) : "<< dF<< "\t dT(ms) : "<< dT *1000.<< endl;int itime=TIME_PIXEL_INDEX;int ifreq=FREQ_PIXEL_INDEX;int index=(levels+1)*itime+ifreq;double time=itime *dT;double freq=(ifreq >0)?ifreq *dF:dF/4;cout<< endl;cout<< "PIXEL TIME = "<< time<< " sec "<< endl;cout<< "PIXEL FREQ = "<< freq<< " Hz "<< endl;cout<< endl;wavearray< double > x
double phase
wavearray< double > psd(33)
virtual void ReadBinary(const char *, int=0)
Definition: wavearray.cc:392
WaveData LineSD
STL namespace.
wavearray< wavereal > WaveData
Definition: LineFilter.hh:17
float frequency
WaveData NoiseSD
#define M
Definition: UniqSLagsList.C:3
int m
Definition: cwb_net.C:10
void reset()
Clear/release the internal History vector and reset the current time.
virtual void start(double s)
Definition: wavearray.hh:119
double SeedFrequency
int j
Definition: cwb_net.C:10
i drho i
#define N
tuple ff
Definition: cwb_online.py:394
cout<< "Selected Pixels : "<< nPix<< endl;wc.cluster(1, 1);SSeries< double > ss
nT
Definition: cbc_plots.C:659
#define PI
Definition: watfun.hh:14
int Sample(Complex *H, double *t, int N, double totalMass, double Rate, wavearray< double > &hps, wavearray< double > &hxs)
Definition: eBBH.cc:16
size_t mode
virtual double rms()
Definition: wavearray.cc:1188
wavearray< double > w
Definition: Test1.C:27
ofstream out
Definition: cwb_merge.C:196
linefilter * clone(void) const
Clone a linefilter.
float phi
std::vector< f_complex > amplitude
void sub(const wavearray< DataType_t > &, int=0, int=0, int=0)
Definition: wavearray.cc:754
double time[6]
Definition: cbc_plots.C:435
double Frequency
bool LoadTrend(const char *)
unsigned int nFirst
i() int(T_cor *100))
float intensity
unsigned int first
double CurrentTime
int getLayer(wavearray< DataType_t > &w, double n)
param: n - layer number
Definition: wseries.cc:175
#define np
void hann(void)
Definition: wavearray.hh:354
linedata getLine(WaveData &)
char fname[1024]
wavearray< float > getTrend(int, char)
std::vector< float > filter
int k
long intw(double)
WaveData getPSD(const WaveData &, int=1)
double F
void resize(size_t=0)
void setFScan(double f=0., double sn=2., double fS=0.45, int nS=20)
double getOmega(const WaveData &, int=2)
wavearray< double > st
virtual void DumpBinary(const char *, int=0)
Definition: wavearray.cc:335
virtual void FFTW(int=1)
Definition: wavearray.cc:878
s s
Definition: cwb_net.C:137
double axb(double, double)
double gps
linefilter(void)
Build an empty linefilter.
ifstream in
bool DumpTrend(const char *, int=0)
double makeFilter(const WaveData &, int=0)
wavearray< double > ts(N)
virtual void resample(double, int=6)
Definition: wseries.cc:897
double fabs(const Complex &x)
Definition: numpy.cc:37
void resample(const wavearray< DataType_t > &, double, int=6)
Definition: wavearray.cc:485
double Q
void Forward(int n=-1)
param: wavelet - n is number of steps (-1 means full decomposition)
Definition: wseries.cc:228
Meyer< double > S(1024, 2)
double omega
Definition: testWDM_4.C:12
double fScan(const WaveData &)
DataType_t * data
Definition: wavearray.hh:301
linedata getHeteroLine(WaveData &)
double Stack(const wavearray< DataType_t > &, int)
Definition: wavearray.cc:973
std::complex< float > f_complex
Definition: LineFilter.hh:15
double T_current
~linefilter(void)
Destroy the linefilter object and release the function storage.
virtual void resize(unsigned int)
Definition: wavearray.cc:445
void Inverse(int n=-1)
param: n - number of steps (-1 means full reconstruction)
Definition: wseries.cc:273
size_t dumpStart
void putLayer(wavearray< DataType_t > &, double n)
param: n - layer number
Definition: wseries.cc:201
#define SNR
Definition: DrawNRI.C:36
double Interference(WaveData &, double)
std::vector< float > noise
void cpf(const wavearray< DataType_t > &, int=0, int=0, int=0)
Definition: wavearray.cc:699
unsigned int maxLine(int)