-
Notifications
You must be signed in to change notification settings - Fork 12
/
Correlate.cs
255 lines (205 loc) · 8.71 KB
/
Correlate.cs
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
using System.Threading.Tasks;
namespace PasiveRadar
{
class Correlate
{
public uint BufferSize;
uint negative;
uint positive;
uint NegPos;
const int MaxAcceptanceFeilCount = 5;
uint AcceptanceFeilCount = 0;
const int MaxCumulateCorrelateLevel = 20;
double[] ArrayCumulateCorrelateLevel;
int CumulateLevelNr = 0;
public delegate void MyDelegate();
static public event MyDelegate Resynchronise;
uint size;
FFT fft;
Complex[] f;
Complex[] Ff;
Complex[] g;
Complex[] Fg;
Complex[] H;
Complex[] rH;
public Correlate()
{
ArrayCumulateCorrelateLevel = new double[MaxCumulateCorrelateLevel];
fft = new FFT();
}
public void Init(uint size_)
{
size = size_;
fft.Init(size);
f = new Complex[size];
Ff = new Complex[size];
g = new Complex[size];
Fg = new Complex[size];
H = new Complex[size];
rH = new Complex[size];
}
public bool Begin(int[] Data1, int[] Data2, float[] CorrelateArray, ref uint CorrelationShift, Flags flags)
{
CorrelationShift = 0;
float Max = 0;
BufferSize = flags.BufferSize;
negative = flags.Negative;
positive = flags.Positive;
NegPos = negative + positive;
if (NegPos < 1) NegPos = 1;
///////////////////////////////////////////////////////////////////////////////////////////////////////
//If there is no value for corelation do the full corelation
if (Data1.Length < BufferSize + NegPos || Data2.Length < BufferSize + NegPos)
return false;
ThreadCorelate(Data1, Data2, CorrelateArray);
//FourierCorelate(Data1, Data2, CorrelateArray);//much faster
//Find the maximum
for (uint i = 0; i < NegPos; i++)
{
if (CorrelateArray[i] > Max)
{
Max = CorrelateArray[CorrelationShift = i];
}
}
//Normalize corelate to max
float one_max = 1.0f / Max;
float average = 0;
for (int i = 0; i < NegPos; i++)
{
average += CorrelateArray[i] = CorrelateArray[i] * one_max;
}
average /= NegPos;
double AverageLevel = 0;
//Auto correlation level
if (flags.AutoCorrelate)
{
CumulateLevelNr++;
if (CumulateLevelNr >= MaxCumulateCorrelateLevel) CumulateLevelNr = 0;
ArrayCumulateCorrelateLevel[CumulateLevelNr] = average;
for (int i = 0; i < MaxCumulateCorrelateLevel; i++)
AverageLevel += ArrayCumulateCorrelateLevel[i];
AverageLevel /= MaxCumulateCorrelateLevel;
if (AverageLevel < 0.2) AverageLevel = 0.2;
if (AverageLevel > 0.96) AverageLevel = 0.96;
flags.AcceptedLevel = AverageLevel * 1.2;
if (flags.AcceptedLevel > 1) flags.AcceptedLevel = 1;
}
//If the average is higher than acceptance level than correlate is not valid
if (average > flags.AcceptedLevel || AverageLevel > 0.95)
{
AcceptanceFeilCount++;
if (AcceptanceFeilCount > MaxAcceptanceFeilCount)
{
//resynchronise dongles
Resynchronise.Invoke();
AcceptanceFeilCount = 0;
}
return false;
}
//No feils in synchronisation so reset the dlag and go on
AcceptanceFeilCount = 0;
////////////////////////////////////////////////////////////////////////////////////////////////////////
//Shift the second data string to the begining
uint neg_2 = negative * 2; //because the complex number is interleved
uint corcor = CorrelationShift * 2;
for (int i = 0; i < BufferSize; i++)
{
Data1[i] = Data1[i + neg_2];
Data2[i] = Data2[i + corcor];
}
return true;
//So now Data2 is shifted to the correct position of correlation
}
//Data strings from two dongles are shifted in time due to number of reasons. This has to be corrected prior to the ambiguity function.
void ThreadCorelate(int[] Data1, int[] Data2, float[] CorelateArray)
{
// negative positive scale of autocorrelation j index
// |--------------------------|--------------------------|-------------------------------------------|
// |==========================================|
// Data1
// |=========================================|
// Data2
// { j index }
//Divade the task on threads (so expensive method)
uint neg2 = negative;
//Scan for corelation beginning
Parallel.For(0, NegPos / 2, new ParallelOptions { MaxDegreeOfParallelism = 8 }, i =>
{
long sq;
long temp = 0;
long sq2;
long temp2 = 0;
long aa;
long b;
//We start Data1 from i+BufferSize to investigate the corelation also in backward direction )if i and j-0) there would be only forward direction)
//Do corelation of two strings the second string has scaned position. The area to corelate is defined by CorrSize (smaller faster and so on)
for (uint t = 0; t < 1024 * 4; t += 1) //propably part of te full matrix is enough so divide by 2
{
sq = (aa = Data1[t + negative]) * Data2[b = (t + i)];// + Data1[jn+1] * Data2[ij+1];
temp += sq * sq;
sq2 = aa * Data2[b + NegPos / 2];// + Data1[jn+1] * Data2[ij+1];
temp2 += sq2 * sq2;
}
CorelateArray[i] = temp;
CorelateArray[i + NegPos / 2] = temp2;
});
}
//Init(uint size) must be initialized first!
void FourierCorelate(int[] Data1, int[] Data2, float[] CorelateArray)
{
//Copy data
uint a;
for (uint i = 0; i < size / 2 - 1; i++)
{
f[i].Re = Data1[a = i * 2];
f[i].Im = Data1[a + 1];
g[i].Re = Data2[a];
g[i].Im = Data2[a + 1];
}
//First fourier transform both data1 and data2
//f = Data1(t), g = Data2(t)
//F(f) and F(g)
//Multiply H = F(f) * F(g) in frequency domain
//Reverse Fourier transform of the product F^-1 (H)
fft.CalcFFT(f, ref Ff, true);
fft.CalcFFT(g, ref Fg, true);
//Perform two tasks in parallel on the source array
// Parallel.Invoke(
// () =>
// {
// uint a;
// for (uint i = 0; i < size / 2 - 1; i++)
// {
// f[i].Re = Data1[a = i * 2];
// f[i].Im = Data1[a + 1];
// }
// fft.CalcFFT(f, ref Ff, true);
// },
// () =>
// {
// uint a;
// for (uint i = 0; i < size / 2 - 1; i++)
// {
// g[i].Re = Data2[a = i * 2];
// g[i].Im = Data2[a + 1];
// }
// fft.CalcFFT(g, ref Fg, true);
// }
//); //close parallel.invoke
//Multiply both transforms and place it in product H
// (a + ib)(c + id) = (ac - bd) + i(ad + bc).
for (int i = 0; i < size; i++)
{
H[i].Re = Ff[i].Re * Fg[i].Re - Ff[i].Im * Fg[i].Im;
H[i].Im = Ff[i].Re * Fg[i].Im + Ff[i].Im * Fg[i].Re;
}
//Reverse fft for H
fft.CalcFFT(H, ref rH, false);
//Copy data module
for (int i = 0; i < size; i++)
{
CorelateArray[i] = rH[i].Re * rH[i].Re;
}
}
}
}