-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcross_corr
executable file
·174 lines (158 loc) · 3.13 KB
/
cross_corr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/awk -f
BEGIN{
if (ARGC>2)
{
print "#WARING: Average over multiple files!!";
}
if (ARGV[1]=="")
{
print "#FILE: STDIN";
}
else
{
print "#FILE:",ARGV[1];
}
if (col1=="")
{
col1=1;
print "#Setting column1 to",col1,"(use option \"-v col1=X\" to change)";
}
else
{
print "#Using column1",col1;
}
if (col2=="")
{
col2=1;
print "#Setting column2 to",col2,"(use option \"-v col2=X\" to change)";
}
else
{
print "#Using column2",col2;
}
if (timescale=="")
{
timescale=1;
print "#Setting timescale to",timescale,"(use option \"-v timescale=X\" to change)";
}
else
{
print "#Using timescale",timescale;
}
c=0;
warn=0;
if ( col1 > col2)
{
max_col=col1;
}
else
{
max_col=col2;
}
mean_data1=0;
mean_data2=0;
mixed_mean=0;
}
/^[@#]/{
next;
}
{
if (NF>=max_col)
{
data1[c]=$col1;
data2[c]=$col2;
c++;
mean_data1+=$col1;
mean_data2+=$col2;
mixed_mean+=$col1*$col2;
}
else
{
if (warn == 0)
{
print "# Not enough data in line",NR;
warn++;
}
}
}
END{
mean_data1/=c;
mean_data2/=c;
mixed_mean/=c;
mean1_x_mean2=mean_data1*mean_data2;
cor_norm=mixed_mean-mean1_x_mean2;
int_cor=0;
print "#values=",c,"mean1=",mean_data1,"mean2=",mean_data2,"mixed_mean=",mixed_mean,"norm=",cor_norm;
print "#Output format: dt cor_of_dt int_cor log(cor_of_dt) tau_int se_tau_int";
#not so important, will be reset anyway later
max_dt=c/10;
neg=0;
for (dt=0;dt<max_dt;dt++)
{
mixed_mean_dt=0;
mean1_0=0;
mean2_dt=0;
#corrected norm useful for strongly correlated data, but slow !
mixed_mean_0=0;
mean2_0=0;
mean_max=c-dt;
#same statistics for all dt useful for long timeseries
#mean_max=c-max_dt;
for (i=0;i<mean_max;i++)
{
mixed_mean_dt+=data1[i]*data2[i+dt];
mean1_0+=data1[i];
mean2_dt+=data2[i+dt];
#corrected norm useful for strongly correlated data, but slow !
mean2_0+=data2[i];
mixed_mean_0+=data1[i]*data2[i];
#print i,data1[i],data2[i+dt],mixed_mean_dt;
}
mixed_mean_dt/=mean_max;
mean1_0/=mean_max;
mean2_dt/=mean_max;
#corrected norm useful for strongly correlated data, but slow !
mean2_0/=mean_max;
mixed_mean_0/=mean_max;
cor_norm=mixed_mean_0-mean1_0*mean2_0;
#print "norm",mixed_mean_dt;
cor_of_dt=(mixed_mean_dt-mean1_0*mean2_dt)/cor_norm;
#faster for long time serie, BUT mean has higher statistics -> bias
#cor_of_dt=(mixed_mean_dt-mean_data1*mean_data2)/cor_norm;
int_cor+=cor_of_dt;
time=dt*timescale;
if ( cor_of_dt > 0)
{
log_cor=log(cor_of_dt);
}
else
{
log_cor="nan";
}
#tau_int=0.5+sum k=1,inf A(K)=int_cor-0.5
tau_int=int_cor-0.5;
se_tau_int=tau_int*sqrt(2*(2*dt+1)/c);
print time,cor_of_dt,int_cor,log_cor,tau_int,se_tau_int;
#stop calc when:
#a) correlation is negative, ten more steps
# if ((cor_of_dt < 0 ) && (neg==0))
# {
# #stop it!
# #max_dt=dt;
# max_dt=dt+10;
# neg++;
# }
#b) rule of thumb (janke): dt >= 6*tau_int
if (dt >= 6*tau_int){
max_dt=dt;
}
#c) never
#procent on stderr
# procent=dt/max_dt;
# print "#",procent,"%"
# if ( procent%10 == 0 )
# {
# print "#",procent,"%" > /dev/stderr
# }
}
}