blob: 5fac36582d853c2e24ed33e4eabb0ed63a9bac05 [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};
Eric Wildd8a1dee2024-02-21 19:33:09 +0100132static CorrelationSequence *gSCHSequence = NULL;
133static CorrelationSequence *gDummySequence = NULL;
Tom Tsoud3253432016-03-06 03:08:01 -0800134static PulseSequence *GSMPulse1 = NULL;
135static PulseSequence *GSMPulse4 = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000136
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400137void sigProcLibDestroy()
138{
dburgessb3a0ca42011-10-12 07:44:40 +0000139 for (int i = 0; i < 8; i++) {
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400140 delete gMidambles[i];
Tom Tsoud3253432016-03-06 03:08:01 -0800141 delete gEdgeMidambles[i];
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400142 gMidambles[i] = NULL;
Tom Tsoud3253432016-03-06 03:08:01 -0800143 gEdgeMidambles[i] = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000144 }
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400145
Thomas Tsouf79c4d02013-11-09 15:51:56 -0600146 for (int i = 0; i < DELAYFILTS; i++) {
147 delete delayFilters[i];
148 delayFilters[i] = NULL;
149 }
150
Vadim Yanitskiya79bc702018-10-17 11:01:58 +0200151 for (int i = 0; i < 3; i++) {
152 delete gRACHSequences[i];
153 gRACHSequences[i] = NULL;
154 }
155
Eric Wildd8a1dee2024-02-21 19:33:09 +0100156 delete gSCHSequence;
157 gSCHSequence = NULL;
158
159 delete gDummySequence;
160 gDummySequence = NULL;
161
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400162 delete GMSKRotation1;
163 delete GMSKReverseRotation1;
Tom Tsou2079a3c2016-03-06 00:58:56 -0800164 delete GMSKRotation4;
165 delete GMSKReverseRotation4;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400166 delete GSMPulse1;
Tom Tsou2079a3c2016-03-06 00:58:56 -0800167 delete GSMPulse4;
Tom Tsoud3253432016-03-06 03:08:01 -0800168 delete dnsampler;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400169
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400170 GMSKRotation1 = NULL;
Tom Tsou2079a3c2016-03-06 00:58:56 -0800171 GMSKRotation4 = NULL;
172 GMSKReverseRotation4 = NULL;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400173 GMSKReverseRotation1 = NULL;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400174 GSMPulse1 = NULL;
Tom Tsou2079a3c2016-03-06 00:58:56 -0800175 GSMPulse4 = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000176}
177
Tom Tsou70134a02017-06-12 14:23:53 -0700178static float vectorNorm2(const signalVector &x)
dburgessb3a0ca42011-10-12 07:44:40 +0000179{
180 signalVector::const_iterator xPtr = x.begin();
181 float Energy = 0.0;
182 for (;xPtr != x.end();xPtr++) {
183 Energy += xPtr->norm2();
184 }
185 return Energy;
186}
187
Tom Tsou2079a3c2016-03-06 00:58:56 -0800188/*
189 * Initialize 4 sps and 1 sps rotation tables
190 */
191static void initGMSKRotationTables()
Thomas Tsoud24cc2c2013-08-20 15:41:45 -0400192{
Tom Tsou2079a3c2016-03-06 00:58:56 -0800193 size_t len1 = 157, len4 = 625;
194
195 GMSKRotation4 = new signalVector(len4);
196 GMSKReverseRotation4 = new signalVector(len4);
197 signalVector::iterator rotPtr = GMSKRotation4->begin();
198 signalVector::iterator revPtr = GMSKReverseRotation4->begin();
Tom Tsoubb0c68a2017-06-16 17:08:40 -0700199 auto phase = 0.0;
Tom Tsou2079a3c2016-03-06 00:58:56 -0800200 while (rotPtr != GMSKRotation4->end()) {
Tom Tsoubb0c68a2017-06-16 17:08:40 -0700201 *rotPtr++ = complex(cos(phase), sin(phase));
202 *revPtr++ = complex(cos(-phase), sin(-phase));
203 phase += M_PI / 2.0 / 4.0;
dburgessb3a0ca42011-10-12 07:44:40 +0000204 }
dburgessb3a0ca42011-10-12 07:44:40 +0000205
Tom Tsou2079a3c2016-03-06 00:58:56 -0800206 GMSKRotation1 = new signalVector(len1);
207 GMSKReverseRotation1 = new signalVector(len1);
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400208 rotPtr = GMSKRotation1->begin();
209 revPtr = GMSKReverseRotation1->begin();
210 phase = 0.0;
211 while (rotPtr != GMSKRotation1->end()) {
Tom Tsoubb0c68a2017-06-16 17:08:40 -0700212 *rotPtr++ = complex(cos(phase), sin(phase));
213 *revPtr++ = complex(cos(-phase), sin(-phase));
214 phase += M_PI / 2.0;
Thomas Tsoue57004d2013-08-20 18:55:33 -0400215 }
dburgessb3a0ca42011-10-12 07:44:40 +0000216}
217
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400218static void GMSKRotate(signalVector &x, int sps)
219{
Thomas Tsou0a3dc4c2013-11-09 02:29:55 -0500220#if HAVE_NEON
221 size_t len;
222 signalVector *a, *b, *out;
223
224 a = &x;
225 out = &x;
226 len = out->size();
227
228 if (len == 157)
229 len--;
230
231 if (sps == 1)
232 b = GMSKRotation1;
233 else
Tom Tsou2079a3c2016-03-06 00:58:56 -0800234 b = GMSKRotation4;
Thomas Tsou0a3dc4c2013-11-09 02:29:55 -0500235
236 mul_complex((float *) out->begin(),
237 (float *) a->begin(),
238 (float *) b->begin(), len);
239#else
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400240 signalVector::iterator rotPtr, xPtr = x.begin();
241
242 if (sps == 1)
243 rotPtr = GMSKRotation1->begin();
244 else
Tom Tsou2079a3c2016-03-06 00:58:56 -0800245 rotPtr = GMSKRotation4->begin();
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400246
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500247 if (x.isReal()) {
dburgessb3a0ca42011-10-12 07:44:40 +0000248 while (xPtr < x.end()) {
249 *xPtr = *rotPtr++ * (xPtr->real());
250 xPtr++;
251 }
252 }
253 else {
254 while (xPtr < x.end()) {
255 *xPtr = *rotPtr++ * (*xPtr);
256 xPtr++;
257 }
258 }
Thomas Tsou0a3dc4c2013-11-09 02:29:55 -0500259#endif
dburgessb3a0ca42011-10-12 07:44:40 +0000260}
261
Tom Tsou2079a3c2016-03-06 00:58:56 -0800262static bool GMSKReverseRotate(signalVector &x, int sps)
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400263{
264 signalVector::iterator rotPtr, xPtr= x.begin();
265
266 if (sps == 1)
267 rotPtr = GMSKReverseRotation1->begin();
Tom Tsou2079a3c2016-03-06 00:58:56 -0800268 else if (sps == 4)
269 rotPtr = GMSKReverseRotation4->begin();
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400270 else
Tom Tsou2079a3c2016-03-06 00:58:56 -0800271 return false;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400272
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500273 if (x.isReal()) {
dburgessb3a0ca42011-10-12 07:44:40 +0000274 while (xPtr < x.end()) {
275 *xPtr = *rotPtr++ * (xPtr->real());
276 xPtr++;
277 }
278 }
279 else {
280 while (xPtr < x.end()) {
281 *xPtr = *rotPtr++ * (*xPtr);
282 xPtr++;
283 }
284 }
Tom Tsou2079a3c2016-03-06 00:58:56 -0800285
286 return true;
dburgessb3a0ca42011-10-12 07:44:40 +0000287}
288
Tom Tsou70134a02017-06-12 14:23:53 -0700289/** Convolution type indicator */
290enum ConvType {
291 START_ONLY,
292 NO_DELAY,
293 CUSTOM,
294 UNDEFINED,
295};
296
297static signalVector *convolve(const signalVector *x, const signalVector *h,
298 signalVector *y, ConvType spanType,
Sylvain Munauta3934a12018-12-20 19:10:26 +0100299 size_t start = 0, size_t len = 0)
dburgessb3a0ca42011-10-12 07:44:40 +0000300{
Thomas Tsou3f32ab52013-11-15 16:32:54 -0500301 int rc;
302 size_t head = 0, tail = 0;
Thomas Tsou3eaae802013-08-20 19:31:14 -0400303 bool alloc = false, append = false;
304 const signalVector *_x = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000305
Thomas Tsou3eaae802013-08-20 19:31:14 -0400306 if (!x || !h)
dburgessb3a0ca42011-10-12 07:44:40 +0000307 return NULL;
308
Thomas Tsou3eaae802013-08-20 19:31:14 -0400309 switch (spanType) {
310 case START_ONLY:
311 start = 0;
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500312 head = h->size() - 1;
Thomas Tsou3eaae802013-08-20 19:31:14 -0400313 len = x->size();
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500314
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500315 if (x->getStart() < head)
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500316 append = true;
dburgessb3a0ca42011-10-12 07:44:40 +0000317 break;
Thomas Tsou3eaae802013-08-20 19:31:14 -0400318 case NO_DELAY:
319 start = h->size() / 2;
320 head = start;
321 tail = start;
322 len = x->size();
323 append = true;
324 break;
325 case CUSTOM:
Eric Wildd8a1dee2024-02-21 19:33:09 +0100326 // FIXME: x->getstart?
Thomas Tsou3eaae802013-08-20 19:31:14 -0400327 if (start < h->size() - 1) {
328 head = h->size() - start;
329 append = true;
330 }
331 if (start + len > x->size()) {
332 tail = start + len - x->size();
333 append = true;
dburgessb3a0ca42011-10-12 07:44:40 +0000334 }
335 break;
336 default:
337 return NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000338 }
dburgessb3a0ca42011-10-12 07:44:40 +0000339
Thomas Tsou3eaae802013-08-20 19:31:14 -0400340 /*
341 * Error if the output vector is too small. Create the output vector
342 * if the pointer is NULL.
343 */
344 if (y && (len > y->size()))
345 return NULL;
346 if (!y) {
Pau Espin Pedrolf7331762018-12-03 17:46:04 +0100347 y = new signalVector(len, convolve_h_alloc, free);
Thomas Tsou3eaae802013-08-20 19:31:14 -0400348 alloc = true;
349 }
350
351 /* Prepend or post-pend the input vector if the parameters require it */
352 if (append)
353 _x = new signalVector(*x, head, tail);
354 else
355 _x = x;
356
357 /*
Martin Hauke066fd042019-10-13 19:08:00 +0200358 * Four convolve types:
Thomas Tsou3eaae802013-08-20 19:31:14 -0400359 * 1. Complex-Real (aligned)
360 * 2. Complex-Complex (aligned)
361 * 3. Complex-Real (!aligned)
362 * 4. Complex-Complex (!aligned)
363 */
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500364 if (h->isReal() && h->isAligned()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -0400365 rc = convolve_real((float *) _x->begin(), _x->size(),
366 (float *) h->begin(), h->size(),
367 (float *) y->begin(), y->size(),
Sylvain Munauta3934a12018-12-20 19:10:26 +0100368 start, len);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500369 } else if (!h->isReal() && h->isAligned()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -0400370 rc = convolve_complex((float *) _x->begin(), _x->size(),
371 (float *) h->begin(), h->size(),
372 (float *) y->begin(), y->size(),
Sylvain Munauta3934a12018-12-20 19:10:26 +0100373 start, len);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500374 } else if (h->isReal() && !h->isAligned()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -0400375 rc = base_convolve_real((float *) _x->begin(), _x->size(),
376 (float *) h->begin(), h->size(),
377 (float *) y->begin(), y->size(),
Sylvain Munauta3934a12018-12-20 19:10:26 +0100378 start, len);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500379 } else if (!h->isReal() && !h->isAligned()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -0400380 rc = base_convolve_complex((float *) _x->begin(), _x->size(),
381 (float *) h->begin(), h->size(),
382 (float *) y->begin(), y->size(),
Sylvain Munauta3934a12018-12-20 19:10:26 +0100383 start, len);
Thomas Tsou3eaae802013-08-20 19:31:14 -0400384 } else {
385 rc = -1;
386 }
387
388 if (append)
389 delete _x;
390
391 if (rc < 0) {
392 if (alloc)
393 delete y;
394 return NULL;
395 }
396
397 return y;
398}
dburgessb3a0ca42011-10-12 07:44:40 +0000399
Tom Tsoud3253432016-03-06 03:08:01 -0800400/*
401 * Generate static EDGE linear equalizer. This equalizer is not adaptive.
402 * Filter taps are generated from the inverted 1 SPS impulse response of
403 * the EDGE pulse shape captured after the downsampling filter.
404 */
405static bool generateInvertC0Pulse(PulseSequence *pulse)
406{
407 if (!pulse)
408 return false;
409
Pau Espin Pedrolf7331762018-12-03 17:46:04 +0100410 pulse->c0_inv = new signalVector((complex *) convolve_h_alloc(5), 0, 5, convolve_h_alloc, free);
Tom Tsoud3253432016-03-06 03:08:01 -0800411 pulse->c0_inv->isReal(true);
412 pulse->c0_inv->setAligned(false);
413
414 signalVector::iterator xP = pulse->c0_inv->begin();
415 *xP++ = 0.15884;
416 *xP++ = -0.43176;
417 *xP++ = 1.00000;
418 *xP++ = -0.42608;
419 *xP++ = 0.14882;
420
421 return true;
422}
423
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400424static bool generateC1Pulse(int sps, PulseSequence *pulse)
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800425{
426 int len;
427
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400428 if (!pulse)
429 return false;
430
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800431 switch (sps) {
432 case 4:
433 len = 8;
434 break;
435 default:
436 return false;
437 }
438
Pau Espin Pedrolf7331762018-12-03 17:46:04 +0100439 pulse->c1 = new signalVector((complex *) convolve_h_alloc(len), 0, len, convolve_h_alloc, free);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500440 pulse->c1->isReal(true);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800441
442 /* Enable alignment for SSE usage */
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400443 pulse->c1->setAligned(true);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800444
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400445 signalVector::iterator xP = pulse->c1->begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800446
447 switch (sps) {
448 case 4:
449 /* BT = 0.30 */
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400450 *xP++ = 0.0;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800451 *xP++ = 8.16373112e-03;
452 *xP++ = 2.84385729e-02;
453 *xP++ = 5.64158904e-02;
454 *xP++ = 7.05463553e-02;
455 *xP++ = 5.64158904e-02;
456 *xP++ = 2.84385729e-02;
457 *xP++ = 8.16373112e-03;
458 }
459
460 return true;
461}
462
Tom Tsou2079a3c2016-03-06 00:58:56 -0800463static PulseSequence *generateGSMPulse(int sps)
dburgessb3a0ca42011-10-12 07:44:40 +0000464{
Thomas Tsou83e06892013-08-20 16:10:01 -0400465 int len;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800466 float arg, avg, center;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400467 PulseSequence *pulse;
Thomas Tsou83e06892013-08-20 16:10:01 -0400468
Tom Tsoud3253432016-03-06 03:08:01 -0800469 if ((sps != 1) && (sps != 4))
470 return NULL;
471
Thomas Tsou83e06892013-08-20 16:10:01 -0400472 /* Store a single tap filter used for correlation sequence generation */
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400473 pulse = new PulseSequence();
474 pulse->empty = new signalVector(1);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500475 pulse->empty->isReal(true);
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400476 *(pulse->empty->begin()) = 1.0f;
Thomas Tsou83e06892013-08-20 16:10:01 -0400477
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400478 /*
479 * For 4 samples-per-symbol use a precomputed single pulse Laurent
480 * approximation. This should yields below 2 degrees of phase error at
481 * the modulator output. Use the existing pulse approximation for all
482 * other oversampling factors.
483 */
484 switch (sps) {
485 case 4:
486 len = 16;
487 break;
Tom Tsoud3253432016-03-06 03:08:01 -0800488 case 1:
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400489 default:
Tom Tsou2079a3c2016-03-06 00:58:56 -0800490 len = 4;
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400491 }
Thomas Tsou3eaae802013-08-20 19:31:14 -0400492
Pau Espin Pedrolf7331762018-12-03 17:46:04 +0100493 pulse->c0 = new signalVector((complex *) convolve_h_alloc(len), 0, len, convolve_h_alloc, free);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500494 pulse->c0->isReal(true);
Thomas Tsou3eaae802013-08-20 19:31:14 -0400495
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800496 /* Enable alingnment for SSE usage */
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400497 pulse->c0->setAligned(true);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800498
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400499 signalVector::iterator xP = pulse->c0->begin();
Thomas Tsou83e06892013-08-20 16:10:01 -0400500
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400501 if (sps == 4) {
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800502 *xP++ = 0.0;
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400503 *xP++ = 4.46348606e-03;
504 *xP++ = 2.84385729e-02;
505 *xP++ = 1.03184855e-01;
506 *xP++ = 2.56065552e-01;
507 *xP++ = 4.76375085e-01;
508 *xP++ = 7.05961177e-01;
509 *xP++ = 8.71291644e-01;
510 *xP++ = 9.29453645e-01;
511 *xP++ = 8.71291644e-01;
512 *xP++ = 7.05961177e-01;
513 *xP++ = 4.76375085e-01;
514 *xP++ = 2.56065552e-01;
515 *xP++ = 1.03184855e-01;
516 *xP++ = 2.84385729e-02;
517 *xP++ = 4.46348606e-03;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400518 generateC1Pulse(sps, pulse);
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400519 } else {
520 center = (float) (len - 1.0) / 2.0;
Thomas Tsou83e06892013-08-20 16:10:01 -0400521
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400522 /* GSM pulse approximation */
523 for (int i = 0; i < len; i++) {
524 arg = ((float) i - center) / (float) sps;
525 *xP++ = 0.96 * exp(-1.1380 * arg * arg -
526 0.527 * arg * arg * arg * arg);
527 }
dburgessb3a0ca42011-10-12 07:44:40 +0000528
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400529 avg = sqrtf(vectorNorm2(*pulse->c0) / sps);
530 xP = pulse->c0->begin();
531 for (int i = 0; i < len; i++)
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800532 *xP++ /= avg;
533 }
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400534
Tom Tsoud3253432016-03-06 03:08:01 -0800535 /*
536 * Current form of the EDGE equalization filter non-realizable at 4 SPS.
537 * Load the onto both 1 SPS and 4 SPS objects for convenience. Note that
538 * the EDGE demodulator downsamples to 1 SPS prior to equalization.
539 */
540 generateInvertC0Pulse(pulse);
541
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400542 return pulse;
dburgessb3a0ca42011-10-12 07:44:40 +0000543}
544
Pau Espin Pedrol7dc07b92019-07-01 17:55:01 +0200545/* Convert -1..+1 soft bits to 0..1 soft bits */
546void vectorSlicer(float *dest, const float *src, size_t len)
Tom Tsoud3253432016-03-06 03:08:01 -0800547{
Pau Espin Pedrol7dc07b92019-07-01 17:55:01 +0200548 size_t i;
549 for (i = 0; i < len; i++) {
550 dest[i] = 0.5 * (src[i] + 1.0f);
551 if (dest[i] > 1.0)
552 dest[i] = 1.0;
553 else if (dest[i] < 0.0)
554 dest[i] = 0.0;
555 }
Tom Tsoud3253432016-03-06 03:08:01 -0800556}
557
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800558static signalVector *rotateBurst(const BitVector &wBurst,
559 int guardPeriodLength, int sps)
560{
561 int burst_len;
Tom Tsou7278a872017-06-14 14:50:39 -0700562 signalVector *pulse, rotated;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800563 signalVector::iterator itr;
564
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400565 pulse = GSMPulse1->empty;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800566 burst_len = sps * (wBurst.size() + guardPeriodLength);
567 rotated = signalVector(burst_len);
568 itr = rotated.begin();
569
570 for (unsigned i = 0; i < wBurst.size(); i++) {
571 *itr = 2.0 * (wBurst[i] & 0x01) - 1.0;
572 itr += sps;
573 }
574
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400575 GMSKRotate(rotated, sps);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500576 rotated.isReal(false);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800577
578 /* Dummy filter operation */
Tom Tsou7278a872017-06-14 14:50:39 -0700579 return convolve(&rotated, pulse, NULL, START_ONLY);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800580}
581
Tom Tsoud3253432016-03-06 03:08:01 -0800582static void rotateBurst2(signalVector &burst, double phase)
583{
584 Complex<float> rot = Complex<float>(cos(phase), sin(phase));
585
586 for (size_t i = 0; i < burst.size(); i++)
587 burst[i] = burst[i] * rot;
588}
589
Tom Tsou4dfd64a2016-03-06 20:31:51 -0800590/*
591 * Ignore the guard length argument in the GMSK modulator interface
592 * because it results in 624/628 sized bursts instead of the preferred
593 * burst length of 625. Only 4 SPS is supported.
594 */
595static signalVector *modulateBurstLaurent(const BitVector &bits)
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800596{
Tom Tsou4dfd64a2016-03-06 20:31:51 -0800597 int burst_len, sps = 4;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800598 float phase;
Tom Tsou7278a872017-06-14 14:50:39 -0700599 signalVector *c0_pulse, *c1_pulse, *c0_shaped, *c1_shaped;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800600 signalVector::iterator c0_itr, c1_itr;
601
Tom Tsou2079a3c2016-03-06 00:58:56 -0800602 c0_pulse = GSMPulse4->c0;
603 c1_pulse = GSMPulse4->c1;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800604
Tom Tsou4dfd64a2016-03-06 20:31:51 -0800605 if (bits.size() > 156)
606 return NULL;
607
608 burst_len = 625;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800609
Tom Tsou7278a872017-06-14 14:50:39 -0700610 signalVector c0_burst(burst_len, c0_pulse->size());
611 c0_burst.isReal(true);
612 c0_itr = c0_burst.begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800613
Tom Tsou7278a872017-06-14 14:50:39 -0700614 signalVector c1_burst(burst_len, c1_pulse->size());
615 c1_itr = c1_burst.begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800616
Tom Tsouaa15d622016-08-11 14:36:23 -0700617 /* Padded differential tail bits */
618 *c0_itr = 2.0 * (0x00 & 0x01) - 1.0;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800619 c0_itr += sps;
620
621 /* Main burst bits */
622 for (unsigned i = 0; i < bits.size(); i++) {
623 *c0_itr = 2.0 * (bits[i] & 0x01) - 1.0;
624 c0_itr += sps;
625 }
626
Tom Tsouaa15d622016-08-11 14:36:23 -0700627 /* Padded differential tail bits */
628 *c0_itr = 2.0 * (0x00 & 0x01) - 1.0;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800629
630 /* Generate C0 phase coefficients */
Tom Tsou7278a872017-06-14 14:50:39 -0700631 GMSKRotate(c0_burst, sps);
632 c0_burst.isReal(false);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800633
Tom Tsou7278a872017-06-14 14:50:39 -0700634 c0_itr = c0_burst.begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800635 c0_itr += sps * 2;
636 c1_itr += sps * 2;
637
638 /* Start magic */
639 phase = 2.0 * ((0x01 & 0x01) ^ (0x01 & 0x01)) - 1.0;
640 *c1_itr = *c0_itr * Complex<float>(0, phase);
641 c0_itr += sps;
642 c1_itr += sps;
643
644 /* Generate C1 phase coefficients */
645 for (unsigned i = 2; i < bits.size(); i++) {
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 c0_itr += sps;
650 c1_itr += sps;
651 }
652
653 /* End magic */
654 int i = bits.size();
655 phase = 2.0 * ((bits[i-1] & 0x01) ^ (bits[i-2] & 0x01)) - 1.0;
656 *c1_itr = *c0_itr * Complex<float>(0, phase);
657
658 /* Primary (C0) and secondary (C1) pulse shaping */
Tom Tsou7278a872017-06-14 14:50:39 -0700659 c0_shaped = convolve(&c0_burst, c0_pulse, NULL, START_ONLY);
660 c1_shaped = convolve(&c1_burst, c1_pulse, NULL, START_ONLY);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800661
662 /* Sum shaped outputs into C0 */
663 c0_itr = c0_shaped->begin();
664 c1_itr = c1_shaped->begin();
665 for (unsigned i = 0; i < c0_shaped->size(); i++ )
666 *c0_itr++ += *c1_itr++;
667
668 delete c1_shaped;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800669 return c0_shaped;
670}
671
Tom Tsoud3253432016-03-06 03:08:01 -0800672static signalVector *rotateEdgeBurst(const signalVector &symbols, int sps)
673{
674 signalVector *burst;
675 signalVector::iterator burst_itr;
676
677 burst = new signalVector(symbols.size() * sps);
678 burst_itr = burst->begin();
679
680 for (size_t i = 0; i < symbols.size(); i++) {
681 float phase = i * 3.0f * M_PI / 8.0f;
682 Complex<float> rot = Complex<float>(cos(phase), sin(phase));
683
684 *burst_itr = symbols[i] * rot;
685 burst_itr += sps;
686 }
687
688 return burst;
689}
690
691static signalVector *derotateEdgeBurst(const signalVector &symbols, int sps)
692{
693 signalVector *burst;
694 signalVector::iterator burst_itr;
695
696 if (symbols.size() % sps)
697 return NULL;
698
699 burst = new signalVector(symbols.size() / sps);
700 burst_itr = burst->begin();
701
702 for (size_t i = 0; i < burst->size(); i++) {
703 float phase = (float) (i % 16) * 3.0f * M_PI / 8.0f;
704 Complex<float> rot = Complex<float>(cosf(phase), -sinf(phase));
705
706 *burst_itr = symbols[sps * i] * rot;
707 burst_itr++;
708 }
709
710 return burst;
711}
712
713static signalVector *mapEdgeSymbols(const BitVector &bits)
714{
715 if (bits.size() % 3)
716 return NULL;
717
718 signalVector *symbols = new signalVector(bits.size() / 3);
719
720 for (size_t i = 0; i < symbols->size(); i++) {
721 unsigned index = (((unsigned) bits[3 * i + 0] & 0x01) << 0) |
722 (((unsigned) bits[3 * i + 1] & 0x01) << 1) |
723 (((unsigned) bits[3 * i + 2] & 0x01) << 2);
724
725 (*symbols)[i] = psk8_table[index];
726 }
727
728 return symbols;
729}
730
Tom Tsoud2b07032016-04-26 19:28:59 -0700731/*
732 * EDGE 8-PSK rotate and pulse shape
733 *
734 * Delay the EDGE downlink bursts by one symbol in order to match GMSK pulse
735 * shaping group delay. The difference in group delay arises from the dual
Martin Hauke066fd042019-10-13 19:08:00 +0200736 * pulse filter combination of the GMSK Laurent representation whereas 8-PSK
Tom Tsoud2b07032016-04-26 19:28:59 -0700737 * uses a single pulse linear filter.
738 */
Tom Tsoud3253432016-03-06 03:08:01 -0800739static signalVector *shapeEdgeBurst(const signalVector &symbols)
740{
Tom Tsoud2b07032016-04-26 19:28:59 -0700741 size_t nsyms, nsamps = 625, sps = 4;
Tom Tsoud3253432016-03-06 03:08:01 -0800742 signalVector::iterator burst_itr;
743
744 nsyms = symbols.size();
745
Tom Tsoud2b07032016-04-26 19:28:59 -0700746 if (nsyms * sps > nsamps)
Tom Tsoud3253432016-03-06 03:08:01 -0800747 nsyms = 156;
748
Tom Tsou7278a872017-06-14 14:50:39 -0700749 signalVector burst(nsamps, GSMPulse4->c0->size());
Tom Tsoud3253432016-03-06 03:08:01 -0800750
Tom Tsoud2b07032016-04-26 19:28:59 -0700751 /* Delay burst by 1 symbol */
Tom Tsou7278a872017-06-14 14:50:39 -0700752 burst_itr = burst.begin() + sps;
Tom Tsou06676ea2016-07-19 12:50:21 -0700753 for (size_t i = 0; i < nsyms; i++) {
Tom Tsoud3253432016-03-06 03:08:01 -0800754 float phase = i * 3.0f * M_PI / 8.0f;
755 Complex<float> rot = Complex<float>(cos(phase), sin(phase));
756
757 *burst_itr = symbols[i] * rot;
Tom Tsoud2b07032016-04-26 19:28:59 -0700758 burst_itr += sps;
Tom Tsoud3253432016-03-06 03:08:01 -0800759 }
760
761 /* Single Gaussian pulse approximation shaping */
Tom Tsou7278a872017-06-14 14:50:39 -0700762 return convolve(&burst, GSMPulse4->c0, NULL, START_ONLY);
Tom Tsoud3253432016-03-06 03:08:01 -0800763}
764
765/*
Tom Tsou8ee2f382016-03-06 20:57:34 -0800766 * Generate a random GSM normal burst.
767 */
768signalVector *genRandNormalBurst(int tsc, int sps, int tn)
769{
770 if ((tsc < 0) || (tsc > 7) || (tn < 0) || (tn > 7))
771 return NULL;
772 if ((sps != 1) && (sps != 4))
773 return NULL;
774
775 int i = 0;
Tom Tsou7278a872017-06-14 14:50:39 -0700776 BitVector bits(148);
Tom Tsou8ee2f382016-03-06 20:57:34 -0800777
778 /* Tail bits */
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700779 for (; i < 3; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700780 bits[i] = 0;
Tom Tsou8ee2f382016-03-06 20:57:34 -0800781
782 /* Random bits */
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700783 for (; i < 60; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700784 bits[i] = rand() % 2;
Tom Tsou8ee2f382016-03-06 20:57:34 -0800785
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700786 /* Stealing bit */
Tom Tsou7278a872017-06-14 14:50:39 -0700787 bits[i++] = 0;
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700788
Tom Tsou8ee2f382016-03-06 20:57:34 -0800789 /* Training sequence */
790 for (int n = 0; i < 87; i++, n++)
Tom Tsou7278a872017-06-14 14:50:39 -0700791 bits[i] = gTrainingSequence[tsc][n];
Tom Tsou8ee2f382016-03-06 20:57:34 -0800792
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700793 /* Stealing bit */
Tom Tsou7278a872017-06-14 14:50:39 -0700794 bits[i++] = 0;
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700795
Tom Tsou8ee2f382016-03-06 20:57:34 -0800796 /* Random bits */
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700797 for (; i < 145; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700798 bits[i] = rand() % 2;
Tom Tsou8ee2f382016-03-06 20:57:34 -0800799
800 /* Tail bits */
801 for (; i < 148; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700802 bits[i] = 0;
Tom Tsou8ee2f382016-03-06 20:57:34 -0800803
804 int guard = 8 + !(tn % 4);
Tom Tsou7278a872017-06-14 14:50:39 -0700805 return modulateBurst(bits, guard, sps);
Tom Tsou8ee2f382016-03-06 20:57:34 -0800806}
807
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300808/*
809 * Generate a random GSM access burst.
810 */
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300811signalVector *genRandAccessBurst(int delay, int sps, int tn)
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300812{
813 if ((tn < 0) || (tn > 7))
814 return NULL;
815 if ((sps != 1) && (sps != 4))
816 return NULL;
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300817 if (delay > 68)
818 return NULL;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300819
820 int i = 0;
Tom Tsou7278a872017-06-14 14:50:39 -0700821 BitVector bits(88 + delay);
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300822
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300823 /* delay */
824 for (; i < delay; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700825 bits[i] = 0;
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300826
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300827 /* head and synch bits */
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300828 for (int n = 0; i < 49+delay; i++, n++)
Tom Tsou7278a872017-06-14 14:50:39 -0700829 bits[i] = gRACHBurst[n];
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300830
831 /* Random bits */
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300832 for (; i < 85+delay; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700833 bits[i] = rand() % 2;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300834
835 /* Tail bits */
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300836 for (; i < 88+delay; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700837 bits[i] = 0;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300838
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300839 int guard = 68-delay + !(tn % 4);
Tom Tsou7278a872017-06-14 14:50:39 -0700840 return modulateBurst(bits, guard, sps);
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300841}
842
Tom Tsou8ee2f382016-03-06 20:57:34 -0800843signalVector *generateEmptyBurst(int sps, int tn)
844{
845 if ((tn < 0) || (tn > 7))
846 return NULL;
847
848 if (sps == 4)
849 return new signalVector(625);
850 else if (sps == 1)
851 return new signalVector(148 + 8 + !(tn % 4));
852 else
853 return NULL;
854}
855
856signalVector *generateDummyBurst(int sps, int tn)
857{
858 if (((sps != 1) && (sps != 4)) || (tn < 0) || (tn > 7))
859 return NULL;
860
861 return modulateBurst(gDummyBurst, 8 + !(tn % 4), sps);
862}
863
864/*
Tom Tsoud3253432016-03-06 03:08:01 -0800865 * Generate a random 8-PSK EDGE burst. Only 4 SPS is supported with
866 * the returned burst being 625 samples in length.
867 */
868signalVector *generateEdgeBurst(int tsc)
869{
870 int tail = 9 / 3;
871 int data = 174 / 3;
872 int train = 78 / 3;
873
874 if ((tsc < 0) || (tsc > 7))
875 return NULL;
876
Tom Tsou7278a872017-06-14 14:50:39 -0700877 signalVector burst(148);
Tom Tsoud3253432016-03-06 03:08:01 -0800878 const BitVector *midamble = &gEdgeTrainingSequence[tsc];
879
880 /* Tail */
881 int n, i = 0;
882 for (; i < tail; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700883 burst[i] = psk8_table[7];
Tom Tsoud3253432016-03-06 03:08:01 -0800884
885 /* Body */
886 for (; i < tail + data; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700887 burst[i] = psk8_table[rand() % 8];
Tom Tsoud3253432016-03-06 03:08:01 -0800888
889 /* TSC */
890 for (n = 0; i < tail + data + train; i++, n++) {
891 unsigned index = (((unsigned) (*midamble)[3 * n + 0] & 0x01) << 0) |
892 (((unsigned) (*midamble)[3 * n + 1] & 0x01) << 1) |
893 (((unsigned) (*midamble)[3 * n + 2] & 0x01) << 2);
894
Tom Tsou7278a872017-06-14 14:50:39 -0700895 burst[i] = psk8_table[index];
Tom Tsoud3253432016-03-06 03:08:01 -0800896 }
897
898 /* Body */
899 for (; i < tail + data + train + data; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700900 burst[i] = psk8_table[rand() % 8];
Tom Tsoud3253432016-03-06 03:08:01 -0800901
902 /* Tail */
903 for (; i < tail + data + train + data + tail; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700904 burst[i] = psk8_table[7];
Tom Tsoud3253432016-03-06 03:08:01 -0800905
Tom Tsou7278a872017-06-14 14:50:39 -0700906 return shapeEdgeBurst(burst);
Tom Tsoud3253432016-03-06 03:08:01 -0800907}
908
909/*
910 * Modulate 8-PSK burst. When empty pulse shaping (rotation only)
911 * is enabled, the output vector length will be bit sequence length
912 * times the SPS value. When pulse shaping is enabled, the output
Alexander Chemeris9270a5a2017-03-17 13:03:41 -0700913 * vector length is fixed at 625 samples (156.25 symbols at 4 SPS).
Tom Tsoud3253432016-03-06 03:08:01 -0800914 * Pulse shaped bit sequences that go beyond one burst are truncated.
915 * Pulse shaping at anything but 4 SPS is not supported.
916 */
917signalVector *modulateEdgeBurst(const BitVector &bits,
918 int sps, bool empty)
919{
920 signalVector *shape, *burst;
921
922 if ((sps != 4) && !empty)
923 return NULL;
924
925 burst = mapEdgeSymbols(bits);
926 if (!burst)
927 return NULL;
928
929 if (empty)
930 shape = rotateEdgeBurst(*burst, sps);
931 else
932 shape = shapeEdgeBurst(*burst);
933
934 delete burst;
935 return shape;
936}
937
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800938static signalVector *modulateBurstBasic(const BitVector &bits,
939 int guard_len, int sps)
940{
941 int burst_len;
Tom Tsou7278a872017-06-14 14:50:39 -0700942 signalVector *pulse;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800943 signalVector::iterator burst_itr;
944
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400945 if (sps == 1)
946 pulse = GSMPulse1->c0;
947 else
Tom Tsou2079a3c2016-03-06 00:58:56 -0800948 pulse = GSMPulse4->c0;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400949
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800950 burst_len = sps * (bits.size() + guard_len);
951
Tom Tsou7278a872017-06-14 14:50:39 -0700952 signalVector burst(burst_len, pulse->size());
953 burst.isReal(true);
954 burst_itr = burst.begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800955
956 /* Raw bits are not differentially encoded */
957 for (unsigned i = 0; i < bits.size(); i++) {
958 *burst_itr = 2.0 * (bits[i] & 0x01) - 1.0;
959 burst_itr += sps;
960 }
961
Tom Tsou7278a872017-06-14 14:50:39 -0700962 GMSKRotate(burst, sps);
963 burst.isReal(false);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800964
965 /* Single Gaussian pulse approximation shaping */
Tom Tsou7278a872017-06-14 14:50:39 -0700966 return convolve(&burst, pulse, NULL, START_ONLY);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800967}
968
Thomas Tsou3eaae802013-08-20 19:31:14 -0400969/* Assume input bits are not differentially encoded */
Thomas Tsou83e06892013-08-20 16:10:01 -0400970signalVector *modulateBurst(const BitVector &wBurst, int guardPeriodLength,
971 int sps, bool emptyPulse)
dburgessb3a0ca42011-10-12 07:44:40 +0000972{
Thomas Tsou83e06892013-08-20 16:10:01 -0400973 if (emptyPulse)
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800974 return rotateBurst(wBurst, guardPeriodLength, sps);
975 else if (sps == 4)
Tom Tsou4dfd64a2016-03-06 20:31:51 -0800976 return modulateBurstLaurent(wBurst);
Thomas Tsou83e06892013-08-20 16:10:01 -0400977 else
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800978 return modulateBurstBasic(wBurst, guardPeriodLength, sps);
dburgessb3a0ca42011-10-12 07:44:40 +0000979}
980
Tom Tsou2079a3c2016-03-06 00:58:56 -0800981static void generateSincTable()
Thomas Tsou0e0e1f42013-11-09 22:08:51 -0500982{
Thomas Tsou0e0e1f42013-11-09 22:08:51 -0500983 for (int i = 0; i < TABLESIZE; i++) {
Tom Tsoua3dce852017-06-16 17:14:31 -0700984 auto x = (double) i / TABLESIZE * 8 * M_PI;
985 auto y = sin(x) / x;
Tom Tsou35474132017-06-19 16:00:34 -0700986 sincTable[i] = std::isnan(y) ? 1.0 : y;
Thomas Tsou0e0e1f42013-11-09 22:08:51 -0500987 }
988}
989
Tom Tsou70134a02017-06-12 14:23:53 -0700990static float sinc(float x)
dburgessb3a0ca42011-10-12 07:44:40 +0000991{
Thomas Tsou0e0e1f42013-11-09 22:08:51 -0500992 if (fabs(x) >= 8 * M_PI)
993 return 0.0;
994
995 int index = (int) floorf(fabs(x) / (8 * M_PI) * TABLESIZE);
996
997 return sincTable[index];
dburgessb3a0ca42011-10-12 07:44:40 +0000998}
999
Thomas Tsouf79c4d02013-11-09 15:51:56 -06001000/*
1001 * Create fractional delay filterbank with Blackman-harris windowed
1002 * sinc function generator. The number of filters generated is specified
1003 * by the DELAYFILTS value.
1004 */
Tom Tsou70134a02017-06-12 14:23:53 -07001005static void generateDelayFilters()
Thomas Tsouf79c4d02013-11-09 15:51:56 -06001006{
1007 int h_len = 20;
1008 complex *data;
1009 signalVector *h;
1010 signalVector::iterator itr;
1011
1012 float k, sum;
1013 float a0 = 0.35875;
1014 float a1 = 0.48829;
1015 float a2 = 0.14128;
1016 float a3 = 0.01168;
1017
1018 for (int i = 0; i < DELAYFILTS; i++) {
1019 data = (complex *) convolve_h_alloc(h_len);
Pau Espin Pedrolf7331762018-12-03 17:46:04 +01001020 h = new signalVector(data, 0, h_len, convolve_h_alloc, free);
Thomas Tsouf79c4d02013-11-09 15:51:56 -06001021 h->setAligned(true);
1022 h->isReal(true);
1023
1024 sum = 0.0;
1025 itr = h->end();
1026 for (int n = 0; n < h_len; n++) {
1027 k = (float) n;
1028 *--itr = (complex) sinc(M_PI_F *
1029 (k - (float) h_len / 2.0 - (float) i / DELAYFILTS));
1030 *itr *= a0 -
1031 a1 * cos(2 * M_PI * n / (h_len - 1)) +
1032 a2 * cos(4 * M_PI * n / (h_len - 1)) -
1033 a3 * cos(6 * M_PI * n / (h_len - 1));
1034
1035 sum += itr->real();
1036 }
1037
1038 itr = h->begin();
1039 for (int n = 0; n < h_len; n++)
1040 *itr++ /= sum;
1041
1042 delayFilters[i] = h;
1043 }
1044}
1045
Alexander Chemerise0c12182017-03-18 13:27:48 -07001046signalVector *delayVector(const signalVector *in, signalVector *out, float delay)
dburgessb3a0ca42011-10-12 07:44:40 +00001047{
Thomas Tsouf79c4d02013-11-09 15:51:56 -06001048 int whole, index;
Thomas Tsou2c282f52013-10-08 21:34:35 -04001049 float frac;
Thomas Tsou94edaae2013-11-09 22:19:19 -05001050 signalVector *h, *shift, *fshift = NULL;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001051
Thomas Tsou2c282f52013-10-08 21:34:35 -04001052 whole = floor(delay);
1053 frac = delay - whole;
1054
1055 /* Sinc interpolated fractional shift (if allowable) */
1056 if (fabs(frac) > 1e-2) {
Thomas Tsouf79c4d02013-11-09 15:51:56 -06001057 index = floorf(frac * (float) DELAYFILTS);
1058 h = delayFilters[index];
Thomas Tsou2c282f52013-10-08 21:34:35 -04001059
Thomas Tsou94edaae2013-11-09 22:19:19 -05001060 fshift = convolve(in, h, NULL, NO_DELAY);
1061 if (!fshift)
1062 return NULL;
dburgessb3a0ca42011-10-12 07:44:40 +00001063 }
1064
Thomas Tsou94edaae2013-11-09 22:19:19 -05001065 if (!fshift)
1066 shift = new signalVector(*in);
1067 else
1068 shift = fshift;
1069
Thomas Tsou2c282f52013-10-08 21:34:35 -04001070 /* Integer sample shift */
1071 if (whole < 0) {
1072 whole = -whole;
Thomas Tsou94edaae2013-11-09 22:19:19 -05001073 signalVector::iterator wBurstItr = shift->begin();
1074 signalVector::iterator shiftedItr = shift->begin() + whole;
Thomas Tsou2c282f52013-10-08 21:34:35 -04001075
Thomas Tsou94edaae2013-11-09 22:19:19 -05001076 while (shiftedItr < shift->end())
dburgessb3a0ca42011-10-12 07:44:40 +00001077 *wBurstItr++ = *shiftedItr++;
Thomas Tsou2c282f52013-10-08 21:34:35 -04001078
Thomas Tsou94edaae2013-11-09 22:19:19 -05001079 while (wBurstItr < shift->end())
1080 *wBurstItr++ = 0.0;
1081 } else if (whole >= 0) {
1082 signalVector::iterator wBurstItr = shift->end() - 1;
1083 signalVector::iterator shiftedItr = shift->end() - 1 - whole;
1084
1085 while (shiftedItr >= shift->begin())
dburgessb3a0ca42011-10-12 07:44:40 +00001086 *wBurstItr-- = *shiftedItr--;
Thomas Tsou94edaae2013-11-09 22:19:19 -05001087
1088 while (wBurstItr >= shift->begin())
dburgessb3a0ca42011-10-12 07:44:40 +00001089 *wBurstItr-- = 0.0;
1090 }
Thomas Tsou2c282f52013-10-08 21:34:35 -04001091
Thomas Tsou94edaae2013-11-09 22:19:19 -05001092 if (!out)
1093 return shift;
1094
1095 out->clone(*shift);
1096 delete shift;
1097 return out;
dburgessb3a0ca42011-10-12 07:44:40 +00001098}
Thomas Tsou2c282f52013-10-08 21:34:35 -04001099
Tom Tsou70134a02017-06-12 14:23:53 -07001100static complex interpolatePoint(const signalVector &inSig, float ix)
dburgessb3a0ca42011-10-12 07:44:40 +00001101{
dburgessb3a0ca42011-10-12 07:44:40 +00001102 int start = (int) (floor(ix) - 10);
1103 if (start < 0) start = 0;
1104 int end = (int) (floor(ix) + 11);
1105 if ((unsigned) end > inSig.size()-1) end = inSig.size()-1;
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001106
dburgessb3a0ca42011-10-12 07:44:40 +00001107 complex pVal = 0.0;
Thomas Tsou20eb6d62013-11-09 14:30:41 -05001108 if (!inSig.isReal()) {
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001109 for (int i = start; i < end; i++)
dburgessb3a0ca42011-10-12 07:44:40 +00001110 pVal += inSig[i] * sinc(M_PI_F*(i-ix));
1111 }
1112 else {
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001113 for (int i = start; i < end; i++)
dburgessb3a0ca42011-10-12 07:44:40 +00001114 pVal += inSig[i].real() * sinc(M_PI_F*(i-ix));
1115 }
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001116
dburgessb3a0ca42011-10-12 07:44:40 +00001117 return pVal;
1118}
1119
Thomas Tsou8181b012013-08-20 21:17:19 -04001120static complex fastPeakDetect(const signalVector &rxBurst, float *index)
1121{
1122 float val, max = 0.0f;
1123 complex amp;
1124 int _index = -1;
1125
Thomas Tsou3f32ab52013-11-15 16:32:54 -05001126 for (size_t i = 0; i < rxBurst.size(); i++) {
Thomas Tsou8181b012013-08-20 21:17:19 -04001127 val = rxBurst[i].norm2();
1128 if (val > max) {
1129 max = val;
1130 _index = i;
1131 amp = rxBurst[i];
1132 }
1133 }
1134
1135 if (index)
1136 *index = (float) _index;
1137
1138 return amp;
1139}
1140
Tom Tsou70134a02017-06-12 14:23:53 -07001141static complex peakDetect(const signalVector &rxBurst,
1142 float *peakIndex, float *avgPwr)
dburgessb3a0ca42011-10-12 07:44:40 +00001143{
dburgessb3a0ca42011-10-12 07:44:40 +00001144 complex maxVal = 0.0;
1145 float maxIndex = -1;
1146 float sumPower = 0.0;
1147
1148 for (unsigned int i = 0; i < rxBurst.size(); i++) {
1149 float samplePower = rxBurst[i].norm2();
1150 if (samplePower > maxVal.real()) {
1151 maxVal = samplePower;
1152 maxIndex = i;
1153 }
1154 sumPower += samplePower;
1155 }
1156
1157 // interpolate around the peak
1158 // to save computation, we'll use early-late balancing
1159 float earlyIndex = maxIndex-1;
1160 float lateIndex = maxIndex+1;
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001161
dburgessb3a0ca42011-10-12 07:44:40 +00001162 float incr = 0.5;
1163 while (incr > 1.0/1024.0) {
1164 complex earlyP = interpolatePoint(rxBurst,earlyIndex);
1165 complex lateP = interpolatePoint(rxBurst,lateIndex);
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001166 if (earlyP < lateP)
dburgessb3a0ca42011-10-12 07:44:40 +00001167 earlyIndex += incr;
1168 else if (earlyP > lateP)
1169 earlyIndex -= incr;
1170 else break;
1171 incr /= 2.0;
1172 lateIndex = earlyIndex + 2.0;
1173 }
1174
1175 maxIndex = earlyIndex + 1.0;
1176 maxVal = interpolatePoint(rxBurst,maxIndex);
1177
1178 if (peakIndex!=NULL)
1179 *peakIndex = maxIndex;
1180
1181 if (avgPwr!=NULL)
1182 *avgPwr = (sumPower-maxVal.norm2()) / (rxBurst.size()-1);
1183
1184 return maxVal;
1185
1186}
1187
1188void scaleVector(signalVector &x,
1189 complex scale)
1190{
Thomas Tsou7e4e5362013-10-30 21:18:55 -04001191#ifdef HAVE_NEON
1192 int len = x.size();
1193
1194 scale_complex((float *) x.begin(),
1195 (float *) x.begin(),
1196 (float *) &scale, len);
1197#else
dburgessb3a0ca42011-10-12 07:44:40 +00001198 signalVector::iterator xP = x.begin();
1199 signalVector::iterator xPEnd = x.end();
Thomas Tsou20eb6d62013-11-09 14:30:41 -05001200 if (!x.isReal()) {
dburgessb3a0ca42011-10-12 07:44:40 +00001201 while (xP < xPEnd) {
1202 *xP = *xP * scale;
1203 xP++;
1204 }
1205 }
1206 else {
1207 while (xP < xPEnd) {
1208 *xP = xP->real() * scale;
1209 xP++;
1210 }
1211 }
Thomas Tsou7e4e5362013-10-30 21:18:55 -04001212#endif
dburgessb3a0ca42011-10-12 07:44:40 +00001213}
1214
1215/** in-place conjugation */
Tom Tsou70134a02017-06-12 14:23:53 -07001216static void conjugateVector(signalVector &x)
dburgessb3a0ca42011-10-12 07:44:40 +00001217{
Thomas Tsou20eb6d62013-11-09 14:30:41 -05001218 if (x.isReal()) return;
dburgessb3a0ca42011-10-12 07:44:40 +00001219 signalVector::iterator xP = x.begin();
1220 signalVector::iterator xPEnd = x.end();
1221 while (xP < xPEnd) {
1222 *xP = xP->conj();
1223 xP++;
1224 }
1225}
1226
Tom Tsou2079a3c2016-03-06 00:58:56 -08001227static bool generateMidamble(int sps, int tsc)
dburgessb3a0ca42011-10-12 07:44:40 +00001228{
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001229 bool status = true;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001230 float toa;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001231 complex *data = NULL;
1232 signalVector *autocorr = NULL, *midamble = NULL;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001233 signalVector *midMidamble = NULL, *_midMidamble = NULL;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001234
Thomas Tsou3eaae802013-08-20 19:31:14 -04001235 if ((tsc < 0) || (tsc > 7))
dburgessb3a0ca42011-10-12 07:44:40 +00001236 return false;
1237
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001238 delete gMidambles[tsc];
Thomas Tsou3eaae802013-08-20 19:31:14 -04001239
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001240 /* Use middle 16 bits of each TSC. Correlation sequence is not pulse shaped */
1241 midMidamble = modulateBurst(gTrainingSequence[tsc].segment(5,16), 0, sps, true);
1242 if (!midMidamble)
1243 return false;
1244
Thomas Tsou3eaae802013-08-20 19:31:14 -04001245 /* Simulated receive sequence is pulse shaped */
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001246 midamble = modulateBurst(gTrainingSequence[tsc], 0, sps, false);
1247 if (!midamble) {
1248 status = false;
1249 goto release;
dburgessb3a0ca42011-10-12 07:44:40 +00001250 }
Thomas Tsou3eaae802013-08-20 19:31:14 -04001251
dburgessb3a0ca42011-10-12 07:44:40 +00001252 // NOTE: Because ideal TSC 16-bit midamble is 66 symbols into burst,
1253 // the ideal TSC has an + 180 degree phase shift,
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001254 // due to the pi/2 frequency shift, that
dburgessb3a0ca42011-10-12 07:44:40 +00001255 // needs to be accounted for.
1256 // 26-midamble is 61 symbols into burst, has +90 degree phase shift.
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001257 scaleVector(*midMidamble, complex(-1.0, 0.0));
1258 scaleVector(*midamble, complex(0.0, 1.0));
dburgessb3a0ca42011-10-12 07:44:40 +00001259
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001260 conjugateVector(*midMidamble);
dburgessb3a0ca42011-10-12 07:44:40 +00001261
Thomas Tsou3eaae802013-08-20 19:31:14 -04001262 /* For SSE alignment, reallocate the midamble sequence on 16-byte boundary */
1263 data = (complex *) convolve_h_alloc(midMidamble->size());
Pau Espin Pedrolf7331762018-12-03 17:46:04 +01001264 _midMidamble = new signalVector(data, 0, midMidamble->size(), convolve_h_alloc, free);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001265 _midMidamble->setAligned(true);
Pau Espin Pedrol5e68cde2018-08-30 20:45:14 +02001266 midMidamble->copyTo(*_midMidamble);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001267
1268 autocorr = convolve(midamble, _midMidamble, NULL, NO_DELAY);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001269 if (!autocorr) {
1270 status = false;
1271 goto release;
1272 }
dburgessb3a0ca42011-10-12 07:44:40 +00001273
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001274 gMidambles[tsc] = new CorrelationSequence;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001275 gMidambles[tsc]->sequence = _midMidamble;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001276 gMidambles[tsc]->gain = peakDetect(*autocorr, &toa, NULL);
1277
1278 /* For 1 sps only
1279 * (Half of correlation length - 1) + midpoint of pulse shape + remainder
1280 * 13.5 = (16 / 2 - 1) + 1.5 + (26 - 10) / 2
1281 */
1282 if (sps == 1)
1283 gMidambles[tsc]->toa = toa - 13.5;
1284 else
1285 gMidambles[tsc]->toa = 0;
dburgessb3a0ca42011-10-12 07:44:40 +00001286
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001287release:
dburgessb3a0ca42011-10-12 07:44:40 +00001288 delete autocorr;
1289 delete midamble;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001290 delete midMidamble;
dburgessb3a0ca42011-10-12 07:44:40 +00001291
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001292 if (!status) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001293 delete _midMidamble;
1294 free(data);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001295 gMidambles[tsc] = NULL;
1296 }
1297
1298 return status;
dburgessb3a0ca42011-10-12 07:44:40 +00001299}
1300
Eric Wildd8a1dee2024-02-21 19:33:09 +01001301static bool generateDummyMidamble(int sps)
1302{
1303 bool status = true;
1304 float toa;
1305 complex *data = NULL;
1306 signalVector *autocorr = NULL, *midamble = NULL;
1307 signalVector *midMidamble = NULL, *_midMidamble = NULL;
1308
1309 delete gDummySequence;
1310
1311 /* Use middle 16 bits of each TSC. Correlation sequence is not pulse shaped */
1312 midMidamble = modulateBurst(gDummyBurstTSC.segment(5,16), 0, sps, true);
1313 if (!midMidamble)
1314 return false;
1315
1316 /* Simulated receive sequence is pulse shaped */
1317 midamble = modulateBurst(gDummyBurstTSC, 0, sps, false);
1318 if (!midamble) {
1319 status = false;
1320 goto release;
1321 }
1322
1323 // NOTE: Because ideal TSC 16-bit midamble is 66 symbols into burst,
1324 // the ideal TSC has an + 180 degree phase shift,
1325 // due to the pi/2 frequency shift, that
1326 // needs to be accounted for.
1327 // 26-midamble is 61 symbols into burst, has +90 degree phase shift.
1328 scaleVector(*midMidamble, complex(-1.0, 0.0));
1329 scaleVector(*midamble, complex(0.0, 1.0));
1330
1331 conjugateVector(*midMidamble);
1332
1333 /* For SSE alignment, reallocate the midamble sequence on 16-byte boundary */
1334 data = (complex *) convolve_h_alloc(midMidamble->size());
1335 _midMidamble = new signalVector(data, 0, midMidamble->size(), convolve_h_alloc, free);
1336 _midMidamble->setAligned(true);
1337 midMidamble->copyTo(*_midMidamble);
1338
1339 autocorr = convolve(midamble, _midMidamble, NULL, NO_DELAY);
1340 if (!autocorr) {
1341 status = false;
1342 goto release;
1343 }
1344
1345 gDummySequence = new CorrelationSequence;
1346 gDummySequence->sequence = _midMidamble;
1347 gDummySequence->gain = peakDetect(*autocorr, &toa, NULL);
1348
1349 /* For 1 sps only
1350 * (Half of correlation length - 1) + midpoint of pulse shape + remainder
1351 * 13.5 = (16 / 2 - 1) + 1.5 + (26 - 10) / 2
1352 */
1353 if (sps == 1)
1354 gDummySequence->toa = toa - 13.5;
1355 else
1356 gDummySequence->toa = 0;
1357
1358release:
1359 delete autocorr;
1360 delete midamble;
1361 delete midMidamble;
1362
1363 if (!status) {
1364 delete _midMidamble;
1365 free(data);
1366 gDummySequence = NULL;
1367 }
1368
1369 return status;
1370}
1371
Tom Tsou70134a02017-06-12 14:23:53 -07001372static CorrelationSequence *generateEdgeMidamble(int tsc)
Tom Tsoud3253432016-03-06 03:08:01 -08001373{
1374 complex *data = NULL;
1375 signalVector *midamble = NULL, *_midamble = NULL;
1376 CorrelationSequence *seq;
1377
1378 if ((tsc < 0) || (tsc > 7))
1379 return NULL;
1380
1381 /* Use middle 48 bits of each TSC. Correlation sequence is not pulse shaped */
1382 const BitVector *bits = &gEdgeTrainingSequence[tsc];
1383 midamble = modulateEdgeBurst(bits->segment(15, 48), 1, true);
1384 if (!midamble)
1385 return NULL;
1386
1387 conjugateVector(*midamble);
1388
1389 data = (complex *) convolve_h_alloc(midamble->size());
Pau Espin Pedrolf7331762018-12-03 17:46:04 +01001390 _midamble = new signalVector(data, 0, midamble->size(), convolve_h_alloc, free);
Tom Tsoud3253432016-03-06 03:08:01 -08001391 _midamble->setAligned(true);
Pau Espin Pedrol5e68cde2018-08-30 20:45:14 +02001392 midamble->copyTo(*_midamble);
Tom Tsoud3253432016-03-06 03:08:01 -08001393
1394 /* Channel gain is an empirically measured value */
1395 seq = new CorrelationSequence;
Tom Tsoud3253432016-03-06 03:08:01 -08001396 seq->sequence = _midamble;
1397 seq->gain = Complex<float>(-19.6432, 19.5006) / 1.18;
1398 seq->toa = 0;
1399
1400 delete midamble;
1401
1402 return seq;
1403}
1404
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001405static bool generateRACHSequence(CorrelationSequence **seq, const BitVector &bv, int sps)
dburgessb3a0ca42011-10-12 07:44:40 +00001406{
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001407 bool status = true;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001408 float toa;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001409 complex *data = NULL;
1410 signalVector *autocorr = NULL;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001411 signalVector *seq0 = NULL, *seq1 = NULL, *_seq1 = NULL;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001412
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001413 if (*seq != NULL)
1414 delete *seq;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001415
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001416 seq0 = modulateBurst(bv, 0, sps, false);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001417 if (!seq0)
1418 return false;
1419
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001420 seq1 = modulateBurst(bv.segment(0, 40), 0, sps, true);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001421 if (!seq1) {
1422 status = false;
1423 goto release;
dburgessb3a0ca42011-10-12 07:44:40 +00001424 }
1425
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001426 conjugateVector(*seq1);
dburgessb3a0ca42011-10-12 07:44:40 +00001427
Thomas Tsou3eaae802013-08-20 19:31:14 -04001428 /* For SSE alignment, reallocate the midamble sequence on 16-byte boundary */
1429 data = (complex *) convolve_h_alloc(seq1->size());
Pau Espin Pedrolf7331762018-12-03 17:46:04 +01001430 _seq1 = new signalVector(data, 0, seq1->size(), convolve_h_alloc, free);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001431 _seq1->setAligned(true);
Pau Espin Pedrol5e68cde2018-08-30 20:45:14 +02001432 seq1->copyTo(*_seq1);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001433
1434 autocorr = convolve(seq0, _seq1, autocorr, NO_DELAY);
1435 if (!autocorr) {
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001436 status = false;
1437 goto release;
1438 }
dburgessb3a0ca42011-10-12 07:44:40 +00001439
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001440 *seq = new CorrelationSequence;
1441 (*seq)->sequence = _seq1;
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001442 (*seq)->gain = peakDetect(*autocorr, &toa, NULL);
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001443
1444 /* For 1 sps only
1445 * (Half of correlation length - 1) + midpoint of pulse shaping filer
1446 * 20.5 = (40 / 2 - 1) + 1.5
1447 */
1448 if (sps == 1)
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001449 (*seq)->toa = toa - 20.5;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001450 else
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001451 (*seq)->toa = 0.0;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001452
1453release:
dburgessb3a0ca42011-10-12 07:44:40 +00001454 delete autocorr;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001455 delete seq0;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001456 delete seq1;
dburgessb3a0ca42011-10-12 07:44:40 +00001457
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001458 if (!status) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001459 delete _seq1;
1460 free(data);
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001461 *seq = NULL;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001462 }
dburgessb3a0ca42011-10-12 07:44:40 +00001463
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001464 return status;
dburgessb3a0ca42011-10-12 07:44:40 +00001465}
Thomas Tsou3eaae802013-08-20 19:31:14 -04001466
Eric Wildd8a1dee2024-02-21 19:33:09 +01001467bool generateSCHSequence(int sps)
1468{
1469 bool status = true;
1470 float toa;
1471 complex *data = NULL;
1472 signalVector *autocorr = NULL;
1473 signalVector *seq0 = NULL, *seq1 = NULL, *_seq1 = NULL;
1474
1475 delete gSCHSequence;
1476
1477 seq0 = modulateBurst(gSCHSynchSequence, 0, sps, false);
1478 if (!seq0)
1479 return false;
1480
1481 seq1 = modulateBurst(gSCHSynchSequence, 0, sps, true);
1482 if (!seq1) {
1483 status = false;
1484 goto release;
1485 }
1486
1487 conjugateVector(*seq1);
1488
1489 /* For SSE alignment, reallocate the midamble sequence on 16-byte boundary */
1490 data = (complex *) convolve_h_alloc(seq1->size());
1491 _seq1 = new signalVector(data, 0, seq1->size(), convolve_h_alloc, free);
1492 _seq1->setAligned(true);
1493 seq1->copyTo(*_seq1);
1494
1495 autocorr = convolve(seq0, _seq1, autocorr, NO_DELAY);
1496 if (!autocorr) {
1497 status = false;
1498 goto release;
1499 }
1500
1501 gSCHSequence = new CorrelationSequence;
1502 gSCHSequence->sequence = _seq1;
1503 gSCHSequence->buffer = data;
1504 gSCHSequence->gain = peakDetect(*autocorr, &toa, NULL);
1505
1506 /* For 1 sps only
1507 * (Half of correlation length - 1) + midpoint of pulse shaping filer
1508 * 20.5 = (64 / 2 - 1) + 1.5
1509 */
1510 if (sps == 1)
1511 gSCHSequence->toa = toa - 32.5;
1512 else
1513 gSCHSequence->toa = 0.0;
1514
1515release:
1516 delete autocorr;
1517 delete seq0;
1518 delete seq1;
1519
1520 if (!status) {
1521 delete _seq1;
1522 free(data);
1523 gSCHSequence = NULL;
1524 }
1525
1526 return status;
1527}
1528
1529
Tom Tsoua84e1622016-06-29 14:50:25 -07001530/*
1531 * Peak-to-average computation +/- range from peak in symbols
1532 */
1533#define COMPUTE_PEAK_MIN 2
1534#define COMPUTE_PEAK_MAX 5
1535
1536/*
1537 * Minimum number of values needed to compute peak-to-average
1538 */
1539#define COMPUTE_PEAK_CNT 5
1540
Thomas Tsou865bca42013-08-21 20:58:00 -04001541static float computePeakRatio(signalVector *corr,
1542 int sps, float toa, complex amp)
dburgessb3a0ca42011-10-12 07:44:40 +00001543{
Thomas Tsou865bca42013-08-21 20:58:00 -04001544 int num = 0;
1545 complex *peak;
1546 float rms, avg = 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001547
Thomas Tsou865bca42013-08-21 20:58:00 -04001548 /* Check for bogus results */
1549 if ((toa < 0.0) || (toa > corr->size()))
1550 return 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001551
Alexander Chemeris1e9b4d52015-06-04 19:05:28 -04001552 peak = corr->begin() + (int) rint(toa);
1553
Tom Tsoua84e1622016-06-29 14:50:25 -07001554 for (int i = COMPUTE_PEAK_MIN * sps; i <= COMPUTE_PEAK_MAX * sps; i++) {
Thomas Tsou865bca42013-08-21 20:58:00 -04001555 if (peak - i >= corr->begin()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001556 avg += (peak - i)->norm2();
1557 num++;
1558 }
Thomas Tsou865bca42013-08-21 20:58:00 -04001559 if (peak + i < corr->end()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001560 avg += (peak + i)->norm2();
1561 num++;
1562 }
dburgessb3a0ca42011-10-12 07:44:40 +00001563 }
1564
Tom Tsoua84e1622016-06-29 14:50:25 -07001565 if (num < COMPUTE_PEAK_CNT)
Thomas Tsou865bca42013-08-21 20:58:00 -04001566 return 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001567
Thomas Tsou3eaae802013-08-20 19:31:14 -04001568 rms = sqrtf(avg / (float) num) + 0.00001;
dburgessb3a0ca42011-10-12 07:44:40 +00001569
Thomas Tsou865bca42013-08-21 20:58:00 -04001570 return (amp.abs()) / rms;
dburgessb3a0ca42011-10-12 07:44:40 +00001571}
1572
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001573float energyDetect(const signalVector &rxBurst, unsigned windowLength)
dburgessb3a0ca42011-10-12 07:44:40 +00001574{
1575
1576 signalVector::const_iterator windowItr = rxBurst.begin(); //+rxBurst.size()/2 - 5*windowLength/2;
1577 float energy = 0.0;
Tom Tsou2af14402017-03-23 14:54:00 -07001578 if (windowLength == 0) return 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001579 if (windowLength > rxBurst.size()) windowLength = rxBurst.size();
1580 for (unsigned i = 0; i < windowLength; i++) {
1581 energy += windowItr->norm2();
1582 windowItr+=4;
1583 }
Alexander Chemeris1dd05cf2017-03-15 23:23:36 +03001584 return energy/windowLength;
dburgessb3a0ca42011-10-12 07:44:40 +00001585}
dburgessb3a0ca42011-10-12 07:44:40 +00001586
Eric Wildd8a1dee2024-02-21 19:33:09 +01001587static signalVector *downsampleBurst(const signalVector &burst, int in_len = DOWNSAMPLE_IN_LEN,
1588 int out_len = DOWNSAMPLE_OUT_LEN)
Tom Tsou70134a02017-06-12 14:23:53 -07001589{
Eric Wildd8a1dee2024-02-21 19:33:09 +01001590 signalVector in(in_len, dnsampler->len());
1591 // gSCHSequence->sequence->size(), ensure next conv has no realloc
1592 signalVector *out = new signalVector(out_len, 64);
1593 burst.copyToSegment(in, 0, in_len);
Tom Tsou70134a02017-06-12 14:23:53 -07001594
Eric Wildd8a1dee2024-02-21 19:33:09 +01001595 if (dnsampler->rotate((float *)in.begin(), in_len, (float *)out->begin(), out_len) < 0) {
Tom Tsou70134a02017-06-12 14:23:53 -07001596 delete out;
1597 out = NULL;
1598 }
1599
Tom Tsou70134a02017-06-12 14:23:53 -07001600 return out;
1601};
1602
Thomas Tsou865bca42013-08-21 20:58:00 -04001603/*
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001604 * Computes C/I (Carrier-to-Interference ratio) in dB (deciBels).
1605 * It is computed from the training sequence of each received burst,
1606 * by comparing the "ideal" training sequence with the actual one.
1607 */
Pau Espin Pedrol27bd2f62021-09-01 19:46:39 +02001608static float computeCI(const signalVector *burst, const CorrelationSequence *sync,
Pau Espin Pedrold16eb312021-09-01 19:51:28 +02001609 float toa, int start, const complex &xcorr)
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001610{
Pau Espin Pedrol7f696802021-09-01 20:08:55 +02001611 const int N = sync->sequence->size();
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001612 float S, C;
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001613 /* Integer position where the sequence starts */
Pau Espin Pedrolcdd77a42021-09-01 20:10:04 +02001614 const int ps = start + 1 - N + (int)roundf(toa);
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001615
Eric Wildd8a1dee2024-02-21 19:33:09 +01001616 if(ps < 0) // might be -22 for toa 40 with N=64, if off by a lot during sch ms sync
1617 return 0;
1618
1619 if (ps + N > (int)burst->size())
1620 return 0;
1621
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001622 /* Estimate Signal power */
1623 S = 0.0f;
Pau Espin Pedrol7f696802021-09-01 20:08:55 +02001624 for (int i=0, j=ps; i<(int)N; i++,j++)
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001625 S += (*burst)[j].norm2();
Pau Espin Pedrol7f696802021-09-01 20:08:55 +02001626 S /= N;
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001627
1628 /* Esimate Carrier power */
Pau Espin Pedrol7f696802021-09-01 20:08:55 +02001629 C = xcorr.norm2() / ((N - 1) * sync->gain.abs());
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001630
Pau Espin Pedrol98569412021-09-03 13:48:29 +02001631 /* Interference = Signal - Carrier, so C/I = C / (S - C).
1632 * Calculated in dB:
1633 * C/I_dB = 10 * log10(C/I)
1634 * C/I_dB = 10 * (1/log2(10)) * log2(C/I)
1635 * C/I_dB = 10 * 0.30103 * log2(C/I)
1636 * C/I_dB = 3.0103 * log2(C/I)
1637 */
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001638 return 3.0103f * log2f(C / (S - C));
1639}
1640
1641/*
Thomas Tsou865bca42013-08-21 20:58:00 -04001642 * Detect a burst based on correlation and peak-to-average ratio
1643 *
1644 * For one sampler-per-symbol, perform fast peak detection (no interpolation)
1645 * for initial gating. We do this because energy detection should be disabled.
1646 * For higher oversampling values, we assume the energy detector is in place
1647 * and we run full interpolating peak detection.
1648 */
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001649static int detectBurst(const signalVector &burst,
Pau Espin Pedrol27bd2f62021-09-01 19:46:39 +02001650 signalVector &corr, const CorrelationSequence *sync,
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001651 float thresh, int sps, int start, int len,
1652 struct estim_burst_params *ebp)
dburgessb3a0ca42011-10-12 07:44:40 +00001653{
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001654 const signalVector *corr_in;
1655 signalVector *dec = NULL;
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001656 complex xcorr;
1657 int rc = 1;
Tom Tsoud3253432016-03-06 03:08:01 -08001658
Pau Espin Pedrol8803f922021-09-01 19:42:46 +02001659 switch (sps) {
1660 case 1:
Tom Tsoud3253432016-03-06 03:08:01 -08001661 corr_in = &burst;
Pau Espin Pedrol8803f922021-09-01 19:42:46 +02001662 break;
1663 case 4:
1664 dec = downsampleBurst(burst);
1665 /* Running at the downsampled rate at this point: */
1666 corr_in = dec;
1667 sps = 1;
1668 break;
1669 default:
1670 osmo_panic("%s:%d SPS %d not supported! Only 1 or 4 supported", __FILE__, __LINE__, sps);
Tom Tsoud3253432016-03-06 03:08:01 -08001671 }
1672
Thomas Tsou865bca42013-08-21 20:58:00 -04001673 /* Correlate */
Tom Tsoud3253432016-03-06 03:08:01 -08001674 if (!convolve(corr_in, sync->sequence, &corr,
Sylvain Munauta3934a12018-12-20 19:10:26 +01001675 CUSTOM, start, len)) {
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001676 rc = -1;
1677 goto del_ret;
dburgessb3a0ca42011-10-12 07:44:40 +00001678 }
1679
Thomas Tsou865bca42013-08-21 20:58:00 -04001680 /* Peak detection - place restrictions at correlation edges */
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001681 ebp->amp = fastPeakDetect(corr, &ebp->toa);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001682
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001683 if ((ebp->toa < 3 * sps) || (ebp->toa > len - 3 * sps)) {
1684 rc = 0;
1685 goto del_ret;
1686 }
Thomas Tsou3eaae802013-08-20 19:31:14 -04001687
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001688 /* Peak-to-average ratio */
1689 if (computePeakRatio(&corr, sps, ebp->toa, ebp->amp) < thresh) {
1690 rc = 0;
1691 goto del_ret;
1692 }
Thomas Tsou865bca42013-08-21 20:58:00 -04001693
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001694 /* Refine TOA and correlation value */
1695 xcorr = peakDetect(corr, &ebp->toa, NULL);
1696
1697 /* Compute C/I */
1698 ebp->ci = computeCI(corr_in, sync, ebp->toa, start, xcorr);
Thomas Tsou865bca42013-08-21 20:58:00 -04001699
1700 /* Normalize our channel gain */
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001701 ebp->amp = xcorr / sync->gain;
Thomas Tsou865bca42013-08-21 20:58:00 -04001702
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001703 /* Compensate for residuate time lag */
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001704 ebp->toa = ebp->toa - sync->toa;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001705
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001706del_ret:
1707 delete dec;
1708 return rc;
Thomas Tsou865bca42013-08-21 20:58:00 -04001709}
1710
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001711static float maxAmplitude(const signalVector &burst)
Tom Tsou577cd022015-05-18 13:57:54 -07001712{
Alexander Chemeris954b1182015-06-04 15:39:41 -04001713 float max = 0.0;
1714 for (size_t i = 0; i < burst.size(); i++) {
1715 if (fabs(burst[i].real()) > max)
1716 max = fabs(burst[i].real());
1717 if (fabs(burst[i].imag()) > max)
1718 max = fabs(burst[i].imag());
1719 }
Tom Tsou577cd022015-05-18 13:57:54 -07001720
Alexander Chemeris954b1182015-06-04 15:39:41 -04001721 return max;
Tom Tsou577cd022015-05-18 13:57:54 -07001722}
1723
Alexander Chemeris130a8002015-06-09 20:52:11 -04001724/*
1725 * RACH/Normal burst detection with clipping detection
Thomas Tsou865bca42013-08-21 20:58:00 -04001726 *
1727 * Correlation window parameters:
Alexander Chemeris130a8002015-06-09 20:52:11 -04001728 * target: Tail bits + burst length
1729 * head: Search symbols before target
1730 * tail: Search symbols after target
Thomas Tsou865bca42013-08-21 20:58:00 -04001731 */
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001732static int detectGeneralBurst(const signalVector &rxBurst, float thresh, int sps,
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001733 int target, int head, int tail,
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001734 CorrelationSequence *sync,
1735 struct estim_burst_params *ebp)
Thomas Tsou865bca42013-08-21 20:58:00 -04001736{
Alexander Chemeris130a8002015-06-09 20:52:11 -04001737 int rc, start, len;
Alexander Chemeris954b1182015-06-04 15:39:41 -04001738 bool clipping = false;
Thomas Tsou865bca42013-08-21 20:58:00 -04001739
1740 if ((sps != 1) && (sps != 4))
Tom Tsou577cd022015-05-18 13:57:54 -07001741 return -SIGERR_UNSUPPORTED;
1742
Alexander Chemeris954b1182015-06-04 15:39:41 -04001743 // Detect potential clipping
1744 // We still may be able to demod the burst, so we'll give it a try
1745 // and only report clipping if we can't demod.
1746 float maxAmpl = maxAmplitude(rxBurst);
1747 if (maxAmpl > CLIP_THRESH) {
Pau Espin Pedrole6fdf8f2021-09-01 20:36:22 +02001748 LOG(INFO) << "max burst amplitude: " << maxAmpl << " is above the clipping threshold: " << CLIP_THRESH << std::endl;
Alexander Chemeris954b1182015-06-04 15:39:41 -04001749 clipping = true;
1750 }
Thomas Tsou865bca42013-08-21 20:58:00 -04001751
Tom Tsoud3253432016-03-06 03:08:01 -08001752 start = target - head - 1;
1753 len = head + tail;
Tom Tsou7278a872017-06-14 14:50:39 -07001754 signalVector corr(len);
Thomas Tsou865bca42013-08-21 20:58:00 -04001755
Tom Tsou7278a872017-06-14 14:50:39 -07001756 rc = detectBurst(rxBurst, corr, sync,
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001757 thresh, sps, start, len, ebp);
Thomas Tsou865bca42013-08-21 20:58:00 -04001758 if (rc < 0) {
Tom Tsou577cd022015-05-18 13:57:54 -07001759 return -SIGERR_INTERNAL;
Thomas Tsou865bca42013-08-21 20:58:00 -04001760 } else if (!rc) {
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001761 ebp->amp = 0.0f;
1762 ebp->toa = 0.0f;
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001763 ebp->ci = 0.0f;
Alexander Chemeris954b1182015-06-04 15:39:41 -04001764 return clipping?-SIGERR_CLIP:SIGERR_NONE;
dburgessb3a0ca42011-10-12 07:44:40 +00001765 }
1766
Thomas Tsou865bca42013-08-21 20:58:00 -04001767 /* Subtract forward search bits from delay */
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001768 ebp->toa -= head;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001769
Thomas Tsou865bca42013-08-21 20:58:00 -04001770 return 1;
1771}
Thomas Tsou3eaae802013-08-20 19:31:14 -04001772
Alexander Chemeris130a8002015-06-09 20:52:11 -04001773
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001774/*
Alexander Chemeris130a8002015-06-09 20:52:11 -04001775 * RACH burst detection
1776 *
1777 * Correlation window parameters:
1778 * target: Tail bits + RACH length (reduced from 41 to a multiple of 4)
Tom Tsoue90c24c2016-06-21 16:14:39 -07001779 * head: Search 8 symbols before target
1780 * tail: Search 8 symbols + maximum expected delay
Alexander Chemeris130a8002015-06-09 20:52:11 -04001781 */
Tom Tsou70134a02017-06-12 14:23:53 -07001782static int detectRACHBurst(const signalVector &burst, float threshold, int sps,
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001783 unsigned max_toa, bool ext, struct estim_burst_params *ebp)
Alexander Chemeris130a8002015-06-09 20:52:11 -04001784{
1785 int rc, target, head, tail;
Vadim Yanitskiy444ff342018-10-22 02:25:23 +02001786 int i, num_seq;
Alexander Chemeris130a8002015-06-09 20:52:11 -04001787
1788 target = 8 + 40;
Tom Tsoue90c24c2016-06-21 16:14:39 -07001789 head = 8;
Alexander Chemeris14d13b62017-03-17 15:12:17 -07001790 tail = 8 + max_toa;
Vadim Yanitskiy444ff342018-10-22 02:25:23 +02001791 num_seq = ext ? 3 : 1;
Alexander Chemeris130a8002015-06-09 20:52:11 -04001792
Vadim Yanitskiy444ff342018-10-22 02:25:23 +02001793 for (i = 0; i < num_seq; i++) {
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001794 rc = detectGeneralBurst(burst, threshold, sps, target, head, tail,
1795 gRACHSequences[i], ebp);
Pau Espin Pedrolc3d68c12019-07-04 16:27:47 +02001796 if (rc > 0) {
1797 ebp->tsc = i;
Vadim Yanitskiy444ff342018-10-22 02:25:23 +02001798 break;
Pau Espin Pedrolc3d68c12019-07-04 16:27:47 +02001799 }
Vadim Yanitskiy444ff342018-10-22 02:25:23 +02001800 }
Alexander Chemeris130a8002015-06-09 20:52:11 -04001801
1802 return rc;
1803}
1804
Eric Wildd8a1dee2024-02-21 19:33:09 +01001805int detectSCHBurst(signalVector &burst,
1806 float thresh,
1807 int sps,
1808 sch_detect_type state, struct estim_burst_params *ebp)
1809{
1810 int rc, start, target, head, tail, len;
1811 complex _amp;
1812 CorrelationSequence *sync;
1813
1814 if ((sps != 1) && (sps != 4))
1815 return -1;
1816
1817 target = 3 + 39 + 64;
1818
1819 switch (state) {
1820 case sch_detect_type::SCH_DETECT_NARROW:
1821 head = 4;
1822 tail = 4;
1823 break;
1824 case sch_detect_type::SCH_DETECT_BUFFER:
1825 target = 1;
1826 head = 0;
1827 tail = (12 * 8 * 625) / 4; // 12 frames, downsampled /4 to 1 sps
1828 break;
1829 case sch_detect_type::SCH_DETECT_FULL:
1830 default:
1831 head = target - 1;
1832 tail = 39 + 3 + 9;
1833 break;
1834 }
1835
1836 start = (target - head) * 1 - 1;
1837 len = (head + tail) * 1;
1838 sync = gSCHSequence;
1839 signalVector corr(len);
1840
1841 signalVector *dec = downsampleBurst(burst, len * 4, len);
1842 rc = detectBurst(*dec, corr, sync, thresh, 1, start, len, ebp);
1843 delete dec;
1844
1845 if (rc < 0) {
1846 return -1;
1847 } else if (!rc) {
1848 ebp->amp = 0.0f;
1849 ebp->toa = 0.0f;
1850 return 0;
1851 }
1852
1853 if (state == sch_detect_type::SCH_DETECT_BUFFER)
1854 ebp->toa = ebp->toa - (3 + 39 + 64);
1855 else {
1856 /* Subtract forward search bits from delay */
1857 ebp->toa = ebp->toa - head;
1858 }
1859
1860 return rc;
1861}
1862
1863static int detectDummyBurst(const signalVector &burst, float threshold,
1864 int sps, unsigned max_toa, struct estim_burst_params *ebp)
1865{
1866 int rc, target, head, tail;
1867 CorrelationSequence *sync;
1868
1869 target = 3 + 58 + 16 + 5;
1870 head = 10;
1871 tail = 6 + max_toa;
1872 sync = gDummySequence;
1873
1874 ebp->tsc = 0;
1875 rc = detectGeneralBurst(burst, threshold, sps, target, head, tail, sync, ebp);
1876 return rc;
1877}
1878
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001879/*
Thomas Tsou865bca42013-08-21 20:58:00 -04001880 * Normal burst detection
1881 *
1882 * Correlation window parameters:
1883 * target: Tail + data + mid-midamble + 1/2 remaining midamblebits
Tom Tsoue90c24c2016-06-21 16:14:39 -07001884 * head: Search 6 symbols before target
1885 * tail: Search 6 symbols + maximum expected delay
Thomas Tsou865bca42013-08-21 20:58:00 -04001886 */
Tom Tsou70134a02017-06-12 14:23:53 -07001887static int analyzeTrafficBurst(const signalVector &burst, unsigned tsc, float threshold,
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001888 int sps, unsigned max_toa, struct estim_burst_params *ebp)
Thomas Tsou865bca42013-08-21 20:58:00 -04001889{
Alexander Chemeris130a8002015-06-09 20:52:11 -04001890 int rc, target, head, tail;
Thomas Tsou865bca42013-08-21 20:58:00 -04001891 CorrelationSequence *sync;
1892
Tom Tsouae91f132017-03-28 14:40:38 -07001893 if (tsc > 7)
Tom Tsou577cd022015-05-18 13:57:54 -07001894 return -SIGERR_UNSUPPORTED;
1895
Thomas Tsou865bca42013-08-21 20:58:00 -04001896 target = 3 + 58 + 16 + 5;
Eric Wildd8a1dee2024-02-21 19:33:09 +01001897 head = 10;
Tom Tsoue90c24c2016-06-21 16:14:39 -07001898 tail = 6 + max_toa;
Thomas Tsou865bca42013-08-21 20:58:00 -04001899 sync = gMidambles[tsc];
Thomas Tsou865bca42013-08-21 20:58:00 -04001900
Pau Espin Pedrolc3d68c12019-07-04 16:27:47 +02001901 ebp->tsc = tsc;
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001902 rc = detectGeneralBurst(burst, threshold, sps, target, head, tail, sync, ebp);
Alexander Chemeris130a8002015-06-09 20:52:11 -04001903 return rc;
dburgessb3a0ca42011-10-12 07:44:40 +00001904}
1905
Tom Tsou70134a02017-06-12 14:23:53 -07001906static int detectEdgeBurst(const signalVector &burst, unsigned tsc, float threshold,
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001907 int sps, unsigned max_toa, struct estim_burst_params *ebp)
Tom Tsoud3253432016-03-06 03:08:01 -08001908{
1909 int rc, target, head, tail;
1910 CorrelationSequence *sync;
1911
Tom Tsouae91f132017-03-28 14:40:38 -07001912 if (tsc > 7)
Tom Tsoud3253432016-03-06 03:08:01 -08001913 return -SIGERR_UNSUPPORTED;
1914
1915 target = 3 + 58 + 16 + 5;
Tom Tsoue90c24c2016-06-21 16:14:39 -07001916 head = 6;
1917 tail = 6 + max_toa;
Tom Tsoud3253432016-03-06 03:08:01 -08001918 sync = gEdgeMidambles[tsc];
1919
Pau Espin Pedrolc3d68c12019-07-04 16:27:47 +02001920 ebp->tsc = tsc;
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001921 rc = detectGeneralBurst(burst, threshold, sps,
1922 target, head, tail, sync, ebp);
Tom Tsoud3253432016-03-06 03:08:01 -08001923 return rc;
1924}
1925
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001926int detectAnyBurst(const signalVector &burst, unsigned tsc, float threshold,
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001927 int sps, CorrType type, unsigned max_toa,
1928 struct estim_burst_params *ebp)
Alexander Chemeris4e6c9382017-03-17 15:24:18 -07001929{
1930 int rc = 0;
1931
1932 switch (type) {
1933 case EDGE:
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001934 rc = detectEdgeBurst(burst, tsc, threshold, sps, max_toa, ebp);
Alexander Chemeris4e6c9382017-03-17 15:24:18 -07001935 if (rc > 0)
1936 break;
1937 else
1938 type = TSC;
1939 case TSC:
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001940 rc = analyzeTrafficBurst(burst, tsc, threshold, sps, max_toa, ebp);
Alexander Chemeris4e6c9382017-03-17 15:24:18 -07001941 break;
Vadim Yanitskiy444ff342018-10-22 02:25:23 +02001942 case EXT_RACH:
Alexander Chemeris4e6c9382017-03-17 15:24:18 -07001943 case RACH:
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001944 rc = detectRACHBurst(burst, threshold, sps, max_toa, type == EXT_RACH, ebp);
Alexander Chemeris4e6c9382017-03-17 15:24:18 -07001945 break;
Eric Wildd8a1dee2024-02-21 19:33:09 +01001946 case IDLE:
1947 rc = detectDummyBurst(burst, threshold, sps, max_toa, ebp);
1948 break;
Alexander Chemeris4e6c9382017-03-17 15:24:18 -07001949 default:
1950 LOG(ERR) << "Invalid correlation type";
1951 }
1952
1953 if (rc > 0)
1954 return type;
1955
1956 return rc;
1957}
1958
Tom Tsoud3253432016-03-06 03:08:01 -08001959/*
1960 * Soft 8-PSK decoding using Manhattan distance metric
1961 */
1962static SoftVector *softSliceEdgeBurst(signalVector &burst)
1963{
1964 size_t nsyms = 148;
1965
1966 if (burst.size() < nsyms)
1967 return NULL;
1968
1969 signalVector::iterator itr;
1970 SoftVector *bits = new SoftVector(nsyms * 3);
1971
1972 /*
1973 * Bits 0 and 1 - First and second bits of the symbol respectively
1974 */
1975 rotateBurst2(burst, -M_PI / 8.0);
1976 itr = burst.begin();
1977 for (size_t i = 0; i < nsyms; i++) {
1978 (*bits)[3 * i + 0] = -itr->imag();
1979 (*bits)[3 * i + 1] = itr->real();
1980 itr++;
1981 }
1982
1983 /*
1984 * Bit 2 - Collapse symbols into quadrant 0 (positive X and Y).
1985 * Decision area is then simplified to X=Y axis. Rotate again to
1986 * place decision boundary on X-axis.
1987 */
1988 itr = burst.begin();
1989 for (size_t i = 0; i < burst.size(); i++) {
1990 burst[i] = Complex<float>(fabs(itr->real()), fabs(itr->imag()));
1991 itr++;
1992 }
1993
1994 rotateBurst2(burst, -M_PI / 4.0);
1995 itr = burst.begin();
1996 for (size_t i = 0; i < nsyms; i++) {
1997 (*bits)[3 * i + 2] = -itr->imag();
1998 itr++;
1999 }
2000
2001 signalVector soft(bits->size());
2002 for (size_t i = 0; i < bits->size(); i++)
2003 soft[i] = (*bits)[i];
2004
2005 return bits;
2006}
2007
2008/*
Alexander Chemeris132fb242017-03-17 17:22:33 -07002009 * Convert signalVector to SoftVector by taking real part of the signal.
2010 */
2011static SoftVector *signalToSoftVector(signalVector *dec)
2012{
2013 SoftVector *bits = new SoftVector(dec->size());
2014
2015 SoftVector::iterator bit_itr = bits->begin();
2016 signalVector::iterator burst_itr = dec->begin();
2017
2018 for (; burst_itr < dec->end(); burst_itr++)
2019 *bit_itr++ = burst_itr->real();
2020
2021 return bits;
2022}
2023
2024/*
Tom Tsou7fec3032016-03-06 22:33:20 -08002025 * Shared portion of GMSK and EDGE demodulators consisting of timing
2026 * recovery and single tap channel correction. For 4 SPS (if activated),
2027 * the output is downsampled prior to the 1 SPS modulation specific
2028 * stages.
2029 */
Alexander Chemerise0c12182017-03-18 13:27:48 -07002030static signalVector *demodCommon(const signalVector &burst, int sps,
Sylvain Munautad202d72021-02-04 20:37:01 +01002031 const struct estim_burst_params *ebp)
Tom Tsou7fec3032016-03-06 22:33:20 -08002032{
2033 signalVector *delay, *dec;
2034
2035 if ((sps != 1) && (sps != 4))
2036 return NULL;
2037
Sylvain Munautad202d72021-02-04 20:37:01 +01002038 delay = delayVector(&burst, NULL, -ebp->toa * (float) sps);
2039 scaleVector(*delay, (complex) 1.0 / ebp->amp);
Tom Tsou7fec3032016-03-06 22:33:20 -08002040
2041 if (sps == 1)
2042 return delay;
2043
2044 dec = downsampleBurst(*delay);
2045
2046 delete delay;
2047 return dec;
2048}
2049
2050/*
Tom Tsoud3253432016-03-06 03:08:01 -08002051 * Demodulate GSMK burst. Prior to symbol rotation, operate at
2052 * 4 SPS (if activated) to minimize distortion through the fractional
2053 * delay filters. Symbol rotation and after always operates at 1 SPS.
2054 */
Sylvain Munautad202d72021-02-04 20:37:01 +01002055static SoftVector *demodGmskBurst(const signalVector &rxBurst, int sps,
2056 const struct estim_burst_params *ebp)
dburgessb3a0ca42011-10-12 07:44:40 +00002057{
Thomas Tsou94edaae2013-11-09 22:19:19 -05002058 SoftVector *bits;
Tom Tsou7fec3032016-03-06 22:33:20 -08002059 signalVector *dec;
dburgessb3a0ca42011-10-12 07:44:40 +00002060
Sylvain Munautad202d72021-02-04 20:37:01 +01002061 dec = demodCommon(rxBurst, sps, ebp);
Tom Tsou7fec3032016-03-06 22:33:20 -08002062 if (!dec)
2063 return NULL;
dburgessb3a0ca42011-10-12 07:44:40 +00002064
Tom Tsoud3253432016-03-06 03:08:01 -08002065 /* Shift up by a quarter of a frequency */
2066 GMSKReverseRotate(*dec, 1);
Alexander Chemeris132fb242017-03-17 17:22:33 -07002067 /* Take real part of the signal */
2068 bits = signalToSoftVector(dec);
Thomas Tsou94edaae2013-11-09 22:19:19 -05002069 delete dec;
dburgessb3a0ca42011-10-12 07:44:40 +00002070
Thomas Tsou94edaae2013-11-09 22:19:19 -05002071 return bits;
dburgessb3a0ca42011-10-12 07:44:40 +00002072}
Thomas Tsou94edaae2013-11-09 22:19:19 -05002073
Sylvain Munautad202d72021-02-04 20:37:01 +01002074static float computeEdgeCI(const signalVector *rot)
2075{
2076 float err_pwr = 0.0f;
2077 float step = 2.0f * M_PI_F / 8.0f;
2078
2079 for (size_t i = 8; i < rot->size() - 8; i++) {
2080 /* Compute the ideal symbol */
2081 complex sym = (*rot)[i];
2082 float phase = step * roundf(sym.arg() / step);
2083 complex ideal = complex(cos(phase), sin(phase));
2084
2085 /* Compute the error vector */
2086 complex err = ideal - sym;
2087
2088 /* Accumulate power */
2089 err_pwr += err.norm2();
2090 }
2091
2092 return 3.0103f * log2f(1.0f * (rot->size() - 16) / err_pwr);
2093}
2094
Tom Tsoud3253432016-03-06 03:08:01 -08002095/*
2096 * Demodulate an 8-PSK burst. Prior to symbol rotation, operate at
2097 * 4 SPS (if activated) to minimize distortion through the fractional
2098 * delay filters. Symbol rotation and after always operates at 1 SPS.
2099 *
2100 * Allow 1 SPS demodulation here, but note that other parts of the
Pau Espin Pedrole16d0e12021-09-01 20:43:03 +02002101 * transceiver restrict EDGE operation to 4 SPS - 8-PSK distortion
Tom Tsoud3253432016-03-06 03:08:01 -08002102 * through the fractional delay filters at 1 SPS renders signal
2103 * nearly unrecoverable.
2104 */
Sylvain Munautad202d72021-02-04 20:37:01 +01002105static SoftVector *demodEdgeBurst(const signalVector &burst, int sps,
2106 struct estim_burst_params *ebp)
Tom Tsoud3253432016-03-06 03:08:01 -08002107{
2108 SoftVector *bits;
Tom Tsou7fec3032016-03-06 22:33:20 -08002109 signalVector *dec, *rot, *eq;
Tom Tsoud3253432016-03-06 03:08:01 -08002110
Sylvain Munautad202d72021-02-04 20:37:01 +01002111 dec = demodCommon(burst, sps, ebp);
Tom Tsou7fec3032016-03-06 22:33:20 -08002112 if (!dec)
Tom Tsoud3253432016-03-06 03:08:01 -08002113 return NULL;
2114
Tom Tsou7fec3032016-03-06 22:33:20 -08002115 /* Equalize and derotate */
Tom Tsoud3253432016-03-06 03:08:01 -08002116 eq = convolve(dec, GSMPulse4->c0_inv, NULL, NO_DELAY);
2117 rot = derotateEdgeBurst(*eq, 1);
Sylvain Munautad202d72021-02-04 20:37:01 +01002118 ebp->ci = computeEdgeCI(rot);
Tom Tsoud3253432016-03-06 03:08:01 -08002119
Tom Tsou7fec3032016-03-06 22:33:20 -08002120 /* Soft slice and normalize */
Tom Tsou04795622016-04-26 21:17:36 -07002121 bits = softSliceEdgeBurst(*rot);
Tom Tsoud3253432016-03-06 03:08:01 -08002122
2123 delete dec;
2124 delete eq;
2125 delete rot;
2126
2127 return bits;
2128}
2129
Sylvain Munautad202d72021-02-04 20:37:01 +01002130SoftVector *demodAnyBurst(const signalVector &burst, CorrType type,
2131 int sps, struct estim_burst_params *ebp)
Alexander Chemeris6e1dffd2017-03-17 16:13:51 -07002132{
2133 if (type == EDGE)
Sylvain Munautad202d72021-02-04 20:37:01 +01002134 return demodEdgeBurst(burst, sps, ebp);
Alexander Chemeris6e1dffd2017-03-17 16:13:51 -07002135 else
Sylvain Munautad202d72021-02-04 20:37:01 +01002136 return demodGmskBurst(burst, sps, ebp);
Alexander Chemeris6e1dffd2017-03-17 16:13:51 -07002137}
2138
Tom Tsou2079a3c2016-03-06 00:58:56 -08002139bool sigProcLibSetup()
Thomas Tsouc1f7c422013-10-11 13:49:55 -04002140{
Thomas Tsou0e0e1f42013-11-09 22:08:51 -05002141 generateSincTable();
Tom Tsou2079a3c2016-03-06 00:58:56 -08002142 initGMSKRotationTables();
Thomas Tsouc1f7c422013-10-11 13:49:55 -04002143
Tom Tsou2079a3c2016-03-06 00:58:56 -08002144 GSMPulse1 = generateGSMPulse(1);
2145 GSMPulse4 = generateGSMPulse(4);
Thomas Tsouc1f7c422013-10-11 13:49:55 -04002146
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02002147 generateRACHSequence(&gRACHSequences[0], gRACHSynchSequenceTS0, 1);
2148 generateRACHSequence(&gRACHSequences[1], gRACHSynchSequenceTS1, 1);
2149 generateRACHSequence(&gRACHSequences[2], gRACHSynchSequenceTS2, 1);
2150
Eric Wildd8a1dee2024-02-21 19:33:09 +01002151 generateSCHSequence(1);
2152 generateDummyMidamble(1);
2153
Tom Tsoud3253432016-03-06 03:08:01 -08002154 for (int tsc = 0; tsc < 8; tsc++) {
Tom Tsou2079a3c2016-03-06 00:58:56 -08002155 generateMidamble(1, tsc);
Tom Tsoud3253432016-03-06 03:08:01 -08002156 gEdgeMidambles[tsc] = generateEdgeMidamble(tsc);
2157 }
Thomas Tsouc1f7c422013-10-11 13:49:55 -04002158
Thomas Tsouf79c4d02013-11-09 15:51:56 -06002159 generateDelayFilters();
2160
Tom Tsoud3253432016-03-06 03:08:01 -08002161 dnsampler = new Resampler(1, 4);
2162 if (!dnsampler->init()) {
2163 LOG(ALERT) << "Rx resampler failed to initialize";
2164 goto fail;
2165 }
2166
Thomas Tsouc1f7c422013-10-11 13:49:55 -04002167 return true;
Tom Tsoud3253432016-03-06 03:08:01 -08002168
2169fail:
2170 sigProcLibDestroy();
2171 return false;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04002172}