Logo coherent WaveBurst  
Library Reference Guide
Logo
wavecor.cc
Go to the documentation of this file.
1 /*
2 # Copyright (C) 2019 Sergey Klimenko
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17 
18 
19 // ++++++++++++++++++++++++++++++++++++++++++++++
20 // S. Klimenko, University of Florida
21 // WAT cross-correlation class
22 // ++++++++++++++++++++++++++++++++++++++++++++++
23 
24 #define WAVECOR_CC
25 #include <time.h>
26 #include <iostream>
27 #include <stdexcept>
28 #include "wavecor.hh"
29 
30 ClassImp(wavecor) // used by THtml doc
31 
32 using namespace std;
33 
34 // constructors
35 
37 {
38  shift = 0.;
39  ifo = 0;
40  run = 0;
41  window= 0.;
42  lagint= 0.;
43 }
44 
46 {
47  cList.clear();
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 }
57 
58 // destructor
59 
61 
62 //: operator =
63 
65 {
66  cList = value.cList;
67  xcor = value.xcor;
68  xlag = value.xlag;
69  shift = value.shift;
70  ifo = value.ifo;
71  run = value.run;
72  window= value.window;
73  lagint= value.lagint;
74  return *this;
75 }
76 
77 
78 
79 //**************************************************************************
80 // initialize wavecor from two time series with Kendall x-correlation
81 //**************************************************************************
83  double w, double t, size_t skip)
84 {
85  cList.clear();
86  ifo=0; shift=0.;
87  window = w;
88  lagint = t;
89 
90  if(a.start()!=b.start() || a.size()!=b.size() || a.rate()!=b.rate()) {
91  cout<<"wavecor::init() invalid arguments"<<endl;
92  return;
93  }
94 
95  int l;
96  short sign;
97  size_t i,j,k;
98  size_t last=0;
99  size_t N = a.size();
100 
101  double x,y;
102  double sum = 0.;
103  double* pa=NULL;
104  double* pb=NULL;
105 
106  size_t n = size_t(fabs(t)*a.rate()); // # of time lags
107  n = n/2; // # of time lags on one side
108 
109  size_t m = size_t(fabs(w)*a.rate()); // # of samples in running window
110  if(!(m&1)) m++; // # of samples in running window
111 
112  short** P = (short **)malloc(m*sizeof(short*));
113  for(i=0; i<m; i++) P[i] = (short*)malloc(m*sizeof(short));
114 
115  xcor.start(a.start()); // set start time
116  xlag.start(a.start()); // set start time
117  xcor.rate(a.rate()/skip);
118  xlag.rate(a.rate()/skip);
119  if(xcor.size()!=N/skip) xcor.resize(N/skip);
120  if(xlag.size()!=N/skip) xlag.resize(N/skip);
121 
122  xcor = 0.;
123 
124  for(l=-int(n); l<=int(n); l++){
125 
126  pa = l<0 ? a.data-l : a.data; // a shifted right
127  pb = l>0 ? b.data+l : b.data; // b shifted right
128 
129  sum = 0.;
130  for(i=0; i<m; i++){
131  x = pa[i];
132  y = pb[i];
133  for(j=i; j<m; j++){
134  sign = x>pa[j] ? 1 : -1;
135  sign *= y>pb[j] ? 1 : -1;
136  P[i][j] = sign;
137  P[j][i] = sign;
138  sum += 2.*sign;
139  }
140  sum -= 2.;
141  }
142 
143 // cout<<sum<<" "<<m<<" "<<n<<endl;
144 
145  last = 0;
146  for(i=0; i<N; i++){
147 
148  k = i/skip;
149  if(i==k*skip) {
150  if(fabs(xcor.data[k]) < fabs(sum)) {
151  xcor.data[k] = sum;
152  xlag.data[k] = float(l);
153  }
154  }
155 
156  if(i+m+abs(l)>=N) continue;
157 
158  x = pa[i+m];
159  y = pb[i+m];
160 
161  for(j=0; j<m; j++) {
162  sum -= P[last][j];
163  sign = x>pa[i+j+1] ? 1 : -1;
164  sign *= y>pb[i+j+1] ? 1 : -1;
165  P[last][j] = sign;
166  sum += double(sign);
167  }
168 
169  last++;
170  if(last>=m) last=0;
171  }
172 
173  }
174  xcor *= 1./double(m*(m-1));
175  for(i=0; i<m; i++) delete P[i];
176  delete P;
177  return;
178 }
179 
180 
181 //**************************************************************************
182 // initialize wavecor from two time series
183 //**************************************************************************
185  double w, double t, size_t skip)
186 {
187  cList.clear();
188  ifo=0; shift=0.;
189  window = w;
190  lagint = t;
191 
192  if(a.start()!=b.start() || a.size()!=b.size() || a.rate()!=b.rate()) {
193  cout<<"wavecor::init() invalid arguments"<<endl;
194  return;
195  }
196 
197  size_t i,j;
198  size_t N = a.size();
199  size_t m = size_t(fabs(w)*a.rate()); // samples in integration window
200  size_t n = size_t(fabs(t)*a.rate()/2); // one side time lag interval
201  size_t M = N/skip; // take every skip sample from x
202  float r = a.rate();
203  float var;
204 
205  if(!(m&1)) m++; // # of samples in integration window
206 
207  xcor.start(a.start()); // set start time
208  xlag.start(a.start()); // set start time
209  xcor.rate(a.rate()/skip);
210  xlag.rate(a.rate()/skip);
211  if(xcor.size()!=M) xcor.resize(M);
212  if(xlag.size()!=M) xlag.resize(M);
213 
214 
216  x.rate(a.rate());
217 
218  x = a;
219  x *= b;
220 
221  if(w<0.) x.median(fabs(w),NULL,false,skip);
222  else x.mean(fabs(w),NULL);
223 
224  for(i=0; i<M; i++) {
225  xcor.data[i] = float(x.data[i*skip]);
226  xlag.data[i] = 0.;
227  }
228 
229  for(i=0; i<n; i++){
230  x.cpf(b,N-n+i,n-i); // channel1 start = start
231  // channel2 start = start+lag (positive)
232  for(j=N-n+i; j<N; j++) x.data[j]=0.;
233 
234  x *= a;
235 
236  if(w<0.) x.median(fabs(w),NULL,false,skip);
237  else x.mean(fabs(w),NULL);
238 
239  for(j=0; j<M; j++) {
240  var = float(x.data[j*skip]);
241  if(fabs(var)>fabs(xcor.data[j])) {
242  xcor.data[j] = var;
243  xlag.data[j] = float(n-i)/r; // positive lag
244  }
245  }
246 
247  x.cpf(a,N-n+i,n-i); // channel2 start = start
248  // channel1 start = start-lag (negative)
249  for(j=N-n+i; j<N; j++) x.data[j]=0.;
250 
251  x *= b;
252  if(w<0.) x.median(fabs(w),NULL,false,skip);
253  else x.mean(fabs(w),NULL);
254 
255  for(j=0; j<M; j++) {
256  var = float(x.data[j*skip]);
257  if(fabs(var)>fabs(xcor.data[j])) {
258  xcor.data[j] = var;
259  xlag.data[j] = -float(n-i)/r; // negative lag
260  }
261  }
262  }
263  xcor *= sqrt(double(m))/2.;
264 }
265 
266 
267 
268 
269 //**************************************************************************
270 // select x-correlation samples above threshold T
271 //**************************************************************************
272 double wavecor::select(double T)
273 {
274  if(T <= 0.) return 1.;
275 
276  size_t i;
277  size_t nonZero=0;
278  size_t N = xcor.size();
279 
280  for(i=0; i<N; i++) {
281  if(fabs(xcor.data[i]) < T) xcor.data[i]=0.;
282  else nonZero++;
283  }
284  return double(nonZero)/xcor.size();
285 }
286 
287 
288 //**************************************************************************
289 // select x-correlation samples
290 //**************************************************************************
291 double wavecor::coincidence(double w, wavecor* pw)
292 {
293  if(w<=0. || pw==NULL) return 0.;
294 
295  size_t i,last;
296  size_t nonZero=0;
297  size_t N = xcor.size();
298  size_t n = size_t(fabs(w)*xcor.rate());
299  size_t count=0;
300  size_t nM = n/2; // index of median sample
301  size_t nL = N-int(nM+1);
302 
303  if(n&1) n--;
304 
305  wavearray<double> x(n+1);
306  float* px = pw->xcor.data;
307  x.rate(xcor.rate());
308 
309  for(i=0; i<=n; i++) {
310  x.data[i] = fabs(*(px++));
311  if(x.data[i] > 0.) count++; // number of non-zero samples in x
312  }
313 
314  last = 0;
315  nonZero = 0;
316 
317  for(i=0; i<N; i++){
318 
319  if(!count) xcor.data[i]=0.;
320 
321  if(i>=nM && i<nL) { // copy next sample into last
322  if(x.data[last] > 0.) count--;
323  x.data[last] = fabs(*(px++));
324  if(x.data[last++] > 0.) count++;
325  }
326 
327  if(last>n) last = 0;
328  if(xcor.data[i]!=0.) nonZero++;
329  }
330  return double(nonZero)/xcor.size();
331 }
332 
333 
334 
335 
336 
337 
338 
339 
340 
341 
342 
343 
wavearray< double > t(hp.size())
double M
Definition: DrawEBHH.C:13
par [0] value
virtual double select(double)
Definition: wavecor.cc:272
virtual void rate(double r)
Definition: wavearray.hh:141
CWB run(runID)
wavearray< double > a(hp.size())
int n
Definition: cwb_net.C:28
double window
Definition: wavecor.hh:94
std::list< vector_int > cList
Definition: wavecor.hh:102
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<< "\ layers : "<< nLAYERS<< "\ dF(hz) : "<< dF<< "\ 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
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:184
float xlag
double lagint
Definition: wavecor.hh:95
int m
Definition: cwb_net.C:28
virtual void start(double s)
Definition: wavearray.hh:137
int j
Definition: cwb_net.C:28
i drho i
#define N
int ifo
Definition: wavecor.hh:92
char ifo[NIFO_MAX][8]
float shift
Definition: wavecor.hh:91
wavearray< double > w
Definition: Test1.C:27
virtual size_t size() const
Definition: wavearray.hh:145
wavearray< double > * px
i() int(T_cor *100))
int l
int k
int run
Definition: wavecor.hh:93
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:82
virtual ~wavecor()
Definition: wavecor.cc:60
wavearray< float > xcor
Definition: wavecor.hh:98
double T
Definition: testWDM_4.C:11
virtual double coincidence(double, wavecor *)
param: coincidence window, pointer to wavecor object
Definition: wavecor.cc:291
virtual double mean() const
Definition: wavearray.cc:1071
wavecor()
Definition: wavecor.cc:36
wavearray< float > xlag
Definition: wavecor.hh:100
double fabs(const Complex &x)
Definition: numpy.cc:55
DataType_t * data
Definition: wavearray.hh:319
double shift[NIFO_MAX]
virtual double median(size_t=0, size_t=0) const
Definition: wavearray.cc:1576
wavearray< double > y
Definition: Test10.C:31
double xcor
void cpf(const wavearray< DataType_t > &, int=0, int=0, int=0)
Definition: wavearray.cc:717
wavecor & operator=(const wavecor &)
Definition: wavecor.cc:64