-
Notifications
You must be signed in to change notification settings - Fork 87
/
vna_measurement.cpp
232 lines (212 loc) · 6.55 KB
/
vna_measurement.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
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
#include "vna_measurement.hpp"
#include <board.hpp>
VNAMeasurement::VNAMeasurement(): sampleProcessor(_emitValue_t {this}) {
}
void VNAMeasurement::init() {
sampleProcessor.init();
}
void VNAMeasurement::setCorrelationTable(const int16_t* table, int length) {
sampleProcessor.setCorrelationTable(table, length);
sampleProcessor.emitValue = _emitValue_t {this};
}
void VNAMeasurement::processSamples(uint16_t* buf, int len) {
sampleProcessor.process(buf, len);
}
void VNAMeasurement::setSweep(freqHz_t startFreqHz, freqHz_t stepFreqHz, int points, int dataPointsPerFreq) {
sweepStartHz = startFreqHz;
sweepStepHz = stepFreqHz;
sweepPoints = points;
sweepDataPointsPerFreq = dataPointsPerFreq;
resetSweep();
}
void VNAMeasurement::resetSweep() {
__sync_synchronize();
sweepCurrPoint = -1;
}
void VNAMeasurement::setMeasurementPhase(VNAMeasurementPhases ph) {
phaseChanged(ph);
measurementPhase = ph;
periodCounterSwitch = 0;
currDP_re = 0;
currDP_im = 0;
gainChangeOccurred = false;
#ifdef BOARD_DISABLE_ECAL
// Disabled ecal, use only nPeriods
nMeasureCount = nPeriods * nPeriodsMultiplier;
#elif 1
// For ecal use nPeriodsCalibrating always, for other use nPeriods
if (ph > VNAMeasurementPhases::THRU) nMeasureCount = nPeriodsCalibrating * nPeriodsMultiplier;
else nMeasureCount = nPeriods * nPeriodsMultiplier;
#else
// On calibration or first step (ecalIntervalPoints == 1) use nPeriodsCalibrating, for other use nPeriods
nMeasureCount = ((ecalIntervalPoints == 1) ? nPeriodsCalibrating : nPeriods) * nPeriodsMultiplier;
#endif
}
static inline complexf to_complexf(VNAMeasurement::complexi value) {
return {(float) value.real(), (float) value.imag()};
}
void VNAMeasurement::sweepAdvance() {
sweepCurrPoint++;
if(sweepCurrPoint >= sweepPoints)
sweepCurrPoint = 0;
currFreq = sweepStartHz + sweepStepHz*sweepCurrPoint;
frequencyChanged(currFreq);
periodCounterSynth = nWaitSynth;
periodCounterSwitch = 0;
if(sweepCurrPoint == 0) {
periodCounterSynth = BOARD_MEASUREMENT_FIRST_POINT_WAIT; // for first point need more wait
currThruGain = gainMax;
ecalCounter = ecalCounterOffset;
ecalCounterOffset++;
if(ecalCounterOffset >= ecalIntervalPoints)
ecalCounterOffset = 0;
}
}
void VNAMeasurement::sampleProcessor_emitValue(int32_t valRe, int32_t valIm, bool clipped) {
auto currPoint = sweepCurrPoint;
/* If -1 then we restart */
if(currPoint == -1) {
freqHz_t start = sweepStartHz;
freqHz_t stop = start + sweepStepHz*sweepPoints;
sweepSetupChanged(start, stop);
dpCounterSynth = 0;
setMeasurementPhase(VNAMeasurementPhases::REFERENCE);
ecalCounterOffset = 0;
sweepAdvance();
return;
}
/* If periodCounterSynth not elapsed, decrement and wait for it */
if(periodCounterSynth > 0) {
// still waiting for synthesizer
periodCounterSynth--;
gainChangeOccurred = false;
return;
}
if(periodCounterSwitch >= nWaitSwitch) {
currDP_re+= valRe;
currDP_im+= valIm;
if(measurementPhase == VNAMeasurementPhases::THRU) {
if(clipped) {
// ADC clip occurred during a measurement period
if(currThruGain > gainMin) {
// decrease gain and redo measurement
currThruGain--;
gainChanged(currThruGain);
periodCounterSwitch = 0;
currDP_re = 0;
currDP_im = 0;
sampleProcessor.clipFlag = false;
gainChangeOccurred = true;
return;
}
}
}
else // not show clippederror on thru measure
clipFlag |= clipped;
} else {
sampleProcessor.clipFlag = false;
}
periodCounterSwitch++;
/* If switch time not elapsed, wait some more */
if(periodCounterSwitch < (nWaitSwitch + nMeasureCount)) {
return;
}
// Real measure count
periodCounterSwitch-=nWaitSwitch;
// Get current point measured data (not depend from measure count)
complexf currDP = complexf{(float)currDP_re/periodCounterSwitch, (float)currDP_im/periodCounterSwitch};
// Loop through measurement phase
switch(measurementPhase) {
case VNAMeasurementPhases::REFERENCE:
currFwd = currDP;
setMeasurementPhase(VNAMeasurementPhases::REFL);
break;
case VNAMeasurementPhases::REFL:
currRefl = currDP;
setMeasurementPhase(VNAMeasurementPhases::THRU);
break;
case VNAMeasurementPhases::THRU:
if(currThruGain < gainMax && !gainChangeOccurred) {
float mag = abs(currDP);
if(mag < (adcFullScale * 0.15f)) {
// signal level too low; increase gain and retry
currThruGain++;
gainChanged(currThruGain);
gainChangeOccurred = true;
periodCounterSwitch = 0;
currDP_re = 0;
currDP_im = 0;
return;
}
}
currThru = currDP;
switch(measurement_mode) {
case MEASURE_MODE_FULL:
#ifdef BOARD_DISABLE_ECAL
setMeasurementPhase(VNAMeasurementPhases::REFERENCE);
doEmitValue(false);
#else
if(ecalCounter == 0) {
#ifdef ECAL_PARTIAL
setMeasurementPhase(VNAMeasurementPhases::ECALLOAD);
#else
setMeasurementPhase(VNAMeasurementPhases::ECALTHRU);
#endif
} else {
setMeasurementPhase(VNAMeasurementPhases::REFERENCE);
doEmitValue(false);
}
ecalCounter++;
if(ecalCounter >= ecalIntervalPoints)
ecalCounter = 0;
#endif
break;
case MEASURE_MODE_REFL_THRU_REFRENCE: /* AKA no ECAL */
/* Go back to the start: REFERENCE */
setMeasurementPhase(VNAMeasurementPhases::REFERENCE);
doEmitValue(false);
break;
case MEASURE_MODE_REFL_THRU:
/* aka CW mode
* And keep the signal on the ouput */
setMeasurementPhase(VNAMeasurementPhases::REFL);
doEmitValue(false);
break;
}
break;
case VNAMeasurementPhases::ECALTHRU:
ecal[2] = currDP;
setMeasurementPhase(VNAMeasurementPhases::ECALLOAD);
break;
case VNAMeasurementPhases::ECALLOAD:
ecal[0] = currDP;
#ifdef ECAL_PARTIAL
/* Go back to the start: REFERENCE */
setMeasurementPhase(VNAMeasurementPhases::REFERENCE);
doEmitValue(true);
#else
setMeasurementPhase(VNAMeasurementPhases::ECALSHORT);
#endif
break;
case VNAMeasurementPhases::ECALSHORT:
ecal[1] = currDP;
/* Go back to the start: REFERENCE */
setMeasurementPhase(VNAMeasurementPhases::REFERENCE);
doEmitValue(true);
break;
}
}
void VNAMeasurement::doEmitValue(bool ecal) {
// emit new data point
VNAObservationSet value = {currRefl, currFwd, currThru};
emitDataPoint(sweepCurrPoint, currFreq, value, ecal ? this->ecal : nullptr);
clipFlag = false;
dpCounterSynth++;
if(dpCounterSynth >= sweepDataPointsPerFreq && sweepPoints > 1) {
dpCounterSynth = 0;
sweepAdvance();
}
}
void VNAMeasurement::_emitValue_t::operator()(int32_t* valRe, int32_t* valIm) {
m->sampleProcessor_emitValue(*valRe, *valIm, m->sampleProcessor.clipFlag);
}