-
Notifications
You must be signed in to change notification settings - Fork 1
/
gain_cal.cpp
79 lines (66 loc) · 2.02 KB
/
gain_cal.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
#include "vna_measurement.hpp"
#include "ui.hpp"
#include "fifo.hpp"
#include "main.hpp"
#include "ili9341.hpp"
#include "globals.hpp"
#include <board.hpp>
#include <mculib/printf.hpp>
#include <mculib/message_log.hpp>
#include <mculib/printk.hpp>
#include <libopencm3/cm3/scb.h>
using namespace mculib;
using namespace board;
template<int fifoSize>
static void discardPoints(FIFO<complexf, fifoSize>& dpFIFO, int n) {
dpFIFO.clear();
// skip n data points
for(int i=0; i<n; i++) {
while(!dpFIFO.readable());
dpFIFO.dequeue();
}
}
// measure the attenuation at each gain setting
void performGainCal(VNAMeasurement& vnaMeasurement, float* gainTable, int maxGain) {
auto old_emitDataPoint = vnaMeasurement.emitDataPoint;
auto old_phaseChanged = vnaMeasurement.phaseChanged;
FIFO<complexf, 32> dpFIFO;
volatile int currGain = 0;
// override phaseChanged, set bbgain to desired value
vnaMeasurement.phaseChanged = [&](VNAMeasurementPhases ph) {
rfsw(RFSW_REFL, RFSW_REFL_ON);
rfsw(RFSW_RECV, RFSW_RECV_REFL);
rfsw(RFSW_ECAL, RFSW_ECAL_OPEN);
rfsw(RFSW_BBGAIN, RFSW_BBGAIN_GAIN(currGain));
};
// disable ecal during gain cal
vnaMeasurement.ecalIntervalPoints = 10000;
vnaMeasurement.nPeriods = MEASUREMENT_NPERIODS_NORMAL;
vnaMeasurement.setSweep(DEFAULT_FREQ, 0, 1, 1);
vnaMeasurement.emitDataPoint = [&](int freqIndex, freqHz_t freqHz, const VNAObservation& v, const complexf* ecal) {
dpFIFO.enqueue(v[1]);
};
for(currGain=0; currGain <= maxGain; currGain++) {
discardPoints(dpFIFO, 3);
int nValues = 10;
if(currGain == 3)
nValues = 40;
float mag = 0;
for(int i=0; i<nValues; i++) {
while(!dpFIFO.readable());
auto& dp = dpFIFO.read();
mag += abs(dp);
dpFIFO.dequeue();
}
mag /= nValues;
gainTable[currGain] = 1.f/mag;
}
// normalize first entry to 1.0
for(currGain=1; currGain <= maxGain; currGain++) {
gainTable[currGain] /= gainTable[0];
}
gainTable[0] = 1.f;
// reset callbacks
vnaMeasurement.emitDataPoint = old_emitDataPoint;
vnaMeasurement.phaseChanged = old_phaseChanged;
}