-
Notifications
You must be signed in to change notification settings - Fork 3
/
corr.cpp
107 lines (86 loc) · 1.78 KB
/
corr.cpp
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
#include <math.h>
#include <iostream>
// #ifdef NO_OMP
// #define omp_get_thread_num() 0
// #else
// #include <omp.h>
// #endif
#include "corr.h"
using namespace std;
Corr::Corr(int N_, float * ar1, float * ar2, float * ar3, short norm_) {
N = N_;
ar1_mean = mean_no_zero(ar1);
ar2_mean = mean_no_zero(ar2);
norm = norm_;
ar1_stdev = stdev_no_zero(ar1, ar1_mean);
ar2_stdev = stdev_no_zero(ar2, ar2_mean);
if ( norm==0)
norm_factor = ar1_mean * ar2_mean;
else if ( norm==1)
norm_factor = ar1_stdev * ar2_stdev;
else
norm_factor = 1;
correlate ( ar1,ar2,ar3);
}
void Corr::correlate(float * ar1, float * ar2, float * arC)
{
// #pragma omp parallel for shared(arC)
for ( int phi=0; phi < N; phi++ )
{
float counts(0); // keep track of the number of good pairs
for ( int i=0; i < N; i++)
{
int j = i + phi;
if (j >= N)
j -= N;
if (ar1[i] > 0 && ar2[j] > 0)
{
arC[phi] += ( ar1[i] - ar1_mean) *( ar2[j]-ar2_mean) ;
counts += 1;
}
}
arC[phi] = arC[phi] / (norm_factor * counts);
}
}
float Corr::mean_no_zero(float * ar)
{
float ar_mean(0);
float counts(0);
int i(0);
while(i < N)
{
if(ar[i] > 0)
{
ar_mean += ar[i];
counts ++;
}
i ++;
}
if(counts > 0)
return ar_mean / counts;
else
return 0;
}
float Corr::stdev_no_zero( float * ar, float ar_mean)
{
// the array ar is already mean subtracted
float ar_stdev(0);
float counts(0);
int i(0);
while(i < N)
{
if(ar[i] > 0)
{
ar_stdev += ( ar[i]- ar_mean )* ( ar[i] - ar_mean ) ;
counts ++;
}
i ++;
}
if(counts > 0)
return sqrt( ar_stdev / counts );
else
return 0;
}
Corr::~Corr() {
// destructor
}