esp-sdr-zephyr
Raw I/Q receive and transmit on the ESP32-S3/C6 Wi-Fi radio, for Zephyr
Loading...
Searching...
No Matches
esp_sdr_cal.h
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-3.0-or-later
3 */
4
14
15#ifndef ESP_SDR_ESP_SDR_CAL_H_
16#define ESP_SDR_ESP_SDR_CAL_H_
17
18#include <errno.h>
19#include <stdint.h>
20
21#ifdef __cplusplus
22extern "C" {
23#endif
24
29
31#define ESP_SDR_CAL_TEMP_UNKNOWN INT16_MIN
32
34#define ESP_SDR_CAL_POINTS 8
35
37#define ESP_SDR_CAL_POINT_MERGE_DC 20
38
40#define ESP_SDR_CAL_EXTRAPOLATE_DC 100
41
43#define ESP_SDR_CAL_QUADRATIC_SPAN_DC 100
44
48 int16_t temp_dc;
50 int16_t reserved;
52 int32_t ppb;
53};
54
58 int32_t ppb;
60 int32_t spread_ppb;
62 uint32_t carrier_khz;
64 int16_t cell_id;
66 uint16_t estimates;
68 int16_t temp_dc;
70 uint16_t reserved;
72 uint8_t points;
74 uint8_t reserved2[3];
77};
78
89static inline void esp_sdr_cal_add_point(struct esp_sdr_cal_record *rec, int16_t temp_dc,
90 int32_t ppb)
91{
92 uint8_t drop = ESP_SDR_CAL_POINTS;
93
94 for (uint8_t i = 0; i < rec->points; i++) {
95 int32_t d = (int32_t)rec->point[i].temp_dc - temp_dc;
96
97 if (d <= ESP_SDR_CAL_POINT_MERGE_DC && d >= -ESP_SDR_CAL_POINT_MERGE_DC) {
98 drop = i;
99 break;
100 }
101 }
102 if (drop == ESP_SDR_CAL_POINTS && rec->points == ESP_SDR_CAL_POINTS) {
103 drop = 0;
104 }
105 if (drop < ESP_SDR_CAL_POINTS) {
106 for (uint8_t i = drop; i + 1U < rec->points; i++) {
107 rec->point[i] = rec->point[i + 1U];
108 }
109 rec->points--;
110 }
111 rec->point[rec->points].temp_dc = temp_dc;
112 rec->point[rec->points].reserved = 0;
113 rec->point[rec->points].ppb = ppb;
114 rec->points++;
115}
116
130static inline int esp_sdr_cal_eval(const struct esp_sdr_cal_record *rec, int16_t temp_dc,
131 int32_t *ppb)
132{
133 uint8_t n = rec->points > ESP_SDR_CAL_POINTS ? ESP_SDR_CAL_POINTS : rec->points;
134 int32_t lo = INT32_MAX, hi = INT32_MIN;
135 double mean_t = 0.0, mean_y = 0.0, t, y;
136
137 if (n == 0U) {
138 return -ENOENT;
139 }
140 for (uint8_t i = 0; i < n; i++) {
141 lo = rec->point[i].temp_dc < lo ? rec->point[i].temp_dc : lo;
142 hi = rec->point[i].temp_dc > hi ? rec->point[i].temp_dc : hi;
143 mean_t += rec->point[i].temp_dc;
144 mean_y += rec->point[i].ppb;
145 }
146 mean_t /= n;
147 mean_y /= n;
148 t = temp_dc;
149 if (t < (double)lo - ESP_SDR_CAL_EXTRAPOLATE_DC) {
150 t = (double)lo - ESP_SDR_CAL_EXTRAPOLATE_DC;
151 } else if (t > (double)hi + ESP_SDR_CAL_EXTRAPOLATE_DC) {
152 t = (double)hi + ESP_SDR_CAL_EXTRAPOLATE_DC;
153 }
154 /* Centred and in units of 10 degC, for a well conditioned fit. */
155 double x = (t - mean_t) / 100.0;
156 double s2 = 0.0, s3 = 0.0, s4 = 0.0, sy = 0.0, sxy = 0.0, sx2y = 0.0;
157
158 for (uint8_t i = 0; i < n; i++) {
159 double xi = (rec->point[i].temp_dc - mean_t) / 100.0;
160 double yi = rec->point[i].ppb - mean_y;
161
162 s2 += xi * xi;
163 s3 += xi * xi * xi;
164 s4 += xi * xi * xi * xi;
165 sy += yi;
166 sxy += xi * yi;
167 sx2y += xi * xi * yi;
168 }
169 y = mean_y;
170 if (n >= 3U && hi - lo >= ESP_SDR_CAL_QUADRATIC_SPAN_DC) {
171 /* y - mean = a + b x + c x^2 with sum x = 0: the normal equations by Cramer's rule. */
172 double m[3][3] = {{n, 0.0, s2}, {0.0, s2, s3}, {s2, s3, s4}};
173 double r[3] = {sy, sxy, sx2y};
174 double det = m[0][0] * (m[1][1] * m[2][2] - m[1][2] * m[2][1]) -
175 m[0][1] * (m[1][0] * m[2][2] - m[1][2] * m[2][0]) +
176 m[0][2] * (m[1][0] * m[2][1] - m[1][1] * m[2][0]);
177
178 if (det > 1e-9 || det < -1e-9) {
179 double c[3];
180
181 for (int k = 0; k < 3; k++) {
182 double a[3][3];
183
184 for (int i = 0; i < 3; i++) {
185 for (int j = 0; j < 3; j++) {
186 a[i][j] = j == k ? r[i] : m[i][j];
187 }
188 }
189 c[k] = (a[0][0] * (a[1][1] * a[2][2] - a[1][2] * a[2][1]) -
190 a[0][1] * (a[1][0] * a[2][2] - a[1][2] * a[2][0]) +
191 a[0][2] * (a[1][0] * a[2][1] - a[1][1] * a[2][0])) /
192 det;
193 }
194 y += c[0] + c[1] * x + c[2] * x * x;
195 *ppb = (int32_t)(y + (y < 0.0 ? -0.5 : 0.5));
196 return 0;
197 }
198 }
199 if (n >= 2U && s2 > 0.0) {
200 y += sxy / s2 * x;
201 }
202 *ppb = (int32_t)(y + (y < 0.0 ? -0.5 : 0.5));
203 return 0;
204}
205
219
227
245int esp_sdr_cal_update(const struct esp_sdr_cal_record *meas, struct esp_sdr_cal_record *out);
246
247#if defined(CONFIG_ESP_SDR_CALSTORE) || defined(__DOXYGEN__)
257int esp_sdr_cal_load(struct esp_sdr_cal_record *rec);
258
264int esp_sdr_cal_save(const struct esp_sdr_cal_record *rec);
265
271int esp_sdr_cal_clear(void);
272#else
273static inline int esp_sdr_cal_load(struct esp_sdr_cal_record *rec)
274{
275 (void)rec;
276 return -ENOTSUP;
277}
278
279static inline int esp_sdr_cal_save(const struct esp_sdr_cal_record *rec)
280{
281 (void)rec;
282 return -ENOTSUP;
283}
284
285static inline int esp_sdr_cal_clear(void)
286{
287 return -ENOTSUP;
288}
289#endif
290
294 uint32_t start_mhz;
296 uint32_t stop_mhz;
298 uint8_t candidates;
300 uint8_t tries;
302 uint8_t estimates;
304 int8_t gain;
305};
306
334
336#define ESP_SDR_LTE_SCAN_DEFAULT \
337 { \
338 .start_mhz = 2110, .stop_mhz = 2170, .candidates = 6, .tries = 12, .estimates = 5, \
339 .gain = -1, \
340 }
341
364int esp_sdr_lte_calibrate(const struct esp_sdr_lte_scan *scan, struct esp_sdr_lte_result *res);
365
367
368#ifdef __cplusplus
369}
370#endif
371
372#endif /* ESP_SDR_ESP_SDR_CAL_H_ */
#define ESP_SDR_CAL_POINTS
Definition esp_sdr_cal.h:34
void esp_sdr_get_freq_corr_model(struct esp_sdr_cal_record *rec)
The calibration in force: the record esp_sdr_set_freq_corr_model() took, or for a constant correction...
#define ESP_SDR_CAL_QUADRATIC_SPAN_DC
Definition esp_sdr_cal.h:43
static void esp_sdr_cal_add_point(struct esp_sdr_cal_record *rec, int16_t temp_dc, int32_t ppb)
Add a measurement to a record's points.
Definition esp_sdr_cal.h:89
int esp_sdr_set_freq_corr_model(const struct esp_sdr_cal_record *rec)
Correct tuning with a record's temperature model.
#define ESP_SDR_CAL_POINT_MERGE_DC
Definition esp_sdr_cal.h:37
int esp_sdr_cal_update(const struct esp_sdr_cal_record *meas, struct esp_sdr_cal_record *out)
Take a measured crystal error into the correction in force, and store it.
static int esp_sdr_cal_eval(const struct esp_sdr_cal_record *rec, int16_t temp_dc, int32_t *ppb)
The crystal's error at a temperature, from a record's points.
Definition esp_sdr_cal.h:130
int esp_sdr_lte_calibrate(const struct esp_sdr_lte_scan *scan, struct esp_sdr_lte_result *res)
Measure the crystal's error against an LTE cell and apply it.
#define ESP_SDR_CAL_EXTRAPOLATE_DC
Definition esp_sdr_cal.h:40
Definition esp_sdr_cal.h:46
int32_t ppb
Definition esp_sdr_cal.h:52
int16_t temp_dc
Definition esp_sdr_cal.h:48
int16_t reserved
Definition esp_sdr_cal.h:50
Definition esp_sdr_cal.h:56
int16_t cell_id
Definition esp_sdr_cal.h:64
int32_t ppb
Definition esp_sdr_cal.h:58
uint8_t reserved2[3]
Definition esp_sdr_cal.h:74
uint32_t carrier_khz
Definition esp_sdr_cal.h:62
int32_t spread_ppb
Definition esp_sdr_cal.h:60
uint16_t estimates
Definition esp_sdr_cal.h:66
uint16_t reserved
Definition esp_sdr_cal.h:70
struct esp_sdr_cal_point point[ESP_SDR_CAL_POINTS]
Definition esp_sdr_cal.h:76
uint8_t points
Definition esp_sdr_cal.h:72
int16_t temp_dc
Definition esp_sdr_cal.h:68
Definition esp_sdr_cal.h:308
int estimates
Definition esp_sdr_cal.h:320
int32_t ppb
Definition esp_sdr_cal.h:310
int32_t spread_ppb
Definition esp_sdr_cal.h:314
int32_t delta_ppb
Definition esp_sdr_cal.h:312
uint32_t carrier_khz
Definition esp_sdr_cal.h:316
int decoded
Definition esp_sdr_cal.h:324
int unreachable
Definition esp_sdr_cal.h:326
int captures
Definition esp_sdr_cal.h:322
int temp_dc
Definition esp_sdr_cal.h:328
int cell_id
Definition esp_sdr_cal.h:318
int temp_drift_dc
Definition esp_sdr_cal.h:330
int points
Definition esp_sdr_cal.h:332
Definition esp_sdr_cal.h:292
uint8_t tries
Definition esp_sdr_cal.h:300
uint8_t estimates
Definition esp_sdr_cal.h:302
uint32_t start_mhz
Definition esp_sdr_cal.h:294
int8_t gain
Definition esp_sdr_cal.h:304
uint8_t candidates
Definition esp_sdr_cal.h:298
uint32_t stop_mhz
Definition esp_sdr_cal.h:296