Logo Coherent WaveBurst  
Reference Guide
Logo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Regression_Sine_Gaus_Bic.C
Go to the documentation of this file.
1 //
2 // How to apply regression to subtract upconverted 2Hz noise at 100 +/- 2 Hz
3 // before the upconversion the 2Hz noise is modify by a transfer function
4 // the transfer function take into account the coupling between witness and target channels
5 // Note: in this example the data are cleaned without removing the lines at 100 Hz and 102Hz
6 //
7 
8 {
9  //Time
10  #define LENGHT 1200
11  #define RATE 2048
12  //Frequency near 180Hz
13  #define F1 95
14  #define F2 105
15 
16  //Draw plot of target and target-prediction
18  TCanvas *c1 = plot.canvas;
19  c1->Divide(2,2);
20 
21  double omega = TMath::TwoPi()*100;
22  double omega_lf = TMath::TwoPi()*2;
23 
24  int totalscratch=32;
26  double dt = 1./double(RATE);
27  double df = double(RATE)/N/2.;
28 
29  TRandom3 rnd(1);
30 
31  using namespace CWB;
32 
33  //Fill wavearray h with target channel
35  target.rate(RATE);
36  target.resize(N);
37  target.start(0);
38  target.stop(LENGHT+2*totalscratch);
39  for(int i=0; i<N; i++) target[i] = rnd.Gaus(); // white gaussian noise
40 
41  // generate witness channel : gauss noise @ 2Hz with gauss shape
42  wavearray<double> witness = target;
43  for(int i=0; i<N; i++) witness[i] = rnd.Gaus(); // white gaussian noise
44  witness.FFT(1);
45  for(int i=0;i<N;i++) { // select freq range [1,3] with gauss shape
46  double f = double(i)*df;
47  witness[i]*=1000.*exp(-pow(2*(f-2.)/1.,2));
48  witness[i]/=RATE;
49  }
50  witness.FFT(-1);
51  for(int i=0; i<N; i++) witness[i] += 0.01*rnd.Gaus();// add white noise
52 
53  // plot witness channel
54  c1->cd(1);
55  gPad->SetLogy();
56  plot.plot(witness, const_cast<char*>("alp logy"), 1, totalscratch, totalscratch+LENGHT, true, 0, 5, true, 50);
57 
58  // define 100Hz line
59  wavearray<double> line_100hz = target;
60  for(int i=0; i<N; i++) line_100hz[i] = sin((omega)*i*dt);
61 
62  // define 102Hz line
63  wavearray<double> line_102hz = target;
64  for(int i=0; i<N; i++) line_102hz[i] = 0.2*sin((omega+omega_lf)*i*dt);
65 
66  // apply to witness channel a transfer function (coupling between witness and target channels)
67  wavearray<double> witness_tf = witness;
68  witness_tf.FFT(1);
69  for(int i=0;i<N;i++) {
70  double f = double(i)*df;
71  if(f<=1) witness_tf[i]*=0.35*(pow(1-1,4)+1);
72  if(f>1&&f<3) witness_tf[i]*=0.35*(pow(f-1,4)+1);
73  if(f>=3) witness_tf[i]*=0.35*(pow(3-1,4)+1);
74  }
75  witness_tf.FFT(-1);
76  plot.plot(witness_tf, const_cast<char*>("same"), 2, totalscratch, totalscratch+LENGHT, true, 0, 5, true, 50);
77  plot.graph[0]->SetTitle("Black : Witness Channel - Red : After Transfer Function");;
78 
79  // add to target channel a sine wave @ 100Hz + sine wave @ 102Hz + bicoherence 100Hz & 2Hz:
80  for(int i=0; i<N; i++) target[i] += line_100hz[i] + line_102hz[i] + 50.*line_100hz[i]*witness_tf[i];
81  // plot target channel
82  c1->cd(2);
83  gPad->SetLogy();
84  plot.plot(target, const_cast<char*>("alp logy"), 1, totalscratch, totalscratch+LENGHT, true, F1, F2, true, 50);
85  plot.graph[2]->SetTitle("Gauss + 100Hz Line + bicoherence 100Hz line & 2Hz line + 102Hz line");;
86 
87  //Make the target channel WDM transform, resolution = 0.5Hz
88  int lev=target.rate()/2;
89  lev*=2;
90  WDM<double> wdtf(lev, 2*lev, 6, 10);
92  tfmap.Forward(target, wdtf);
93 
94  //Adding target channel
95  regression r;
96  r.add(tfmap,const_cast<char*>("hchannel"));
97 
98  //Considering only the interested frequency band
99  r.mask(0);
100  r.unmask(0,F1,F2);
101 
102  //Adding witness channels
103  r.add(target,const_cast<char*>("witness"),99.7,100.2); // select target channel at range [99.7,100.2]
104  r.add(witness,const_cast<char*>("witness_lf"),0,6); // select witness channel at range [0,6]
105  //Constructing channel describing multi-linear coupling
106  r.add(1,2,const_cast<char*>("bic_witness"));
107  //masking carrier channels and
108  r.mask(1);
109  r.mask(2);
110 
111  //Calculate prediction
112  r.setFilter(4);
113  r.setMatrix(totalscratch);
114  r.solve(0., 20, 's');
115  r.apply(0.2);
116 
117  // Draw cleaned data
118  wavearray<double> clean = r.getClean();
119  c1->cd(3);
120  gPad->SetLogy();
121  plot.plot(target, const_cast<char*>("alp logy"), 1, totalscratch, totalscratch+LENGHT, true, F1, F2, true, 50);
122  plot.plot(clean, const_cast<char*>("same"), 2, totalscratch, totalscratch+LENGHT, true, F1, F2, true, 50);
123  plot.graph[3]->SetTitle("Black : noisy data | Red : cleaned data");;
124 
125  // Draw cleaned data after subtraction of injected 100Hz & 102Hz lines
126  c1->cd(4);
127  gPad->SetLogy();
128  plot.plot(clean, const_cast<char*>("alp logy"), 1, totalscratch, totalscratch+LENGHT, true, F1, F2, true, 50);
129  for(int i=0; i<N; i++) clean[i] -= line_100hz[i];
130  for(int i=0; i<N; i++) clean[i] -= line_102hz[i];
131  plot.plot(clean, const_cast<char*>("same"), 2, totalscratch, totalscratch+LENGHT, true, F1, F2, true, 50);
132  plot.graph[5]->GetHistogram()->GetYaxis()->SetRangeUser(0.015,2.8);;
133  plot.graph[5]->SetTitle("cleaned data, black: original, red: after subtraction of injected 100Hz & 102Hz lines");;
134 
135  // save plots
136  c1->SaveAs("Regression_Sine_Gaus_Bic.png");
137 
138  //Write ranking for each frequency layer
139  wavearray<double> freq=r.vfreq;
141  cout << "Ranking:" << endl;
142  for (int i=0; i<freq.size(); i++) cout << freq.data[i] << " " << rank.data[i] << endl;
143  cout << endl;
144 
145  //Write eigenvalues for each frequency layer
146  cout << "Eigen-values" << endl;
147  wavearray<double>* eigen = new wavearray<double>[freq.size()];
148  for (int i=0; i<freq.size(); i++) eigen[i]=r.getVEIGEN(i);
149  for (int j=0; j<eigen[0].size(); j++) {
150  for (int i=0; i<freq.size(); i++) printf("%.3f\t",eigen[i].data[j]);
151  cout << endl;
152  }
153 
154 }
TRandom3 rnd(1)
virtual size_t size() const
Definition: wavearray.hh:127
void setMatrix(double edge=0., double f=1.)
Definition: regression.cc:407
tuple f
Definition: cwb_online.py:91
printf("total live time: non-zero lags = %10.1f \n", liveTot)
Definition: ced.hh:24
virtual void rate(double r)
Definition: wavearray.hh:123
wavearray< double > target
float * rank
#define F2
cout<< "skymap size : "<< L<< endl;for(int l=0;l< L;l++) sm.set(l, l);sm > const_cast< char * >("skymap.dat")
double omega_lf
size_t add(WSeries< double > &target, char *name, double fL=0., double fH=0.)
Definition: regression.cc:73
virtual void start(double s)
Definition: wavearray.hh:119
int j
Definition: cwb_net.C:10
i drho i
double omega
int totalscratch
#define RATE
#define LENGHT
void apply(double threshold=0., char c='a')
Definition: regression.cc:691
wavearray< double > getRank(int n)
Definition: regression.hh:134
WSeries< double > tfmap
void solve(double th, int nE=0, char c='s')
Definition: regression.cc:592
#define F1
wavearray< double > vfreq
Definition: regression.hh:171
size_t setFilter(size_t)
Definition: regression.cc:258
plot plot(witness_tf, const_cast< char * >("same"), 2, totalscratch, totalscratch+LENGHT, true, 0, 5, true, 50)
wavearray< double > getVEIGEN(int n=-1)
Definition: regression.cc:339
wavearray< double > getClean()
Definition: regression.hh:117
regression r
Definition: Regression_H1.C:44
WDM< double > wdtf(lev, 2 *lev, 6, 10)
virtual void stop(double s)
Definition: wavearray.hh:121
void Forward(int n=-1)
param: wavelet - n is number of steps (-1 means full decomposition)
Definition: wseries.cc:228
void unmask(int n, double flow=0., double fhigh=0.)
Definition: regression.cc:321
void mask(int n, double flow=0., double fhigh=0.)
Definition: regression.cc:303
double df
DataType_t * data
Definition: wavearray.hh:301
double dt
int lev
Definition: Regression_H1.C:38
virtual void resize(unsigned int)
Definition: wavearray.cc:445
virtual void FFT(int=1)
Definition: wavearray.cc:814
TCanvas * c1