blob: df87f94b6a71acafcf5a37f762b8d890b8a793da [file] [log] [blame]
dburgessb3a0ca42011-10-12 07:44:40 +00001/*
kurtis.heimerla198d452011-11-26 03:19:28 +00002* Copyright 2008, 2011 Free Software Foundation, Inc.
dburgessb3a0ca42011-10-12 07:44:40 +00003*
Pau Espin Pedrol21d03d32019-07-22 12:05:52 +02004* SPDX-License-Identifier: AGPL-3.0+
5*
dburgessb3a0ca42011-10-12 07:44:40 +00006* This software is distributed under the terms of the GNU Affero Public License.
7* See the COPYING file in the main directory for details.
8*
9* This use of this software may be subject to additional restrictions.
10* See the LEGAL file in the main directory for details.
11
12 This program is free software: you can redistribute it and/or modify
13 it under the terms of the GNU Affero General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU Affero General Public License for more details.
21
22 You should have received a copy of the GNU Affero General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
24
25*/
26
Thomas Tsou7e4e5362013-10-30 21:18:55 -040027#ifdef HAVE_CONFIG_H
28#include "config.h"
29#endif
30
dburgessb3a0ca42011-10-12 07:44:40 +000031#include "sigProcLib.h"
32#include "GSMCommon.h"
Alexander Chemeris954b1182015-06-04 15:39:41 -040033#include "Logger.h"
Tom Tsoud3253432016-03-06 03:08:01 -080034#include "Resampler.h"
dburgessb3a0ca42011-10-12 07:44:40 +000035
Thomas Tsou3eaae802013-08-20 19:31:14 -040036extern "C" {
Pau Espin Pedrol8803f922021-09-01 19:42:46 +020037#include <osmocom/core/panic.h>
Thomas Tsou3eaae802013-08-20 19:31:14 -040038#include "convolve.h"
Thomas Tsou7e4e5362013-10-30 21:18:55 -040039#include "scale.h"
Thomas Tsou0a3dc4c2013-11-09 02:29:55 -050040#include "mult.h"
Thomas Tsou3eaae802013-08-20 19:31:14 -040041}
42
Thomas Tsou7e4e5362013-10-30 21:18:55 -040043using namespace GSM;
44
Thomas Tsouf79c4d02013-11-09 15:51:56 -060045#define TABLESIZE 1024
46#define DELAYFILTS 64
dburgessb3a0ca42011-10-12 07:44:40 +000047
Tom Tsou577cd022015-05-18 13:57:54 -070048/* Clipping detection threshold */
49#define CLIP_THRESH 30000.0f
50
dburgessb3a0ca42011-10-12 07:44:40 +000051/** Lookup tables for trigonometric approximation */
Tom Tsoubb0c68a2017-06-16 17:08:40 -070052static float sincTable[TABLESIZE+1]; // add 1 element for wrap around
dburgessb3a0ca42011-10-12 07:44:40 +000053
54/** Constants */
55static const float M_PI_F = (float)M_PI;
dburgessb3a0ca42011-10-12 07:44:40 +000056
Thomas Tsouc1f7c422013-10-11 13:49:55 -040057/* Precomputed rotation vectors */
Tom Tsou2079a3c2016-03-06 00:58:56 -080058static signalVector *GMSKRotation4 = NULL;
59static signalVector *GMSKReverseRotation4 = NULL;
Thomas Tsouc1f7c422013-10-11 13:49:55 -040060static signalVector *GMSKRotation1 = NULL;
61static signalVector *GMSKReverseRotation1 = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +000062
Thomas Tsouf79c4d02013-11-09 15:51:56 -060063/* Precomputed fractional delay filters */
64static signalVector *delayFilters[DELAYFILTS];
65
Tom Tsou70134a02017-06-12 14:23:53 -070066static const Complex<float> psk8_table[8] = {
Tom Tsoud3253432016-03-06 03:08:01 -080067 Complex<float>(-0.70710678, 0.70710678),
68 Complex<float>( 0.0, -1.0),
69 Complex<float>( 0.0, 1.0),
70 Complex<float>( 0.70710678, -0.70710678),
71 Complex<float>(-1.0, 0.0),
72 Complex<float>(-0.70710678, -0.70710678),
73 Complex<float>( 0.70710678, 0.70710678),
74 Complex<float>( 1.0, 0.0),
75};
76
77/* Downsampling filterbank - 4 SPS to 1 SPS */
78#define DOWNSAMPLE_IN_LEN 624
79#define DOWNSAMPLE_OUT_LEN 156
80
81static Resampler *dnsampler = NULL;
Tom Tsoud3253432016-03-06 03:08:01 -080082
Thomas Tsoue5dcfc42013-08-20 16:27:12 -040083/*
Thomas Tsou3eaae802013-08-20 19:31:14 -040084 * RACH and midamble correlation waveforms. Store the buffer separately
85 * because we need to allocate it explicitly outside of the signal vector
86 * constructor. This is because C++ (prior to C++11) is unable to natively
87 * perform 16-byte memory alignment required by many SSE instructions.
Thomas Tsoue5dcfc42013-08-20 16:27:12 -040088 */
89struct CorrelationSequence {
Harald Welte5c6ca172019-07-21 11:49:44 +020090 CorrelationSequence() : sequence(NULL), buffer(NULL), toa(0.0)
Thomas Tsoue5dcfc42013-08-20 16:27:12 -040091 {
92 }
93
94 ~CorrelationSequence()
95 {
96 delete sequence;
97 }
98
dburgessb3a0ca42011-10-12 07:44:40 +000099 signalVector *sequence;
Thomas Tsou3eaae802013-08-20 19:31:14 -0400100 void *buffer;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400101 float toa;
dburgessb3a0ca42011-10-12 07:44:40 +0000102 complex gain;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400103};
dburgessb3a0ca42011-10-12 07:44:40 +0000104
Thomas Tsou83e06892013-08-20 16:10:01 -0400105/*
Thomas Tsou3eaae802013-08-20 19:31:14 -0400106 * Gaussian and empty modulation pulses. Like the correlation sequences,
107 * store the runtime (Gaussian) buffer separately because of needed alignment
108 * for SSE instructions.
Thomas Tsou83e06892013-08-20 16:10:01 -0400109 */
110struct PulseSequence {
Pau Espin Pedrolf7331762018-12-03 17:46:04 +0100111 PulseSequence() : c0(NULL), c1(NULL), c0_inv(NULL), empty(NULL)
Thomas Tsou83e06892013-08-20 16:10:01 -0400112 {
113 }
114
115 ~PulseSequence()
116 {
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800117 delete c0;
118 delete c1;
Tom Tsoud3253432016-03-06 03:08:01 -0800119 delete c0_inv;
Thomas Tsou83e06892013-08-20 16:10:01 -0400120 delete empty;
121 }
122
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800123 signalVector *c0;
124 signalVector *c1;
Tom Tsoud3253432016-03-06 03:08:01 -0800125 signalVector *c0_inv;
Thomas Tsou83e06892013-08-20 16:10:01 -0400126 signalVector *empty;
127};
128
Tom Tsoud3253432016-03-06 03:08:01 -0800129static CorrelationSequence *gMidambles[] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
130static CorrelationSequence *gEdgeMidambles[] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
Vadim Yanitskiya79bc702018-10-17 11:01:58 +0200131static CorrelationSequence *gRACHSequences[] = {NULL,NULL,NULL};
Tom Tsoud3253432016-03-06 03:08:01 -0800132static PulseSequence *GSMPulse1 = NULL;
133static PulseSequence *GSMPulse4 = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000134
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400135void sigProcLibDestroy()
136{
dburgessb3a0ca42011-10-12 07:44:40 +0000137 for (int i = 0; i < 8; i++) {
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400138 delete gMidambles[i];
Tom Tsoud3253432016-03-06 03:08:01 -0800139 delete gEdgeMidambles[i];
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400140 gMidambles[i] = NULL;
Tom Tsoud3253432016-03-06 03:08:01 -0800141 gEdgeMidambles[i] = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000142 }
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400143
Thomas Tsouf79c4d02013-11-09 15:51:56 -0600144 for (int i = 0; i < DELAYFILTS; i++) {
145 delete delayFilters[i];
146 delayFilters[i] = NULL;
147 }
148
Vadim Yanitskiya79bc702018-10-17 11:01:58 +0200149 for (int i = 0; i < 3; i++) {
150 delete gRACHSequences[i];
151 gRACHSequences[i] = NULL;
152 }
153
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400154 delete GMSKRotation1;
155 delete GMSKReverseRotation1;
Tom Tsou2079a3c2016-03-06 00:58:56 -0800156 delete GMSKRotation4;
157 delete GMSKReverseRotation4;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400158 delete GSMPulse1;
Tom Tsou2079a3c2016-03-06 00:58:56 -0800159 delete GSMPulse4;
Tom Tsoud3253432016-03-06 03:08:01 -0800160 delete dnsampler;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400161
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400162 GMSKRotation1 = NULL;
Tom Tsou2079a3c2016-03-06 00:58:56 -0800163 GMSKRotation4 = NULL;
164 GMSKReverseRotation4 = NULL;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400165 GMSKReverseRotation1 = NULL;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400166 GSMPulse1 = NULL;
Tom Tsou2079a3c2016-03-06 00:58:56 -0800167 GSMPulse4 = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000168}
169
Tom Tsou70134a02017-06-12 14:23:53 -0700170static float vectorNorm2(const signalVector &x)
dburgessb3a0ca42011-10-12 07:44:40 +0000171{
172 signalVector::const_iterator xPtr = x.begin();
173 float Energy = 0.0;
174 for (;xPtr != x.end();xPtr++) {
175 Energy += xPtr->norm2();
176 }
177 return Energy;
178}
179
Tom Tsou2079a3c2016-03-06 00:58:56 -0800180/*
181 * Initialize 4 sps and 1 sps rotation tables
182 */
183static void initGMSKRotationTables()
Thomas Tsoud24cc2c2013-08-20 15:41:45 -0400184{
Tom Tsou2079a3c2016-03-06 00:58:56 -0800185 size_t len1 = 157, len4 = 625;
186
187 GMSKRotation4 = new signalVector(len4);
188 GMSKReverseRotation4 = new signalVector(len4);
189 signalVector::iterator rotPtr = GMSKRotation4->begin();
190 signalVector::iterator revPtr = GMSKReverseRotation4->begin();
Tom Tsoubb0c68a2017-06-16 17:08:40 -0700191 auto phase = 0.0;
Tom Tsou2079a3c2016-03-06 00:58:56 -0800192 while (rotPtr != GMSKRotation4->end()) {
Tom Tsoubb0c68a2017-06-16 17:08:40 -0700193 *rotPtr++ = complex(cos(phase), sin(phase));
194 *revPtr++ = complex(cos(-phase), sin(-phase));
195 phase += M_PI / 2.0 / 4.0;
dburgessb3a0ca42011-10-12 07:44:40 +0000196 }
dburgessb3a0ca42011-10-12 07:44:40 +0000197
Tom Tsou2079a3c2016-03-06 00:58:56 -0800198 GMSKRotation1 = new signalVector(len1);
199 GMSKReverseRotation1 = new signalVector(len1);
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400200 rotPtr = GMSKRotation1->begin();
201 revPtr = GMSKReverseRotation1->begin();
202 phase = 0.0;
203 while (rotPtr != GMSKRotation1->end()) {
Tom Tsoubb0c68a2017-06-16 17:08:40 -0700204 *rotPtr++ = complex(cos(phase), sin(phase));
205 *revPtr++ = complex(cos(-phase), sin(-phase));
206 phase += M_PI / 2.0;
Thomas Tsoue57004d2013-08-20 18:55:33 -0400207 }
dburgessb3a0ca42011-10-12 07:44:40 +0000208}
209
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400210static void GMSKRotate(signalVector &x, int sps)
211{
Thomas Tsou0a3dc4c2013-11-09 02:29:55 -0500212#if HAVE_NEON
213 size_t len;
214 signalVector *a, *b, *out;
215
216 a = &x;
217 out = &x;
218 len = out->size();
219
220 if (len == 157)
221 len--;
222
223 if (sps == 1)
224 b = GMSKRotation1;
225 else
Tom Tsou2079a3c2016-03-06 00:58:56 -0800226 b = GMSKRotation4;
Thomas Tsou0a3dc4c2013-11-09 02:29:55 -0500227
228 mul_complex((float *) out->begin(),
229 (float *) a->begin(),
230 (float *) b->begin(), len);
231#else
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400232 signalVector::iterator rotPtr, xPtr = x.begin();
233
234 if (sps == 1)
235 rotPtr = GMSKRotation1->begin();
236 else
Tom Tsou2079a3c2016-03-06 00:58:56 -0800237 rotPtr = GMSKRotation4->begin();
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400238
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500239 if (x.isReal()) {
dburgessb3a0ca42011-10-12 07:44:40 +0000240 while (xPtr < x.end()) {
241 *xPtr = *rotPtr++ * (xPtr->real());
242 xPtr++;
243 }
244 }
245 else {
246 while (xPtr < x.end()) {
247 *xPtr = *rotPtr++ * (*xPtr);
248 xPtr++;
249 }
250 }
Thomas Tsou0a3dc4c2013-11-09 02:29:55 -0500251#endif
dburgessb3a0ca42011-10-12 07:44:40 +0000252}
253
Tom Tsou2079a3c2016-03-06 00:58:56 -0800254static bool GMSKReverseRotate(signalVector &x, int sps)
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400255{
256 signalVector::iterator rotPtr, xPtr= x.begin();
257
258 if (sps == 1)
259 rotPtr = GMSKReverseRotation1->begin();
Tom Tsou2079a3c2016-03-06 00:58:56 -0800260 else if (sps == 4)
261 rotPtr = GMSKReverseRotation4->begin();
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400262 else
Tom Tsou2079a3c2016-03-06 00:58:56 -0800263 return false;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400264
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500265 if (x.isReal()) {
dburgessb3a0ca42011-10-12 07:44:40 +0000266 while (xPtr < x.end()) {
267 *xPtr = *rotPtr++ * (xPtr->real());
268 xPtr++;
269 }
270 }
271 else {
272 while (xPtr < x.end()) {
273 *xPtr = *rotPtr++ * (*xPtr);
274 xPtr++;
275 }
276 }
Tom Tsou2079a3c2016-03-06 00:58:56 -0800277
278 return true;
dburgessb3a0ca42011-10-12 07:44:40 +0000279}
280
Tom Tsou70134a02017-06-12 14:23:53 -0700281/** Convolution type indicator */
282enum ConvType {
283 START_ONLY,
284 NO_DELAY,
285 CUSTOM,
286 UNDEFINED,
287};
288
289static signalVector *convolve(const signalVector *x, const signalVector *h,
290 signalVector *y, ConvType spanType,
Sylvain Munauta3934a12018-12-20 19:10:26 +0100291 size_t start = 0, size_t len = 0)
dburgessb3a0ca42011-10-12 07:44:40 +0000292{
Thomas Tsou3f32ab52013-11-15 16:32:54 -0500293 int rc;
294 size_t head = 0, tail = 0;
Thomas Tsou3eaae802013-08-20 19:31:14 -0400295 bool alloc = false, append = false;
296 const signalVector *_x = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000297
Thomas Tsou3eaae802013-08-20 19:31:14 -0400298 if (!x || !h)
dburgessb3a0ca42011-10-12 07:44:40 +0000299 return NULL;
300
Thomas Tsou3eaae802013-08-20 19:31:14 -0400301 switch (spanType) {
302 case START_ONLY:
303 start = 0;
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500304 head = h->size() - 1;
Thomas Tsou3eaae802013-08-20 19:31:14 -0400305 len = x->size();
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500306
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500307 if (x->getStart() < head)
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500308 append = true;
dburgessb3a0ca42011-10-12 07:44:40 +0000309 break;
Thomas Tsou3eaae802013-08-20 19:31:14 -0400310 case NO_DELAY:
311 start = h->size() / 2;
312 head = start;
313 tail = start;
314 len = x->size();
315 append = true;
316 break;
317 case CUSTOM:
318 if (start < h->size() - 1) {
319 head = h->size() - start;
320 append = true;
321 }
322 if (start + len > x->size()) {
323 tail = start + len - x->size();
324 append = true;
dburgessb3a0ca42011-10-12 07:44:40 +0000325 }
326 break;
327 default:
328 return NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000329 }
dburgessb3a0ca42011-10-12 07:44:40 +0000330
Thomas Tsou3eaae802013-08-20 19:31:14 -0400331 /*
332 * Error if the output vector is too small. Create the output vector
333 * if the pointer is NULL.
334 */
335 if (y && (len > y->size()))
336 return NULL;
337 if (!y) {
Pau Espin Pedrolf7331762018-12-03 17:46:04 +0100338 y = new signalVector(len, convolve_h_alloc, free);
Thomas Tsou3eaae802013-08-20 19:31:14 -0400339 alloc = true;
340 }
341
342 /* Prepend or post-pend the input vector if the parameters require it */
343 if (append)
344 _x = new signalVector(*x, head, tail);
345 else
346 _x = x;
347
348 /*
Martin Hauke066fd042019-10-13 19:08:00 +0200349 * Four convolve types:
Thomas Tsou3eaae802013-08-20 19:31:14 -0400350 * 1. Complex-Real (aligned)
351 * 2. Complex-Complex (aligned)
352 * 3. Complex-Real (!aligned)
353 * 4. Complex-Complex (!aligned)
354 */
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500355 if (h->isReal() && h->isAligned()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -0400356 rc = convolve_real((float *) _x->begin(), _x->size(),
357 (float *) h->begin(), h->size(),
358 (float *) y->begin(), y->size(),
Sylvain Munauta3934a12018-12-20 19:10:26 +0100359 start, len);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500360 } else if (!h->isReal() && h->isAligned()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -0400361 rc = convolve_complex((float *) _x->begin(), _x->size(),
362 (float *) h->begin(), h->size(),
363 (float *) y->begin(), y->size(),
Sylvain Munauta3934a12018-12-20 19:10:26 +0100364 start, len);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500365 } else if (h->isReal() && !h->isAligned()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -0400366 rc = base_convolve_real((float *) _x->begin(), _x->size(),
367 (float *) h->begin(), h->size(),
368 (float *) y->begin(), y->size(),
Sylvain Munauta3934a12018-12-20 19:10:26 +0100369 start, len);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500370 } else if (!h->isReal() && !h->isAligned()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -0400371 rc = base_convolve_complex((float *) _x->begin(), _x->size(),
372 (float *) h->begin(), h->size(),
373 (float *) y->begin(), y->size(),
Sylvain Munauta3934a12018-12-20 19:10:26 +0100374 start, len);
Thomas Tsou3eaae802013-08-20 19:31:14 -0400375 } else {
376 rc = -1;
377 }
378
379 if (append)
380 delete _x;
381
382 if (rc < 0) {
383 if (alloc)
384 delete y;
385 return NULL;
386 }
387
388 return y;
389}
dburgessb3a0ca42011-10-12 07:44:40 +0000390
Tom Tsoud3253432016-03-06 03:08:01 -0800391/*
392 * Generate static EDGE linear equalizer. This equalizer is not adaptive.
393 * Filter taps are generated from the inverted 1 SPS impulse response of
394 * the EDGE pulse shape captured after the downsampling filter.
395 */
396static bool generateInvertC0Pulse(PulseSequence *pulse)
397{
398 if (!pulse)
399 return false;
400
Pau Espin Pedrolf7331762018-12-03 17:46:04 +0100401 pulse->c0_inv = new signalVector((complex *) convolve_h_alloc(5), 0, 5, convolve_h_alloc, free);
Tom Tsoud3253432016-03-06 03:08:01 -0800402 pulse->c0_inv->isReal(true);
403 pulse->c0_inv->setAligned(false);
404
405 signalVector::iterator xP = pulse->c0_inv->begin();
406 *xP++ = 0.15884;
407 *xP++ = -0.43176;
408 *xP++ = 1.00000;
409 *xP++ = -0.42608;
410 *xP++ = 0.14882;
411
412 return true;
413}
414
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400415static bool generateC1Pulse(int sps, PulseSequence *pulse)
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800416{
417 int len;
418
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400419 if (!pulse)
420 return false;
421
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800422 switch (sps) {
423 case 4:
424 len = 8;
425 break;
426 default:
427 return false;
428 }
429
Pau Espin Pedrolf7331762018-12-03 17:46:04 +0100430 pulse->c1 = new signalVector((complex *) convolve_h_alloc(len), 0, len, convolve_h_alloc, free);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500431 pulse->c1->isReal(true);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800432
433 /* Enable alignment for SSE usage */
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400434 pulse->c1->setAligned(true);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800435
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400436 signalVector::iterator xP = pulse->c1->begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800437
438 switch (sps) {
439 case 4:
440 /* BT = 0.30 */
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400441 *xP++ = 0.0;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800442 *xP++ = 8.16373112e-03;
443 *xP++ = 2.84385729e-02;
444 *xP++ = 5.64158904e-02;
445 *xP++ = 7.05463553e-02;
446 *xP++ = 5.64158904e-02;
447 *xP++ = 2.84385729e-02;
448 *xP++ = 8.16373112e-03;
449 }
450
451 return true;
452}
453
Tom Tsou2079a3c2016-03-06 00:58:56 -0800454static PulseSequence *generateGSMPulse(int sps)
dburgessb3a0ca42011-10-12 07:44:40 +0000455{
Thomas Tsou83e06892013-08-20 16:10:01 -0400456 int len;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800457 float arg, avg, center;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400458 PulseSequence *pulse;
Thomas Tsou83e06892013-08-20 16:10:01 -0400459
Tom Tsoud3253432016-03-06 03:08:01 -0800460 if ((sps != 1) && (sps != 4))
461 return NULL;
462
Thomas Tsou83e06892013-08-20 16:10:01 -0400463 /* Store a single tap filter used for correlation sequence generation */
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400464 pulse = new PulseSequence();
465 pulse->empty = new signalVector(1);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500466 pulse->empty->isReal(true);
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400467 *(pulse->empty->begin()) = 1.0f;
Thomas Tsou83e06892013-08-20 16:10:01 -0400468
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400469 /*
470 * For 4 samples-per-symbol use a precomputed single pulse Laurent
471 * approximation. This should yields below 2 degrees of phase error at
472 * the modulator output. Use the existing pulse approximation for all
473 * other oversampling factors.
474 */
475 switch (sps) {
476 case 4:
477 len = 16;
478 break;
Tom Tsoud3253432016-03-06 03:08:01 -0800479 case 1:
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400480 default:
Tom Tsou2079a3c2016-03-06 00:58:56 -0800481 len = 4;
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400482 }
Thomas Tsou3eaae802013-08-20 19:31:14 -0400483
Pau Espin Pedrolf7331762018-12-03 17:46:04 +0100484 pulse->c0 = new signalVector((complex *) convolve_h_alloc(len), 0, len, convolve_h_alloc, free);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500485 pulse->c0->isReal(true);
Thomas Tsou3eaae802013-08-20 19:31:14 -0400486
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800487 /* Enable alingnment for SSE usage */
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400488 pulse->c0->setAligned(true);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800489
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400490 signalVector::iterator xP = pulse->c0->begin();
Thomas Tsou83e06892013-08-20 16:10:01 -0400491
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400492 if (sps == 4) {
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800493 *xP++ = 0.0;
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400494 *xP++ = 4.46348606e-03;
495 *xP++ = 2.84385729e-02;
496 *xP++ = 1.03184855e-01;
497 *xP++ = 2.56065552e-01;
498 *xP++ = 4.76375085e-01;
499 *xP++ = 7.05961177e-01;
500 *xP++ = 8.71291644e-01;
501 *xP++ = 9.29453645e-01;
502 *xP++ = 8.71291644e-01;
503 *xP++ = 7.05961177e-01;
504 *xP++ = 4.76375085e-01;
505 *xP++ = 2.56065552e-01;
506 *xP++ = 1.03184855e-01;
507 *xP++ = 2.84385729e-02;
508 *xP++ = 4.46348606e-03;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400509 generateC1Pulse(sps, pulse);
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400510 } else {
511 center = (float) (len - 1.0) / 2.0;
Thomas Tsou83e06892013-08-20 16:10:01 -0400512
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400513 /* GSM pulse approximation */
514 for (int i = 0; i < len; i++) {
515 arg = ((float) i - center) / (float) sps;
516 *xP++ = 0.96 * exp(-1.1380 * arg * arg -
517 0.527 * arg * arg * arg * arg);
518 }
dburgessb3a0ca42011-10-12 07:44:40 +0000519
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400520 avg = sqrtf(vectorNorm2(*pulse->c0) / sps);
521 xP = pulse->c0->begin();
522 for (int i = 0; i < len; i++)
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800523 *xP++ /= avg;
524 }
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400525
Tom Tsoud3253432016-03-06 03:08:01 -0800526 /*
527 * Current form of the EDGE equalization filter non-realizable at 4 SPS.
528 * Load the onto both 1 SPS and 4 SPS objects for convenience. Note that
529 * the EDGE demodulator downsamples to 1 SPS prior to equalization.
530 */
531 generateInvertC0Pulse(pulse);
532
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400533 return pulse;
dburgessb3a0ca42011-10-12 07:44:40 +0000534}
535
Pau Espin Pedrol7dc07b92019-07-01 17:55:01 +0200536/* Convert -1..+1 soft bits to 0..1 soft bits */
537void vectorSlicer(float *dest, const float *src, size_t len)
Tom Tsoud3253432016-03-06 03:08:01 -0800538{
Pau Espin Pedrol7dc07b92019-07-01 17:55:01 +0200539 size_t i;
540 for (i = 0; i < len; i++) {
541 dest[i] = 0.5 * (src[i] + 1.0f);
542 if (dest[i] > 1.0)
543 dest[i] = 1.0;
544 else if (dest[i] < 0.0)
545 dest[i] = 0.0;
546 }
Tom Tsoud3253432016-03-06 03:08:01 -0800547}
548
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800549static signalVector *rotateBurst(const BitVector &wBurst,
550 int guardPeriodLength, int sps)
551{
552 int burst_len;
Tom Tsou7278a872017-06-14 14:50:39 -0700553 signalVector *pulse, rotated;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800554 signalVector::iterator itr;
555
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400556 pulse = GSMPulse1->empty;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800557 burst_len = sps * (wBurst.size() + guardPeriodLength);
558 rotated = signalVector(burst_len);
559 itr = rotated.begin();
560
561 for (unsigned i = 0; i < wBurst.size(); i++) {
562 *itr = 2.0 * (wBurst[i] & 0x01) - 1.0;
563 itr += sps;
564 }
565
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400566 GMSKRotate(rotated, sps);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500567 rotated.isReal(false);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800568
569 /* Dummy filter operation */
Tom Tsou7278a872017-06-14 14:50:39 -0700570 return convolve(&rotated, pulse, NULL, START_ONLY);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800571}
572
Tom Tsoud3253432016-03-06 03:08:01 -0800573static void rotateBurst2(signalVector &burst, double phase)
574{
575 Complex<float> rot = Complex<float>(cos(phase), sin(phase));
576
577 for (size_t i = 0; i < burst.size(); i++)
578 burst[i] = burst[i] * rot;
579}
580
Tom Tsou4dfd64a2016-03-06 20:31:51 -0800581/*
582 * Ignore the guard length argument in the GMSK modulator interface
583 * because it results in 624/628 sized bursts instead of the preferred
584 * burst length of 625. Only 4 SPS is supported.
585 */
586static signalVector *modulateBurstLaurent(const BitVector &bits)
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800587{
Tom Tsou4dfd64a2016-03-06 20:31:51 -0800588 int burst_len, sps = 4;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800589 float phase;
Tom Tsou7278a872017-06-14 14:50:39 -0700590 signalVector *c0_pulse, *c1_pulse, *c0_shaped, *c1_shaped;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800591 signalVector::iterator c0_itr, c1_itr;
592
Tom Tsou2079a3c2016-03-06 00:58:56 -0800593 c0_pulse = GSMPulse4->c0;
594 c1_pulse = GSMPulse4->c1;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800595
Tom Tsou4dfd64a2016-03-06 20:31:51 -0800596 if (bits.size() > 156)
597 return NULL;
598
599 burst_len = 625;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800600
Tom Tsou7278a872017-06-14 14:50:39 -0700601 signalVector c0_burst(burst_len, c0_pulse->size());
602 c0_burst.isReal(true);
603 c0_itr = c0_burst.begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800604
Tom Tsou7278a872017-06-14 14:50:39 -0700605 signalVector c1_burst(burst_len, c1_pulse->size());
606 c1_itr = c1_burst.begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800607
Tom Tsouaa15d622016-08-11 14:36:23 -0700608 /* Padded differential tail bits */
609 *c0_itr = 2.0 * (0x00 & 0x01) - 1.0;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800610 c0_itr += sps;
611
612 /* Main burst bits */
613 for (unsigned i = 0; i < bits.size(); i++) {
614 *c0_itr = 2.0 * (bits[i] & 0x01) - 1.0;
615 c0_itr += sps;
616 }
617
Tom Tsouaa15d622016-08-11 14:36:23 -0700618 /* Padded differential tail bits */
619 *c0_itr = 2.0 * (0x00 & 0x01) - 1.0;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800620
621 /* Generate C0 phase coefficients */
Tom Tsou7278a872017-06-14 14:50:39 -0700622 GMSKRotate(c0_burst, sps);
623 c0_burst.isReal(false);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800624
Tom Tsou7278a872017-06-14 14:50:39 -0700625 c0_itr = c0_burst.begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800626 c0_itr += sps * 2;
627 c1_itr += sps * 2;
628
629 /* Start magic */
630 phase = 2.0 * ((0x01 & 0x01) ^ (0x01 & 0x01)) - 1.0;
631 *c1_itr = *c0_itr * Complex<float>(0, phase);
632 c0_itr += sps;
633 c1_itr += sps;
634
635 /* Generate C1 phase coefficients */
636 for (unsigned i = 2; i < bits.size(); i++) {
637 phase = 2.0 * ((bits[i - 1] & 0x01) ^ (bits[i - 2] & 0x01)) - 1.0;
638 *c1_itr = *c0_itr * Complex<float>(0, phase);
639
640 c0_itr += sps;
641 c1_itr += sps;
642 }
643
644 /* End magic */
645 int i = bits.size();
646 phase = 2.0 * ((bits[i-1] & 0x01) ^ (bits[i-2] & 0x01)) - 1.0;
647 *c1_itr = *c0_itr * Complex<float>(0, phase);
648
649 /* Primary (C0) and secondary (C1) pulse shaping */
Tom Tsou7278a872017-06-14 14:50:39 -0700650 c0_shaped = convolve(&c0_burst, c0_pulse, NULL, START_ONLY);
651 c1_shaped = convolve(&c1_burst, c1_pulse, NULL, START_ONLY);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800652
653 /* Sum shaped outputs into C0 */
654 c0_itr = c0_shaped->begin();
655 c1_itr = c1_shaped->begin();
656 for (unsigned i = 0; i < c0_shaped->size(); i++ )
657 *c0_itr++ += *c1_itr++;
658
659 delete c1_shaped;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800660 return c0_shaped;
661}
662
Tom Tsoud3253432016-03-06 03:08:01 -0800663static signalVector *rotateEdgeBurst(const signalVector &symbols, int sps)
664{
665 signalVector *burst;
666 signalVector::iterator burst_itr;
667
668 burst = new signalVector(symbols.size() * sps);
669 burst_itr = burst->begin();
670
671 for (size_t i = 0; i < symbols.size(); i++) {
672 float phase = i * 3.0f * M_PI / 8.0f;
673 Complex<float> rot = Complex<float>(cos(phase), sin(phase));
674
675 *burst_itr = symbols[i] * rot;
676 burst_itr += sps;
677 }
678
679 return burst;
680}
681
682static signalVector *derotateEdgeBurst(const signalVector &symbols, int sps)
683{
684 signalVector *burst;
685 signalVector::iterator burst_itr;
686
687 if (symbols.size() % sps)
688 return NULL;
689
690 burst = new signalVector(symbols.size() / sps);
691 burst_itr = burst->begin();
692
693 for (size_t i = 0; i < burst->size(); i++) {
694 float phase = (float) (i % 16) * 3.0f * M_PI / 8.0f;
695 Complex<float> rot = Complex<float>(cosf(phase), -sinf(phase));
696
697 *burst_itr = symbols[sps * i] * rot;
698 burst_itr++;
699 }
700
701 return burst;
702}
703
704static signalVector *mapEdgeSymbols(const BitVector &bits)
705{
706 if (bits.size() % 3)
707 return NULL;
708
709 signalVector *symbols = new signalVector(bits.size() / 3);
710
711 for (size_t i = 0; i < symbols->size(); i++) {
712 unsigned index = (((unsigned) bits[3 * i + 0] & 0x01) << 0) |
713 (((unsigned) bits[3 * i + 1] & 0x01) << 1) |
714 (((unsigned) bits[3 * i + 2] & 0x01) << 2);
715
716 (*symbols)[i] = psk8_table[index];
717 }
718
719 return symbols;
720}
721
Tom Tsoud2b07032016-04-26 19:28:59 -0700722/*
723 * EDGE 8-PSK rotate and pulse shape
724 *
725 * Delay the EDGE downlink bursts by one symbol in order to match GMSK pulse
726 * shaping group delay. The difference in group delay arises from the dual
Martin Hauke066fd042019-10-13 19:08:00 +0200727 * pulse filter combination of the GMSK Laurent representation whereas 8-PSK
Tom Tsoud2b07032016-04-26 19:28:59 -0700728 * uses a single pulse linear filter.
729 */
Tom Tsoud3253432016-03-06 03:08:01 -0800730static signalVector *shapeEdgeBurst(const signalVector &symbols)
731{
Tom Tsoud2b07032016-04-26 19:28:59 -0700732 size_t nsyms, nsamps = 625, sps = 4;
Tom Tsoud3253432016-03-06 03:08:01 -0800733 signalVector::iterator burst_itr;
734
735 nsyms = symbols.size();
736
Tom Tsoud2b07032016-04-26 19:28:59 -0700737 if (nsyms * sps > nsamps)
Tom Tsoud3253432016-03-06 03:08:01 -0800738 nsyms = 156;
739
Tom Tsou7278a872017-06-14 14:50:39 -0700740 signalVector burst(nsamps, GSMPulse4->c0->size());
Tom Tsoud3253432016-03-06 03:08:01 -0800741
Tom Tsoud2b07032016-04-26 19:28:59 -0700742 /* Delay burst by 1 symbol */
Tom Tsou7278a872017-06-14 14:50:39 -0700743 burst_itr = burst.begin() + sps;
Tom Tsou06676ea2016-07-19 12:50:21 -0700744 for (size_t i = 0; i < nsyms; i++) {
Tom Tsoud3253432016-03-06 03:08:01 -0800745 float phase = i * 3.0f * M_PI / 8.0f;
746 Complex<float> rot = Complex<float>(cos(phase), sin(phase));
747
748 *burst_itr = symbols[i] * rot;
Tom Tsoud2b07032016-04-26 19:28:59 -0700749 burst_itr += sps;
Tom Tsoud3253432016-03-06 03:08:01 -0800750 }
751
752 /* Single Gaussian pulse approximation shaping */
Tom Tsou7278a872017-06-14 14:50:39 -0700753 return convolve(&burst, GSMPulse4->c0, NULL, START_ONLY);
Tom Tsoud3253432016-03-06 03:08:01 -0800754}
755
756/*
Tom Tsou8ee2f382016-03-06 20:57:34 -0800757 * Generate a random GSM normal burst.
758 */
759signalVector *genRandNormalBurst(int tsc, int sps, int tn)
760{
761 if ((tsc < 0) || (tsc > 7) || (tn < 0) || (tn > 7))
762 return NULL;
763 if ((sps != 1) && (sps != 4))
764 return NULL;
765
766 int i = 0;
Tom Tsou7278a872017-06-14 14:50:39 -0700767 BitVector bits(148);
Tom Tsou8ee2f382016-03-06 20:57:34 -0800768
769 /* Tail bits */
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700770 for (; i < 3; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700771 bits[i] = 0;
Tom Tsou8ee2f382016-03-06 20:57:34 -0800772
773 /* Random bits */
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700774 for (; i < 60; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700775 bits[i] = rand() % 2;
Tom Tsou8ee2f382016-03-06 20:57:34 -0800776
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700777 /* Stealing bit */
Tom Tsou7278a872017-06-14 14:50:39 -0700778 bits[i++] = 0;
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700779
Tom Tsou8ee2f382016-03-06 20:57:34 -0800780 /* Training sequence */
781 for (int n = 0; i < 87; i++, n++)
Tom Tsou7278a872017-06-14 14:50:39 -0700782 bits[i] = gTrainingSequence[tsc][n];
Tom Tsou8ee2f382016-03-06 20:57:34 -0800783
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700784 /* Stealing bit */
Tom Tsou7278a872017-06-14 14:50:39 -0700785 bits[i++] = 0;
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700786
Tom Tsou8ee2f382016-03-06 20:57:34 -0800787 /* Random bits */
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700788 for (; i < 145; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700789 bits[i] = rand() % 2;
Tom Tsou8ee2f382016-03-06 20:57:34 -0800790
791 /* Tail bits */
792 for (; i < 148; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700793 bits[i] = 0;
Tom Tsou8ee2f382016-03-06 20:57:34 -0800794
795 int guard = 8 + !(tn % 4);
Tom Tsou7278a872017-06-14 14:50:39 -0700796 return modulateBurst(bits, guard, sps);
Tom Tsou8ee2f382016-03-06 20:57:34 -0800797}
798
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300799/*
800 * Generate a random GSM access burst.
801 */
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300802signalVector *genRandAccessBurst(int delay, int sps, int tn)
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300803{
804 if ((tn < 0) || (tn > 7))
805 return NULL;
806 if ((sps != 1) && (sps != 4))
807 return NULL;
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300808 if (delay > 68)
809 return NULL;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300810
811 int i = 0;
Tom Tsou7278a872017-06-14 14:50:39 -0700812 BitVector bits(88 + delay);
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300813
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300814 /* delay */
815 for (; i < delay; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700816 bits[i] = 0;
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300817
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300818 /* head and synch bits */
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300819 for (int n = 0; i < 49+delay; i++, n++)
Tom Tsou7278a872017-06-14 14:50:39 -0700820 bits[i] = gRACHBurst[n];
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300821
822 /* Random bits */
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300823 for (; i < 85+delay; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700824 bits[i] = rand() % 2;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300825
826 /* Tail bits */
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300827 for (; i < 88+delay; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700828 bits[i] = 0;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300829
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300830 int guard = 68-delay + !(tn % 4);
Tom Tsou7278a872017-06-14 14:50:39 -0700831 return modulateBurst(bits, guard, sps);
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300832}
833
Tom Tsou8ee2f382016-03-06 20:57:34 -0800834signalVector *generateEmptyBurst(int sps, int tn)
835{
836 if ((tn < 0) || (tn > 7))
837 return NULL;
838
839 if (sps == 4)
840 return new signalVector(625);
841 else if (sps == 1)
842 return new signalVector(148 + 8 + !(tn % 4));
843 else
844 return NULL;
845}
846
847signalVector *generateDummyBurst(int sps, int tn)
848{
849 if (((sps != 1) && (sps != 4)) || (tn < 0) || (tn > 7))
850 return NULL;
851
852 return modulateBurst(gDummyBurst, 8 + !(tn % 4), sps);
853}
854
855/*
Tom Tsoud3253432016-03-06 03:08:01 -0800856 * Generate a random 8-PSK EDGE burst. Only 4 SPS is supported with
857 * the returned burst being 625 samples in length.
858 */
859signalVector *generateEdgeBurst(int tsc)
860{
861 int tail = 9 / 3;
862 int data = 174 / 3;
863 int train = 78 / 3;
864
865 if ((tsc < 0) || (tsc > 7))
866 return NULL;
867
Tom Tsou7278a872017-06-14 14:50:39 -0700868 signalVector burst(148);
Tom Tsoud3253432016-03-06 03:08:01 -0800869 const BitVector *midamble = &gEdgeTrainingSequence[tsc];
870
871 /* Tail */
872 int n, i = 0;
873 for (; i < tail; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700874 burst[i] = psk8_table[7];
Tom Tsoud3253432016-03-06 03:08:01 -0800875
876 /* Body */
877 for (; i < tail + data; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700878 burst[i] = psk8_table[rand() % 8];
Tom Tsoud3253432016-03-06 03:08:01 -0800879
880 /* TSC */
881 for (n = 0; i < tail + data + train; i++, n++) {
882 unsigned index = (((unsigned) (*midamble)[3 * n + 0] & 0x01) << 0) |
883 (((unsigned) (*midamble)[3 * n + 1] & 0x01) << 1) |
884 (((unsigned) (*midamble)[3 * n + 2] & 0x01) << 2);
885
Tom Tsou7278a872017-06-14 14:50:39 -0700886 burst[i] = psk8_table[index];
Tom Tsoud3253432016-03-06 03:08:01 -0800887 }
888
889 /* Body */
890 for (; i < tail + data + train + data; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700891 burst[i] = psk8_table[rand() % 8];
Tom Tsoud3253432016-03-06 03:08:01 -0800892
893 /* Tail */
894 for (; i < tail + data + train + data + tail; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700895 burst[i] = psk8_table[7];
Tom Tsoud3253432016-03-06 03:08:01 -0800896
Tom Tsou7278a872017-06-14 14:50:39 -0700897 return shapeEdgeBurst(burst);
Tom Tsoud3253432016-03-06 03:08:01 -0800898}
899
900/*
901 * Modulate 8-PSK burst. When empty pulse shaping (rotation only)
902 * is enabled, the output vector length will be bit sequence length
903 * times the SPS value. When pulse shaping is enabled, the output
Alexander Chemeris9270a5a2017-03-17 13:03:41 -0700904 * vector length is fixed at 625 samples (156.25 symbols at 4 SPS).
Tom Tsoud3253432016-03-06 03:08:01 -0800905 * Pulse shaped bit sequences that go beyond one burst are truncated.
906 * Pulse shaping at anything but 4 SPS is not supported.
907 */
908signalVector *modulateEdgeBurst(const BitVector &bits,
909 int sps, bool empty)
910{
911 signalVector *shape, *burst;
912
913 if ((sps != 4) && !empty)
914 return NULL;
915
916 burst = mapEdgeSymbols(bits);
917 if (!burst)
918 return NULL;
919
920 if (empty)
921 shape = rotateEdgeBurst(*burst, sps);
922 else
923 shape = shapeEdgeBurst(*burst);
924
925 delete burst;
926 return shape;
927}
928
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800929static signalVector *modulateBurstBasic(const BitVector &bits,
930 int guard_len, int sps)
931{
932 int burst_len;
Tom Tsou7278a872017-06-14 14:50:39 -0700933 signalVector *pulse;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800934 signalVector::iterator burst_itr;
935
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400936 if (sps == 1)
937 pulse = GSMPulse1->c0;
938 else
Tom Tsou2079a3c2016-03-06 00:58:56 -0800939 pulse = GSMPulse4->c0;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400940
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800941 burst_len = sps * (bits.size() + guard_len);
942
Tom Tsou7278a872017-06-14 14:50:39 -0700943 signalVector burst(burst_len, pulse->size());
944 burst.isReal(true);
945 burst_itr = burst.begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800946
947 /* Raw bits are not differentially encoded */
948 for (unsigned i = 0; i < bits.size(); i++) {
949 *burst_itr = 2.0 * (bits[i] & 0x01) - 1.0;
950 burst_itr += sps;
951 }
952
Tom Tsou7278a872017-06-14 14:50:39 -0700953 GMSKRotate(burst, sps);
954 burst.isReal(false);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800955
956 /* Single Gaussian pulse approximation shaping */
Tom Tsou7278a872017-06-14 14:50:39 -0700957 return convolve(&burst, pulse, NULL, START_ONLY);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800958}
959
Thomas Tsou3eaae802013-08-20 19:31:14 -0400960/* Assume input bits are not differentially encoded */
Thomas Tsou83e06892013-08-20 16:10:01 -0400961signalVector *modulateBurst(const BitVector &wBurst, int guardPeriodLength,
962 int sps, bool emptyPulse)
dburgessb3a0ca42011-10-12 07:44:40 +0000963{
Thomas Tsou83e06892013-08-20 16:10:01 -0400964 if (emptyPulse)
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800965 return rotateBurst(wBurst, guardPeriodLength, sps);
966 else if (sps == 4)
Tom Tsou4dfd64a2016-03-06 20:31:51 -0800967 return modulateBurstLaurent(wBurst);
Thomas Tsou83e06892013-08-20 16:10:01 -0400968 else
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800969 return modulateBurstBasic(wBurst, guardPeriodLength, sps);
dburgessb3a0ca42011-10-12 07:44:40 +0000970}
971
Tom Tsou2079a3c2016-03-06 00:58:56 -0800972static void generateSincTable()
Thomas Tsou0e0e1f42013-11-09 22:08:51 -0500973{
Thomas Tsou0e0e1f42013-11-09 22:08:51 -0500974 for (int i = 0; i < TABLESIZE; i++) {
Tom Tsoua3dce852017-06-16 17:14:31 -0700975 auto x = (double) i / TABLESIZE * 8 * M_PI;
976 auto y = sin(x) / x;
Tom Tsou35474132017-06-19 16:00:34 -0700977 sincTable[i] = std::isnan(y) ? 1.0 : y;
Thomas Tsou0e0e1f42013-11-09 22:08:51 -0500978 }
979}
980
Tom Tsou70134a02017-06-12 14:23:53 -0700981static float sinc(float x)
dburgessb3a0ca42011-10-12 07:44:40 +0000982{
Thomas Tsou0e0e1f42013-11-09 22:08:51 -0500983 if (fabs(x) >= 8 * M_PI)
984 return 0.0;
985
986 int index = (int) floorf(fabs(x) / (8 * M_PI) * TABLESIZE);
987
988 return sincTable[index];
dburgessb3a0ca42011-10-12 07:44:40 +0000989}
990
Thomas Tsouf79c4d02013-11-09 15:51:56 -0600991/*
992 * Create fractional delay filterbank with Blackman-harris windowed
993 * sinc function generator. The number of filters generated is specified
994 * by the DELAYFILTS value.
995 */
Tom Tsou70134a02017-06-12 14:23:53 -0700996static void generateDelayFilters()
Thomas Tsouf79c4d02013-11-09 15:51:56 -0600997{
998 int h_len = 20;
999 complex *data;
1000 signalVector *h;
1001 signalVector::iterator itr;
1002
1003 float k, sum;
1004 float a0 = 0.35875;
1005 float a1 = 0.48829;
1006 float a2 = 0.14128;
1007 float a3 = 0.01168;
1008
1009 for (int i = 0; i < DELAYFILTS; i++) {
1010 data = (complex *) convolve_h_alloc(h_len);
Pau Espin Pedrolf7331762018-12-03 17:46:04 +01001011 h = new signalVector(data, 0, h_len, convolve_h_alloc, free);
Thomas Tsouf79c4d02013-11-09 15:51:56 -06001012 h->setAligned(true);
1013 h->isReal(true);
1014
1015 sum = 0.0;
1016 itr = h->end();
1017 for (int n = 0; n < h_len; n++) {
1018 k = (float) n;
1019 *--itr = (complex) sinc(M_PI_F *
1020 (k - (float) h_len / 2.0 - (float) i / DELAYFILTS));
1021 *itr *= a0 -
1022 a1 * cos(2 * M_PI * n / (h_len - 1)) +
1023 a2 * cos(4 * M_PI * n / (h_len - 1)) -
1024 a3 * cos(6 * M_PI * n / (h_len - 1));
1025
1026 sum += itr->real();
1027 }
1028
1029 itr = h->begin();
1030 for (int n = 0; n < h_len; n++)
1031 *itr++ /= sum;
1032
1033 delayFilters[i] = h;
1034 }
1035}
1036
Alexander Chemerise0c12182017-03-18 13:27:48 -07001037signalVector *delayVector(const signalVector *in, signalVector *out, float delay)
dburgessb3a0ca42011-10-12 07:44:40 +00001038{
Thomas Tsouf79c4d02013-11-09 15:51:56 -06001039 int whole, index;
Thomas Tsou2c282f52013-10-08 21:34:35 -04001040 float frac;
Thomas Tsou94edaae2013-11-09 22:19:19 -05001041 signalVector *h, *shift, *fshift = NULL;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001042
Thomas Tsou2c282f52013-10-08 21:34:35 -04001043 whole = floor(delay);
1044 frac = delay - whole;
1045
1046 /* Sinc interpolated fractional shift (if allowable) */
1047 if (fabs(frac) > 1e-2) {
Thomas Tsouf79c4d02013-11-09 15:51:56 -06001048 index = floorf(frac * (float) DELAYFILTS);
1049 h = delayFilters[index];
Thomas Tsou2c282f52013-10-08 21:34:35 -04001050
Thomas Tsou94edaae2013-11-09 22:19:19 -05001051 fshift = convolve(in, h, NULL, NO_DELAY);
1052 if (!fshift)
1053 return NULL;
dburgessb3a0ca42011-10-12 07:44:40 +00001054 }
1055
Thomas Tsou94edaae2013-11-09 22:19:19 -05001056 if (!fshift)
1057 shift = new signalVector(*in);
1058 else
1059 shift = fshift;
1060
Thomas Tsou2c282f52013-10-08 21:34:35 -04001061 /* Integer sample shift */
1062 if (whole < 0) {
1063 whole = -whole;
Thomas Tsou94edaae2013-11-09 22:19:19 -05001064 signalVector::iterator wBurstItr = shift->begin();
1065 signalVector::iterator shiftedItr = shift->begin() + whole;
Thomas Tsou2c282f52013-10-08 21:34:35 -04001066
Thomas Tsou94edaae2013-11-09 22:19:19 -05001067 while (shiftedItr < shift->end())
dburgessb3a0ca42011-10-12 07:44:40 +00001068 *wBurstItr++ = *shiftedItr++;
Thomas Tsou2c282f52013-10-08 21:34:35 -04001069
Thomas Tsou94edaae2013-11-09 22:19:19 -05001070 while (wBurstItr < shift->end())
1071 *wBurstItr++ = 0.0;
1072 } else if (whole >= 0) {
1073 signalVector::iterator wBurstItr = shift->end() - 1;
1074 signalVector::iterator shiftedItr = shift->end() - 1 - whole;
1075
1076 while (shiftedItr >= shift->begin())
dburgessb3a0ca42011-10-12 07:44:40 +00001077 *wBurstItr-- = *shiftedItr--;
Thomas Tsou94edaae2013-11-09 22:19:19 -05001078
1079 while (wBurstItr >= shift->begin())
dburgessb3a0ca42011-10-12 07:44:40 +00001080 *wBurstItr-- = 0.0;
1081 }
Thomas Tsou2c282f52013-10-08 21:34:35 -04001082
Thomas Tsou94edaae2013-11-09 22:19:19 -05001083 if (!out)
1084 return shift;
1085
1086 out->clone(*shift);
1087 delete shift;
1088 return out;
dburgessb3a0ca42011-10-12 07:44:40 +00001089}
Thomas Tsou2c282f52013-10-08 21:34:35 -04001090
Tom Tsou70134a02017-06-12 14:23:53 -07001091static complex interpolatePoint(const signalVector &inSig, float ix)
dburgessb3a0ca42011-10-12 07:44:40 +00001092{
dburgessb3a0ca42011-10-12 07:44:40 +00001093 int start = (int) (floor(ix) - 10);
1094 if (start < 0) start = 0;
1095 int end = (int) (floor(ix) + 11);
1096 if ((unsigned) end > inSig.size()-1) end = inSig.size()-1;
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001097
dburgessb3a0ca42011-10-12 07:44:40 +00001098 complex pVal = 0.0;
Thomas Tsou20eb6d62013-11-09 14:30:41 -05001099 if (!inSig.isReal()) {
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001100 for (int i = start; i < end; i++)
dburgessb3a0ca42011-10-12 07:44:40 +00001101 pVal += inSig[i] * sinc(M_PI_F*(i-ix));
1102 }
1103 else {
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001104 for (int i = start; i < end; i++)
dburgessb3a0ca42011-10-12 07:44:40 +00001105 pVal += inSig[i].real() * sinc(M_PI_F*(i-ix));
1106 }
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001107
dburgessb3a0ca42011-10-12 07:44:40 +00001108 return pVal;
1109}
1110
Thomas Tsou8181b012013-08-20 21:17:19 -04001111static complex fastPeakDetect(const signalVector &rxBurst, float *index)
1112{
1113 float val, max = 0.0f;
1114 complex amp;
1115 int _index = -1;
1116
Thomas Tsou3f32ab52013-11-15 16:32:54 -05001117 for (size_t i = 0; i < rxBurst.size(); i++) {
Thomas Tsou8181b012013-08-20 21:17:19 -04001118 val = rxBurst[i].norm2();
1119 if (val > max) {
1120 max = val;
1121 _index = i;
1122 amp = rxBurst[i];
1123 }
1124 }
1125
1126 if (index)
1127 *index = (float) _index;
1128
1129 return amp;
1130}
1131
Tom Tsou70134a02017-06-12 14:23:53 -07001132static complex peakDetect(const signalVector &rxBurst,
1133 float *peakIndex, float *avgPwr)
dburgessb3a0ca42011-10-12 07:44:40 +00001134{
dburgessb3a0ca42011-10-12 07:44:40 +00001135 complex maxVal = 0.0;
1136 float maxIndex = -1;
1137 float sumPower = 0.0;
1138
1139 for (unsigned int i = 0; i < rxBurst.size(); i++) {
1140 float samplePower = rxBurst[i].norm2();
1141 if (samplePower > maxVal.real()) {
1142 maxVal = samplePower;
1143 maxIndex = i;
1144 }
1145 sumPower += samplePower;
1146 }
1147
1148 // interpolate around the peak
1149 // to save computation, we'll use early-late balancing
1150 float earlyIndex = maxIndex-1;
1151 float lateIndex = maxIndex+1;
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001152
dburgessb3a0ca42011-10-12 07:44:40 +00001153 float incr = 0.5;
1154 while (incr > 1.0/1024.0) {
1155 complex earlyP = interpolatePoint(rxBurst,earlyIndex);
1156 complex lateP = interpolatePoint(rxBurst,lateIndex);
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001157 if (earlyP < lateP)
dburgessb3a0ca42011-10-12 07:44:40 +00001158 earlyIndex += incr;
1159 else if (earlyP > lateP)
1160 earlyIndex -= incr;
1161 else break;
1162 incr /= 2.0;
1163 lateIndex = earlyIndex + 2.0;
1164 }
1165
1166 maxIndex = earlyIndex + 1.0;
1167 maxVal = interpolatePoint(rxBurst,maxIndex);
1168
1169 if (peakIndex!=NULL)
1170 *peakIndex = maxIndex;
1171
1172 if (avgPwr!=NULL)
1173 *avgPwr = (sumPower-maxVal.norm2()) / (rxBurst.size()-1);
1174
1175 return maxVal;
1176
1177}
1178
1179void scaleVector(signalVector &x,
1180 complex scale)
1181{
Thomas Tsou7e4e5362013-10-30 21:18:55 -04001182#ifdef HAVE_NEON
1183 int len = x.size();
1184
1185 scale_complex((float *) x.begin(),
1186 (float *) x.begin(),
1187 (float *) &scale, len);
1188#else
dburgessb3a0ca42011-10-12 07:44:40 +00001189 signalVector::iterator xP = x.begin();
1190 signalVector::iterator xPEnd = x.end();
Thomas Tsou20eb6d62013-11-09 14:30:41 -05001191 if (!x.isReal()) {
dburgessb3a0ca42011-10-12 07:44:40 +00001192 while (xP < xPEnd) {
1193 *xP = *xP * scale;
1194 xP++;
1195 }
1196 }
1197 else {
1198 while (xP < xPEnd) {
1199 *xP = xP->real() * scale;
1200 xP++;
1201 }
1202 }
Thomas Tsou7e4e5362013-10-30 21:18:55 -04001203#endif
dburgessb3a0ca42011-10-12 07:44:40 +00001204}
1205
1206/** in-place conjugation */
Tom Tsou70134a02017-06-12 14:23:53 -07001207static void conjugateVector(signalVector &x)
dburgessb3a0ca42011-10-12 07:44:40 +00001208{
Thomas Tsou20eb6d62013-11-09 14:30:41 -05001209 if (x.isReal()) return;
dburgessb3a0ca42011-10-12 07:44:40 +00001210 signalVector::iterator xP = x.begin();
1211 signalVector::iterator xPEnd = x.end();
1212 while (xP < xPEnd) {
1213 *xP = xP->conj();
1214 xP++;
1215 }
1216}
1217
Tom Tsou2079a3c2016-03-06 00:58:56 -08001218static bool generateMidamble(int sps, int tsc)
dburgessb3a0ca42011-10-12 07:44:40 +00001219{
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001220 bool status = true;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001221 float toa;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001222 complex *data = NULL;
1223 signalVector *autocorr = NULL, *midamble = NULL;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001224 signalVector *midMidamble = NULL, *_midMidamble = NULL;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001225
Thomas Tsou3eaae802013-08-20 19:31:14 -04001226 if ((tsc < 0) || (tsc > 7))
dburgessb3a0ca42011-10-12 07:44:40 +00001227 return false;
1228
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001229 delete gMidambles[tsc];
Thomas Tsou3eaae802013-08-20 19:31:14 -04001230
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001231 /* Use middle 16 bits of each TSC. Correlation sequence is not pulse shaped */
1232 midMidamble = modulateBurst(gTrainingSequence[tsc].segment(5,16), 0, sps, true);
1233 if (!midMidamble)
1234 return false;
1235
Thomas Tsou3eaae802013-08-20 19:31:14 -04001236 /* Simulated receive sequence is pulse shaped */
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001237 midamble = modulateBurst(gTrainingSequence[tsc], 0, sps, false);
1238 if (!midamble) {
1239 status = false;
1240 goto release;
dburgessb3a0ca42011-10-12 07:44:40 +00001241 }
Thomas Tsou3eaae802013-08-20 19:31:14 -04001242
dburgessb3a0ca42011-10-12 07:44:40 +00001243 // NOTE: Because ideal TSC 16-bit midamble is 66 symbols into burst,
1244 // the ideal TSC has an + 180 degree phase shift,
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001245 // due to the pi/2 frequency shift, that
dburgessb3a0ca42011-10-12 07:44:40 +00001246 // needs to be accounted for.
1247 // 26-midamble is 61 symbols into burst, has +90 degree phase shift.
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001248 scaleVector(*midMidamble, complex(-1.0, 0.0));
1249 scaleVector(*midamble, complex(0.0, 1.0));
dburgessb3a0ca42011-10-12 07:44:40 +00001250
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001251 conjugateVector(*midMidamble);
dburgessb3a0ca42011-10-12 07:44:40 +00001252
Thomas Tsou3eaae802013-08-20 19:31:14 -04001253 /* For SSE alignment, reallocate the midamble sequence on 16-byte boundary */
1254 data = (complex *) convolve_h_alloc(midMidamble->size());
Pau Espin Pedrolf7331762018-12-03 17:46:04 +01001255 _midMidamble = new signalVector(data, 0, midMidamble->size(), convolve_h_alloc, free);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001256 _midMidamble->setAligned(true);
Pau Espin Pedrol5e68cde2018-08-30 20:45:14 +02001257 midMidamble->copyTo(*_midMidamble);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001258
1259 autocorr = convolve(midamble, _midMidamble, NULL, NO_DELAY);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001260 if (!autocorr) {
1261 status = false;
1262 goto release;
1263 }
dburgessb3a0ca42011-10-12 07:44:40 +00001264
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001265 gMidambles[tsc] = new CorrelationSequence;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001266 gMidambles[tsc]->sequence = _midMidamble;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001267 gMidambles[tsc]->gain = peakDetect(*autocorr, &toa, NULL);
1268
1269 /* For 1 sps only
1270 * (Half of correlation length - 1) + midpoint of pulse shape + remainder
1271 * 13.5 = (16 / 2 - 1) + 1.5 + (26 - 10) / 2
1272 */
1273 if (sps == 1)
1274 gMidambles[tsc]->toa = toa - 13.5;
1275 else
1276 gMidambles[tsc]->toa = 0;
dburgessb3a0ca42011-10-12 07:44:40 +00001277
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001278release:
dburgessb3a0ca42011-10-12 07:44:40 +00001279 delete autocorr;
1280 delete midamble;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001281 delete midMidamble;
dburgessb3a0ca42011-10-12 07:44:40 +00001282
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001283 if (!status) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001284 delete _midMidamble;
1285 free(data);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001286 gMidambles[tsc] = NULL;
1287 }
1288
1289 return status;
dburgessb3a0ca42011-10-12 07:44:40 +00001290}
1291
Tom Tsou70134a02017-06-12 14:23:53 -07001292static CorrelationSequence *generateEdgeMidamble(int tsc)
Tom Tsoud3253432016-03-06 03:08:01 -08001293{
1294 complex *data = NULL;
1295 signalVector *midamble = NULL, *_midamble = NULL;
1296 CorrelationSequence *seq;
1297
1298 if ((tsc < 0) || (tsc > 7))
1299 return NULL;
1300
1301 /* Use middle 48 bits of each TSC. Correlation sequence is not pulse shaped */
1302 const BitVector *bits = &gEdgeTrainingSequence[tsc];
1303 midamble = modulateEdgeBurst(bits->segment(15, 48), 1, true);
1304 if (!midamble)
1305 return NULL;
1306
1307 conjugateVector(*midamble);
1308
1309 data = (complex *) convolve_h_alloc(midamble->size());
Pau Espin Pedrolf7331762018-12-03 17:46:04 +01001310 _midamble = new signalVector(data, 0, midamble->size(), convolve_h_alloc, free);
Tom Tsoud3253432016-03-06 03:08:01 -08001311 _midamble->setAligned(true);
Pau Espin Pedrol5e68cde2018-08-30 20:45:14 +02001312 midamble->copyTo(*_midamble);
Tom Tsoud3253432016-03-06 03:08:01 -08001313
1314 /* Channel gain is an empirically measured value */
1315 seq = new CorrelationSequence;
Tom Tsoud3253432016-03-06 03:08:01 -08001316 seq->sequence = _midamble;
1317 seq->gain = Complex<float>(-19.6432, 19.5006) / 1.18;
1318 seq->toa = 0;
1319
1320 delete midamble;
1321
1322 return seq;
1323}
1324
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001325static bool generateRACHSequence(CorrelationSequence **seq, const BitVector &bv, int sps)
dburgessb3a0ca42011-10-12 07:44:40 +00001326{
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001327 bool status = true;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001328 float toa;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001329 complex *data = NULL;
1330 signalVector *autocorr = NULL;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001331 signalVector *seq0 = NULL, *seq1 = NULL, *_seq1 = NULL;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001332
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001333 if (*seq != NULL)
1334 delete *seq;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001335
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001336 seq0 = modulateBurst(bv, 0, sps, false);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001337 if (!seq0)
1338 return false;
1339
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001340 seq1 = modulateBurst(bv.segment(0, 40), 0, sps, true);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001341 if (!seq1) {
1342 status = false;
1343 goto release;
dburgessb3a0ca42011-10-12 07:44:40 +00001344 }
1345
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001346 conjugateVector(*seq1);
dburgessb3a0ca42011-10-12 07:44:40 +00001347
Thomas Tsou3eaae802013-08-20 19:31:14 -04001348 /* For SSE alignment, reallocate the midamble sequence on 16-byte boundary */
1349 data = (complex *) convolve_h_alloc(seq1->size());
Pau Espin Pedrolf7331762018-12-03 17:46:04 +01001350 _seq1 = new signalVector(data, 0, seq1->size(), convolve_h_alloc, free);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001351 _seq1->setAligned(true);
Pau Espin Pedrol5e68cde2018-08-30 20:45:14 +02001352 seq1->copyTo(*_seq1);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001353
1354 autocorr = convolve(seq0, _seq1, autocorr, NO_DELAY);
1355 if (!autocorr) {
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001356 status = false;
1357 goto release;
1358 }
dburgessb3a0ca42011-10-12 07:44:40 +00001359
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001360 *seq = new CorrelationSequence;
1361 (*seq)->sequence = _seq1;
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001362 (*seq)->gain = peakDetect(*autocorr, &toa, NULL);
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001363
1364 /* For 1 sps only
1365 * (Half of correlation length - 1) + midpoint of pulse shaping filer
1366 * 20.5 = (40 / 2 - 1) + 1.5
1367 */
1368 if (sps == 1)
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001369 (*seq)->toa = toa - 20.5;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001370 else
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001371 (*seq)->toa = 0.0;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001372
1373release:
dburgessb3a0ca42011-10-12 07:44:40 +00001374 delete autocorr;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001375 delete seq0;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001376 delete seq1;
dburgessb3a0ca42011-10-12 07:44:40 +00001377
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001378 if (!status) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001379 delete _seq1;
1380 free(data);
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001381 *seq = NULL;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001382 }
dburgessb3a0ca42011-10-12 07:44:40 +00001383
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001384 return status;
dburgessb3a0ca42011-10-12 07:44:40 +00001385}
Thomas Tsou3eaae802013-08-20 19:31:14 -04001386
Tom Tsoua84e1622016-06-29 14:50:25 -07001387/*
1388 * Peak-to-average computation +/- range from peak in symbols
1389 */
1390#define COMPUTE_PEAK_MIN 2
1391#define COMPUTE_PEAK_MAX 5
1392
1393/*
1394 * Minimum number of values needed to compute peak-to-average
1395 */
1396#define COMPUTE_PEAK_CNT 5
1397
Thomas Tsou865bca42013-08-21 20:58:00 -04001398static float computePeakRatio(signalVector *corr,
1399 int sps, float toa, complex amp)
dburgessb3a0ca42011-10-12 07:44:40 +00001400{
Thomas Tsou865bca42013-08-21 20:58:00 -04001401 int num = 0;
1402 complex *peak;
1403 float rms, avg = 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001404
Thomas Tsou865bca42013-08-21 20:58:00 -04001405 /* Check for bogus results */
1406 if ((toa < 0.0) || (toa > corr->size()))
1407 return 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001408
Alexander Chemeris1e9b4d52015-06-04 19:05:28 -04001409 peak = corr->begin() + (int) rint(toa);
1410
Tom Tsoua84e1622016-06-29 14:50:25 -07001411 for (int i = COMPUTE_PEAK_MIN * sps; i <= COMPUTE_PEAK_MAX * sps; i++) {
Thomas Tsou865bca42013-08-21 20:58:00 -04001412 if (peak - i >= corr->begin()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001413 avg += (peak - i)->norm2();
1414 num++;
1415 }
Thomas Tsou865bca42013-08-21 20:58:00 -04001416 if (peak + i < corr->end()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001417 avg += (peak + i)->norm2();
1418 num++;
1419 }
dburgessb3a0ca42011-10-12 07:44:40 +00001420 }
1421
Tom Tsoua84e1622016-06-29 14:50:25 -07001422 if (num < COMPUTE_PEAK_CNT)
Thomas Tsou865bca42013-08-21 20:58:00 -04001423 return 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001424
Thomas Tsou3eaae802013-08-20 19:31:14 -04001425 rms = sqrtf(avg / (float) num) + 0.00001;
dburgessb3a0ca42011-10-12 07:44:40 +00001426
Thomas Tsou865bca42013-08-21 20:58:00 -04001427 return (amp.abs()) / rms;
dburgessb3a0ca42011-10-12 07:44:40 +00001428}
1429
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001430float energyDetect(const signalVector &rxBurst, unsigned windowLength)
dburgessb3a0ca42011-10-12 07:44:40 +00001431{
1432
1433 signalVector::const_iterator windowItr = rxBurst.begin(); //+rxBurst.size()/2 - 5*windowLength/2;
1434 float energy = 0.0;
Tom Tsou2af14402017-03-23 14:54:00 -07001435 if (windowLength == 0) return 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001436 if (windowLength > rxBurst.size()) windowLength = rxBurst.size();
1437 for (unsigned i = 0; i < windowLength; i++) {
1438 energy += windowItr->norm2();
1439 windowItr+=4;
1440 }
Alexander Chemeris1dd05cf2017-03-15 23:23:36 +03001441 return energy/windowLength;
dburgessb3a0ca42011-10-12 07:44:40 +00001442}
dburgessb3a0ca42011-10-12 07:44:40 +00001443
Tom Tsou70134a02017-06-12 14:23:53 -07001444static signalVector *downsampleBurst(const signalVector &burst)
1445{
Tom Tsou7278a872017-06-14 14:50:39 -07001446 signalVector in(DOWNSAMPLE_IN_LEN, dnsampler->len());
1447 signalVector *out = new signalVector(DOWNSAMPLE_OUT_LEN);
Pau Espin Pedrol5e68cde2018-08-30 20:45:14 +02001448 burst.copyToSegment(in, 0, DOWNSAMPLE_IN_LEN);
Tom Tsou70134a02017-06-12 14:23:53 -07001449
Tom Tsou7278a872017-06-14 14:50:39 -07001450 if (dnsampler->rotate((float *) in.begin(), DOWNSAMPLE_IN_LEN,
Tom Tsou70134a02017-06-12 14:23:53 -07001451 (float *) out->begin(), DOWNSAMPLE_OUT_LEN) < 0) {
1452 delete out;
1453 out = NULL;
1454 }
1455
Tom Tsou70134a02017-06-12 14:23:53 -07001456 return out;
1457};
1458
Thomas Tsou865bca42013-08-21 20:58:00 -04001459/*
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001460 * Computes C/I (Carrier-to-Interference ratio) in dB (deciBels).
1461 * It is computed from the training sequence of each received burst,
1462 * by comparing the "ideal" training sequence with the actual one.
1463 */
Pau Espin Pedrol27bd2f62021-09-01 19:46:39 +02001464static float computeCI(const signalVector *burst, const CorrelationSequence *sync,
Pau Espin Pedrold16eb312021-09-01 19:51:28 +02001465 float toa, int start, const complex &xcorr)
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001466{
Pau Espin Pedrol7f696802021-09-01 20:08:55 +02001467 const int N = sync->sequence->size();
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001468 float S, C;
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001469 /* Integer position where the sequence starts */
Pau Espin Pedrolcdd77a42021-09-01 20:10:04 +02001470 const int ps = start + 1 - N + (int)roundf(toa);
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001471
1472 /* Estimate Signal power */
1473 S = 0.0f;
Pau Espin Pedrol7f696802021-09-01 20:08:55 +02001474 for (int i=0, j=ps; i<(int)N; i++,j++)
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001475 S += (*burst)[j].norm2();
Pau Espin Pedrol7f696802021-09-01 20:08:55 +02001476 S /= N;
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001477
1478 /* Esimate Carrier power */
Pau Espin Pedrol7f696802021-09-01 20:08:55 +02001479 C = xcorr.norm2() / ((N - 1) * sync->gain.abs());
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001480
Pau Espin Pedrol98569412021-09-03 13:48:29 +02001481 /* Interference = Signal - Carrier, so C/I = C / (S - C).
1482 * Calculated in dB:
1483 * C/I_dB = 10 * log10(C/I)
1484 * C/I_dB = 10 * (1/log2(10)) * log2(C/I)
1485 * C/I_dB = 10 * 0.30103 * log2(C/I)
1486 * C/I_dB = 3.0103 * log2(C/I)
1487 */
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001488 return 3.0103f * log2f(C / (S - C));
1489}
1490
1491/*
Thomas Tsou865bca42013-08-21 20:58:00 -04001492 * Detect a burst based on correlation and peak-to-average ratio
1493 *
1494 * For one sampler-per-symbol, perform fast peak detection (no interpolation)
1495 * for initial gating. We do this because energy detection should be disabled.
1496 * For higher oversampling values, we assume the energy detector is in place
1497 * and we run full interpolating peak detection.
1498 */
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001499static int detectBurst(const signalVector &burst,
Pau Espin Pedrol27bd2f62021-09-01 19:46:39 +02001500 signalVector &corr, const CorrelationSequence *sync,
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001501 float thresh, int sps, int start, int len,
1502 struct estim_burst_params *ebp)
dburgessb3a0ca42011-10-12 07:44:40 +00001503{
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001504 const signalVector *corr_in;
1505 signalVector *dec = NULL;
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001506 complex xcorr;
1507 int rc = 1;
Tom Tsoud3253432016-03-06 03:08:01 -08001508
Pau Espin Pedrol8803f922021-09-01 19:42:46 +02001509 switch (sps) {
1510 case 1:
Tom Tsoud3253432016-03-06 03:08:01 -08001511 corr_in = &burst;
Pau Espin Pedrol8803f922021-09-01 19:42:46 +02001512 break;
1513 case 4:
1514 dec = downsampleBurst(burst);
1515 /* Running at the downsampled rate at this point: */
1516 corr_in = dec;
1517 sps = 1;
1518 break;
1519 default:
1520 osmo_panic("%s:%d SPS %d not supported! Only 1 or 4 supported", __FILE__, __LINE__, sps);
Tom Tsoud3253432016-03-06 03:08:01 -08001521 }
1522
Thomas Tsou865bca42013-08-21 20:58:00 -04001523 /* Correlate */
Tom Tsoud3253432016-03-06 03:08:01 -08001524 if (!convolve(corr_in, sync->sequence, &corr,
Sylvain Munauta3934a12018-12-20 19:10:26 +01001525 CUSTOM, start, len)) {
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001526 rc = -1;
1527 goto del_ret;
dburgessb3a0ca42011-10-12 07:44:40 +00001528 }
1529
Thomas Tsou865bca42013-08-21 20:58:00 -04001530 /* Peak detection - place restrictions at correlation edges */
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001531 ebp->amp = fastPeakDetect(corr, &ebp->toa);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001532
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001533 if ((ebp->toa < 3 * sps) || (ebp->toa > len - 3 * sps)) {
1534 rc = 0;
1535 goto del_ret;
1536 }
Thomas Tsou3eaae802013-08-20 19:31:14 -04001537
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001538 /* Peak-to-average ratio */
1539 if (computePeakRatio(&corr, sps, ebp->toa, ebp->amp) < thresh) {
1540 rc = 0;
1541 goto del_ret;
1542 }
Thomas Tsou865bca42013-08-21 20:58:00 -04001543
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001544 /* Refine TOA and correlation value */
1545 xcorr = peakDetect(corr, &ebp->toa, NULL);
1546
1547 /* Compute C/I */
1548 ebp->ci = computeCI(corr_in, sync, ebp->toa, start, xcorr);
Thomas Tsou865bca42013-08-21 20:58:00 -04001549
1550 /* Normalize our channel gain */
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001551 ebp->amp = xcorr / sync->gain;
Thomas Tsou865bca42013-08-21 20:58:00 -04001552
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001553 /* Compensate for residuate time lag */
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001554 ebp->toa = ebp->toa - sync->toa;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001555
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001556del_ret:
1557 delete dec;
1558 return rc;
Thomas Tsou865bca42013-08-21 20:58:00 -04001559}
1560
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001561static float maxAmplitude(const signalVector &burst)
Tom Tsou577cd022015-05-18 13:57:54 -07001562{
Alexander Chemeris954b1182015-06-04 15:39:41 -04001563 float max = 0.0;
1564 for (size_t i = 0; i < burst.size(); i++) {
1565 if (fabs(burst[i].real()) > max)
1566 max = fabs(burst[i].real());
1567 if (fabs(burst[i].imag()) > max)
1568 max = fabs(burst[i].imag());
1569 }
Tom Tsou577cd022015-05-18 13:57:54 -07001570
Alexander Chemeris954b1182015-06-04 15:39:41 -04001571 return max;
Tom Tsou577cd022015-05-18 13:57:54 -07001572}
1573
Alexander Chemeris130a8002015-06-09 20:52:11 -04001574/*
1575 * RACH/Normal burst detection with clipping detection
Thomas Tsou865bca42013-08-21 20:58:00 -04001576 *
1577 * Correlation window parameters:
Alexander Chemeris130a8002015-06-09 20:52:11 -04001578 * target: Tail bits + burst length
1579 * head: Search symbols before target
1580 * tail: Search symbols after target
Thomas Tsou865bca42013-08-21 20:58:00 -04001581 */
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001582static int detectGeneralBurst(const signalVector &rxBurst, float thresh, int sps,
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001583 int target, int head, int tail,
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001584 CorrelationSequence *sync,
1585 struct estim_burst_params *ebp)
Thomas Tsou865bca42013-08-21 20:58:00 -04001586{
Alexander Chemeris130a8002015-06-09 20:52:11 -04001587 int rc, start, len;
Alexander Chemeris954b1182015-06-04 15:39:41 -04001588 bool clipping = false;
Thomas Tsou865bca42013-08-21 20:58:00 -04001589
1590 if ((sps != 1) && (sps != 4))
Tom Tsou577cd022015-05-18 13:57:54 -07001591 return -SIGERR_UNSUPPORTED;
1592
Alexander Chemeris954b1182015-06-04 15:39:41 -04001593 // Detect potential clipping
1594 // We still may be able to demod the burst, so we'll give it a try
1595 // and only report clipping if we can't demod.
1596 float maxAmpl = maxAmplitude(rxBurst);
1597 if (maxAmpl > CLIP_THRESH) {
Pau Espin Pedrole6fdf8f2021-09-01 20:36:22 +02001598 LOG(INFO) << "max burst amplitude: " << maxAmpl << " is above the clipping threshold: " << CLIP_THRESH << std::endl;
Alexander Chemeris954b1182015-06-04 15:39:41 -04001599 clipping = true;
1600 }
Thomas Tsou865bca42013-08-21 20:58:00 -04001601
Tom Tsoud3253432016-03-06 03:08:01 -08001602 start = target - head - 1;
1603 len = head + tail;
Tom Tsou7278a872017-06-14 14:50:39 -07001604 signalVector corr(len);
Thomas Tsou865bca42013-08-21 20:58:00 -04001605
Tom Tsou7278a872017-06-14 14:50:39 -07001606 rc = detectBurst(rxBurst, corr, sync,
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001607 thresh, sps, start, len, ebp);
Thomas Tsou865bca42013-08-21 20:58:00 -04001608 if (rc < 0) {
Tom Tsou577cd022015-05-18 13:57:54 -07001609 return -SIGERR_INTERNAL;
Thomas Tsou865bca42013-08-21 20:58:00 -04001610 } else if (!rc) {
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001611 ebp->amp = 0.0f;
1612 ebp->toa = 0.0f;
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001613 ebp->ci = 0.0f;
Alexander Chemeris954b1182015-06-04 15:39:41 -04001614 return clipping?-SIGERR_CLIP:SIGERR_NONE;
dburgessb3a0ca42011-10-12 07:44:40 +00001615 }
1616
Thomas Tsou865bca42013-08-21 20:58:00 -04001617 /* Subtract forward search bits from delay */
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001618 ebp->toa -= head;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001619
Thomas Tsou865bca42013-08-21 20:58:00 -04001620 return 1;
1621}
Thomas Tsou3eaae802013-08-20 19:31:14 -04001622
Alexander Chemeris130a8002015-06-09 20:52:11 -04001623
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001624/*
Alexander Chemeris130a8002015-06-09 20:52:11 -04001625 * RACH burst detection
1626 *
1627 * Correlation window parameters:
1628 * target: Tail bits + RACH length (reduced from 41 to a multiple of 4)
Tom Tsoue90c24c2016-06-21 16:14:39 -07001629 * head: Search 8 symbols before target
1630 * tail: Search 8 symbols + maximum expected delay
Alexander Chemeris130a8002015-06-09 20:52:11 -04001631 */
Tom Tsou70134a02017-06-12 14:23:53 -07001632static int detectRACHBurst(const signalVector &burst, float threshold, int sps,
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001633 unsigned max_toa, bool ext, struct estim_burst_params *ebp)
Alexander Chemeris130a8002015-06-09 20:52:11 -04001634{
1635 int rc, target, head, tail;
Vadim Yanitskiy444ff342018-10-22 02:25:23 +02001636 int i, num_seq;
Alexander Chemeris130a8002015-06-09 20:52:11 -04001637
1638 target = 8 + 40;
Tom Tsoue90c24c2016-06-21 16:14:39 -07001639 head = 8;
Alexander Chemeris14d13b62017-03-17 15:12:17 -07001640 tail = 8 + max_toa;
Vadim Yanitskiy444ff342018-10-22 02:25:23 +02001641 num_seq = ext ? 3 : 1;
Alexander Chemeris130a8002015-06-09 20:52:11 -04001642
Vadim Yanitskiy444ff342018-10-22 02:25:23 +02001643 for (i = 0; i < num_seq; i++) {
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001644 rc = detectGeneralBurst(burst, threshold, sps, target, head, tail,
1645 gRACHSequences[i], ebp);
Pau Espin Pedrolc3d68c12019-07-04 16:27:47 +02001646 if (rc > 0) {
1647 ebp->tsc = i;
Vadim Yanitskiy444ff342018-10-22 02:25:23 +02001648 break;
Pau Espin Pedrolc3d68c12019-07-04 16:27:47 +02001649 }
Vadim Yanitskiy444ff342018-10-22 02:25:23 +02001650 }
Alexander Chemeris130a8002015-06-09 20:52:11 -04001651
1652 return rc;
1653}
1654
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001655/*
Thomas Tsou865bca42013-08-21 20:58:00 -04001656 * Normal burst detection
1657 *
1658 * Correlation window parameters:
1659 * target: Tail + data + mid-midamble + 1/2 remaining midamblebits
Tom Tsoue90c24c2016-06-21 16:14:39 -07001660 * head: Search 6 symbols before target
1661 * tail: Search 6 symbols + maximum expected delay
Thomas Tsou865bca42013-08-21 20:58:00 -04001662 */
Tom Tsou70134a02017-06-12 14:23:53 -07001663static int analyzeTrafficBurst(const signalVector &burst, unsigned tsc, float threshold,
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001664 int sps, unsigned max_toa, struct estim_burst_params *ebp)
Thomas Tsou865bca42013-08-21 20:58:00 -04001665{
Alexander Chemeris130a8002015-06-09 20:52:11 -04001666 int rc, target, head, tail;
Thomas Tsou865bca42013-08-21 20:58:00 -04001667 CorrelationSequence *sync;
1668
Tom Tsouae91f132017-03-28 14:40:38 -07001669 if (tsc > 7)
Tom Tsou577cd022015-05-18 13:57:54 -07001670 return -SIGERR_UNSUPPORTED;
1671
Thomas Tsou865bca42013-08-21 20:58:00 -04001672 target = 3 + 58 + 16 + 5;
Tom Tsoue90c24c2016-06-21 16:14:39 -07001673 head = 6;
1674 tail = 6 + max_toa;
Thomas Tsou865bca42013-08-21 20:58:00 -04001675 sync = gMidambles[tsc];
Thomas Tsou865bca42013-08-21 20:58:00 -04001676
Pau Espin Pedrolc3d68c12019-07-04 16:27:47 +02001677 ebp->tsc = tsc;
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001678 rc = detectGeneralBurst(burst, threshold, sps, target, head, tail, sync, ebp);
Alexander Chemeris130a8002015-06-09 20:52:11 -04001679 return rc;
dburgessb3a0ca42011-10-12 07:44:40 +00001680}
1681
Tom Tsou70134a02017-06-12 14:23:53 -07001682static int detectEdgeBurst(const signalVector &burst, unsigned tsc, float threshold,
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001683 int sps, unsigned max_toa, struct estim_burst_params *ebp)
Tom Tsoud3253432016-03-06 03:08:01 -08001684{
1685 int rc, target, head, tail;
1686 CorrelationSequence *sync;
1687
Tom Tsouae91f132017-03-28 14:40:38 -07001688 if (tsc > 7)
Tom Tsoud3253432016-03-06 03:08:01 -08001689 return -SIGERR_UNSUPPORTED;
1690
1691 target = 3 + 58 + 16 + 5;
Tom Tsoue90c24c2016-06-21 16:14:39 -07001692 head = 6;
1693 tail = 6 + max_toa;
Tom Tsoud3253432016-03-06 03:08:01 -08001694 sync = gEdgeMidambles[tsc];
1695
Pau Espin Pedrolc3d68c12019-07-04 16:27:47 +02001696 ebp->tsc = tsc;
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001697 rc = detectGeneralBurst(burst, threshold, sps,
1698 target, head, tail, sync, ebp);
Tom Tsoud3253432016-03-06 03:08:01 -08001699 return rc;
1700}
1701
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001702int detectAnyBurst(const signalVector &burst, unsigned tsc, float threshold,
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001703 int sps, CorrType type, unsigned max_toa,
1704 struct estim_burst_params *ebp)
Alexander Chemeris4e6c9382017-03-17 15:24:18 -07001705{
1706 int rc = 0;
1707
1708 switch (type) {
1709 case EDGE:
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001710 rc = detectEdgeBurst(burst, tsc, threshold, sps, max_toa, ebp);
Alexander Chemeris4e6c9382017-03-17 15:24:18 -07001711 if (rc > 0)
1712 break;
1713 else
1714 type = TSC;
1715 case TSC:
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001716 rc = analyzeTrafficBurst(burst, tsc, threshold, sps, max_toa, ebp);
Alexander Chemeris4e6c9382017-03-17 15:24:18 -07001717 break;
Vadim Yanitskiy444ff342018-10-22 02:25:23 +02001718 case EXT_RACH:
Alexander Chemeris4e6c9382017-03-17 15:24:18 -07001719 case RACH:
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001720 rc = detectRACHBurst(burst, threshold, sps, max_toa, type == EXT_RACH, ebp);
Alexander Chemeris4e6c9382017-03-17 15:24:18 -07001721 break;
1722 default:
1723 LOG(ERR) << "Invalid correlation type";
1724 }
1725
1726 if (rc > 0)
1727 return type;
1728
1729 return rc;
1730}
1731
Tom Tsoud3253432016-03-06 03:08:01 -08001732/*
1733 * Soft 8-PSK decoding using Manhattan distance metric
1734 */
1735static SoftVector *softSliceEdgeBurst(signalVector &burst)
1736{
1737 size_t nsyms = 148;
1738
1739 if (burst.size() < nsyms)
1740 return NULL;
1741
1742 signalVector::iterator itr;
1743 SoftVector *bits = new SoftVector(nsyms * 3);
1744
1745 /*
1746 * Bits 0 and 1 - First and second bits of the symbol respectively
1747 */
1748 rotateBurst2(burst, -M_PI / 8.0);
1749 itr = burst.begin();
1750 for (size_t i = 0; i < nsyms; i++) {
1751 (*bits)[3 * i + 0] = -itr->imag();
1752 (*bits)[3 * i + 1] = itr->real();
1753 itr++;
1754 }
1755
1756 /*
1757 * Bit 2 - Collapse symbols into quadrant 0 (positive X and Y).
1758 * Decision area is then simplified to X=Y axis. Rotate again to
1759 * place decision boundary on X-axis.
1760 */
1761 itr = burst.begin();
1762 for (size_t i = 0; i < burst.size(); i++) {
1763 burst[i] = Complex<float>(fabs(itr->real()), fabs(itr->imag()));
1764 itr++;
1765 }
1766
1767 rotateBurst2(burst, -M_PI / 4.0);
1768 itr = burst.begin();
1769 for (size_t i = 0; i < nsyms; i++) {
1770 (*bits)[3 * i + 2] = -itr->imag();
1771 itr++;
1772 }
1773
1774 signalVector soft(bits->size());
1775 for (size_t i = 0; i < bits->size(); i++)
1776 soft[i] = (*bits)[i];
1777
1778 return bits;
1779}
1780
1781/*
Alexander Chemeris132fb242017-03-17 17:22:33 -07001782 * Convert signalVector to SoftVector by taking real part of the signal.
1783 */
1784static SoftVector *signalToSoftVector(signalVector *dec)
1785{
1786 SoftVector *bits = new SoftVector(dec->size());
1787
1788 SoftVector::iterator bit_itr = bits->begin();
1789 signalVector::iterator burst_itr = dec->begin();
1790
1791 for (; burst_itr < dec->end(); burst_itr++)
1792 *bit_itr++ = burst_itr->real();
1793
1794 return bits;
1795}
1796
1797/*
Tom Tsou7fec3032016-03-06 22:33:20 -08001798 * Shared portion of GMSK and EDGE demodulators consisting of timing
1799 * recovery and single tap channel correction. For 4 SPS (if activated),
1800 * the output is downsampled prior to the 1 SPS modulation specific
1801 * stages.
1802 */
Alexander Chemerise0c12182017-03-18 13:27:48 -07001803static signalVector *demodCommon(const signalVector &burst, int sps,
Sylvain Munautad202d72021-02-04 20:37:01 +01001804 const struct estim_burst_params *ebp)
Tom Tsou7fec3032016-03-06 22:33:20 -08001805{
1806 signalVector *delay, *dec;
1807
1808 if ((sps != 1) && (sps != 4))
1809 return NULL;
1810
Sylvain Munautad202d72021-02-04 20:37:01 +01001811 delay = delayVector(&burst, NULL, -ebp->toa * (float) sps);
1812 scaleVector(*delay, (complex) 1.0 / ebp->amp);
Tom Tsou7fec3032016-03-06 22:33:20 -08001813
1814 if (sps == 1)
1815 return delay;
1816
1817 dec = downsampleBurst(*delay);
1818
1819 delete delay;
1820 return dec;
1821}
1822
1823/*
Tom Tsoud3253432016-03-06 03:08:01 -08001824 * Demodulate GSMK burst. Prior to symbol rotation, operate at
1825 * 4 SPS (if activated) to minimize distortion through the fractional
1826 * delay filters. Symbol rotation and after always operates at 1 SPS.
1827 */
Sylvain Munautad202d72021-02-04 20:37:01 +01001828static SoftVector *demodGmskBurst(const signalVector &rxBurst, int sps,
1829 const struct estim_burst_params *ebp)
dburgessb3a0ca42011-10-12 07:44:40 +00001830{
Thomas Tsou94edaae2013-11-09 22:19:19 -05001831 SoftVector *bits;
Tom Tsou7fec3032016-03-06 22:33:20 -08001832 signalVector *dec;
dburgessb3a0ca42011-10-12 07:44:40 +00001833
Sylvain Munautad202d72021-02-04 20:37:01 +01001834 dec = demodCommon(rxBurst, sps, ebp);
Tom Tsou7fec3032016-03-06 22:33:20 -08001835 if (!dec)
1836 return NULL;
dburgessb3a0ca42011-10-12 07:44:40 +00001837
Tom Tsoud3253432016-03-06 03:08:01 -08001838 /* Shift up by a quarter of a frequency */
1839 GMSKReverseRotate(*dec, 1);
Alexander Chemeris132fb242017-03-17 17:22:33 -07001840 /* Take real part of the signal */
1841 bits = signalToSoftVector(dec);
Thomas Tsou94edaae2013-11-09 22:19:19 -05001842 delete dec;
dburgessb3a0ca42011-10-12 07:44:40 +00001843
Thomas Tsou94edaae2013-11-09 22:19:19 -05001844 return bits;
dburgessb3a0ca42011-10-12 07:44:40 +00001845}
Thomas Tsou94edaae2013-11-09 22:19:19 -05001846
Sylvain Munautad202d72021-02-04 20:37:01 +01001847static float computeEdgeCI(const signalVector *rot)
1848{
1849 float err_pwr = 0.0f;
1850 float step = 2.0f * M_PI_F / 8.0f;
1851
1852 for (size_t i = 8; i < rot->size() - 8; i++) {
1853 /* Compute the ideal symbol */
1854 complex sym = (*rot)[i];
1855 float phase = step * roundf(sym.arg() / step);
1856 complex ideal = complex(cos(phase), sin(phase));
1857
1858 /* Compute the error vector */
1859 complex err = ideal - sym;
1860
1861 /* Accumulate power */
1862 err_pwr += err.norm2();
1863 }
1864
1865 return 3.0103f * log2f(1.0f * (rot->size() - 16) / err_pwr);
1866}
1867
Tom Tsoud3253432016-03-06 03:08:01 -08001868/*
1869 * Demodulate an 8-PSK burst. Prior to symbol rotation, operate at
1870 * 4 SPS (if activated) to minimize distortion through the fractional
1871 * delay filters. Symbol rotation and after always operates at 1 SPS.
1872 *
1873 * Allow 1 SPS demodulation here, but note that other parts of the
Pau Espin Pedrole16d0e12021-09-01 20:43:03 +02001874 * transceiver restrict EDGE operation to 4 SPS - 8-PSK distortion
Tom Tsoud3253432016-03-06 03:08:01 -08001875 * through the fractional delay filters at 1 SPS renders signal
1876 * nearly unrecoverable.
1877 */
Sylvain Munautad202d72021-02-04 20:37:01 +01001878static SoftVector *demodEdgeBurst(const signalVector &burst, int sps,
1879 struct estim_burst_params *ebp)
Tom Tsoud3253432016-03-06 03:08:01 -08001880{
1881 SoftVector *bits;
Tom Tsou7fec3032016-03-06 22:33:20 -08001882 signalVector *dec, *rot, *eq;
Tom Tsoud3253432016-03-06 03:08:01 -08001883
Sylvain Munautad202d72021-02-04 20:37:01 +01001884 dec = demodCommon(burst, sps, ebp);
Tom Tsou7fec3032016-03-06 22:33:20 -08001885 if (!dec)
Tom Tsoud3253432016-03-06 03:08:01 -08001886 return NULL;
1887
Tom Tsou7fec3032016-03-06 22:33:20 -08001888 /* Equalize and derotate */
Tom Tsoud3253432016-03-06 03:08:01 -08001889 eq = convolve(dec, GSMPulse4->c0_inv, NULL, NO_DELAY);
1890 rot = derotateEdgeBurst(*eq, 1);
Sylvain Munautad202d72021-02-04 20:37:01 +01001891 ebp->ci = computeEdgeCI(rot);
Tom Tsoud3253432016-03-06 03:08:01 -08001892
Tom Tsou7fec3032016-03-06 22:33:20 -08001893 /* Soft slice and normalize */
Tom Tsou04795622016-04-26 21:17:36 -07001894 bits = softSliceEdgeBurst(*rot);
Tom Tsoud3253432016-03-06 03:08:01 -08001895
1896 delete dec;
1897 delete eq;
1898 delete rot;
1899
1900 return bits;
1901}
1902
Sylvain Munautad202d72021-02-04 20:37:01 +01001903SoftVector *demodAnyBurst(const signalVector &burst, CorrType type,
1904 int sps, struct estim_burst_params *ebp)
Alexander Chemeris6e1dffd2017-03-17 16:13:51 -07001905{
1906 if (type == EDGE)
Sylvain Munautad202d72021-02-04 20:37:01 +01001907 return demodEdgeBurst(burst, sps, ebp);
Alexander Chemeris6e1dffd2017-03-17 16:13:51 -07001908 else
Sylvain Munautad202d72021-02-04 20:37:01 +01001909 return demodGmskBurst(burst, sps, ebp);
Alexander Chemeris6e1dffd2017-03-17 16:13:51 -07001910}
1911
Tom Tsou2079a3c2016-03-06 00:58:56 -08001912bool sigProcLibSetup()
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001913{
Thomas Tsou0e0e1f42013-11-09 22:08:51 -05001914 generateSincTable();
Tom Tsou2079a3c2016-03-06 00:58:56 -08001915 initGMSKRotationTables();
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001916
Tom Tsou2079a3c2016-03-06 00:58:56 -08001917 GSMPulse1 = generateGSMPulse(1);
1918 GSMPulse4 = generateGSMPulse(4);
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001919
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001920 generateRACHSequence(&gRACHSequences[0], gRACHSynchSequenceTS0, 1);
1921 generateRACHSequence(&gRACHSequences[1], gRACHSynchSequenceTS1, 1);
1922 generateRACHSequence(&gRACHSequences[2], gRACHSynchSequenceTS2, 1);
1923
Tom Tsoud3253432016-03-06 03:08:01 -08001924 for (int tsc = 0; tsc < 8; tsc++) {
Tom Tsou2079a3c2016-03-06 00:58:56 -08001925 generateMidamble(1, tsc);
Tom Tsoud3253432016-03-06 03:08:01 -08001926 gEdgeMidambles[tsc] = generateEdgeMidamble(tsc);
1927 }
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001928
Thomas Tsouf79c4d02013-11-09 15:51:56 -06001929 generateDelayFilters();
1930
Tom Tsoud3253432016-03-06 03:08:01 -08001931 dnsampler = new Resampler(1, 4);
1932 if (!dnsampler->init()) {
1933 LOG(ALERT) << "Rx resampler failed to initialize";
1934 goto fail;
1935 }
1936
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001937 return true;
Tom Tsoud3253432016-03-06 03:08:01 -08001938
1939fail:
1940 sigProcLibDestroy();
1941 return false;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001942}