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_lte.h
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-3.0-or-later
3 */
4
19
20#ifndef ESP_SDR_ESP_SDR_LTE_H_
21#define ESP_SDR_ESP_SDR_LTE_H_
22
23#include <errno.h>
24#include <math.h>
25#include <stddef.h>
26#include <stdint.h>
27#include <stdlib.h>
28
29#include <esp_sdr/esp_sdr_dsp.h>
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
39
41#define ESP_SDR_LTE_DF 15000.0
43#define ESP_SDR_LTE_SYNC_PERIOD_S 5e-3
45#define ESP_SDR_LTE_SSS_LEAD_S ((2048.0 + 144.0) / 30.72e6)
47#define ESP_SDR_LTE_SC 62
49#define ESP_SDR_LTE_L_MAX 272
51#define ESP_SDR_LTE_RASTER_HZ 100e3
52
55 int16_t i;
56 int16_t q;
57};
58
65 uint32_t n_fft;
67 float *xr, *xi;
69 float *hr, *hi;
71 float *wr, *wi;
73 float *en;
75 float *twc, *tws;
76};
77
79#define ESP_SDR_LTE_WORK_FLOATS(n_fft) (8U * (n_fft))
80
117
121 int n_id2;
123 double offset_hz;
125 size_t pos;
127 float ratio;
129 float metric;
130};
131
132static inline size_t esp_sdr_lte_symbol_len(double fs)
133{
134 return (size_t)(fs / ESP_SDR_LTE_DF + 0.5);
135}
136
144static inline void esp_sdr_lte_work_init(struct esp_sdr_lte_work *w, float *mem, uint32_t n_fft)
145{
146 w->n_fft = n_fft;
147 w->xr = mem;
148 w->xi = mem + n_fft;
149 w->hr = mem + 2U * n_fft;
150 w->hi = mem + 3U * n_fft;
151 w->wr = mem + 4U * n_fft;
152 w->wi = mem + 5U * n_fft;
153 w->en = mem + 6U * n_fft;
154 w->twc = mem + 7U * n_fft;
155 w->tws = w->twc + n_fft / 2U;
156 esp_sdr_fft_twiddles(w->twc, w->tws, n_fft);
157}
158
160static inline int esp_sdr_lte_k(int idx)
161{
162 return idx < 31 ? idx - 31 : idx - 30;
163}
164
172static inline void esp_sdr_lte_pss(int n_id2, float *re, float *im)
173{
174 static const int root[3] = {25, 29, 34};
175 int u = root[n_id2];
176
177 for (int n = 0; n < ESP_SDR_LTE_SC; n++) {
178 int m = n < 31 ? n * (n + 1) : (n + 1) * (n + 2);
179 float a = -(float)(ESP_SDR_TAU / 2.0 / 63.0) * (float)((u * m) % 126);
180
181 re[n] = cosf(a);
182 im[n] = sinf(a);
183 }
184}
185
186/* |re + j im|^2 in double: the running window sums need it. */
187static inline double lte_pow(float re, float im)
188{
189 double a = (double)re, b = (double)im;
190
191 return a * a + b * b;
192}
193
194static inline void lte_lfsr(const int *taps, int n_taps, int *out)
195{
196 int x[31] = {0, 0, 0, 0, 1};
197
198 for (int i = 0; i + 5 < 31; i++) {
199 int v = 0;
200
201 for (int t = 0; t < n_taps; t++) {
202 v ^= x[i + taps[t]];
203 }
204 x[i + 5] = v;
205 }
206 for (int i = 0; i < 31; i++) {
207 out[i] = 1 - 2 * x[i];
208 }
209}
210
219static inline void esp_sdr_lte_sss(int n_id1, int n_id2, int half, float *d)
220{
221 static const int s_taps[] = {2, 0};
222 static const int c_taps[] = {3, 0};
223 static const int z_taps[] = {4, 2, 1, 0};
224 int s[31], c[31], z[31];
225 int qp = n_id1 / 30;
226 int q = (n_id1 + qp * (qp + 1) / 2) / 30;
227 int m = n_id1 + q * (q + 1) / 2;
228 int m0 = m % 31;
229 int m1 = (m0 + m / 31 + 1) % 31;
230
231 lte_lfsr(s_taps, 2, s);
232 lte_lfsr(c_taps, 2, c);
233 lte_lfsr(z_taps, 4, z);
234 for (int n = 0; n < 31; n++) {
235 float s0 = (float)s[(n + m0) % 31];
236 float s1 = (float)s[(n + m1) % 31];
237 float c0 = (float)c[(n + n_id2) % 31];
238 float c1 = (float)c[(n + n_id2 + 3) % 31];
239 float z0 = (float)z[(n + m0 % 8) % 31];
240 float z1 = (float)z[(n + m1 % 8) % 31];
241
242 if (half == 0) {
243 d[2 * n] = s0 * c0;
244 d[2 * n + 1] = s1 * c1 * z0;
245 } else {
246 d[2 * n] = s1 * c0;
247 d[2 * n + 1] = s0 * c1 * z1;
248 }
249 }
250}
251
252/*
253 * Demodulate subcarriers kk[] (units of 15 kHz from the carrier) of one symbol, the capture's DC
254 * (dc_i, dc_q) taken off first. See esp_sdr_lte_demod().
255 */
256static inline int lte_demod_sc(const struct esp_sdr_lte_iq *x, size_t n, double fs, double start,
257 double f_hz, float dc_i, float dc_q, const int *kk, int count,
258 float *yr, float *yi)
259{
260 size_t len = esp_sdr_lte_symbol_len(fs);
261 long first = (long)ceil(start - 1e-9);
263
264 if (first < 0 || (size_t)first + len > n || len > ESP_SDR_LTE_L_MAX) {
265 return -ERANGE;
266 }
267 double w = -ESP_SDR_TAU * f_hz / fs;
268 float cs = cosf((float)w), sn = sinf((float)w);
269 double p0 = fmod(w * (double)first, ESP_SDR_TAU);
270 float br = cosf((float)p0), bi = sinf((float)p0);
271
272 for (size_t i = 0; i < len; i++) {
273 float xr = (float)x[first + (long)i].i - dc_i;
274 float xi = (float)x[first + (long)i].q - dc_q;
275
276 zr[i] = xr * br - xi * bi;
277 zi[i] = xr * bi + xi * br;
278 float t = br * cs - bi * sn;
279
280 bi = br * sn + bi * cs;
281 br = t;
282 }
283 for (int idx = 0; idx < count; idx++) {
284 double ws = -ESP_SDR_TAU * (double)kk[idx] * ESP_SDR_LTE_DF / fs;
285 double ph = fmod(ws * ((double)first - start), ESP_SDR_TAU);
286 float rc = cosf((float)ws), rs = sinf((float)ws);
287 float pr = cosf((float)ph), pi_ = sinf((float)ph);
288 float ar = 0.0f, ai = 0.0f;
289
290 for (size_t i = 0; i < len; i++) {
291 ar += zr[i] * pr - zi[i] * pi_;
292 ai += zr[i] * pi_ + zi[i] * pr;
293 float t = pr * rc - pi_ * rs;
294
295 pi_ = pr * rs + pi_ * rc;
296 pr = t;
297 }
298 yr[idx] = ar / (float)len;
299 yi[idx] = ai / (float)len;
300 }
301 return 0;
302}
303
320static inline int esp_sdr_lte_demod(const struct esp_sdr_lte_iq *x, size_t n, double fs,
321 double start, double f_hz, float *yr, float *yi)
322{
323 int kk[ESP_SDR_LTE_SC];
324
325 for (int idx = 0; idx < ESP_SDR_LTE_SC; idx++) {
326 kk[idx] = esp_sdr_lte_k(idx);
327 }
328 return lte_demod_sc(x, n, fs, start, f_hz, 0.0f, 0.0f, kk, ESP_SDR_LTE_SC, yr, yi);
329}
330
332static inline float lte_pss_metric(const float *yr, const float *yi, const float *dr,
333 const float *di)
334{
335 float cr = 0.0f, ci = 0.0f, a = 0.0f;
336
337 for (int k = 0; k < ESP_SDR_LTE_SC; k++) {
338 cr += dr[k] * yr[k] + di[k] * yi[k];
339 ci += dr[k] * yi[k] - di[k] * yr[k];
340 a += sqrtf(yr[k] * yr[k] + yi[k] * yi[k]);
341 }
342 return a > 0.0f ? sqrtf(cr * cr + ci * ci) / a : 0.0f;
343}
344
345static inline void lte_ifft(struct esp_sdr_lte_work *w)
346{
347 uint32_t n = w->n_fft;
348
349 for (uint32_t i = 0; i < n; i++) {
350 w->wi[i] = -w->wi[i];
351 }
352 esp_sdr_fft(w->wr, w->wi, n, w->twc, w->tws, n);
353 for (uint32_t i = 0; i < n; i++) {
354 w->wr[i] /= (float)n;
355 w->wi[i] /= -(float)n;
356 }
357}
358
359/* Coarse PSS search candidates kept for the final choice. */
360#define LTE_CANDIDATES 6
361#define ARRAY_SIZE_LTE(a) ((int)(sizeof(a) / sizeof((a)[0])))
362
363/* Spectrum of the sector's useful-part template, carrier at the capture centre, in w->hr/hi. */
364static inline void lte_template_spectrum(struct esp_sdr_lte_work *w, int u, double fs, size_t len)
365{
366 uint32_t nf = w->n_fft;
367 float dr[ESP_SDR_LTE_SC], di[ESP_SDR_LTE_SC];
368 float norm = 0.0f;
369
370 esp_sdr_lte_pss(u, dr, di);
371 for (uint32_t i = 0; i < nf; i++) {
372 w->hr[i] = 0.0f;
373 w->hi[i] = 0.0f;
374 }
375 for (size_t i = 0; i < len; i++) {
376 float sr = 0.0f, si = 0.0f;
377
378 for (int idx = 0; idx < ESP_SDR_LTE_SC; idx++) {
379 float a = (float)(ESP_SDR_TAU * esp_sdr_lte_k(idx) * ESP_SDR_LTE_DF / fs *
380 (double)i);
381 float c = cosf(a), s = sinf(a);
382
383 sr += dr[idx] * c - di[idx] * s;
384 si += dr[idx] * s + di[idx] * c;
385 }
386 w->hr[i] = sr;
387 w->hi[i] = si;
388 norm += sr * sr + si * si;
389 }
390 norm = sqrtf(norm);
391 for (size_t i = 0; i < len; i++) {
392 w->hr[i] /= norm;
393 w->hi[i] /= norm;
394 }
395 esp_sdr_fft(w->hr, w->hi, nf, w->twc, w->tws, nf);
396}
397
398/* Correlate the capture with the template at a carrier offset of `bins` transform bins. */
399static inline float lte_corr_peak(struct esp_sdr_lte_work *w, int bins, size_t n, size_t len,
400 size_t *at, float *ratio)
401{
402 uint32_t nf = w->n_fft;
403 uint32_t sh = (uint32_t)(bins & (int)(nf - 1U));
404 float peak = 0.0f, sum = 0.0f;
405 size_t cnt = n - len + 1U;
406
407 for (uint32_t b = 0; b < nf; b++) {
408 uint32_t xb = (b + sh) & (nf - 1U);
409
410 w->wr[b] = w->xr[xb] * w->hr[b] + w->xi[xb] * w->hi[b];
411 w->wi[b] = w->xi[xb] * w->hr[b] - w->xr[xb] * w->hi[b];
412 }
413 lte_ifft(w);
414 for (size_t i = 0; i < cnt; i++) {
415 float r = (w->wr[i] * w->wr[i] + w->wi[i] * w->wi[i]) / w->en[i];
416
417 sum += r;
418 if (r > peak) {
419 peak = r;
420 *at = i;
421 }
422 }
423 *ratio = peak / (sum / (float)cnt + 1e-20f);
424 return peak;
425}
426
446static inline int esp_sdr_lte_pss_search(const struct esp_sdr_lte_iq *x, size_t n, double fs,
447 double max_offset_hz, struct esp_sdr_lte_work *w,
448 struct esp_sdr_lte_cand *out, int max_out)
449{
450 size_t len = esp_sdr_lte_symbol_len(fs);
451 uint32_t nf = w->n_fft;
452 /* The part searched: what the transform holds with a symbol to spare. */
453 size_t room = nf > len ? nf - len : 0U;
454 size_t ns = n < room ? n : room;
455
456 if (len > ESP_SDR_LTE_L_MAX || ns < len + 1U) {
457 return -EINVAL;
458 }
459 double mx = 0.0, my = 0.0;
460
461 for (size_t i = 0; i < ns; i++) {
462 mx += x[i].i;
463 my += x[i].q;
464 }
465 mx /= (double)ns;
466 my /= (double)ns;
467 for (uint32_t i = 0; i < nf; i++) {
468 w->xr[i] = i < ns ? (float)((double)x[i].i - mx) : 0.0f;
469 w->xi[i] = i < ns ? (float)((double)x[i].q - my) : 0.0f;
470 }
471 /* Energy of each window of one symbol, by a running sum (kept in double). */
472 double e = 0.0;
473
474 for (size_t i = 0; i < len; i++) {
475 e += lte_pow(w->xr[i], w->xi[i]);
476 }
477 for (size_t i = 0; i + len <= ns; i++) {
478 w->en[i] = (float)e + 1e-3f;
479 if (i + len < ns) {
480 e += lte_pow(w->xr[i + len], w->xi[i + len]);
481 e -= lte_pow(w->xr[i], w->xi[i]);
482 if (e < 0.0) {
483 e = 0.0;
484 }
485 }
486 }
487 esp_sdr_fft(w->xr, w->xi, nf, w->twc, w->tws, nf);
488
489 double bin_hz = fs / (double)nf;
490 int step = (int)(ESP_SDR_LTE_DF / bin_hz + 0.5);
491 int fine = (int)(2500.0 / bin_hz + 0.5);
492 int max_s = (int)(max_offset_hz / (step * bin_hz));
493 struct {
494 float peak;
495 int u, bins;
496 } cand[LTE_CANDIDATES] = {{0}};
497
498 /*
499 * Coarse pass. Keep the strongest few points apart from each other: neither the peak
500 * height nor the detection ratio is reliable on its own along the ambiguity ridge.
501 */
502 for (int u = 0; u < 3; u++) {
503 lte_template_spectrum(w, u, fs, len);
504 for (int s = -max_s; s <= max_s; s++) {
505 size_t at;
506 float rat, peak = lte_corr_peak(w, s * step, ns, len, &at, &rat);
507 int worst = 0, near = -1;
508
509 for (int k = 0; k < LTE_CANDIDATES; k++) {
510 if (cand[k].peak > 0.0f && cand[k].u == u &&
511 abs(cand[k].bins - s * step) < 2 * step) {
512 near = k;
513 }
514 if (cand[k].peak < cand[worst].peak) {
515 worst = k;
516 }
517 }
518 if (near >= 0) {
519 if (peak > cand[near].peak) {
520 cand[near].peak = peak;
521 cand[near].bins = s * step;
522 }
523 } else if (peak > cand[worst].peak) {
524 cand[worst].peak = peak;
525 cand[worst].u = u;
526 cand[worst].bins = s * step;
527 }
528 }
529 }
530 /* Fine pass for each, then the demodulated PSS metric orders them. */
531 struct esp_sdr_lte_cand res[LTE_CANDIDATES];
532 int count = 0;
533
534 for (int k = 0; k < LTE_CANDIDATES; k++) {
535 if (cand[k].peak <= 0.0f) {
536 continue;
537 }
539 float fpeak = 0.0f, frat = 0.0f;
540 int fbins = cand[k].bins;
541 size_t fat = 0;
542 struct esp_sdr_lte_cand r = {.n_id2 = cand[k].u, .metric = -1.0f};
543
544 lte_template_spectrum(w, cand[k].u, fs, len);
545 for (int j = -6; j <= 6; j++) {
546 size_t at;
547 float rat, peak = lte_corr_peak(w, cand[k].bins + j * fine, ns, len, &at, &rat);
548
549 if (peak > fpeak) {
550 fpeak = peak;
551 frat = rat;
552 fbins = cand[k].bins + j * fine;
553 fat = at;
554 }
555 }
556 esp_sdr_lte_pss(cand[k].u, dr, di);
557 for (int dt = -2; dt <= 2; dt++) {
558 for (int df = -1; df <= 1; df++) {
559 double f = (double)fbins * bin_hz + 1250.0 * df;
560
561 if (esp_sdr_lte_demod(x, n, fs, (double)fat + 0.25 * dt, f, yr, yi) != 0) {
562 continue;
563 }
564 float m = lte_pss_metric(yr, yi, dr, di);
565
566 if (m > r.metric) {
567 r.metric = m;
568 r.ratio = frat;
569 r.pos = fat;
570 r.offset_hz = f;
571 }
572 }
573 }
574 if (r.metric < 0.0f) {
575 continue;
576 }
577 int at = count++;
578
579 while (at > 0 && res[at - 1].metric < r.metric) {
580 res[at] = res[at - 1];
581 at--;
582 }
583 res[at] = r;
584 }
585 for (int k = 0; k < count && k < max_out; k++) {
586 out[k] = res[k];
587 }
588 return count < max_out ? count : max_out;
589}
590
591/* Refine one PSS candidate and decode its cell with the SSS in front of each of its repeats. */
592static inline int lte_decode(const struct esp_sdr_lte_iq *x, size_t n, double fs,
593 const struct esp_sdr_lte_cand *cd, struct esp_sdr_lte_cell *cell)
594{
595 size_t len = esp_sdr_lte_symbol_len(fs);
596 double lead = ESP_SDR_LTE_SSS_LEAD_S * fs;
597 double period = ESP_SDR_LTE_SYNC_PERIOD_S * fs;
598 int n_id2 = cd->n_id2;
599 double c_hz = cd->offset_hz, first;
600 size_t at = cd->pos;
601 float dpr[ESP_SDR_LTE_SC], dpi[ESP_SDR_LTE_SC];
602
603 *cell = (struct esp_sdr_lte_cell){0};
604 cell->pss_ratio = cd->ratio;
605 esp_sdr_lte_pss(n_id2, dpr, dpi);
606 first = (double)at;
607 /* Move to the earliest synchronization signal pair the capture holds. */
608 while (first - period - lead >= 0.0) {
609 first -= period;
610 }
611
612 /*
613 * Timing and frequency refinement on the strongest pair: the search left the timing within
614 * a sample and the frequency within 1.25 kHz; the metric falls from 0.9 to 0.6 a sample off,
615 * so a quarter-sample grid, then an eighth.
616 */
617 double s_best = (double)at, f_best = c_hz;
618 float m_best = 0.0f;
619 float yr[ESP_SDR_LTE_SC], yi[ESP_SDR_LTE_SC];
620
621 for (int pass = 0; pass < 2; pass++) {
622 double s0 = s_best, f0 = f_best;
623 int dt_max = pass == 0 ? 6 : 4;
624 int df_max = pass == 0 ? 4 : 4;
625 double dt_step = pass == 0 ? 0.5 : 0.125;
626 double df_step = pass == 0 ? 750.0 : 250.0;
627
628 for (int dt = -dt_max; dt <= dt_max; dt++) {
629 for (int df = -df_max; df <= df_max; df++) {
630 double s = s0 + dt_step * dt, f = f0 + df_step * df;
631
632 if (esp_sdr_lte_demod(x, n, fs, s, f, yr, yi) != 0) {
633 continue;
634 }
635 float m = lte_pss_metric(yr, yi, dpr, dpi);
636
637 if (m > m_best) {
638 m_best = m;
639 s_best = s;
640 f_best = f;
641 }
642 }
643 }
644 }
645 cell->n_id2 = n_id2;
646 cell->pss_metric = m_best;
647 first = s_best;
648 while (first - period - lead >= 0.0) {
649 first -= period;
650 }
651
652 /* The SSS of every pair: z = Y_sss conj(Y_pss) removes the channel. */
653 float best = 0.0f, second = 0.0f;
654 int best_n1 = 0, best_half = 0;
655 double best_f = f_best, best_phase = 0.0;
656 int pairs = 0;
657
658 for (double s = first; s + (double)len <= (double)n; s += period) {
659 if (s - lead >= 0.0) {
660 pairs++;
661 }
662 }
663 cell->pairs = pairs;
664 if (pairs == 0) {
665 return -ENOENT;
666 }
667 for (int dfi = -4; dfi <= 4; dfi++) {
668 double f = f_best + 5000.0 * dfi;
669 float zr[4][ESP_SDR_LTE_SC], zi[4][ESP_SDR_LTE_SC];
670 float zabs = 0.0f;
671 int occ[4];
672 int np = 0, idx = 0;
673
674 for (double s = first; s + (double)len <= (double)n && np < 4; s += period, idx++) {
675 float sr[ESP_SDR_LTE_SC], si[ESP_SDR_LTE_SC];
676
677 if (s - lead < 0.0 || esp_sdr_lte_demod(x, n, fs, s, f, yr, yi) != 0 ||
678 esp_sdr_lte_demod(x, n, fs, s - lead, f, sr, si) != 0) {
679 continue;
680 }
681 for (int k = 0; k < ESP_SDR_LTE_SC; k++) {
682 zr[np][k] = sr[k] * yr[k] + si[k] * yi[k];
683 zi[np][k] = si[k] * yr[k] - sr[k] * yi[k];
684 zabs += sqrtf(zr[np][k] * zr[np][k] + zi[np][k] * zi[np][k]);
685 }
686 occ[np++] = idx;
687 }
688 if (np == 0 || zabs <= 0.0f) {
689 continue;
690 }
691 for (int n1 = 0; n1 < 168; n1++) {
692 for (int h = 0; h < 2; h++) {
693 float cr = 0.0f, ci = 0.0f;
694
695 for (int j = 0; j < np; j++) {
696 float d[ESP_SDR_LTE_SC];
697
698 esp_sdr_lte_sss(n1, n_id2, (h + occ[j]) & 1, d);
699 for (int k = 0; k < ESP_SDR_LTE_SC; k++) {
700 /* z * dS * dP: real and positive for a match. */
701 float ar = zr[j][k] * dpr[k] - zi[j][k] * dpi[k];
702 float ai = zr[j][k] * dpi[k] + zi[j][k] * dpr[k];
703
704 cr += d[k] * ar;
705 ci += d[k] * ai;
706 }
707 }
708 float m = sqrtf(cr * cr + ci * ci) / zabs;
709
710 if (m > best) {
711 if (n1 != best_n1) {
712 second = best;
713 }
714 best = m;
715 best_n1 = n1;
716 best_half = h;
717 best_f = f;
718 best_phase = atan2((double)ci, (double)cr);
719 } else if (n1 != best_n1 && m > second) {
720 second = m;
721 }
722 }
723 }
724 }
725 double delta = best_phase / (ESP_SDR_TAU * ESP_SDR_LTE_SSS_LEAD_S);
726
727 cell->n_id1 = best_n1;
728 cell->cell_id = 3 * best_n1 + n_id2;
729 cell->half = best_half;
730 cell->offset_hz = best_f - delta;
731 cell->offset_sync_hz = cell->offset_hz;
732 cell->timing = first;
733 cell->sss_metric = best;
734 cell->sss_second = second;
735 cell->valid = best >= 0.55f && best > 1.25f * second;
736 return 0;
737}
738
739/* Reference signal subcarriers used: port 0 in the central 6 resource blocks (every bandwidth). */
740#define LTE_CRS_J 6
741#define LTE_CRS_RE (2 * LTE_CRS_J)
742/* LTE's basic time unit Ts = 1 / 30.72 MHz; a slot is 15360 Ts. */
743#define LTE_TS_HZ 30.72e6
744#define LTE_SLOT_TS 15360.0
745/* Useful part of symbol l in its slot, in Ts (normal cyclic prefix: 160, then 144 each). */
746#define LTE_SYM_TS(l) (160.0 + 2192.0 * (double)(l))
747/* Reference symbols farther from the receiver's centre than this fraction of fs are left out. */
748#define LTE_CRS_EDGE 0.42
749/* Below this coherence 0.5 ms apart the reference signals are not used. */
750#define LTE_CRS_MIN_COHERENCE 0.5f
751
752/* Bits first to first + count - 1 of the pseudo-random sequence c(n), 3GPP TS 36.211 7.2. */
753static inline void lte_gold(uint32_t c_init, int first, int count, uint8_t *out)
754{
755 uint32_t x1 = 1U, x2 = c_init & 0x7fffffffU;
756
757 for (int i = 0; i < 1600 + first + count; i++) {
758 if (i >= 1600 + first) {
759 out[i - 1600 - first] = (uint8_t)((x1 ^ x2) & 1U);
760 }
761 uint32_t b1 = (x1 ^ (x1 >> 3)) & 1U;
762 uint32_t b2 = (x2 ^ (x2 >> 1) ^ (x2 >> 2) ^ (x2 >> 3)) & 1U;
763
764 x1 = (x1 >> 1) | (b1 << 30);
765 x2 = (x2 >> 1) | (b2 << 30);
766 }
767}
768
769/*
770 * The port 0 cell-specific reference signal of slot ns (0 to 19), symbol l (0 or 4) in the central
771 * 6 resource blocks (TS 36.211 6.10.1, normal cyclic prefix): subcarrier offsets kk[] in units of
772 * 15 kHz from the carrier (no DC subcarrier) and the values. Its index m' counts from the
773 * centre, so the carrier's bandwidth does not matter.
774 */
775static inline void lte_crs(int cell_id, int ns, int l, int *kk, float *rr, float *ri)
776{
777 uint32_t id = (uint32_t)cell_id;
778 uint32_t c_init = 1024U * (7U * (uint32_t)(ns + 1) + (uint32_t)l + 1U) * (2U * id + 1U) +
779 2U * id + 1U;
780 int koff = ((l == 0 ? 0 : 3) + cell_id % 6) % 6;
781 uint8_t c[2 * LTE_CRS_RE];
782
783 /* m' = 110 + j for j = -LTE_CRS_J .. LTE_CRS_J - 1 (110 resource blocks at most). */
784 lte_gold(c_init, 2 * (110 - LTE_CRS_J), 2 * LTE_CRS_RE, c);
785 for (int i = 0; i < LTE_CRS_RE; i++) {
786 int j = i - LTE_CRS_J;
787
788 kk[i] = 6 * j + koff + (j >= 0 ? 1 : 0);
789 rr[i] = c[2 * i] != 0U ? -0.70710678f : 0.70710678f;
790 ri[i] = c[2 * i + 1] != 0U ? -0.70710678f : 0.70710678f;
791 }
792}
793
794/* One reference symbol's channel (H re, H im per subcarrier), window start and a valid flag. */
795#define LTE_CRS_FLOATS (2 * LTE_CRS_RE + 2)
796
797/* Sum over subcarriers of b's channel times a's conjugate, a's taken at b's subcarriers. */
798static inline void lte_crs_prod(const float *a, const int *ka, const float *b, const int *kb,
799 const uint8_t *ok, double *sr, double *si, double *mag)
800{
801 *sr = *si = *mag = 0.0;
802 for (int i = 0; i < LTE_CRS_RE; i++) {
803 float ar, ai;
804
805 if (!ok[i]) {
806 continue;
807 }
808 if (ka == kb) {
809 ar = a[i];
810 ai = a[LTE_CRS_RE + i];
811 } else {
812 /* a's subcarriers either side of kb[i], linearly interpolated. */
813 int lo = -1;
814
815 for (int k = 0; k + 1 < LTE_CRS_RE; k++) {
816 if (ka[k] < kb[i] && ka[k + 1] > kb[i]) {
817 lo = k;
818 }
819 }
820 if (lo < 0 || !ok[LTE_CRS_RE + lo] || !ok[LTE_CRS_RE + lo + 1]) {
821 continue;
822 }
823 float t = (float)(kb[i] - ka[lo]) / (float)(ka[lo + 1] - ka[lo]);
824
825 ar = a[lo] + t * (a[lo + 1] - a[lo]);
826 ai = a[LTE_CRS_RE + lo] + t * (a[LTE_CRS_RE + lo + 1] - a[LTE_CRS_RE + lo]);
827 }
828 float br = b[i], bi = b[LTE_CRS_RE + i];
829 double qr = (double)(br * ar + bi * ai), qi = (double)(bi * ar - br * ai);
830
831 *sr += qr;
832 *si += qi;
833 *mag += sqrt(qr * qr + qi * qi);
834 }
835}
836
837/*
838 * Fine carrier offset from the port 0 reference signals of a decoded cell. The channel at each
839 * reference subcarrier, symbol by symbol (symbols 0 and 4 of every slot), turns at the residual
840 * offset. The symbols 0.21 and 0.29 ms apart (on interleaved subcarriers) tell it unambiguously
841 * within +-7 kHz, like a vernier; the same subcarriers 0.5, 2 and 8 ms apart then refine it,
842 * each step well inside the range of the next.
843 */
844static inline void lte_crs_refine(const struct esp_sdr_lte_iq *x, size_t n, double fs,
845 struct esp_sdr_lte_work *w, struct esp_sdr_lte_cell *cell)
846{
847 static const int lags[] = {1, 4, 16};
848 double ts = fs / LTE_TS_HZ;
849 double slot = LTE_SLOT_TS * ts;
850 /* Start of the slot of the first PSS (symbol 6 of slot 0 or 10). */
851 double t0 = cell->timing - LTE_SYM_TS(6) * ts;
852 long d_min = -(long)ceil(t0 / slot) - 1;
853 long d_max = (long)ceil(((double)n - t0) / slot);
854 /* Work memory after the search: everything but the twiddles. */
855 float *st = w->xr;
856 size_t max_sym = 7U * (size_t)w->n_fft / LTE_CRS_FLOATS;
857 size_t syms = (size_t)(d_max - d_min + 1) * 2U;
858 int kk[2][LTE_CRS_RE];
859 uint8_t ok[2][2 * LTE_CRS_RE];
860 double mi = 0.0, mq = 0.0;
861 int used = 0;
862
863 cell->crs_symbols = 0;
864 cell->crs_metric = 0.0f;
865 if (syms > max_sym) {
866 syms = max_sym & ~(size_t)1U;
867 }
868 for (size_t i = 0; i < n; i++) {
869 mi += x[i].i;
870 mq += x[i].q;
871 }
872 mi /= (double)n;
873 mq /= (double)n;
874
875 /* The channel of every reference symbol inside the capture. */
876 for (size_t s = 0; s < syms; s++) {
877 long d = d_min + (long)(s / 2U);
878 int l = (s & 1U) != 0U ? 4 : 0;
879 int ns = (int)(((10L * cell->half + d) % 20L + 20L) % 20L);
880 double start = t0 + (double)d * slot + LTE_SYM_TS(l) * ts;
881 float *h = st + s * LTE_CRS_FLOATS;
882 float rr[LTE_CRS_RE], ri[LTE_CRS_RE], yr[LTE_CRS_RE], yi[LTE_CRS_RE];
883 int *k = kk[l == 4];
884
885 lte_crs(cell->cell_id, ns, l, k, rr, ri);
886 h[2 * LTE_CRS_RE + 1] = 0.0f;
887 if (lte_demod_sc(x, n, fs, start, cell->offset_hz, (float)mi, (float)mq, k,
888 LTE_CRS_RE, yr, yi) != 0) {
889 continue;
890 }
891 for (int i = 0; i < LTE_CRS_RE; i++) {
892 /* Inside the receiver's passband, and Y conj(r) (|r| = 1). */
893 double f = cell->offset_hz + (double)k[i] * ESP_SDR_LTE_DF;
894
895 ok[l == 4][i] = fabs(f) <= LTE_CRS_EDGE * fs;
896 ok[l == 4][LTE_CRS_RE + i] = ok[l == 4][i];
897 h[i] = yr[i] * rr[i] + yi[i] * ri[i];
898 h[LTE_CRS_RE + i] = yi[i] * rr[i] - yr[i] * ri[i];
899 }
900 h[2 * LTE_CRS_RE] = (float)ceil(start - 1e-9);
901 h[2 * LTE_CRS_RE + 1] = 1.0f;
902 used++;
903 }
904 if (used < 4) {
905 return;
906 }
907
908 /*
909 * Neighbours: symbol 0 to 4 is 8768 Ts, 4 to the next slot's 0 is 6592 Ts. Their products
910 * peak together only at the true offset within +-7 kHz (the next coincidence is 14 kHz).
911 */
912 double a_r[2] = {0.0, 0.0}, a_i[2] = {0.0, 0.0};
913 double dt[2] = {8768.0 / LTE_TS_HZ, 6592.0 / LTE_TS_HZ};
914
915 for (size_t s = 0; s + 1U < syms; s++) {
916 const float *a = st + s * LTE_CRS_FLOATS, *b = a + LTE_CRS_FLOATS;
917 int la = (int)(s & 1U);
918 double cr, ci, cm;
919
920 if (a[2 * LTE_CRS_RE + 1] == 0.0f || b[2 * LTE_CRS_RE + 1] == 0.0f) {
921 continue;
922 }
923 /* ok[] of b's subcarriers, then of a's (for the interpolation). */
924 uint8_t okab[2 * LTE_CRS_RE];
925
926 for (int i = 0; i < LTE_CRS_RE; i++) {
927 okab[i] = ok[1 - la][i];
928 okab[LTE_CRS_RE + i] = ok[la][i];
929 }
930 lte_crs_prod(a, kk[la], b, kk[1 - la], okab, &cr, &ci, &cm);
931 a_r[la] += cr;
932 a_i[la] += ci;
933 }
934 double best = -1.0, f_a = 0.0;
935
936 for (int pass = 0; pass < 2; pass++) {
937 double f0 = f_a, stp = pass == 0 ? 25.0 : 1.0;
938 int k_max = pass == 0 ? 260 : 25;
939
940 for (int k = -k_max; k <= k_max; k++) {
941 double f = f0 + stp * k, m = 0.0;
942
943 for (int g = 0; g < 2; g++) {
944 double th = ESP_SDR_TAU * f * dt[g];
945
946 m += a_r[g] * cos(th) + a_i[g] * sin(th);
947 }
948 if (m > best) {
949 best = m;
950 f_a = f;
951 }
952 }
953 }
954 /* Both neighbour phases agree there, or it is a side peak (noise or a wrong cell). */
955 if (best < 0.9 * (sqrt(a_r[0] * a_r[0] + a_i[0] * a_i[0]) +
956 sqrt(a_r[1] * a_r[1] + a_i[1] * a_i[1]))) {
957 return;
958 }
959
960 /* The same subcarriers further apart, each lag inside the range the last one leaves. */
961 double f = f_a;
962 float coherence = 0.0f;
963
964 for (size_t g = 0; g < sizeof(lags) / sizeof(lags[0]); g++) {
965 size_t gap = 2U * (size_t)lags[g];
966 double br = 0.0, bi = 0.0, bm = 0.0;
967 int pairs = 0;
968
969 for (size_t s = 0; s + gap < syms; s++) {
970 const float *a = st + s * LTE_CRS_FLOATS, *b = st + (s + gap) * LTE_CRS_FLOATS;
971 const uint8_t *oks = ok[s & 1U];
972 double cr, ci, cm;
973
974 if (a[2 * LTE_CRS_RE + 1] == 0.0f || b[2 * LTE_CRS_RE + 1] == 0.0f) {
975 continue;
976 }
977 lte_crs_prod(a, kk[s & 1U], b, kk[s & 1U], oks, &cr, &ci, &cm);
978 /* Take off what the offset so far turns over the windows' distance. */
979 double th = -ESP_SDR_TAU * f * (double)(b[2 * LTE_CRS_RE] - a[2 * LTE_CRS_RE]) / fs;
980 double c = cos(th), sn = sin(th);
981
982 br += cr * c - ci * sn;
983 bi += cr * sn + ci * c;
984 bm += cm;
985 pairs++;
986 }
987 if (pairs == 0 || bm <= 0.0) {
988 if (g == 0) {
989 return;
990 }
991 break;
992 }
993 if (g == 0) {
994 coherence = (float)(sqrt(br * br + bi * bi) / bm);
995 if (coherence < LTE_CRS_MIN_COHERENCE) {
996 return;
997 }
998 }
999 f += atan2(bi, br) / (ESP_SDR_TAU * (double)lags[g] * LTE_SLOT_TS / LTE_TS_HZ);
1000 }
1001 cell->offset_hz += f;
1002 cell->crs_symbols = used;
1003 cell->crs_metric = coherence;
1004}
1005
1033static inline int esp_sdr_lte_detect(const struct esp_sdr_lte_iq *x, size_t n, double fs,
1034 double max_offset_hz, float min_ratio,
1035 struct esp_sdr_lte_work *w, struct esp_sdr_lte_cell *cell)
1036{
1037 size_t len = esp_sdr_lte_symbol_len(fs);
1038 /* The positions one window searches (esp_sdr_lte_pss_search()), and those needed. */
1039 size_t step = w->n_fft > 2U * len ? w->n_fft - 2U * len + 1U : 1U;
1040 size_t span = (size_t)(ESP_SDR_LTE_SYNC_PERIOD_S * fs);
1041 int found = 0;
1042
1043 *cell = (struct esp_sdr_lte_cell){0};
1044 for (size_t start = 0; start == 0 || (start < span && start + len < n); start += step) {
1045 struct esp_sdr_lte_cand cd[LTE_CANDIDATES];
1046 int count = esp_sdr_lte_pss_search(x + start, n - start, fs, max_offset_hz, w, cd,
1047 ARRAY_SIZE_LTE(cd));
1048
1049 if (count < 0) {
1050 if (start == 0) {
1051 return count;
1052 }
1053 break;
1054 }
1055 for (int k = 0; k < count; k++) {
1056 struct esp_sdr_lte_cell c;
1057
1058 cd[k].pos += start;
1059 if (cd[k].ratio < min_ratio || lte_decode(x, n, fs, &cd[k], &c) != 0) {
1060 continue;
1061 }
1062 if (!found || (c.valid && !cell->valid) ||
1063 (c.valid == cell->valid && c.sss_metric > cell->sss_metric)) {
1064 *cell = c;
1065 found = 1;
1066 }
1067 }
1068 if (found && cell->valid) {
1069 break;
1070 }
1071 }
1072 if (found && cell->valid) {
1073 lte_crs_refine(x, n, fs, w, cell);
1074 }
1075 return found ? 0 : -ENOENT;
1076}
1077
1089static inline double esp_sdr_lte_ppb(double lo_hz, double offset_hz, double *raster_hz)
1090{
1091 double carrier = lo_hz + offset_hz;
1092 double nominal = ESP_SDR_LTE_RASTER_HZ * floor(carrier / ESP_SDR_LTE_RASTER_HZ + 0.5);
1093
1094 if (raster_hz != NULL) {
1095 *raster_hz = nominal;
1096 }
1097 return 1e9 * (nominal - carrier) / lo_hz;
1098}
1099
1101
1102#ifdef __cplusplus
1103}
1104#endif
1105
1106#endif /* ESP_SDR_ESP_SDR_LTE_H_ */
Small signal processing helpers shared by the library, the apps, the tests and the host tools.
static void esp_sdr_fft_twiddles(float *tw_cos, float *tw_sin, uint32_t n)
Twiddle factors for esp_sdr_fft().
Definition esp_sdr_dsp.h:156
static void esp_sdr_fft(float *re, float *im, uint32_t n, const float *tw_cos, const float *tw_sin, uint32_t tw_n)
Forward FFT in place, radix 2, output in natural order (DC first).
Definition esp_sdr_dsp.h:176
#define ESP_SDR_TAU
Definition esp_sdr_dsp.h:32
static double esp_sdr_lte_ppb(double lo_hz, double offset_hz, double *raster_hz)
Crystal error from a measured carrier.
Definition esp_sdr_lte.h:1089
static void esp_sdr_lte_work_init(struct esp_sdr_lte_work *w, float *mem, uint32_t n_fft)
Set up the work memory.
Definition esp_sdr_lte.h:144
#define ESP_SDR_LTE_RASTER_HZ
Definition esp_sdr_lte.h:51
static int esp_sdr_lte_demod(const struct esp_sdr_lte_iq *x, size_t n, double fs, double start, double f_hz, float *yr, float *yi)
Demodulate the 62 synchronization subcarriers of one symbol.
Definition esp_sdr_lte.h:320
#define ESP_SDR_LTE_SC
Definition esp_sdr_lte.h:47
#define ESP_SDR_LTE_DF
Definition esp_sdr_lte.h:41
static float lte_pss_metric(const float *yr, const float *yi, const float *dr, const float *di)
Definition esp_sdr_lte.h:332
static int esp_sdr_lte_pss_search(const struct esp_sdr_lte_iq *x, size_t n, double fs, double max_offset_hz, struct esp_sdr_lte_work *w, struct esp_sdr_lte_cand *out, int max_out)
Search the capture for a PSS.
Definition esp_sdr_lte.h:446
static int esp_sdr_lte_k(int idx)
Definition esp_sdr_lte.h:160
#define ESP_SDR_LTE_L_MAX
Definition esp_sdr_lte.h:49
static void esp_sdr_lte_pss(int n_id2, float *re, float *im)
The PSS Zadoff-Chu sequence of a sector.
Definition esp_sdr_lte.h:172
static int esp_sdr_lte_detect(const struct esp_sdr_lte_iq *x, size_t n, double fs, double max_offset_hz, float min_ratio, struct esp_sdr_lte_work *w, struct esp_sdr_lte_cell *cell)
Find a cell in a capture and measure its carrier offset.
Definition esp_sdr_lte.h:1033
#define ESP_SDR_LTE_SSS_LEAD_S
Definition esp_sdr_lte.h:45
#define ESP_SDR_LTE_SYNC_PERIOD_S
Definition esp_sdr_lte.h:43
static void esp_sdr_lte_sss(int n_id1, int n_id2, int half, float *d)
The SSS sequence of a cell group (3GPP TS 36.211, 6.11.2), +-1 per subcarrier.
Definition esp_sdr_lte.h:219
Definition esp_sdr_lte.h:119
float ratio
Definition esp_sdr_lte.h:127
float metric
Definition esp_sdr_lte.h:129
size_t pos
Definition esp_sdr_lte.h:125
int n_id2
Definition esp_sdr_lte.h:121
double offset_hz
Definition esp_sdr_lte.h:123
Definition esp_sdr_lte.h:82
float sss_metric
Definition esp_sdr_lte.h:102
float sss_second
Definition esp_sdr_lte.h:104
double offset_sync_hz
Definition esp_sdr_lte.h:94
int n_id1
Definition esp_sdr_lte.h:86
double timing
Definition esp_sdr_lte.h:96
int pairs
Definition esp_sdr_lte.h:106
int n_id2
Definition esp_sdr_lte.h:84
float pss_metric
Definition esp_sdr_lte.h:100
int cell_id
Definition esp_sdr_lte.h:88
int valid
Definition esp_sdr_lte.h:108
float crs_metric
Definition esp_sdr_lte.h:115
float pss_ratio
Definition esp_sdr_lte.h:98
int crs_symbols
Definition esp_sdr_lte.h:113
double offset_hz
Definition esp_sdr_lte.h:92
int half
Definition esp_sdr_lte.h:90
Definition esp_sdr_lte.h:54
Definition esp_sdr_lte.h:60
float * hr
Definition esp_sdr_lte.h:69
float * en
Definition esp_sdr_lte.h:73
float * wr
Definition esp_sdr_lte.h:71
float * twc
Definition esp_sdr_lte.h:75
uint32_t n_fft
Definition esp_sdr_lte.h:65
float * xr
Definition esp_sdr_lte.h:67