-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFluxHistogram.cpp
More file actions
286 lines (252 loc) · 7.53 KB
/
Copy pathFluxHistogram.cpp
File metadata and controls
286 lines (252 loc) · 7.53 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2026 Daniele Terdina
#include "MdvDecode.h"
void RunningAverage(vector<int>& data)
{
int saved = data[0];
for (int i = 0; i < data.size() - 1; i++)
{
int newVal = saved + data[i] + data[i + 1];
saved = data[i];
data[i] = newVal;
}
}
bool ComputeCentroid(const vector<int>& hist, int from, int to, int& centroid)
{
__int64 sum1 = 0;
__int64 sum2 = 0;
for (int x = from; x <= to; x++)
{
sum1 += x * hist[x];
sum2 += hist[x];
}
if (sum2 == 0)
return false;
centroid = (sum1 + sum2 / 2 + 1) / sum2;
return true;
}
HistResult ProcessHist(const vector<int>& hist, int theoreticalPeriod)
{
vector<int> origHist = hist;
int x1 = std::max_element(hist.begin() + 3 * theoreticalPeriod / 16, hist.begin() + theoreticalPeriod * 11 / 16) - hist.begin();
int x2 = std::max_element(hist.begin() + 13 * theoreticalPeriod / 16, hist.begin() + theoreticalPeriod * 23 / 16) - hist.begin();
// Now find minimum in-between
int firstMin = x1;
int mid = x1;
int minValue = hist[x1];
int maxLen = 1;
for (int x = x1 + 1; x < x2; x++)
{
int v = hist[x];
if (v < minValue)
{
minValue = v;
firstMin = x;
mid = x;
maxLen = 1;
}
else if (v == minValue)
{
int len = 1 + x - firstMin;
if (len > maxLen)
{
maxLen = len;
mid = (x + firstMin + 1) / 2;
}
}
else
{
firstMin = x + 1;
}
}
// Experiment: approximate centroid:
int delta = 3 * theoreticalPeriod / 16;
int centroid;
if (!ComputeCentroid(origHist, x2 - delta, x2 + delta, centroid))
return { (float)theoreticalPeriod, INT_MAX, 0.5f * theoreticalPeriod, mid };
int lowCentroid;
if (!ComputeCentroid(origHist, max(1, x1 - delta), x1 + delta, lowCentroid))
lowCentroid = 0;
//printf("%d, %d, %d, %d, %d\n", x1, x2, centroid, mid, hist[mid]);
return { (float)centroid, hist[mid], (float)lowCentroid, mid };
}
HistResult MergeHistResult(HistResult hr1, HistResult hr2)
{
float result;
if (hr1.quality < hr2.quality)
result = hr1.period;
else if (hr2.quality < hr1.quality)
result = hr2.period;
else
result = (hr1.period + hr2.period) * 0.5f;
int quality = min(hr1.quality, hr2.quality);
return { result, quality };
}
HistResult DoHistogramAndImproveFlux(const vector<int>& flux, vector<int>& result)
{
static int count = 0;
const int NUM_BIN = 500;
vector<int> histEven(NUM_BIN);
vector<int> histOdd(NUM_BIN);
result.clear();
// Note: skip first sample
for (size_t i = 1; i < flux.size(); i++)
{
int f = flux[i];
f = min(f, NUM_BIN - 1);
if (i & 1)
histOdd[f]++;
else
histEven[f]++;
}
for (int i = 0; i < 10; i++)
{
RunningAverage(histOdd);
RunningAverage(histEven);
}
//printf("Block %d odd: ", count / 2);
HistResult hrOdd = ProcessHist(histOdd, 240);
//printf("Block %d even: ", count / 2);
HistResult hrEven = ProcessHist(histEven, 240);
count++;
if (hrOdd.quality != INT_MAX && hrOdd.shortPeriod != 0 &&
hrEven.quality != INT_MAX && hrEven.shortPeriod)
{
result = RegularizeFlux(flux, hrEven, hrOdd);
}
return MergeHistResult(hrOdd, hrEven);
}
enum FluxLen
{
FLUX_TOO_SHORT,
FLUX_SHORT,
FLUX_MID,
FLUX_LONG,
FLUX_TOO_LONG
};
class FluxClassifier
{
int tooLowThreshold;
int lowThreshold;
int highThreshold;
int tooHighThreshold;
public:
FluxClassifier(const HistResult& hr)
{
float mid = (hr.period + hr.shortPeriod) * 0.5;
mid = (mid + hr.mid) * 0.5;
lowThreshold = (int)((mid * 2 + hr.shortPeriod) / 3 + 0.5);
highThreshold = (int)((mid * 2 + hr.period) / 3 + 0.5);
tooLowThreshold = hr.shortPeriod * 3 / 4;
tooHighThreshold = hr.period * 5 / 4;
}
FluxLen Classify(int flux)
{
if (flux <= lowThreshold)
return flux < tooLowThreshold ? FLUX_TOO_SHORT : FLUX_SHORT;
if (flux >= highThreshold)
return flux > tooHighThreshold ? FLUX_TOO_LONG : FLUX_LONG;
return FLUX_MID;
}
};
void RegularizeContiguous(vector<int>& flux, vector<FluxLen>& fluxLen, size_t start, size_t end, bool isLowQuality)
{
int totMid = 0;
for (size_t i = start; i <= end; i++)
{
int numMid = 0;
int numShort = 0;
int lastMid;
while (i <= end && fluxLen[i] < FLUX_LONG)
{
if (fluxLen[i] <= FLUX_SHORT)
numShort++;
else
{
lastMid = i;
numMid++;
}
i++;
}
if (numMid == 1 && i != end)
{
fluxLen[lastMid] = (numShort & 1) ? FLUX_SHORT : FLUX_LONG; // short pulses come in pairs
}
else
totMid += numMid;
}
if (totMid || isLowQuality)
{
// TODO: partially equalize durations
int xx = 1;
}
else
{
// Completely rewrite flux using average durations
__int64 duration = 0;
int count[5] = { 0 };
for (size_t i = start; i <= end; i++)
{
count[fluxLen[i]]++;
duration += flux[i];
}
int totCount = count[FLUX_SHORT] + count[FLUX_TOO_SHORT] + 2 * (count[FLUX_LONG] + count[FLUX_TOO_LONG]);
int halfTotCount = totCount / 2;
__int64 lastTime = 0;
int currentCount = 0;
for (size_t i = start; i <= end; i++)
{
currentCount += (fluxLen[i] >= FLUX_LONG) ? 2 : 1;
__int64 time = (duration * currentCount + halfTotCount) / totCount;
flux[i] = time - lastTime;
lastTime = time;
}
}
}
vector<int> RegularizeFlux(const vector<int>& flux, const HistResult& hrEven, const HistResult& hrOdd)
{
FluxClassifier fluxClassEven(hrEven);
FluxClassifier fluxClassOdd(hrOdd);
vector<FluxLen> fluxLen(flux.size());
for (size_t i = 1; i < flux.size(); i++)
{
int f = flux[i];
if (i & 1)
fluxLen[i] = fluxClassOdd.Classify(f);
else
fluxLen[i] = fluxClassEven.Classify(f);
}
size_t start = 0;
while (start < flux.size() && fluxLen[start] != FLUX_SHORT && fluxLen[start] != FLUX_LONG)
start++;
size_t end = flux.size() - 1;
while (end > start && fluxLen[end] != FLUX_SHORT && fluxLen[end] != FLUX_LONG)
end--;
bool isLowQuality = hrEven.quality > 0 || hrOdd.quality > 0;
vector<int> result(flux);
size_t startSegment = start;
bool hasStartSegment = true;
size_t lastGood = start;
for (size_t i = start; i <= end; i++)
{
FluxLen fl = fluxLen[i];
if (fl == FLUX_SHORT || fl == FLUX_LONG)
{
if (!hasStartSegment)
{
startSegment = i;
hasStartSegment = true;
}
lastGood = i;
}
else if (fl != FLUX_MID)
{
if (hasStartSegment && startSegment + 10 <= lastGood)
RegularizeContiguous(result, fluxLen, startSegment, lastGood, isLowQuality);
hasStartSegment = false;
}
}
if (hasStartSegment && startSegment + 10 <= lastGood)
RegularizeContiguous(result, fluxLen, startSegment, lastGood, isLowQuality);
return result;
}