Logo coherent WaveBurst  
Library Reference Guide
Logo
watfun.hh
Go to the documentation of this file.
1 /*
2 # Copyright (C) 2019 Sergey Klimenko, Gabriele Vedovato
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 // Wavelet Analysis Tool
20 // S.Klimenko, University of Florida
21 // library of general functions
22 
23 #ifndef WATFUN_HH
24 #define WATFUN_HH
25 
26 #include <iostream>
27 #include <stdlib.h>
28 #include <math.h>
29 #include <TMath.h>
30 #include "wat.hh"
31 
32 #define PI 3.141592653589793
33 #define speedlight 299792458.0
34 
35 // WAT functions
36 
37 // Calculates polynomial interpolation coefficient using Lagrange formula.
38 inline double Lagrange(const int n, const int i, const double x)
39 {
40  double c = 1.;
41  double xn = x+n/2.-0.5; // shift to the center of interpolation interval
42 
43  for(int j=0; j<n; j++)
44  if(j!=i) c *= (xn-j)/(i-j);
45 
46  return c;
47 }
48 
49 
50 // Nevill's polynomial interpolation.
51 // x - polynom argument (x stride is allways 1)
52 // n - number of interpolation samples
53 // p - pointer to a sample with x=0.
54 // q - double array of length n
55 template<class DataType_t>
56 inline double Nevill(const double x0,
57  int n,
58  DataType_t* p,
59  double* q)
60 {
61  int i;
62  double x = x0;
63  double xm = 0.5;
64 
65  n--;
66  *q = *p;
67 
68  for(i=0; i<n; i++)
69  q[i] = p[i] + (x--)*(p[i+1]-p[i]);
70 
71  while(--n >= 1){
72  x = x0;
73 
74  q[0] += xm*(x--)*(q[1]-q[0]);
75  if(n == 1) goto M0;
76  q[1] += xm*(x--)*(q[2]-q[1]);
77  if(n == 2) goto M0;
78  q[2] += xm*(x--)*(q[3]-q[2]);
79  if(n == 3) goto M0;
80  q[3] += xm*(x--)*(q[4]-q[3]);
81  if(n == 4) goto M0;
82  q[4] += xm*(x--)*(q[5]-q[4]);
83  if(n == 5) goto M0;
84  q[5] += xm*(x--)*(q[6]-q[5]);
85  if(n == 6) goto M0;
86 
87  for(i=6; i<n; i++)
88  q[i] += xm*(x--)*(q[i+1]-q[i]);
89 
90 M0: xm /= (1.+xm);
91  }
92 
93  return *q;
94 }
95 
96 // calculate significance for sign cross-correlation
97 inline double signPDF(const size_t m, const size_t k)
98 {
99  size_t i;
100  double pdf = 0.;
101  size_t n = m+k;
102  double rho = (double(m)-double(k))/n;
103 
104  if(n < 1) return 0.;
105  if(n < 100) {
106  for(i=1; i<k+1; i++){ pdf -= log(double(m+i)/double(i)); }
107  pdf -= log(double(n))-(n+1)*log(2.);
108  pdf -= log(sqrt(2.*PI/n)); // normalization from Gaussian approximation
109  }
110  else pdf = n*rho*rho/2.;
111  return pdf;
112 }
113 
114 
115 // survival probability for Gamma distribution
116 // calculates integral I=int(x*x^n*exp(-x)) from Y to infinity
117 // returns confidence = -log(I/Gamma(n))
118 // Y - low integration limit
119 // n - Gamma function parameter
120 inline double gammaCLa(double Y, int n){
121  double y = Y;
122  double s = 1.;
123  // if(Y<0.) return 0.;
124  for(int k=1; k<n; k++){
125  s += y;
126  if((y*=Y/(k+1))>1.e290) break;
127  }
128  return Y-log(s);
129 }
130 
131 inline double gammaCL(double x, double n){
132  return x>=n ? x-n + (2+(n-1)/sqrt(2.))/(n+1)-(n-sqrt(n/4.)-1)*log(x/n) :
133  pow(x/n,sqrt(n))*(2+(n-1)/sqrt(2.))/(n+1);
134 }
135 
136 // survival probability for LogNormal distribution
137 // calculates integral I=int(Pln(x)) from Y to infinity
138 // returns one side confidence = -log(I)
139 // Y - low integration limit
140 // p - x peak
141 // s - sigma
142 // a - asymmetry
143 
144 inline double logNormCL(double Y, double p=0., double s=1., double a=0.0001){
145  // if(a <= 0.) a = 0.0001;
146  double z = a*sqrt(log(4.));
147  z = sinh(z)/z;
148  z = 1+a*z*(Y-p)/s;
149  z = z>0 ? fabs(log(z)/a-a) : 0.;
150  return erfc(z/sqrt(2.))/2.;
151 }
152 
153 // calculate value of argument from LogNormal and Normal survival probability
154 // calculates inverse of integral I=int(P(x) dx) from Y to infinity
155 // returns value of the argument Y
156 // CL - single side probability
157 // p - x peak
158 // s - sigma
159 // a - asymmetry
160 
161 inline double logNormArg(double CL, double p=0., double s=1., double a=0.){
162  double z = fabs(sqrt(-2.14*log(2.*CL))-0.506);
163  if(a <= 0.) return p+z*s;
164  double f = a*sqrt(log(4.));
165  z = (exp((z+a)*a)-1)/a;
166  return p+z*s*f/sinh(f);
167 }
168 
169 
170 // Calculates polynomial interpolation coefficients using Lagrange formula.
171 // gap is allouded in the middle of interval, like
172 // n=13, m=5: x x x x o o o o o x x x x
173 inline void fLagrange(int n, int m, double* c)
174 {
175  if(!(n&1)) n++;
176  if(!(m&1)) m++;
177  int i,j;
178 
179  for(i=0; i<n; i++) c[i]=0.;
180 
181  for(i=0; i<n; i++) {
182  if(abs(n/2-i)<=m/2) continue;
183  c[i] = 1.;
184  for(j=0; j<n; j++) {
185  if(abs(n/2-j)<=m/2) continue;
186  if(j!=i) c[i] *= double(n/2-j)/(i-j);
187  }
188  }
189  return;
190 }
191 
192 // complete gamma function
193 inline double Gamma(double r)
194 {
195  return TMath::Gamma(r);
196 }
197 
198 // incomplete regularized gamma function
199 inline double Gamma(double r, double x)
200 {
201  return TMath::Gamma(r,x);
202 }
203 
204 // inverse incomplete regularized gamma function
205 inline double iGamma(double r, double p)
206 {
207  double x = 0;
208  double P = 1.;
209  while(P>p) { x += 1.e-5; P=1-TMath::Gamma(r,x); }
210  return x;
211 }
212 
213 // complete gamma function
214 // used by 1G - only for back compatibility
215 inline double Gamma1G(double r)
216 {
217  if(r<=0.) return 0.;
218  double p0 = 1.000000000190015;
219  double p1 = 76.18009172947146;
220  double p2 = -86.50532032941677;
221  double p3 = 24.01409824083091;
222  double p4 = -1.231739572450155;
223  double p5 = 1.208650973866179e-3;
224  double p6 = -5.395239384953e-6;
225  double g = p0+p1/(r+1)+p2/(r+2)+p3/(r+3)+p4/(r+4)+p5/(r+5)+p6/(r+6);
226  return g*pow(r+5.5,r+0.5)*exp(-(r+5.5))*sqrt(2*PI)/r;
227 }
228 
229 // incomplete regularized gamma function
230 // used by 1G - only for back compatibility
231 inline double Gamma1G(double r, double x)
232 {
233  double a = r>0 ? 1/r : 0;
234  double b = a;
235  int n = int(10*x-r);
236  if(n<100) n = 100;
237  for(int i=1; i<n; i++) {
238  a *= x/(r+i);
239  b += a;
240  }
241  return b*exp(log(x)*r-x)/Gamma1G(r);
242 }
243 
244 // inverse incomplete regularized gamma function (approximation)
245 // used by 1G - only for back compatibility
246 inline double iGamma1G(double r, double p)
247 {
248  double x = 700;
249  double P = 1.;
250  while(P>p) { x *= 0.999; P=Gamma1G(r,x); }
251  return 2*x;
252 }
253 
254 #endif // WATFUN_HH
double rho
double gammaCL(double x, double n)
Definition: watfun.hh:131
static double g(double e)
Definition: GNGen.cc:116
wavearray< double > a(hp.size())
double Gamma(double r)
Definition: watfun.hh:193
int n
Definition: cwb_net.C:28
wavearray< double > z
Definition: Test10.C:32
double iGamma(double r, double p)
Definition: watfun.hh:205
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
double Gamma1G(double r)
Definition: watfun.hh:215
double logNormCL(double Y, double p=0., double s=1., double a=0.0001)
Definition: watfun.hh:144
double Nevill(const double x0, int n, DataType_t *p, double *q)
Definition: watfun.hh:56
int m
Definition: cwb_net.C:28
int j
Definition: cwb_net.C:28
i drho i
cout<< "SNR "<< xsnr<< endl;wavearray< double > f
Definition: ComputeSNR.C:75
#define PI
Definition: watfun.hh:32
watplot p2(const_cast< char *>("TFMap2"))
double signPDF(const size_t m, const size_t k)
Definition: watfun.hh:97
i() int(T_cor *100))
watplot p1(const_cast< char *>("TFMap1"))
bool log
Definition: WaveMDC.C:41
int k
double Lagrange(const int n, const int i, const double x)
Definition: watfun.hh:38
double logNormArg(double CL, double p=0., double s=1., double a=0.)
Definition: watfun.hh:161
void fLagrange(int n, int m, double *c)
Definition: watfun.hh:173
regression r
Definition: Regression_H1.C:44
s s
Definition: cwb_net.C:155
double gammaCLa(double Y, int n)
Definition: watfun.hh:120
double fabs(const Complex &x)
Definition: numpy.cc:55
double iGamma1G(double r, double p)
Definition: watfun.hh:246
wavearray< double > y
Definition: Test10.C:31
Double_t x0