Logo Coherent WaveBurst  
Reference Guide
Logo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
wavecor.cc
Go to the documentation of this file.
1 // ++++++++++++++++++++++++++++++++++++++++++++++
2 // S. Klimenko, University of Florida
3 // WAT cross-correlation class
4 // ++++++++++++++++++++++++++++++++++++++++++++++
5 
6 #define WAVECOR_CC
7 #include <time.h>
8 #include <iostream>
9 #include <stdexcept>
10 #include "wavecor.hh"
11 
12 ClassImp(wavecor) // used by THtml doc
13 
14 using namespace std;
15 
16 // constructors
17 
19 {
20  shift = 0.;
21  ifo = 0;
22  run = 0;
23  window= 0.;
24  lagint= 0.;
25 }
26 
28 {
29  cList.clear();
30  cList = value.cList;
31  xcor = value.xcor;
32  xlag = value.xlag;
33  shift = value.shift;
34  ifo = value.ifo;
35  run = value.run;
36  window= value.window;
37  lagint= value.lagint;
38 }
39 
40 // destructor
41 
43 
44 //: operator =
45 
47 {
48  cList = value.cList;
49  xcor = value.xcor;
50  xlag = value.xlag;
51  shift = value.shift;
52  ifo = value.ifo;
53  run = value.run;
54  window= value.window;
55  lagint= value.lagint;
56  return *this;
57 }
58 
59 
60 
61 //**************************************************************************
62 // initialize wavecor from two time series with Kendall x-correlation
63 //**************************************************************************
65  double w, double t, size_t skip)
66 {
67  cList.clear();
68  ifo=0; shift=0.;
69  window = w;
70  lagint = t;
71 
72  if(a.start()!=b.start() || a.size()!=b.size() || a.rate()!=b.rate()) {
73  cout<<"wavecor::init() invalid arguments"<<endl;
74  return;
75  }
76 
77  int l;
78  short sign;
79  size_t i,j,k;
80  size_t last=0;
81  size_t N = a.size();
82 
83  double x,y;
84  double sum = 0.;
85  double* pa=NULL;
86  double* pb=NULL;
87 
88  size_t n = size_t(fabs(t)*a.rate()); // # of time lags
89  n = n/2; // # of time lags on one side
90 
91  size_t m = size_t(fabs(w)*a.rate()); // # of samples in running window
92  if(!(m&1)) m++; // # of samples in running window
93 
94  short** P = (short **)malloc(m*sizeof(short*));
95  for(i=0; i<m; i++) P[i] = (short*)malloc(m*sizeof(short));
96 
97  xcor.start(a.start()); // set start time
98  xlag.start(a.start()); // set start time
99  xcor.rate(a.rate()/skip);
100  xlag.rate(a.rate()/skip);
101  if(xcor.size()!=N/skip) xcor.resize(N/skip);
102  if(xlag.size()!=N/skip) xlag.resize(N/skip);
103 
104  xcor = 0.;
105 
106  for(l=-int(n); l<=int(n); l++){
107 
108  pa = l<0 ? a.data-l : a.data; // a shifted right
109  pb = l>0 ? b.data+l : b.data; // b shifted right
110 
111  sum = 0.;
112  for(i=0; i<m; i++){
113  x = pa[i];
114  y = pb[i];
115  for(j=i; j<m; j++){
116  sign = x>pa[j] ? 1 : -1;
117  sign *= y>pb[j] ? 1 : -1;
118  P[i][j] = sign;
119  P[j][i] = sign;
120  sum += 2.*sign;
121  }
122  sum -= 2.;
123  }
124 
125 // cout<<sum<<" "<<m<<" "<<n<<endl;
126 
127  last = 0;
128  for(i=0; i<N; i++){
129 
130  k = i/skip;
131  if(i==k*skip) {
132  if(fabs(xcor.data[k]) < fabs(sum)) {
133  xcor.data[k] = sum;
134  xlag.data[k] = float(l);
135  }
136  }
137 
138  if(i+m+abs(l)>=N) continue;
139 
140  x = pa[i+m];
141  y = pb[i+m];
142 
143  for(j=0; j<m; j++) {
144  sum -= P[last][j];
145  sign = x>pa[i+j+1] ? 1 : -1;
146  sign *= y>pb[i+j+1] ? 1 : -1;
147  P[last][j] = sign;
148  sum += double(sign);
149  }
150 
151  last++;
152  if(last>=m) last=0;
153  }
154 
155  }
156  xcor *= 1./double(m*(m-1));
157  for(i=0; i<m; i++) delete P[i];
158  delete P;
159  return;
160 }
161 
162 
163 //**************************************************************************
164 // initialize wavecor from two time series
165 //**************************************************************************
167  double w, double t, size_t skip)
168 {
169  cList.clear();
170  ifo=0; shift=0.;
171  window = w;
172  lagint = t;
173 
174  if(a.start()!=b.start() || a.size()!=b.size() || a.rate()!=b.rate()) {
175  cout<<"wavecor::init() invalid arguments"<<endl;
176  return;
177  }
178 
179  size_t i,j;
180  size_t N = a.size();
181  size_t m = size_t(fabs(w)*a.rate()); // samples in integration window
182  size_t n = size_t(fabs(t)*a.rate()/2); // one side time lag interval
183  size_t M = N/skip; // take every skip sample from x
184  float r = a.rate();
185  float var;
186 
187  if(!(m&1)) m++; // # of samples in integration window
188 
189  xcor.start(a.start()); // set start time
190  xlag.start(a.start()); // set start time
191  xcor.rate(a.rate()/skip);
192  xlag.rate(a.rate()/skip);
193  if(xcor.size()!=M) xcor.resize(M);
194  if(xlag.size()!=M) xlag.resize(M);
195 
196 
198  x.rate(a.rate());
199 
200  x = a;
201  x *= b;
202 
203  if(w<0.) x.median(fabs(w),NULL,false,skip);
204  else x.mean(fabs(w),NULL);
205 
206  for(i=0; i<M; i++) {
207  xcor.data[i] = float(x.data[i*skip]);
208  xlag.data[i] = 0.;
209  }
210 
211  for(i=0; i<n; i++){
212  x.cpf(b,N-n+i,n-i); // channel1 start = start
213  // channel2 start = start+lag (positive)
214  for(j=N-n+i; j<N; j++) x.data[j]=0.;
215 
216  x *= a;
217 
218  if(w<0.) x.median(fabs(w),NULL,false,skip);
219  else x.mean(fabs(w),NULL);
220 
221  for(j=0; j<M; j++) {
222  var = float(x.data[j*skip]);
223  if(fabs(var)>fabs(xcor.data[j])) {
224  xcor.data[j] = var;
225  xlag.data[j] = float(n-i)/r; // positive lag
226  }
227  }
228 
229  x.cpf(a,N-n+i,n-i); // channel2 start = start
230  // channel1 start = start-lag (negative)
231  for(j=N-n+i; j<N; j++) x.data[j]=0.;
232 
233  x *= b;
234  if(w<0.) x.median(fabs(w),NULL,false,skip);
235  else x.mean(fabs(w),NULL);
236 
237  for(j=0; j<M; j++) {
238  var = float(x.data[j*skip]);
239  if(fabs(var)>fabs(xcor.data[j])) {
240  xcor.data[j] = var;
241  xlag.data[j] = -float(n-i)/r; // negative lag
242  }
243  }
244  }
245  xcor *= sqrt(double(m))/2.;
246 }
247 
248 
249 
250 
251 //**************************************************************************
252 // select x-correlation samples above threshold T
253 //**************************************************************************
254 double wavecor::select(double T)
255 {
256  if(T <= 0.) return 1.;
257 
258  size_t i;
259  size_t nonZero=0;
260  size_t N = xcor.size();
261 
262  for(i=0; i<N; i++) {
263  if(fabs(xcor.data[i]) < T) xcor.data[i]=0.;
264  else nonZero++;
265  }
266  return double(nonZero)/xcor.size();
267 }
268 
269 
270 //**************************************************************************
271 // select x-correlation samples
272 //**************************************************************************
273 double wavecor::coincidence(double w, wavecor* pw)
274 {
275  if(w<=0. || pw==NULL) return 0.;
276 
277  size_t i,last;
278  size_t nonZero=0;
279  size_t N = xcor.size();
280  size_t n = size_t(fabs(w)*xcor.rate());
281  size_t count=0;
282  size_t nM = n/2; // index of median sample
283  size_t nL = N-int(nM+1);
284 
285  if(n&1) n--;
286 
287  wavearray<double> x(n+1);
288  float* px = pw->xcor.data;
289  x.rate(xcor.rate());
290 
291  for(i=0; i<=n; i++) {
292  x.data[i] = fabs(*(px++));
293  if(x.data[i] > 0.) count++; // number of non-zero samples in x
294  }
295 
296  last = 0;
297  nonZero = 0;
298 
299  for(i=0; i<N; i++){
300 
301  if(!count) xcor.data[i]=0.;
302 
303  if(i>=nM && i<nL) { // copy next sample into last
304  if(x.data[last] > 0.) count--;
305  x.data[last] = fabs(*(px++));
306  if(x.data[last++] > 0.) count++;
307  }
308 
309  if(last>n) last = 0;
310  if(xcor.data[i]!=0.) nonZero++;
311  }
312  return double(nonZero)/xcor.size();
313 }
314 
315 
316 
317 
318 
319 
320 
321 
322 
323 
324 
325 
wavearray< double > t(hp.size())
virtual size_t size() const
Definition: wavearray.hh:127
par[0] value
virtual double select(double)
Definition: wavecor.cc:254
virtual void rate(double r)
Definition: wavearray.hh:123
CWB run(runID)
wavearray< double > a(hp.size())
int n
Definition: cwb_net.C:10
double window
Definition: wavecor.hh:76
std::list< vector_int > cList
Definition: wavecor.hh:84
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
TRandom3 P
Definition: compare_bkg.C:296
STL namespace.
virtual void init(wavearray< double > &, wavearray< double > &, double, double, size_t=0)
param: two wavearrays, integration window, interval for time lag analysis and skip parameter for runn...
Definition: wavecor.cc:166
double lagint
Definition: wavecor.hh:77
virtual double median(size_t=0, size_t=0) const
Definition: wavearray.cc:1558
#define M
Definition: UniqSLagsList.C:3
int m
Definition: cwb_net.C:10
virtual void start(double s)
Definition: wavearray.hh:119
int j
Definition: cwb_net.C:10
i drho i
#define N
int ifo
Definition: wavecor.hh:74
virtual double mean() const
Definition: wavearray.cc:1053
char ifo[NIFO_MAX][8]
float shift
Definition: wavecor.hh:73
wavearray< double > w
Definition: Test1.C:27
wavearray< double > * px
i() int(T_cor *100))
int k
int run
Definition: wavecor.hh:75
regression r
Definition: Regression_H1.C:44
virtual void kendall(wavearray< double > &, wavearray< double > &, double, double, size_t=0)
param: two wavearrays, integration window, interval for time lag analysis and skip parameter for runn...
Definition: wavecor.cc:64
virtual ~wavecor()
Definition: wavecor.cc:42
wavearray< float > xcor
Definition: wavecor.hh:80
double T
Definition: testWDM_4.C:11
virtual double coincidence(double, wavecor *)
param: coincidence window, pointer to wavecor object
Definition: wavecor.cc:273
wavecor()
Definition: wavecor.cc:18
wavearray< float > xlag
Definition: wavecor.hh:82
double fabs(const Complex &x)
Definition: numpy.cc:37
int l
Definition: cbc_plots.C:434
DataType_t * data
Definition: wavearray.hh:301
double shift[NIFO_MAX]
virtual void resize(unsigned int)
Definition: wavearray.cc:445
wavearray< double > y
Definition: Test10.C:31
void cpf(const wavearray< DataType_t > &, int=0, int=0, int=0)
Definition: wavearray.cc:699
wavecor & operator=(const wavecor &)
Definition: wavecor.cc:46