-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrack.h
More file actions
174 lines (151 loc) · 4.26 KB
/
Copy pathTrack.h
File metadata and controls
174 lines (151 loc) · 4.26 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
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2026 Daniele Terdina
#pragma once
class Track
{
private:
vector<int> track;
size_t index;
float currentTime;
float bitPeriod;
float expectedTime;
float midTime;
int nBits;
bool validTime;
// Runtime debug flag: when true, StartPhaseLock / FindPreamble draw the
// flux + detected bit boundaries to a JPG. Off by default; the caller
// enables it selectively for a single chunk of interest.
bool debug = false;
const char* debugTag = "";
// Bit-alignment collection for post-read flux pictures around a specific
// byte offset. When enabled, ReadBit appends expectedTime per bit call so
// the caller can slice a window and hand it to DrawErrorNamed. Costs one
// boolean branch per ReadBit; off by default.
bool collectAlignment = false;
vector<int> alignmentBuffer;
vector<int> bitValueBuffer; // 0/1 read per cell, paired with alignmentBuffer
public:
void SetDebug(bool d, const char* tag = "") { debug = d; debugTag = tag; }
void EnableAlignmentCollection(bool b)
{
collectAlignment = b;
if (b) { alignmentBuffer.clear(); bitValueBuffer.clear(); }
}
const vector<int>& GetAlignmentBuffer() const { return alignmentBuffer; }
const vector<int>& GetBitValueBuffer() const { return bitValueBuffer; }
const vector<int>& GetFlux() const { return track; }
Track(const vector<int>& t, float startPeriod)
: track(t)
{
Restart(startPeriod);
}
void ReplaceFlux(const vector<int>& newFlux)
{
track = newFlux;
}
void Restart(float startPeriod)
{
index = 0;
currentTime = 0;
bitPeriod = startPeriod;
expectedTime = 0;
nBits = 0;
}
bool EndOfTrack()
{
return index >= track.size();
}
float GetPeriod()
{
return bitPeriod;
}
void SetPeriod(float p)
{
bitPeriod = p;
expectedTime = currentTime;
nBits = 0;
}
float GetTime()
{
return currentTime;
}
float GetExpectedTime()
{
return expectedTime;
}
float GetMidTime()
{
return midTime;
}
void ResetExpectedTime()
{
expectedTime = currentTime;
}
void AdjustExpectedTime(float adjust)
{
expectedTime += adjust;
}
float NextTime()
{
EndOfTrack() ? FLT_MAX : track[index] + currentTime;
}
void Advance()
{
currentTime += track[index++];
}
int AdvanceTo(float t)
{
int numTransitions = 0;
while (index < track.size() && abs(track[index] + currentTime - t) < abs(currentTime - t))
{
Advance();
numTransitions++;
}
return numTransitions;
}
int ReadBit()
{
if (collectAlignment)
alignmentBuffer.push_back((int)(expectedTime + 0.5f));
int n;
if (++nBits <= 5)
{
// First bits may be unreliable
n = AdvanceTo(currentTime + bitPeriod);
expectedTime = currentTime;
validTime = false;
}
else
{
float startTime = currentTime;
expectedTime += bitPeriod;
n = AdvanceTo(expectedTime);
float elapsed = currentTime - startTime;
validTime = elapsed > bitPeriod * 0.7 && elapsed < bitPeriod * 1.3;
if (validTime)
{
bitPeriod = 0.95 * bitPeriod + 0.05 * elapsed; // Update period
expectedTime = 0.8 * expectedTime + 0.2 * currentTime; // Update phase
}
}
int bit = (n >= 2) ? 1 : 0;
if (collectAlignment)
bitValueBuffer.push_back(bit);
return bit;
}
bool ReadByte(BYTE& b)
{
for (int i = 0; i < 8; i++)
{
if (EndOfTrack())
return false;
if (i == 4)
midTime = validTime ? expectedTime : 0;
b >>= 1;
b |= ReadBit() << 7;
}
return true;
}
bool StartPhaseLock(float maxDeviation, int numBytes);
bool FindPreamble(int numZeros, int numOnes, int maxSearchLen, vector<BYTE>& preamble, int* pStartTime);
};