blob: 1042dbbe78d718d69b4cea923149f53edb07e165 [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*
4* This software is distributed under the terms of the GNU Affero Public License.
5* See the COPYING file in the main directory for details.
6*
7* This use of this software may be subject to additional restrictions.
8* See the LEGAL file in the main directory for details.
9
10 This program is free software: you can redistribute it and/or modify
11 it under the terms of the GNU Affero General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Affero General Public License for more details.
19
20 You should have received a copy of the GNU Affero General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
22
23*/
24
Thomas Tsou7e4e5362013-10-30 21:18:55 -040025#ifdef HAVE_CONFIG_H
26#include "config.h"
27#endif
28
dburgessb3a0ca42011-10-12 07:44:40 +000029#include "sigProcLib.h"
30#include "GSMCommon.h"
Alexander Chemeris954b1182015-06-04 15:39:41 -040031#include "Logger.h"
Tom Tsoud3253432016-03-06 03:08:01 -080032#include "Resampler.h"
dburgessb3a0ca42011-10-12 07:44:40 +000033
Thomas Tsou3eaae802013-08-20 19:31:14 -040034extern "C" {
35#include "convolve.h"
Thomas Tsou7e4e5362013-10-30 21:18:55 -040036#include "scale.h"
Thomas Tsou0a3dc4c2013-11-09 02:29:55 -050037#include "mult.h"
Thomas Tsou3eaae802013-08-20 19:31:14 -040038}
39
Thomas Tsou7e4e5362013-10-30 21:18:55 -040040using namespace GSM;
41
Thomas Tsouf79c4d02013-11-09 15:51:56 -060042#define TABLESIZE 1024
43#define DELAYFILTS 64
dburgessb3a0ca42011-10-12 07:44:40 +000044
Tom Tsou577cd022015-05-18 13:57:54 -070045/* Clipping detection threshold */
46#define CLIP_THRESH 30000.0f
47
dburgessb3a0ca42011-10-12 07:44:40 +000048/** Lookup tables for trigonometric approximation */
Tom Tsoubb0c68a2017-06-16 17:08:40 -070049static float sincTable[TABLESIZE+1]; // add 1 element for wrap around
dburgessb3a0ca42011-10-12 07:44:40 +000050
51/** Constants */
52static const float M_PI_F = (float)M_PI;
dburgessb3a0ca42011-10-12 07:44:40 +000053
Thomas Tsouc1f7c422013-10-11 13:49:55 -040054/* Precomputed rotation vectors */
Tom Tsou2079a3c2016-03-06 00:58:56 -080055static signalVector *GMSKRotation4 = NULL;
56static signalVector *GMSKReverseRotation4 = NULL;
Thomas Tsouc1f7c422013-10-11 13:49:55 -040057static signalVector *GMSKRotation1 = NULL;
58static signalVector *GMSKReverseRotation1 = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +000059
Thomas Tsouf79c4d02013-11-09 15:51:56 -060060/* Precomputed fractional delay filters */
61static signalVector *delayFilters[DELAYFILTS];
62
Tom Tsou70134a02017-06-12 14:23:53 -070063static const Complex<float> psk8_table[8] = {
Tom Tsoud3253432016-03-06 03:08:01 -080064 Complex<float>(-0.70710678, 0.70710678),
65 Complex<float>( 0.0, -1.0),
66 Complex<float>( 0.0, 1.0),
67 Complex<float>( 0.70710678, -0.70710678),
68 Complex<float>(-1.0, 0.0),
69 Complex<float>(-0.70710678, -0.70710678),
70 Complex<float>( 0.70710678, 0.70710678),
71 Complex<float>( 1.0, 0.0),
72};
73
74/* Downsampling filterbank - 4 SPS to 1 SPS */
75#define DOWNSAMPLE_IN_LEN 624
76#define DOWNSAMPLE_OUT_LEN 156
77
78static Resampler *dnsampler = NULL;
Tom Tsoud3253432016-03-06 03:08:01 -080079
Thomas Tsoue5dcfc42013-08-20 16:27:12 -040080/*
Thomas Tsou3eaae802013-08-20 19:31:14 -040081 * RACH and midamble correlation waveforms. Store the buffer separately
82 * because we need to allocate it explicitly outside of the signal vector
83 * constructor. This is because C++ (prior to C++11) is unable to natively
84 * perform 16-byte memory alignment required by many SSE instructions.
Thomas Tsoue5dcfc42013-08-20 16:27:12 -040085 */
86struct CorrelationSequence {
Harald Welte5c6ca172019-07-21 11:49:44 +020087 CorrelationSequence() : sequence(NULL), buffer(NULL), toa(0.0)
Thomas Tsoue5dcfc42013-08-20 16:27:12 -040088 {
89 }
90
91 ~CorrelationSequence()
92 {
93 delete sequence;
94 }
95
dburgessb3a0ca42011-10-12 07:44:40 +000096 signalVector *sequence;
Thomas Tsou3eaae802013-08-20 19:31:14 -040097 void *buffer;
Thomas Tsouc1f7c422013-10-11 13:49:55 -040098 float toa;
dburgessb3a0ca42011-10-12 07:44:40 +000099 complex gain;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400100};
dburgessb3a0ca42011-10-12 07:44:40 +0000101
Thomas Tsou83e06892013-08-20 16:10:01 -0400102/*
Thomas Tsou3eaae802013-08-20 19:31:14 -0400103 * Gaussian and empty modulation pulses. Like the correlation sequences,
104 * store the runtime (Gaussian) buffer separately because of needed alignment
105 * for SSE instructions.
Thomas Tsou83e06892013-08-20 16:10:01 -0400106 */
107struct PulseSequence {
Pau Espin Pedrolf7331762018-12-03 17:46:04 +0100108 PulseSequence() : c0(NULL), c1(NULL), c0_inv(NULL), empty(NULL)
Thomas Tsou83e06892013-08-20 16:10:01 -0400109 {
110 }
111
112 ~PulseSequence()
113 {
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800114 delete c0;
115 delete c1;
Tom Tsoud3253432016-03-06 03:08:01 -0800116 delete c0_inv;
Thomas Tsou83e06892013-08-20 16:10:01 -0400117 delete empty;
118 }
119
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800120 signalVector *c0;
121 signalVector *c1;
Tom Tsoud3253432016-03-06 03:08:01 -0800122 signalVector *c0_inv;
Thomas Tsou83e06892013-08-20 16:10:01 -0400123 signalVector *empty;
124};
125
Tom Tsoud3253432016-03-06 03:08:01 -0800126static CorrelationSequence *gMidambles[] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
127static CorrelationSequence *gEdgeMidambles[] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
Vadim Yanitskiya79bc702018-10-17 11:01:58 +0200128static CorrelationSequence *gRACHSequences[] = {NULL,NULL,NULL};
Tom Tsoud3253432016-03-06 03:08:01 -0800129static PulseSequence *GSMPulse1 = NULL;
130static PulseSequence *GSMPulse4 = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000131
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400132void sigProcLibDestroy()
133{
dburgessb3a0ca42011-10-12 07:44:40 +0000134 for (int i = 0; i < 8; i++) {
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400135 delete gMidambles[i];
Tom Tsoud3253432016-03-06 03:08:01 -0800136 delete gEdgeMidambles[i];
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400137 gMidambles[i] = NULL;
Tom Tsoud3253432016-03-06 03:08:01 -0800138 gEdgeMidambles[i] = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000139 }
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400140
Thomas Tsouf79c4d02013-11-09 15:51:56 -0600141 for (int i = 0; i < DELAYFILTS; i++) {
142 delete delayFilters[i];
143 delayFilters[i] = NULL;
144 }
145
Vadim Yanitskiya79bc702018-10-17 11:01:58 +0200146 for (int i = 0; i < 3; i++) {
147 delete gRACHSequences[i];
148 gRACHSequences[i] = NULL;
149 }
150
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400151 delete GMSKRotation1;
152 delete GMSKReverseRotation1;
Tom Tsou2079a3c2016-03-06 00:58:56 -0800153 delete GMSKRotation4;
154 delete GMSKReverseRotation4;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400155 delete GSMPulse1;
Tom Tsou2079a3c2016-03-06 00:58:56 -0800156 delete GSMPulse4;
Tom Tsoud3253432016-03-06 03:08:01 -0800157 delete dnsampler;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400158
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400159 GMSKRotation1 = NULL;
Tom Tsou2079a3c2016-03-06 00:58:56 -0800160 GMSKRotation4 = NULL;
161 GMSKReverseRotation4 = NULL;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400162 GMSKReverseRotation1 = NULL;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400163 GSMPulse1 = NULL;
Tom Tsou2079a3c2016-03-06 00:58:56 -0800164 GSMPulse4 = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000165}
166
Tom Tsou70134a02017-06-12 14:23:53 -0700167static float vectorNorm2(const signalVector &x)
dburgessb3a0ca42011-10-12 07:44:40 +0000168{
169 signalVector::const_iterator xPtr = x.begin();
170 float Energy = 0.0;
171 for (;xPtr != x.end();xPtr++) {
172 Energy += xPtr->norm2();
173 }
174 return Energy;
175}
176
Tom Tsou2079a3c2016-03-06 00:58:56 -0800177/*
178 * Initialize 4 sps and 1 sps rotation tables
179 */
180static void initGMSKRotationTables()
Thomas Tsoud24cc2c2013-08-20 15:41:45 -0400181{
Tom Tsou2079a3c2016-03-06 00:58:56 -0800182 size_t len1 = 157, len4 = 625;
183
184 GMSKRotation4 = new signalVector(len4);
185 GMSKReverseRotation4 = new signalVector(len4);
186 signalVector::iterator rotPtr = GMSKRotation4->begin();
187 signalVector::iterator revPtr = GMSKReverseRotation4->begin();
Tom Tsoubb0c68a2017-06-16 17:08:40 -0700188 auto phase = 0.0;
Tom Tsou2079a3c2016-03-06 00:58:56 -0800189 while (rotPtr != GMSKRotation4->end()) {
Tom Tsoubb0c68a2017-06-16 17:08:40 -0700190 *rotPtr++ = complex(cos(phase), sin(phase));
191 *revPtr++ = complex(cos(-phase), sin(-phase));
192 phase += M_PI / 2.0 / 4.0;
dburgessb3a0ca42011-10-12 07:44:40 +0000193 }
dburgessb3a0ca42011-10-12 07:44:40 +0000194
Tom Tsou2079a3c2016-03-06 00:58:56 -0800195 GMSKRotation1 = new signalVector(len1);
196 GMSKReverseRotation1 = new signalVector(len1);
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400197 rotPtr = GMSKRotation1->begin();
198 revPtr = GMSKReverseRotation1->begin();
199 phase = 0.0;
200 while (rotPtr != GMSKRotation1->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;
Thomas Tsoue57004d2013-08-20 18:55:33 -0400204 }
dburgessb3a0ca42011-10-12 07:44:40 +0000205}
206
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400207static void GMSKRotate(signalVector &x, int sps)
208{
Thomas Tsou0a3dc4c2013-11-09 02:29:55 -0500209#if HAVE_NEON
210 size_t len;
211 signalVector *a, *b, *out;
212
213 a = &x;
214 out = &x;
215 len = out->size();
216
217 if (len == 157)
218 len--;
219
220 if (sps == 1)
221 b = GMSKRotation1;
222 else
Tom Tsou2079a3c2016-03-06 00:58:56 -0800223 b = GMSKRotation4;
Thomas Tsou0a3dc4c2013-11-09 02:29:55 -0500224
225 mul_complex((float *) out->begin(),
226 (float *) a->begin(),
227 (float *) b->begin(), len);
228#else
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400229 signalVector::iterator rotPtr, xPtr = x.begin();
230
231 if (sps == 1)
232 rotPtr = GMSKRotation1->begin();
233 else
Tom Tsou2079a3c2016-03-06 00:58:56 -0800234 rotPtr = GMSKRotation4->begin();
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400235
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500236 if (x.isReal()) {
dburgessb3a0ca42011-10-12 07:44:40 +0000237 while (xPtr < x.end()) {
238 *xPtr = *rotPtr++ * (xPtr->real());
239 xPtr++;
240 }
241 }
242 else {
243 while (xPtr < x.end()) {
244 *xPtr = *rotPtr++ * (*xPtr);
245 xPtr++;
246 }
247 }
Thomas Tsou0a3dc4c2013-11-09 02:29:55 -0500248#endif
dburgessb3a0ca42011-10-12 07:44:40 +0000249}
250
Tom Tsou2079a3c2016-03-06 00:58:56 -0800251static bool GMSKReverseRotate(signalVector &x, int sps)
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400252{
253 signalVector::iterator rotPtr, xPtr= x.begin();
254
255 if (sps == 1)
256 rotPtr = GMSKReverseRotation1->begin();
Tom Tsou2079a3c2016-03-06 00:58:56 -0800257 else if (sps == 4)
258 rotPtr = GMSKReverseRotation4->begin();
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400259 else
Tom Tsou2079a3c2016-03-06 00:58:56 -0800260 return false;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400261
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500262 if (x.isReal()) {
dburgessb3a0ca42011-10-12 07:44:40 +0000263 while (xPtr < x.end()) {
264 *xPtr = *rotPtr++ * (xPtr->real());
265 xPtr++;
266 }
267 }
268 else {
269 while (xPtr < x.end()) {
270 *xPtr = *rotPtr++ * (*xPtr);
271 xPtr++;
272 }
273 }
Tom Tsou2079a3c2016-03-06 00:58:56 -0800274
275 return true;
dburgessb3a0ca42011-10-12 07:44:40 +0000276}
277
Tom Tsou70134a02017-06-12 14:23:53 -0700278/** Convolution type indicator */
279enum ConvType {
280 START_ONLY,
281 NO_DELAY,
282 CUSTOM,
283 UNDEFINED,
284};
285
286static signalVector *convolve(const signalVector *x, const signalVector *h,
287 signalVector *y, ConvType spanType,
Sylvain Munauta3934a12018-12-20 19:10:26 +0100288 size_t start = 0, size_t len = 0)
dburgessb3a0ca42011-10-12 07:44:40 +0000289{
Thomas Tsou3f32ab52013-11-15 16:32:54 -0500290 int rc;
291 size_t head = 0, tail = 0;
Thomas Tsou3eaae802013-08-20 19:31:14 -0400292 bool alloc = false, append = false;
293 const signalVector *_x = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000294
Thomas Tsou3eaae802013-08-20 19:31:14 -0400295 if (!x || !h)
dburgessb3a0ca42011-10-12 07:44:40 +0000296 return NULL;
297
Thomas Tsou3eaae802013-08-20 19:31:14 -0400298 switch (spanType) {
299 case START_ONLY:
300 start = 0;
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500301 head = h->size() - 1;
Thomas Tsou3eaae802013-08-20 19:31:14 -0400302 len = x->size();
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500303
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500304 if (x->getStart() < head)
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500305 append = true;
dburgessb3a0ca42011-10-12 07:44:40 +0000306 break;
Thomas Tsou3eaae802013-08-20 19:31:14 -0400307 case NO_DELAY:
308 start = h->size() / 2;
309 head = start;
310 tail = start;
311 len = x->size();
312 append = true;
313 break;
314 case CUSTOM:
315 if (start < h->size() - 1) {
316 head = h->size() - start;
317 append = true;
318 }
319 if (start + len > x->size()) {
320 tail = start + len - x->size();
321 append = true;
dburgessb3a0ca42011-10-12 07:44:40 +0000322 }
323 break;
324 default:
325 return NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000326 }
dburgessb3a0ca42011-10-12 07:44:40 +0000327
Thomas Tsou3eaae802013-08-20 19:31:14 -0400328 /*
329 * Error if the output vector is too small. Create the output vector
330 * if the pointer is NULL.
331 */
332 if (y && (len > y->size()))
333 return NULL;
334 if (!y) {
Pau Espin Pedrolf7331762018-12-03 17:46:04 +0100335 y = new signalVector(len, convolve_h_alloc, free);
Thomas Tsou3eaae802013-08-20 19:31:14 -0400336 alloc = true;
337 }
338
339 /* Prepend or post-pend the input vector if the parameters require it */
340 if (append)
341 _x = new signalVector(*x, head, tail);
342 else
343 _x = x;
344
345 /*
346 * Four convovle types:
347 * 1. Complex-Real (aligned)
348 * 2. Complex-Complex (aligned)
349 * 3. Complex-Real (!aligned)
350 * 4. Complex-Complex (!aligned)
351 */
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500352 if (h->isReal() && h->isAligned()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -0400353 rc = convolve_real((float *) _x->begin(), _x->size(),
354 (float *) h->begin(), h->size(),
355 (float *) y->begin(), y->size(),
Sylvain Munauta3934a12018-12-20 19:10:26 +0100356 start, len);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500357 } else if (!h->isReal() && h->isAligned()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -0400358 rc = convolve_complex((float *) _x->begin(), _x->size(),
359 (float *) h->begin(), h->size(),
360 (float *) y->begin(), y->size(),
Sylvain Munauta3934a12018-12-20 19:10:26 +0100361 start, len);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500362 } else if (h->isReal() && !h->isAligned()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -0400363 rc = base_convolve_real((float *) _x->begin(), _x->size(),
364 (float *) h->begin(), h->size(),
365 (float *) y->begin(), y->size(),
Sylvain Munauta3934a12018-12-20 19:10:26 +0100366 start, len);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500367 } else if (!h->isReal() && !h->isAligned()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -0400368 rc = base_convolve_complex((float *) _x->begin(), _x->size(),
369 (float *) h->begin(), h->size(),
370 (float *) y->begin(), y->size(),
Sylvain Munauta3934a12018-12-20 19:10:26 +0100371 start, len);
Thomas Tsou3eaae802013-08-20 19:31:14 -0400372 } else {
373 rc = -1;
374 }
375
376 if (append)
377 delete _x;
378
379 if (rc < 0) {
380 if (alloc)
381 delete y;
382 return NULL;
383 }
384
385 return y;
386}
dburgessb3a0ca42011-10-12 07:44:40 +0000387
Tom Tsoud3253432016-03-06 03:08:01 -0800388/*
389 * Generate static EDGE linear equalizer. This equalizer is not adaptive.
390 * Filter taps are generated from the inverted 1 SPS impulse response of
391 * the EDGE pulse shape captured after the downsampling filter.
392 */
393static bool generateInvertC0Pulse(PulseSequence *pulse)
394{
395 if (!pulse)
396 return false;
397
Pau Espin Pedrolf7331762018-12-03 17:46:04 +0100398 pulse->c0_inv = new signalVector((complex *) convolve_h_alloc(5), 0, 5, convolve_h_alloc, free);
Tom Tsoud3253432016-03-06 03:08:01 -0800399 pulse->c0_inv->isReal(true);
400 pulse->c0_inv->setAligned(false);
401
402 signalVector::iterator xP = pulse->c0_inv->begin();
403 *xP++ = 0.15884;
404 *xP++ = -0.43176;
405 *xP++ = 1.00000;
406 *xP++ = -0.42608;
407 *xP++ = 0.14882;
408
409 return true;
410}
411
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400412static bool generateC1Pulse(int sps, PulseSequence *pulse)
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800413{
414 int len;
415
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400416 if (!pulse)
417 return false;
418
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800419 switch (sps) {
420 case 4:
421 len = 8;
422 break;
423 default:
424 return false;
425 }
426
Pau Espin Pedrolf7331762018-12-03 17:46:04 +0100427 pulse->c1 = new signalVector((complex *) convolve_h_alloc(len), 0, len, convolve_h_alloc, free);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500428 pulse->c1->isReal(true);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800429
430 /* Enable alignment for SSE usage */
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400431 pulse->c1->setAligned(true);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800432
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400433 signalVector::iterator xP = pulse->c1->begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800434
435 switch (sps) {
436 case 4:
437 /* BT = 0.30 */
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400438 *xP++ = 0.0;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800439 *xP++ = 8.16373112e-03;
440 *xP++ = 2.84385729e-02;
441 *xP++ = 5.64158904e-02;
442 *xP++ = 7.05463553e-02;
443 *xP++ = 5.64158904e-02;
444 *xP++ = 2.84385729e-02;
445 *xP++ = 8.16373112e-03;
446 }
447
448 return true;
449}
450
Tom Tsou2079a3c2016-03-06 00:58:56 -0800451static PulseSequence *generateGSMPulse(int sps)
dburgessb3a0ca42011-10-12 07:44:40 +0000452{
Thomas Tsou83e06892013-08-20 16:10:01 -0400453 int len;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800454 float arg, avg, center;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400455 PulseSequence *pulse;
Thomas Tsou83e06892013-08-20 16:10:01 -0400456
Tom Tsoud3253432016-03-06 03:08:01 -0800457 if ((sps != 1) && (sps != 4))
458 return NULL;
459
Thomas Tsou83e06892013-08-20 16:10:01 -0400460 /* Store a single tap filter used for correlation sequence generation */
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400461 pulse = new PulseSequence();
462 pulse->empty = new signalVector(1);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500463 pulse->empty->isReal(true);
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400464 *(pulse->empty->begin()) = 1.0f;
Thomas Tsou83e06892013-08-20 16:10:01 -0400465
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400466 /*
467 * For 4 samples-per-symbol use a precomputed single pulse Laurent
468 * approximation. This should yields below 2 degrees of phase error at
469 * the modulator output. Use the existing pulse approximation for all
470 * other oversampling factors.
471 */
472 switch (sps) {
473 case 4:
474 len = 16;
475 break;
Tom Tsoud3253432016-03-06 03:08:01 -0800476 case 1:
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400477 default:
Tom Tsou2079a3c2016-03-06 00:58:56 -0800478 len = 4;
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400479 }
Thomas Tsou3eaae802013-08-20 19:31:14 -0400480
Pau Espin Pedrolf7331762018-12-03 17:46:04 +0100481 pulse->c0 = new signalVector((complex *) convolve_h_alloc(len), 0, len, convolve_h_alloc, free);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500482 pulse->c0->isReal(true);
Thomas Tsou3eaae802013-08-20 19:31:14 -0400483
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800484 /* Enable alingnment for SSE usage */
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400485 pulse->c0->setAligned(true);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800486
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400487 signalVector::iterator xP = pulse->c0->begin();
Thomas Tsou83e06892013-08-20 16:10:01 -0400488
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400489 if (sps == 4) {
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800490 *xP++ = 0.0;
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400491 *xP++ = 4.46348606e-03;
492 *xP++ = 2.84385729e-02;
493 *xP++ = 1.03184855e-01;
494 *xP++ = 2.56065552e-01;
495 *xP++ = 4.76375085e-01;
496 *xP++ = 7.05961177e-01;
497 *xP++ = 8.71291644e-01;
498 *xP++ = 9.29453645e-01;
499 *xP++ = 8.71291644e-01;
500 *xP++ = 7.05961177e-01;
501 *xP++ = 4.76375085e-01;
502 *xP++ = 2.56065552e-01;
503 *xP++ = 1.03184855e-01;
504 *xP++ = 2.84385729e-02;
505 *xP++ = 4.46348606e-03;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400506 generateC1Pulse(sps, pulse);
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400507 } else {
508 center = (float) (len - 1.0) / 2.0;
Thomas Tsou83e06892013-08-20 16:10:01 -0400509
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400510 /* GSM pulse approximation */
511 for (int i = 0; i < len; i++) {
512 arg = ((float) i - center) / (float) sps;
513 *xP++ = 0.96 * exp(-1.1380 * arg * arg -
514 0.527 * arg * arg * arg * arg);
515 }
dburgessb3a0ca42011-10-12 07:44:40 +0000516
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400517 avg = sqrtf(vectorNorm2(*pulse->c0) / sps);
518 xP = pulse->c0->begin();
519 for (int i = 0; i < len; i++)
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800520 *xP++ /= avg;
521 }
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400522
Tom Tsoud3253432016-03-06 03:08:01 -0800523 /*
524 * Current form of the EDGE equalization filter non-realizable at 4 SPS.
525 * Load the onto both 1 SPS and 4 SPS objects for convenience. Note that
526 * the EDGE demodulator downsamples to 1 SPS prior to equalization.
527 */
528 generateInvertC0Pulse(pulse);
529
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400530 return pulse;
dburgessb3a0ca42011-10-12 07:44:40 +0000531}
532
Pau Espin Pedrol7dc07b92019-07-01 17:55:01 +0200533/* Convert -1..+1 soft bits to 0..1 soft bits */
534void vectorSlicer(float *dest, const float *src, size_t len)
Tom Tsoud3253432016-03-06 03:08:01 -0800535{
Pau Espin Pedrol7dc07b92019-07-01 17:55:01 +0200536 size_t i;
537 for (i = 0; i < len; i++) {
538 dest[i] = 0.5 * (src[i] + 1.0f);
539 if (dest[i] > 1.0)
540 dest[i] = 1.0;
541 else if (dest[i] < 0.0)
542 dest[i] = 0.0;
543 }
Tom Tsoud3253432016-03-06 03:08:01 -0800544}
545
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800546static signalVector *rotateBurst(const BitVector &wBurst,
547 int guardPeriodLength, int sps)
548{
549 int burst_len;
Tom Tsou7278a872017-06-14 14:50:39 -0700550 signalVector *pulse, rotated;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800551 signalVector::iterator itr;
552
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400553 pulse = GSMPulse1->empty;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800554 burst_len = sps * (wBurst.size() + guardPeriodLength);
555 rotated = signalVector(burst_len);
556 itr = rotated.begin();
557
558 for (unsigned i = 0; i < wBurst.size(); i++) {
559 *itr = 2.0 * (wBurst[i] & 0x01) - 1.0;
560 itr += sps;
561 }
562
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400563 GMSKRotate(rotated, sps);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500564 rotated.isReal(false);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800565
566 /* Dummy filter operation */
Tom Tsou7278a872017-06-14 14:50:39 -0700567 return convolve(&rotated, pulse, NULL, START_ONLY);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800568}
569
Tom Tsoud3253432016-03-06 03:08:01 -0800570static void rotateBurst2(signalVector &burst, double phase)
571{
572 Complex<float> rot = Complex<float>(cos(phase), sin(phase));
573
574 for (size_t i = 0; i < burst.size(); i++)
575 burst[i] = burst[i] * rot;
576}
577
Tom Tsou4dfd64a2016-03-06 20:31:51 -0800578/*
579 * Ignore the guard length argument in the GMSK modulator interface
580 * because it results in 624/628 sized bursts instead of the preferred
581 * burst length of 625. Only 4 SPS is supported.
582 */
583static signalVector *modulateBurstLaurent(const BitVector &bits)
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800584{
Tom Tsou4dfd64a2016-03-06 20:31:51 -0800585 int burst_len, sps = 4;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800586 float phase;
Tom Tsou7278a872017-06-14 14:50:39 -0700587 signalVector *c0_pulse, *c1_pulse, *c0_shaped, *c1_shaped;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800588 signalVector::iterator c0_itr, c1_itr;
589
Tom Tsou2079a3c2016-03-06 00:58:56 -0800590 c0_pulse = GSMPulse4->c0;
591 c1_pulse = GSMPulse4->c1;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800592
Tom Tsou4dfd64a2016-03-06 20:31:51 -0800593 if (bits.size() > 156)
594 return NULL;
595
596 burst_len = 625;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800597
Tom Tsou7278a872017-06-14 14:50:39 -0700598 signalVector c0_burst(burst_len, c0_pulse->size());
599 c0_burst.isReal(true);
600 c0_itr = c0_burst.begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800601
Tom Tsou7278a872017-06-14 14:50:39 -0700602 signalVector c1_burst(burst_len, c1_pulse->size());
603 c1_itr = c1_burst.begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800604
Tom Tsouaa15d622016-08-11 14:36:23 -0700605 /* Padded differential tail bits */
606 *c0_itr = 2.0 * (0x00 & 0x01) - 1.0;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800607 c0_itr += sps;
608
609 /* Main burst bits */
610 for (unsigned i = 0; i < bits.size(); i++) {
611 *c0_itr = 2.0 * (bits[i] & 0x01) - 1.0;
612 c0_itr += sps;
613 }
614
Tom Tsouaa15d622016-08-11 14:36:23 -0700615 /* Padded differential tail bits */
616 *c0_itr = 2.0 * (0x00 & 0x01) - 1.0;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800617
618 /* Generate C0 phase coefficients */
Tom Tsou7278a872017-06-14 14:50:39 -0700619 GMSKRotate(c0_burst, sps);
620 c0_burst.isReal(false);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800621
Tom Tsou7278a872017-06-14 14:50:39 -0700622 c0_itr = c0_burst.begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800623 c0_itr += sps * 2;
624 c1_itr += sps * 2;
625
626 /* Start magic */
627 phase = 2.0 * ((0x01 & 0x01) ^ (0x01 & 0x01)) - 1.0;
628 *c1_itr = *c0_itr * Complex<float>(0, phase);
629 c0_itr += sps;
630 c1_itr += sps;
631
632 /* Generate C1 phase coefficients */
633 for (unsigned i = 2; i < bits.size(); i++) {
634 phase = 2.0 * ((bits[i - 1] & 0x01) ^ (bits[i - 2] & 0x01)) - 1.0;
635 *c1_itr = *c0_itr * Complex<float>(0, phase);
636
637 c0_itr += sps;
638 c1_itr += sps;
639 }
640
641 /* End magic */
642 int i = bits.size();
643 phase = 2.0 * ((bits[i-1] & 0x01) ^ (bits[i-2] & 0x01)) - 1.0;
644 *c1_itr = *c0_itr * Complex<float>(0, phase);
645
646 /* Primary (C0) and secondary (C1) pulse shaping */
Tom Tsou7278a872017-06-14 14:50:39 -0700647 c0_shaped = convolve(&c0_burst, c0_pulse, NULL, START_ONLY);
648 c1_shaped = convolve(&c1_burst, c1_pulse, NULL, START_ONLY);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800649
650 /* Sum shaped outputs into C0 */
651 c0_itr = c0_shaped->begin();
652 c1_itr = c1_shaped->begin();
653 for (unsigned i = 0; i < c0_shaped->size(); i++ )
654 *c0_itr++ += *c1_itr++;
655
656 delete c1_shaped;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800657 return c0_shaped;
658}
659
Tom Tsoud3253432016-03-06 03:08:01 -0800660static signalVector *rotateEdgeBurst(const signalVector &symbols, int sps)
661{
662 signalVector *burst;
663 signalVector::iterator burst_itr;
664
665 burst = new signalVector(symbols.size() * sps);
666 burst_itr = burst->begin();
667
668 for (size_t i = 0; i < symbols.size(); i++) {
669 float phase = i * 3.0f * M_PI / 8.0f;
670 Complex<float> rot = Complex<float>(cos(phase), sin(phase));
671
672 *burst_itr = symbols[i] * rot;
673 burst_itr += sps;
674 }
675
676 return burst;
677}
678
679static signalVector *derotateEdgeBurst(const signalVector &symbols, int sps)
680{
681 signalVector *burst;
682 signalVector::iterator burst_itr;
683
684 if (symbols.size() % sps)
685 return NULL;
686
687 burst = new signalVector(symbols.size() / sps);
688 burst_itr = burst->begin();
689
690 for (size_t i = 0; i < burst->size(); i++) {
691 float phase = (float) (i % 16) * 3.0f * M_PI / 8.0f;
692 Complex<float> rot = Complex<float>(cosf(phase), -sinf(phase));
693
694 *burst_itr = symbols[sps * i] * rot;
695 burst_itr++;
696 }
697
698 return burst;
699}
700
701static signalVector *mapEdgeSymbols(const BitVector &bits)
702{
703 if (bits.size() % 3)
704 return NULL;
705
706 signalVector *symbols = new signalVector(bits.size() / 3);
707
708 for (size_t i = 0; i < symbols->size(); i++) {
709 unsigned index = (((unsigned) bits[3 * i + 0] & 0x01) << 0) |
710 (((unsigned) bits[3 * i + 1] & 0x01) << 1) |
711 (((unsigned) bits[3 * i + 2] & 0x01) << 2);
712
713 (*symbols)[i] = psk8_table[index];
714 }
715
716 return symbols;
717}
718
Tom Tsoud2b07032016-04-26 19:28:59 -0700719/*
720 * EDGE 8-PSK rotate and pulse shape
721 *
722 * Delay the EDGE downlink bursts by one symbol in order to match GMSK pulse
723 * shaping group delay. The difference in group delay arises from the dual
724 * pulse filter combination of the GMSK Laurent represenation whereas 8-PSK
725 * uses a single pulse linear filter.
726 */
Tom Tsoud3253432016-03-06 03:08:01 -0800727static signalVector *shapeEdgeBurst(const signalVector &symbols)
728{
Tom Tsoud2b07032016-04-26 19:28:59 -0700729 size_t nsyms, nsamps = 625, sps = 4;
Tom Tsoud3253432016-03-06 03:08:01 -0800730 signalVector::iterator burst_itr;
731
732 nsyms = symbols.size();
733
Tom Tsoud2b07032016-04-26 19:28:59 -0700734 if (nsyms * sps > nsamps)
Tom Tsoud3253432016-03-06 03:08:01 -0800735 nsyms = 156;
736
Tom Tsou7278a872017-06-14 14:50:39 -0700737 signalVector burst(nsamps, GSMPulse4->c0->size());
Tom Tsoud3253432016-03-06 03:08:01 -0800738
Tom Tsoud2b07032016-04-26 19:28:59 -0700739 /* Delay burst by 1 symbol */
Tom Tsou7278a872017-06-14 14:50:39 -0700740 burst_itr = burst.begin() + sps;
Tom Tsou06676ea2016-07-19 12:50:21 -0700741 for (size_t i = 0; i < nsyms; i++) {
Tom Tsoud3253432016-03-06 03:08:01 -0800742 float phase = i * 3.0f * M_PI / 8.0f;
743 Complex<float> rot = Complex<float>(cos(phase), sin(phase));
744
745 *burst_itr = symbols[i] * rot;
Tom Tsoud2b07032016-04-26 19:28:59 -0700746 burst_itr += sps;
Tom Tsoud3253432016-03-06 03:08:01 -0800747 }
748
749 /* Single Gaussian pulse approximation shaping */
Tom Tsou7278a872017-06-14 14:50:39 -0700750 return convolve(&burst, GSMPulse4->c0, NULL, START_ONLY);
Tom Tsoud3253432016-03-06 03:08:01 -0800751}
752
753/*
Tom Tsou8ee2f382016-03-06 20:57:34 -0800754 * Generate a random GSM normal burst.
755 */
756signalVector *genRandNormalBurst(int tsc, int sps, int tn)
757{
758 if ((tsc < 0) || (tsc > 7) || (tn < 0) || (tn > 7))
759 return NULL;
760 if ((sps != 1) && (sps != 4))
761 return NULL;
762
763 int i = 0;
Tom Tsou7278a872017-06-14 14:50:39 -0700764 BitVector bits(148);
Tom Tsou8ee2f382016-03-06 20:57:34 -0800765
766 /* Tail bits */
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700767 for (; i < 3; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700768 bits[i] = 0;
Tom Tsou8ee2f382016-03-06 20:57:34 -0800769
770 /* Random bits */
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700771 for (; i < 60; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700772 bits[i] = rand() % 2;
Tom Tsou8ee2f382016-03-06 20:57:34 -0800773
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700774 /* Stealing bit */
Tom Tsou7278a872017-06-14 14:50:39 -0700775 bits[i++] = 0;
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700776
Tom Tsou8ee2f382016-03-06 20:57:34 -0800777 /* Training sequence */
778 for (int n = 0; i < 87; i++, n++)
Tom Tsou7278a872017-06-14 14:50:39 -0700779 bits[i] = gTrainingSequence[tsc][n];
Tom Tsou8ee2f382016-03-06 20:57:34 -0800780
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700781 /* Stealing bit */
Tom Tsou7278a872017-06-14 14:50:39 -0700782 bits[i++] = 0;
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700783
Tom Tsou8ee2f382016-03-06 20:57:34 -0800784 /* Random bits */
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700785 for (; i < 145; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700786 bits[i] = rand() % 2;
Tom Tsou8ee2f382016-03-06 20:57:34 -0800787
788 /* Tail bits */
789 for (; i < 148; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700790 bits[i] = 0;
Tom Tsou8ee2f382016-03-06 20:57:34 -0800791
792 int guard = 8 + !(tn % 4);
Tom Tsou7278a872017-06-14 14:50:39 -0700793 return modulateBurst(bits, guard, sps);
Tom Tsou8ee2f382016-03-06 20:57:34 -0800794}
795
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300796/*
797 * Generate a random GSM access burst.
798 */
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300799signalVector *genRandAccessBurst(int delay, int sps, int tn)
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300800{
801 if ((tn < 0) || (tn > 7))
802 return NULL;
803 if ((sps != 1) && (sps != 4))
804 return NULL;
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300805 if (delay > 68)
806 return NULL;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300807
808 int i = 0;
Tom Tsou7278a872017-06-14 14:50:39 -0700809 BitVector bits(88 + delay);
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300810
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300811 /* delay */
812 for (; i < delay; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700813 bits[i] = 0;
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300814
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300815 /* head and synch bits */
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300816 for (int n = 0; i < 49+delay; i++, n++)
Tom Tsou7278a872017-06-14 14:50:39 -0700817 bits[i] = gRACHBurst[n];
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300818
819 /* Random bits */
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300820 for (; i < 85+delay; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700821 bits[i] = rand() % 2;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300822
823 /* Tail bits */
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300824 for (; i < 88+delay; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700825 bits[i] = 0;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300826
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300827 int guard = 68-delay + !(tn % 4);
Tom Tsou7278a872017-06-14 14:50:39 -0700828 return modulateBurst(bits, guard, sps);
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300829}
830
Tom Tsou8ee2f382016-03-06 20:57:34 -0800831signalVector *generateEmptyBurst(int sps, int tn)
832{
833 if ((tn < 0) || (tn > 7))
834 return NULL;
835
836 if (sps == 4)
837 return new signalVector(625);
838 else if (sps == 1)
839 return new signalVector(148 + 8 + !(tn % 4));
840 else
841 return NULL;
842}
843
844signalVector *generateDummyBurst(int sps, int tn)
845{
846 if (((sps != 1) && (sps != 4)) || (tn < 0) || (tn > 7))
847 return NULL;
848
849 return modulateBurst(gDummyBurst, 8 + !(tn % 4), sps);
850}
851
852/*
Tom Tsoud3253432016-03-06 03:08:01 -0800853 * Generate a random 8-PSK EDGE burst. Only 4 SPS is supported with
854 * the returned burst being 625 samples in length.
855 */
856signalVector *generateEdgeBurst(int tsc)
857{
858 int tail = 9 / 3;
859 int data = 174 / 3;
860 int train = 78 / 3;
861
862 if ((tsc < 0) || (tsc > 7))
863 return NULL;
864
Tom Tsou7278a872017-06-14 14:50:39 -0700865 signalVector burst(148);
Tom Tsoud3253432016-03-06 03:08:01 -0800866 const BitVector *midamble = &gEdgeTrainingSequence[tsc];
867
868 /* Tail */
869 int n, i = 0;
870 for (; i < tail; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700871 burst[i] = psk8_table[7];
Tom Tsoud3253432016-03-06 03:08:01 -0800872
873 /* Body */
874 for (; i < tail + data; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700875 burst[i] = psk8_table[rand() % 8];
Tom Tsoud3253432016-03-06 03:08:01 -0800876
877 /* TSC */
878 for (n = 0; i < tail + data + train; i++, n++) {
879 unsigned index = (((unsigned) (*midamble)[3 * n + 0] & 0x01) << 0) |
880 (((unsigned) (*midamble)[3 * n + 1] & 0x01) << 1) |
881 (((unsigned) (*midamble)[3 * n + 2] & 0x01) << 2);
882
Tom Tsou7278a872017-06-14 14:50:39 -0700883 burst[i] = psk8_table[index];
Tom Tsoud3253432016-03-06 03:08:01 -0800884 }
885
886 /* Body */
887 for (; i < tail + data + train + data; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700888 burst[i] = psk8_table[rand() % 8];
Tom Tsoud3253432016-03-06 03:08:01 -0800889
890 /* Tail */
891 for (; i < tail + data + train + data + tail; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700892 burst[i] = psk8_table[7];
Tom Tsoud3253432016-03-06 03:08:01 -0800893
Tom Tsou7278a872017-06-14 14:50:39 -0700894 return shapeEdgeBurst(burst);
Tom Tsoud3253432016-03-06 03:08:01 -0800895}
896
897/*
898 * Modulate 8-PSK burst. When empty pulse shaping (rotation only)
899 * is enabled, the output vector length will be bit sequence length
900 * times the SPS value. When pulse shaping is enabled, the output
Alexander Chemeris9270a5a2017-03-17 13:03:41 -0700901 * vector length is fixed at 625 samples (156.25 symbols at 4 SPS).
Tom Tsoud3253432016-03-06 03:08:01 -0800902 * Pulse shaped bit sequences that go beyond one burst are truncated.
903 * Pulse shaping at anything but 4 SPS is not supported.
904 */
905signalVector *modulateEdgeBurst(const BitVector &bits,
906 int sps, bool empty)
907{
908 signalVector *shape, *burst;
909
910 if ((sps != 4) && !empty)
911 return NULL;
912
913 burst = mapEdgeSymbols(bits);
914 if (!burst)
915 return NULL;
916
917 if (empty)
918 shape = rotateEdgeBurst(*burst, sps);
919 else
920 shape = shapeEdgeBurst(*burst);
921
922 delete burst;
923 return shape;
924}
925
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800926static signalVector *modulateBurstBasic(const BitVector &bits,
927 int guard_len, int sps)
928{
929 int burst_len;
Tom Tsou7278a872017-06-14 14:50:39 -0700930 signalVector *pulse;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800931 signalVector::iterator burst_itr;
932
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400933 if (sps == 1)
934 pulse = GSMPulse1->c0;
935 else
Tom Tsou2079a3c2016-03-06 00:58:56 -0800936 pulse = GSMPulse4->c0;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400937
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800938 burst_len = sps * (bits.size() + guard_len);
939
Tom Tsou7278a872017-06-14 14:50:39 -0700940 signalVector burst(burst_len, pulse->size());
941 burst.isReal(true);
942 burst_itr = burst.begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800943
944 /* Raw bits are not differentially encoded */
945 for (unsigned i = 0; i < bits.size(); i++) {
946 *burst_itr = 2.0 * (bits[i] & 0x01) - 1.0;
947 burst_itr += sps;
948 }
949
Tom Tsou7278a872017-06-14 14:50:39 -0700950 GMSKRotate(burst, sps);
951 burst.isReal(false);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800952
953 /* Single Gaussian pulse approximation shaping */
Tom Tsou7278a872017-06-14 14:50:39 -0700954 return convolve(&burst, pulse, NULL, START_ONLY);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800955}
956
Thomas Tsou3eaae802013-08-20 19:31:14 -0400957/* Assume input bits are not differentially encoded */
Thomas Tsou83e06892013-08-20 16:10:01 -0400958signalVector *modulateBurst(const BitVector &wBurst, int guardPeriodLength,
959 int sps, bool emptyPulse)
dburgessb3a0ca42011-10-12 07:44:40 +0000960{
Thomas Tsou83e06892013-08-20 16:10:01 -0400961 if (emptyPulse)
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800962 return rotateBurst(wBurst, guardPeriodLength, sps);
963 else if (sps == 4)
Tom Tsou4dfd64a2016-03-06 20:31:51 -0800964 return modulateBurstLaurent(wBurst);
Thomas Tsou83e06892013-08-20 16:10:01 -0400965 else
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800966 return modulateBurstBasic(wBurst, guardPeriodLength, sps);
dburgessb3a0ca42011-10-12 07:44:40 +0000967}
968
Tom Tsou2079a3c2016-03-06 00:58:56 -0800969static void generateSincTable()
Thomas Tsou0e0e1f42013-11-09 22:08:51 -0500970{
Thomas Tsou0e0e1f42013-11-09 22:08:51 -0500971 for (int i = 0; i < TABLESIZE; i++) {
Tom Tsoua3dce852017-06-16 17:14:31 -0700972 auto x = (double) i / TABLESIZE * 8 * M_PI;
973 auto y = sin(x) / x;
Tom Tsou35474132017-06-19 16:00:34 -0700974 sincTable[i] = std::isnan(y) ? 1.0 : y;
Thomas Tsou0e0e1f42013-11-09 22:08:51 -0500975 }
976}
977
Tom Tsou70134a02017-06-12 14:23:53 -0700978static float sinc(float x)
dburgessb3a0ca42011-10-12 07:44:40 +0000979{
Thomas Tsou0e0e1f42013-11-09 22:08:51 -0500980 if (fabs(x) >= 8 * M_PI)
981 return 0.0;
982
983 int index = (int) floorf(fabs(x) / (8 * M_PI) * TABLESIZE);
984
985 return sincTable[index];
dburgessb3a0ca42011-10-12 07:44:40 +0000986}
987
Thomas Tsouf79c4d02013-11-09 15:51:56 -0600988/*
989 * Create fractional delay filterbank with Blackman-harris windowed
990 * sinc function generator. The number of filters generated is specified
991 * by the DELAYFILTS value.
992 */
Tom Tsou70134a02017-06-12 14:23:53 -0700993static void generateDelayFilters()
Thomas Tsouf79c4d02013-11-09 15:51:56 -0600994{
995 int h_len = 20;
996 complex *data;
997 signalVector *h;
998 signalVector::iterator itr;
999
1000 float k, sum;
1001 float a0 = 0.35875;
1002 float a1 = 0.48829;
1003 float a2 = 0.14128;
1004 float a3 = 0.01168;
1005
1006 for (int i = 0; i < DELAYFILTS; i++) {
1007 data = (complex *) convolve_h_alloc(h_len);
Pau Espin Pedrolf7331762018-12-03 17:46:04 +01001008 h = new signalVector(data, 0, h_len, convolve_h_alloc, free);
Thomas Tsouf79c4d02013-11-09 15:51:56 -06001009 h->setAligned(true);
1010 h->isReal(true);
1011
1012 sum = 0.0;
1013 itr = h->end();
1014 for (int n = 0; n < h_len; n++) {
1015 k = (float) n;
1016 *--itr = (complex) sinc(M_PI_F *
1017 (k - (float) h_len / 2.0 - (float) i / DELAYFILTS));
1018 *itr *= a0 -
1019 a1 * cos(2 * M_PI * n / (h_len - 1)) +
1020 a2 * cos(4 * M_PI * n / (h_len - 1)) -
1021 a3 * cos(6 * M_PI * n / (h_len - 1));
1022
1023 sum += itr->real();
1024 }
1025
1026 itr = h->begin();
1027 for (int n = 0; n < h_len; n++)
1028 *itr++ /= sum;
1029
1030 delayFilters[i] = h;
1031 }
1032}
1033
Alexander Chemerise0c12182017-03-18 13:27:48 -07001034signalVector *delayVector(const signalVector *in, signalVector *out, float delay)
dburgessb3a0ca42011-10-12 07:44:40 +00001035{
Thomas Tsouf79c4d02013-11-09 15:51:56 -06001036 int whole, index;
Thomas Tsou2c282f52013-10-08 21:34:35 -04001037 float frac;
Thomas Tsou94edaae2013-11-09 22:19:19 -05001038 signalVector *h, *shift, *fshift = NULL;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001039
Thomas Tsou2c282f52013-10-08 21:34:35 -04001040 whole = floor(delay);
1041 frac = delay - whole;
1042
1043 /* Sinc interpolated fractional shift (if allowable) */
1044 if (fabs(frac) > 1e-2) {
Thomas Tsouf79c4d02013-11-09 15:51:56 -06001045 index = floorf(frac * (float) DELAYFILTS);
1046 h = delayFilters[index];
Thomas Tsou2c282f52013-10-08 21:34:35 -04001047
Thomas Tsou94edaae2013-11-09 22:19:19 -05001048 fshift = convolve(in, h, NULL, NO_DELAY);
1049 if (!fshift)
1050 return NULL;
dburgessb3a0ca42011-10-12 07:44:40 +00001051 }
1052
Thomas Tsou94edaae2013-11-09 22:19:19 -05001053 if (!fshift)
1054 shift = new signalVector(*in);
1055 else
1056 shift = fshift;
1057
Thomas Tsou2c282f52013-10-08 21:34:35 -04001058 /* Integer sample shift */
1059 if (whole < 0) {
1060 whole = -whole;
Thomas Tsou94edaae2013-11-09 22:19:19 -05001061 signalVector::iterator wBurstItr = shift->begin();
1062 signalVector::iterator shiftedItr = shift->begin() + whole;
Thomas Tsou2c282f52013-10-08 21:34:35 -04001063
Thomas Tsou94edaae2013-11-09 22:19:19 -05001064 while (shiftedItr < shift->end())
dburgessb3a0ca42011-10-12 07:44:40 +00001065 *wBurstItr++ = *shiftedItr++;
Thomas Tsou2c282f52013-10-08 21:34:35 -04001066
Thomas Tsou94edaae2013-11-09 22:19:19 -05001067 while (wBurstItr < shift->end())
1068 *wBurstItr++ = 0.0;
1069 } else if (whole >= 0) {
1070 signalVector::iterator wBurstItr = shift->end() - 1;
1071 signalVector::iterator shiftedItr = shift->end() - 1 - whole;
1072
1073 while (shiftedItr >= shift->begin())
dburgessb3a0ca42011-10-12 07:44:40 +00001074 *wBurstItr-- = *shiftedItr--;
Thomas Tsou94edaae2013-11-09 22:19:19 -05001075
1076 while (wBurstItr >= shift->begin())
dburgessb3a0ca42011-10-12 07:44:40 +00001077 *wBurstItr-- = 0.0;
1078 }
Thomas Tsou2c282f52013-10-08 21:34:35 -04001079
Thomas Tsou94edaae2013-11-09 22:19:19 -05001080 if (!out)
1081 return shift;
1082
1083 out->clone(*shift);
1084 delete shift;
1085 return out;
dburgessb3a0ca42011-10-12 07:44:40 +00001086}
Thomas Tsou2c282f52013-10-08 21:34:35 -04001087
Tom Tsou70134a02017-06-12 14:23:53 -07001088static complex interpolatePoint(const signalVector &inSig, float ix)
dburgessb3a0ca42011-10-12 07:44:40 +00001089{
dburgessb3a0ca42011-10-12 07:44:40 +00001090 int start = (int) (floor(ix) - 10);
1091 if (start < 0) start = 0;
1092 int end = (int) (floor(ix) + 11);
1093 if ((unsigned) end > inSig.size()-1) end = inSig.size()-1;
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001094
dburgessb3a0ca42011-10-12 07:44:40 +00001095 complex pVal = 0.0;
Thomas Tsou20eb6d62013-11-09 14:30:41 -05001096 if (!inSig.isReal()) {
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001097 for (int i = start; i < end; i++)
dburgessb3a0ca42011-10-12 07:44:40 +00001098 pVal += inSig[i] * sinc(M_PI_F*(i-ix));
1099 }
1100 else {
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001101 for (int i = start; i < end; i++)
dburgessb3a0ca42011-10-12 07:44:40 +00001102 pVal += inSig[i].real() * sinc(M_PI_F*(i-ix));
1103 }
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001104
dburgessb3a0ca42011-10-12 07:44:40 +00001105 return pVal;
1106}
1107
Thomas Tsou8181b012013-08-20 21:17:19 -04001108static complex fastPeakDetect(const signalVector &rxBurst, float *index)
1109{
1110 float val, max = 0.0f;
1111 complex amp;
1112 int _index = -1;
1113
Thomas Tsou3f32ab52013-11-15 16:32:54 -05001114 for (size_t i = 0; i < rxBurst.size(); i++) {
Thomas Tsou8181b012013-08-20 21:17:19 -04001115 val = rxBurst[i].norm2();
1116 if (val > max) {
1117 max = val;
1118 _index = i;
1119 amp = rxBurst[i];
1120 }
1121 }
1122
1123 if (index)
1124 *index = (float) _index;
1125
1126 return amp;
1127}
1128
Tom Tsou70134a02017-06-12 14:23:53 -07001129static complex peakDetect(const signalVector &rxBurst,
1130 float *peakIndex, float *avgPwr)
dburgessb3a0ca42011-10-12 07:44:40 +00001131{
dburgessb3a0ca42011-10-12 07:44:40 +00001132 complex maxVal = 0.0;
1133 float maxIndex = -1;
1134 float sumPower = 0.0;
1135
1136 for (unsigned int i = 0; i < rxBurst.size(); i++) {
1137 float samplePower = rxBurst[i].norm2();
1138 if (samplePower > maxVal.real()) {
1139 maxVal = samplePower;
1140 maxIndex = i;
1141 }
1142 sumPower += samplePower;
1143 }
1144
1145 // interpolate around the peak
1146 // to save computation, we'll use early-late balancing
1147 float earlyIndex = maxIndex-1;
1148 float lateIndex = maxIndex+1;
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001149
dburgessb3a0ca42011-10-12 07:44:40 +00001150 float incr = 0.5;
1151 while (incr > 1.0/1024.0) {
1152 complex earlyP = interpolatePoint(rxBurst,earlyIndex);
1153 complex lateP = interpolatePoint(rxBurst,lateIndex);
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001154 if (earlyP < lateP)
dburgessb3a0ca42011-10-12 07:44:40 +00001155 earlyIndex += incr;
1156 else if (earlyP > lateP)
1157 earlyIndex -= incr;
1158 else break;
1159 incr /= 2.0;
1160 lateIndex = earlyIndex + 2.0;
1161 }
1162
1163 maxIndex = earlyIndex + 1.0;
1164 maxVal = interpolatePoint(rxBurst,maxIndex);
1165
1166 if (peakIndex!=NULL)
1167 *peakIndex = maxIndex;
1168
1169 if (avgPwr!=NULL)
1170 *avgPwr = (sumPower-maxVal.norm2()) / (rxBurst.size()-1);
1171
1172 return maxVal;
1173
1174}
1175
1176void scaleVector(signalVector &x,
1177 complex scale)
1178{
Thomas Tsou7e4e5362013-10-30 21:18:55 -04001179#ifdef HAVE_NEON
1180 int len = x.size();
1181
1182 scale_complex((float *) x.begin(),
1183 (float *) x.begin(),
1184 (float *) &scale, len);
1185#else
dburgessb3a0ca42011-10-12 07:44:40 +00001186 signalVector::iterator xP = x.begin();
1187 signalVector::iterator xPEnd = x.end();
Thomas Tsou20eb6d62013-11-09 14:30:41 -05001188 if (!x.isReal()) {
dburgessb3a0ca42011-10-12 07:44:40 +00001189 while (xP < xPEnd) {
1190 *xP = *xP * scale;
1191 xP++;
1192 }
1193 }
1194 else {
1195 while (xP < xPEnd) {
1196 *xP = xP->real() * scale;
1197 xP++;
1198 }
1199 }
Thomas Tsou7e4e5362013-10-30 21:18:55 -04001200#endif
dburgessb3a0ca42011-10-12 07:44:40 +00001201}
1202
1203/** in-place conjugation */
Tom Tsou70134a02017-06-12 14:23:53 -07001204static void conjugateVector(signalVector &x)
dburgessb3a0ca42011-10-12 07:44:40 +00001205{
Thomas Tsou20eb6d62013-11-09 14:30:41 -05001206 if (x.isReal()) return;
dburgessb3a0ca42011-10-12 07:44:40 +00001207 signalVector::iterator xP = x.begin();
1208 signalVector::iterator xPEnd = x.end();
1209 while (xP < xPEnd) {
1210 *xP = xP->conj();
1211 xP++;
1212 }
1213}
1214
Tom Tsou2079a3c2016-03-06 00:58:56 -08001215static bool generateMidamble(int sps, int tsc)
dburgessb3a0ca42011-10-12 07:44:40 +00001216{
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001217 bool status = true;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001218 float toa;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001219 complex *data = NULL;
1220 signalVector *autocorr = NULL, *midamble = NULL;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001221 signalVector *midMidamble = NULL, *_midMidamble = NULL;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001222
Thomas Tsou3eaae802013-08-20 19:31:14 -04001223 if ((tsc < 0) || (tsc > 7))
dburgessb3a0ca42011-10-12 07:44:40 +00001224 return false;
1225
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001226 delete gMidambles[tsc];
Thomas Tsou3eaae802013-08-20 19:31:14 -04001227
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001228 /* Use middle 16 bits of each TSC. Correlation sequence is not pulse shaped */
1229 midMidamble = modulateBurst(gTrainingSequence[tsc].segment(5,16), 0, sps, true);
1230 if (!midMidamble)
1231 return false;
1232
Thomas Tsou3eaae802013-08-20 19:31:14 -04001233 /* Simulated receive sequence is pulse shaped */
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001234 midamble = modulateBurst(gTrainingSequence[tsc], 0, sps, false);
1235 if (!midamble) {
1236 status = false;
1237 goto release;
dburgessb3a0ca42011-10-12 07:44:40 +00001238 }
Thomas Tsou3eaae802013-08-20 19:31:14 -04001239
dburgessb3a0ca42011-10-12 07:44:40 +00001240 // NOTE: Because ideal TSC 16-bit midamble is 66 symbols into burst,
1241 // the ideal TSC has an + 180 degree phase shift,
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001242 // due to the pi/2 frequency shift, that
dburgessb3a0ca42011-10-12 07:44:40 +00001243 // needs to be accounted for.
1244 // 26-midamble is 61 symbols into burst, has +90 degree phase shift.
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001245 scaleVector(*midMidamble, complex(-1.0, 0.0));
1246 scaleVector(*midamble, complex(0.0, 1.0));
dburgessb3a0ca42011-10-12 07:44:40 +00001247
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001248 conjugateVector(*midMidamble);
dburgessb3a0ca42011-10-12 07:44:40 +00001249
Thomas Tsou3eaae802013-08-20 19:31:14 -04001250 /* For SSE alignment, reallocate the midamble sequence on 16-byte boundary */
1251 data = (complex *) convolve_h_alloc(midMidamble->size());
Pau Espin Pedrolf7331762018-12-03 17:46:04 +01001252 _midMidamble = new signalVector(data, 0, midMidamble->size(), convolve_h_alloc, free);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001253 _midMidamble->setAligned(true);
Pau Espin Pedrol5e68cde2018-08-30 20:45:14 +02001254 midMidamble->copyTo(*_midMidamble);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001255
1256 autocorr = convolve(midamble, _midMidamble, NULL, NO_DELAY);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001257 if (!autocorr) {
1258 status = false;
1259 goto release;
1260 }
dburgessb3a0ca42011-10-12 07:44:40 +00001261
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001262 gMidambles[tsc] = new CorrelationSequence;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001263 gMidambles[tsc]->sequence = _midMidamble;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001264 gMidambles[tsc]->gain = peakDetect(*autocorr, &toa, NULL);
1265
1266 /* For 1 sps only
1267 * (Half of correlation length - 1) + midpoint of pulse shape + remainder
1268 * 13.5 = (16 / 2 - 1) + 1.5 + (26 - 10) / 2
1269 */
1270 if (sps == 1)
1271 gMidambles[tsc]->toa = toa - 13.5;
1272 else
1273 gMidambles[tsc]->toa = 0;
dburgessb3a0ca42011-10-12 07:44:40 +00001274
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001275release:
dburgessb3a0ca42011-10-12 07:44:40 +00001276 delete autocorr;
1277 delete midamble;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001278 delete midMidamble;
dburgessb3a0ca42011-10-12 07:44:40 +00001279
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001280 if (!status) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001281 delete _midMidamble;
1282 free(data);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001283 gMidambles[tsc] = NULL;
1284 }
1285
1286 return status;
dburgessb3a0ca42011-10-12 07:44:40 +00001287}
1288
Tom Tsou70134a02017-06-12 14:23:53 -07001289static CorrelationSequence *generateEdgeMidamble(int tsc)
Tom Tsoud3253432016-03-06 03:08:01 -08001290{
1291 complex *data = NULL;
1292 signalVector *midamble = NULL, *_midamble = NULL;
1293 CorrelationSequence *seq;
1294
1295 if ((tsc < 0) || (tsc > 7))
1296 return NULL;
1297
1298 /* Use middle 48 bits of each TSC. Correlation sequence is not pulse shaped */
1299 const BitVector *bits = &gEdgeTrainingSequence[tsc];
1300 midamble = modulateEdgeBurst(bits->segment(15, 48), 1, true);
1301 if (!midamble)
1302 return NULL;
1303
1304 conjugateVector(*midamble);
1305
1306 data = (complex *) convolve_h_alloc(midamble->size());
Pau Espin Pedrolf7331762018-12-03 17:46:04 +01001307 _midamble = new signalVector(data, 0, midamble->size(), convolve_h_alloc, free);
Tom Tsoud3253432016-03-06 03:08:01 -08001308 _midamble->setAligned(true);
Pau Espin Pedrol5e68cde2018-08-30 20:45:14 +02001309 midamble->copyTo(*_midamble);
Tom Tsoud3253432016-03-06 03:08:01 -08001310
1311 /* Channel gain is an empirically measured value */
1312 seq = new CorrelationSequence;
Tom Tsoud3253432016-03-06 03:08:01 -08001313 seq->sequence = _midamble;
1314 seq->gain = Complex<float>(-19.6432, 19.5006) / 1.18;
1315 seq->toa = 0;
1316
1317 delete midamble;
1318
1319 return seq;
1320}
1321
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001322static bool generateRACHSequence(CorrelationSequence **seq, const BitVector &bv, int sps)
dburgessb3a0ca42011-10-12 07:44:40 +00001323{
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001324 bool status = true;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001325 float toa;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001326 complex *data = NULL;
1327 signalVector *autocorr = NULL;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001328 signalVector *seq0 = NULL, *seq1 = NULL, *_seq1 = NULL;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001329
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001330 if (*seq != NULL)
1331 delete *seq;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001332
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001333 seq0 = modulateBurst(bv, 0, sps, false);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001334 if (!seq0)
1335 return false;
1336
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001337 seq1 = modulateBurst(bv.segment(0, 40), 0, sps, true);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001338 if (!seq1) {
1339 status = false;
1340 goto release;
dburgessb3a0ca42011-10-12 07:44:40 +00001341 }
1342
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001343 conjugateVector(*seq1);
dburgessb3a0ca42011-10-12 07:44:40 +00001344
Thomas Tsou3eaae802013-08-20 19:31:14 -04001345 /* For SSE alignment, reallocate the midamble sequence on 16-byte boundary */
1346 data = (complex *) convolve_h_alloc(seq1->size());
Pau Espin Pedrolf7331762018-12-03 17:46:04 +01001347 _seq1 = new signalVector(data, 0, seq1->size(), convolve_h_alloc, free);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001348 _seq1->setAligned(true);
Pau Espin Pedrol5e68cde2018-08-30 20:45:14 +02001349 seq1->copyTo(*_seq1);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001350
1351 autocorr = convolve(seq0, _seq1, autocorr, NO_DELAY);
1352 if (!autocorr) {
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001353 status = false;
1354 goto release;
1355 }
dburgessb3a0ca42011-10-12 07:44:40 +00001356
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001357 *seq = new CorrelationSequence;
1358 (*seq)->sequence = _seq1;
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001359 (*seq)->gain = peakDetect(*autocorr, &toa, NULL);
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001360
1361 /* For 1 sps only
1362 * (Half of correlation length - 1) + midpoint of pulse shaping filer
1363 * 20.5 = (40 / 2 - 1) + 1.5
1364 */
1365 if (sps == 1)
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001366 (*seq)->toa = toa - 20.5;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001367 else
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001368 (*seq)->toa = 0.0;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001369
1370release:
dburgessb3a0ca42011-10-12 07:44:40 +00001371 delete autocorr;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001372 delete seq0;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001373 delete seq1;
dburgessb3a0ca42011-10-12 07:44:40 +00001374
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001375 if (!status) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001376 delete _seq1;
1377 free(data);
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001378 *seq = NULL;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001379 }
dburgessb3a0ca42011-10-12 07:44:40 +00001380
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001381 return status;
dburgessb3a0ca42011-10-12 07:44:40 +00001382}
Thomas Tsou3eaae802013-08-20 19:31:14 -04001383
Tom Tsoua84e1622016-06-29 14:50:25 -07001384/*
1385 * Peak-to-average computation +/- range from peak in symbols
1386 */
1387#define COMPUTE_PEAK_MIN 2
1388#define COMPUTE_PEAK_MAX 5
1389
1390/*
1391 * Minimum number of values needed to compute peak-to-average
1392 */
1393#define COMPUTE_PEAK_CNT 5
1394
Thomas Tsou865bca42013-08-21 20:58:00 -04001395static float computePeakRatio(signalVector *corr,
1396 int sps, float toa, complex amp)
dburgessb3a0ca42011-10-12 07:44:40 +00001397{
Thomas Tsou865bca42013-08-21 20:58:00 -04001398 int num = 0;
1399 complex *peak;
1400 float rms, avg = 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001401
Thomas Tsou865bca42013-08-21 20:58:00 -04001402 /* Check for bogus results */
1403 if ((toa < 0.0) || (toa > corr->size()))
1404 return 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001405
Alexander Chemeris1e9b4d52015-06-04 19:05:28 -04001406 peak = corr->begin() + (int) rint(toa);
1407
Tom Tsoua84e1622016-06-29 14:50:25 -07001408 for (int i = COMPUTE_PEAK_MIN * sps; i <= COMPUTE_PEAK_MAX * sps; i++) {
Thomas Tsou865bca42013-08-21 20:58:00 -04001409 if (peak - i >= corr->begin()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001410 avg += (peak - i)->norm2();
1411 num++;
1412 }
Thomas Tsou865bca42013-08-21 20:58:00 -04001413 if (peak + i < corr->end()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001414 avg += (peak + i)->norm2();
1415 num++;
1416 }
dburgessb3a0ca42011-10-12 07:44:40 +00001417 }
1418
Tom Tsoua84e1622016-06-29 14:50:25 -07001419 if (num < COMPUTE_PEAK_CNT)
Thomas Tsou865bca42013-08-21 20:58:00 -04001420 return 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001421
Thomas Tsou3eaae802013-08-20 19:31:14 -04001422 rms = sqrtf(avg / (float) num) + 0.00001;
dburgessb3a0ca42011-10-12 07:44:40 +00001423
Thomas Tsou865bca42013-08-21 20:58:00 -04001424 return (amp.abs()) / rms;
dburgessb3a0ca42011-10-12 07:44:40 +00001425}
1426
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001427float energyDetect(const signalVector &rxBurst, unsigned windowLength)
dburgessb3a0ca42011-10-12 07:44:40 +00001428{
1429
1430 signalVector::const_iterator windowItr = rxBurst.begin(); //+rxBurst.size()/2 - 5*windowLength/2;
1431 float energy = 0.0;
Tom Tsou2af14402017-03-23 14:54:00 -07001432 if (windowLength == 0) return 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001433 if (windowLength > rxBurst.size()) windowLength = rxBurst.size();
1434 for (unsigned i = 0; i < windowLength; i++) {
1435 energy += windowItr->norm2();
1436 windowItr+=4;
1437 }
Alexander Chemeris1dd05cf2017-03-15 23:23:36 +03001438 return energy/windowLength;
dburgessb3a0ca42011-10-12 07:44:40 +00001439}
dburgessb3a0ca42011-10-12 07:44:40 +00001440
Tom Tsou70134a02017-06-12 14:23:53 -07001441static signalVector *downsampleBurst(const signalVector &burst)
1442{
Tom Tsou7278a872017-06-14 14:50:39 -07001443 signalVector in(DOWNSAMPLE_IN_LEN, dnsampler->len());
1444 signalVector *out = new signalVector(DOWNSAMPLE_OUT_LEN);
Pau Espin Pedrol5e68cde2018-08-30 20:45:14 +02001445 burst.copyToSegment(in, 0, DOWNSAMPLE_IN_LEN);
Tom Tsou70134a02017-06-12 14:23:53 -07001446
Tom Tsou7278a872017-06-14 14:50:39 -07001447 if (dnsampler->rotate((float *) in.begin(), DOWNSAMPLE_IN_LEN,
Tom Tsou70134a02017-06-12 14:23:53 -07001448 (float *) out->begin(), DOWNSAMPLE_OUT_LEN) < 0) {
1449 delete out;
1450 out = NULL;
1451 }
1452
Tom Tsou70134a02017-06-12 14:23:53 -07001453 return out;
1454};
1455
Thomas Tsou865bca42013-08-21 20:58:00 -04001456/*
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001457 * Computes C/I (Carrier-to-Interference ratio) in dB (deciBels).
1458 * It is computed from the training sequence of each received burst,
1459 * by comparing the "ideal" training sequence with the actual one.
1460 */
1461static float computeCI(const signalVector *burst, CorrelationSequence *sync,
1462 float toa, int start, complex xcorr)
1463{
1464 float S, C;
1465 int ps;
1466
1467 /* Integer position where the sequence starts */
1468 ps = start + 1 - sync->sequence->size() + (int)roundf(toa);
1469
1470 /* Estimate Signal power */
1471 S = 0.0f;
1472 for (int i=0, j=ps; i<(int)sync->sequence->size(); i++,j++)
1473 S += (*burst)[j].norm2();
1474 S /= sync->sequence->size();
1475
1476 /* Esimate Carrier power */
1477 C = xcorr.norm2() / ((sync->sequence->size() - 1) * sync->gain.abs());
1478
1479 /* Interference = Signal - Carrier, so C/I = C / (S - C) */
1480 return 3.0103f * log2f(C / (S - C));
1481}
1482
1483/*
Thomas Tsou865bca42013-08-21 20:58:00 -04001484 * Detect a burst based on correlation and peak-to-average ratio
1485 *
1486 * For one sampler-per-symbol, perform fast peak detection (no interpolation)
1487 * for initial gating. We do this because energy detection should be disabled.
1488 * For higher oversampling values, we assume the energy detector is in place
1489 * and we run full interpolating peak detection.
1490 */
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001491static int detectBurst(const signalVector &burst,
Thomas Tsou865bca42013-08-21 20:58:00 -04001492 signalVector &corr, CorrelationSequence *sync,
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001493 float thresh, int sps, int start, int len,
1494 struct estim_burst_params *ebp)
dburgessb3a0ca42011-10-12 07:44:40 +00001495{
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001496 const signalVector *corr_in;
1497 signalVector *dec = NULL;
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001498 complex xcorr;
1499 int rc = 1;
Tom Tsoud3253432016-03-06 03:08:01 -08001500
1501 if (sps == 4) {
1502 dec = downsampleBurst(burst);
1503 corr_in = dec;
1504 sps = 1;
1505 } else {
1506 corr_in = &burst;
1507 }
1508
Thomas Tsou865bca42013-08-21 20:58:00 -04001509 /* Correlate */
Tom Tsoud3253432016-03-06 03:08:01 -08001510 if (!convolve(corr_in, sync->sequence, &corr,
Sylvain Munauta3934a12018-12-20 19:10:26 +01001511 CUSTOM, start, len)) {
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001512 rc = -1;
1513 goto del_ret;
dburgessb3a0ca42011-10-12 07:44:40 +00001514 }
1515
Tom Tsoud3253432016-03-06 03:08:01 -08001516 /* Running at the downsampled rate at this point */
1517 sps = 1;
1518
Thomas Tsou865bca42013-08-21 20:58:00 -04001519 /* Peak detection - place restrictions at correlation edges */
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001520 ebp->amp = fastPeakDetect(corr, &ebp->toa);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001521
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001522 if ((ebp->toa < 3 * sps) || (ebp->toa > len - 3 * sps)) {
1523 rc = 0;
1524 goto del_ret;
1525 }
Thomas Tsou3eaae802013-08-20 19:31:14 -04001526
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001527 /* Peak-to-average ratio */
1528 if (computePeakRatio(&corr, sps, ebp->toa, ebp->amp) < thresh) {
1529 rc = 0;
1530 goto del_ret;
1531 }
Thomas Tsou865bca42013-08-21 20:58:00 -04001532
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001533 /* Refine TOA and correlation value */
1534 xcorr = peakDetect(corr, &ebp->toa, NULL);
1535
1536 /* Compute C/I */
1537 ebp->ci = computeCI(corr_in, sync, ebp->toa, start, xcorr);
Thomas Tsou865bca42013-08-21 20:58:00 -04001538
1539 /* Normalize our channel gain */
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001540 ebp->amp = xcorr / sync->gain;
Thomas Tsou865bca42013-08-21 20:58:00 -04001541
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001542 /* Compensate for residuate time lag */
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001543 ebp->toa = ebp->toa - sync->toa;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001544
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001545del_ret:
1546 delete dec;
1547 return rc;
Thomas Tsou865bca42013-08-21 20:58:00 -04001548}
1549
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001550static float maxAmplitude(const signalVector &burst)
Tom Tsou577cd022015-05-18 13:57:54 -07001551{
Alexander Chemeris954b1182015-06-04 15:39:41 -04001552 float max = 0.0;
1553 for (size_t i = 0; i < burst.size(); i++) {
1554 if (fabs(burst[i].real()) > max)
1555 max = fabs(burst[i].real());
1556 if (fabs(burst[i].imag()) > max)
1557 max = fabs(burst[i].imag());
1558 }
Tom Tsou577cd022015-05-18 13:57:54 -07001559
Alexander Chemeris954b1182015-06-04 15:39:41 -04001560 return max;
Tom Tsou577cd022015-05-18 13:57:54 -07001561}
1562
Alexander Chemeris130a8002015-06-09 20:52:11 -04001563/*
1564 * RACH/Normal burst detection with clipping detection
Thomas Tsou865bca42013-08-21 20:58:00 -04001565 *
1566 * Correlation window parameters:
Alexander Chemeris130a8002015-06-09 20:52:11 -04001567 * target: Tail bits + burst length
1568 * head: Search symbols before target
1569 * tail: Search symbols after target
Thomas Tsou865bca42013-08-21 20:58:00 -04001570 */
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001571static int detectGeneralBurst(const signalVector &rxBurst, float thresh, int sps,
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001572 int target, int head, int tail,
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001573 CorrelationSequence *sync,
1574 struct estim_burst_params *ebp)
Thomas Tsou865bca42013-08-21 20:58:00 -04001575{
Alexander Chemeris130a8002015-06-09 20:52:11 -04001576 int rc, start, len;
Alexander Chemeris954b1182015-06-04 15:39:41 -04001577 bool clipping = false;
Thomas Tsou865bca42013-08-21 20:58:00 -04001578
1579 if ((sps != 1) && (sps != 4))
Tom Tsou577cd022015-05-18 13:57:54 -07001580 return -SIGERR_UNSUPPORTED;
1581
Alexander Chemeris954b1182015-06-04 15:39:41 -04001582 // Detect potential clipping
1583 // We still may be able to demod the burst, so we'll give it a try
1584 // and only report clipping if we can't demod.
1585 float maxAmpl = maxAmplitude(rxBurst);
1586 if (maxAmpl > CLIP_THRESH) {
1587 LOG(DEBUG) << "max burst amplitude: " << maxAmpl << " is above the clipping threshold: " << CLIP_THRESH << std::endl;
1588 clipping = true;
1589 }
Thomas Tsou865bca42013-08-21 20:58:00 -04001590
Tom Tsoud3253432016-03-06 03:08:01 -08001591 start = target - head - 1;
1592 len = head + tail;
Tom Tsou7278a872017-06-14 14:50:39 -07001593 signalVector corr(len);
Thomas Tsou865bca42013-08-21 20:58:00 -04001594
Tom Tsou7278a872017-06-14 14:50:39 -07001595 rc = detectBurst(rxBurst, corr, sync,
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001596 thresh, sps, start, len, ebp);
Thomas Tsou865bca42013-08-21 20:58:00 -04001597 if (rc < 0) {
Tom Tsou577cd022015-05-18 13:57:54 -07001598 return -SIGERR_INTERNAL;
Thomas Tsou865bca42013-08-21 20:58:00 -04001599 } else if (!rc) {
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001600 ebp->amp = 0.0f;
1601 ebp->toa = 0.0f;
Sylvain Munautb49a42e2019-05-14 18:23:29 +02001602 ebp->ci = 0.0f;
Alexander Chemeris954b1182015-06-04 15:39:41 -04001603 return clipping?-SIGERR_CLIP:SIGERR_NONE;
dburgessb3a0ca42011-10-12 07:44:40 +00001604 }
1605
Thomas Tsou865bca42013-08-21 20:58:00 -04001606 /* Subtract forward search bits from delay */
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001607 ebp->toa -= head;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001608
Thomas Tsou865bca42013-08-21 20:58:00 -04001609 return 1;
1610}
Thomas Tsou3eaae802013-08-20 19:31:14 -04001611
Alexander Chemeris130a8002015-06-09 20:52:11 -04001612
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001613/*
Alexander Chemeris130a8002015-06-09 20:52:11 -04001614 * RACH burst detection
1615 *
1616 * Correlation window parameters:
1617 * target: Tail bits + RACH length (reduced from 41 to a multiple of 4)
Tom Tsoue90c24c2016-06-21 16:14:39 -07001618 * head: Search 8 symbols before target
1619 * tail: Search 8 symbols + maximum expected delay
Alexander Chemeris130a8002015-06-09 20:52:11 -04001620 */
Tom Tsou70134a02017-06-12 14:23:53 -07001621static int detectRACHBurst(const signalVector &burst, float threshold, int sps,
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001622 unsigned max_toa, bool ext, struct estim_burst_params *ebp)
Alexander Chemeris130a8002015-06-09 20:52:11 -04001623{
1624 int rc, target, head, tail;
Vadim Yanitskiy444ff342018-10-22 02:25:23 +02001625 int i, num_seq;
Alexander Chemeris130a8002015-06-09 20:52:11 -04001626
1627 target = 8 + 40;
Tom Tsoue90c24c2016-06-21 16:14:39 -07001628 head = 8;
Alexander Chemeris14d13b62017-03-17 15:12:17 -07001629 tail = 8 + max_toa;
Vadim Yanitskiy444ff342018-10-22 02:25:23 +02001630 num_seq = ext ? 3 : 1;
Alexander Chemeris130a8002015-06-09 20:52:11 -04001631
Vadim Yanitskiy444ff342018-10-22 02:25:23 +02001632 for (i = 0; i < num_seq; i++) {
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001633 rc = detectGeneralBurst(burst, threshold, sps, target, head, tail,
1634 gRACHSequences[i], ebp);
Pau Espin Pedrolc3d68c12019-07-04 16:27:47 +02001635 if (rc > 0) {
1636 ebp->tsc = i;
Vadim Yanitskiy444ff342018-10-22 02:25:23 +02001637 break;
Pau Espin Pedrolc3d68c12019-07-04 16:27:47 +02001638 }
Vadim Yanitskiy444ff342018-10-22 02:25:23 +02001639 }
Alexander Chemeris130a8002015-06-09 20:52:11 -04001640
1641 return rc;
1642}
1643
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001644/*
Thomas Tsou865bca42013-08-21 20:58:00 -04001645 * Normal burst detection
1646 *
1647 * Correlation window parameters:
1648 * target: Tail + data + mid-midamble + 1/2 remaining midamblebits
Tom Tsoue90c24c2016-06-21 16:14:39 -07001649 * head: Search 6 symbols before target
1650 * tail: Search 6 symbols + maximum expected delay
Thomas Tsou865bca42013-08-21 20:58:00 -04001651 */
Tom Tsou70134a02017-06-12 14:23:53 -07001652static int analyzeTrafficBurst(const signalVector &burst, unsigned tsc, float threshold,
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001653 int sps, unsigned max_toa, struct estim_burst_params *ebp)
Thomas Tsou865bca42013-08-21 20:58:00 -04001654{
Alexander Chemeris130a8002015-06-09 20:52:11 -04001655 int rc, target, head, tail;
Thomas Tsou865bca42013-08-21 20:58:00 -04001656 CorrelationSequence *sync;
1657
Tom Tsouae91f132017-03-28 14:40:38 -07001658 if (tsc > 7)
Tom Tsou577cd022015-05-18 13:57:54 -07001659 return -SIGERR_UNSUPPORTED;
1660
Thomas Tsou865bca42013-08-21 20:58:00 -04001661 target = 3 + 58 + 16 + 5;
Tom Tsoue90c24c2016-06-21 16:14:39 -07001662 head = 6;
1663 tail = 6 + max_toa;
Thomas Tsou865bca42013-08-21 20:58:00 -04001664 sync = gMidambles[tsc];
Thomas Tsou865bca42013-08-21 20:58:00 -04001665
Pau Espin Pedrolc3d68c12019-07-04 16:27:47 +02001666 ebp->tsc = tsc;
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001667 rc = detectGeneralBurst(burst, threshold, sps, target, head, tail, sync, ebp);
Alexander Chemeris130a8002015-06-09 20:52:11 -04001668 return rc;
dburgessb3a0ca42011-10-12 07:44:40 +00001669}
1670
Tom Tsou70134a02017-06-12 14:23:53 -07001671static int detectEdgeBurst(const signalVector &burst, unsigned tsc, float threshold,
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001672 int sps, unsigned max_toa, struct estim_burst_params *ebp)
Tom Tsoud3253432016-03-06 03:08:01 -08001673{
1674 int rc, target, head, tail;
1675 CorrelationSequence *sync;
1676
Tom Tsouae91f132017-03-28 14:40:38 -07001677 if (tsc > 7)
Tom Tsoud3253432016-03-06 03:08:01 -08001678 return -SIGERR_UNSUPPORTED;
1679
1680 target = 3 + 58 + 16 + 5;
Tom Tsoue90c24c2016-06-21 16:14:39 -07001681 head = 6;
1682 tail = 6 + max_toa;
Tom Tsoud3253432016-03-06 03:08:01 -08001683 sync = gEdgeMidambles[tsc];
1684
Pau Espin Pedrolc3d68c12019-07-04 16:27:47 +02001685 ebp->tsc = tsc;
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001686 rc = detectGeneralBurst(burst, threshold, sps,
1687 target, head, tail, sync, ebp);
Tom Tsoud3253432016-03-06 03:08:01 -08001688 return rc;
1689}
1690
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001691int detectAnyBurst(const signalVector &burst, unsigned tsc, float threshold,
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001692 int sps, CorrType type, unsigned max_toa,
1693 struct estim_burst_params *ebp)
Alexander Chemeris4e6c9382017-03-17 15:24:18 -07001694{
1695 int rc = 0;
1696
1697 switch (type) {
1698 case EDGE:
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001699 rc = detectEdgeBurst(burst, tsc, threshold, sps, max_toa, ebp);
Alexander Chemeris4e6c9382017-03-17 15:24:18 -07001700 if (rc > 0)
1701 break;
1702 else
1703 type = TSC;
1704 case TSC:
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001705 rc = analyzeTrafficBurst(burst, tsc, threshold, sps, max_toa, ebp);
Alexander Chemeris4e6c9382017-03-17 15:24:18 -07001706 break;
Vadim Yanitskiy444ff342018-10-22 02:25:23 +02001707 case EXT_RACH:
Alexander Chemeris4e6c9382017-03-17 15:24:18 -07001708 case RACH:
Pau Espin Pedrol7ee2d102019-07-04 13:02:12 +02001709 rc = detectRACHBurst(burst, threshold, sps, max_toa, type == EXT_RACH, ebp);
Alexander Chemeris4e6c9382017-03-17 15:24:18 -07001710 break;
1711 default:
1712 LOG(ERR) << "Invalid correlation type";
1713 }
1714
1715 if (rc > 0)
1716 return type;
1717
1718 return rc;
1719}
1720
Tom Tsoud3253432016-03-06 03:08:01 -08001721/*
1722 * Soft 8-PSK decoding using Manhattan distance metric
1723 */
1724static SoftVector *softSliceEdgeBurst(signalVector &burst)
1725{
1726 size_t nsyms = 148;
1727
1728 if (burst.size() < nsyms)
1729 return NULL;
1730
1731 signalVector::iterator itr;
1732 SoftVector *bits = new SoftVector(nsyms * 3);
1733
1734 /*
1735 * Bits 0 and 1 - First and second bits of the symbol respectively
1736 */
1737 rotateBurst2(burst, -M_PI / 8.0);
1738 itr = burst.begin();
1739 for (size_t i = 0; i < nsyms; i++) {
1740 (*bits)[3 * i + 0] = -itr->imag();
1741 (*bits)[3 * i + 1] = itr->real();
1742 itr++;
1743 }
1744
1745 /*
1746 * Bit 2 - Collapse symbols into quadrant 0 (positive X and Y).
1747 * Decision area is then simplified to X=Y axis. Rotate again to
1748 * place decision boundary on X-axis.
1749 */
1750 itr = burst.begin();
1751 for (size_t i = 0; i < burst.size(); i++) {
1752 burst[i] = Complex<float>(fabs(itr->real()), fabs(itr->imag()));
1753 itr++;
1754 }
1755
1756 rotateBurst2(burst, -M_PI / 4.0);
1757 itr = burst.begin();
1758 for (size_t i = 0; i < nsyms; i++) {
1759 (*bits)[3 * i + 2] = -itr->imag();
1760 itr++;
1761 }
1762
1763 signalVector soft(bits->size());
1764 for (size_t i = 0; i < bits->size(); i++)
1765 soft[i] = (*bits)[i];
1766
1767 return bits;
1768}
1769
1770/*
Alexander Chemeris132fb242017-03-17 17:22:33 -07001771 * Convert signalVector to SoftVector by taking real part of the signal.
1772 */
1773static SoftVector *signalToSoftVector(signalVector *dec)
1774{
1775 SoftVector *bits = new SoftVector(dec->size());
1776
1777 SoftVector::iterator bit_itr = bits->begin();
1778 signalVector::iterator burst_itr = dec->begin();
1779
1780 for (; burst_itr < dec->end(); burst_itr++)
1781 *bit_itr++ = burst_itr->real();
1782
1783 return bits;
1784}
1785
1786/*
Tom Tsou7fec3032016-03-06 22:33:20 -08001787 * Shared portion of GMSK and EDGE demodulators consisting of timing
1788 * recovery and single tap channel correction. For 4 SPS (if activated),
1789 * the output is downsampled prior to the 1 SPS modulation specific
1790 * stages.
1791 */
Alexander Chemerise0c12182017-03-18 13:27:48 -07001792static signalVector *demodCommon(const signalVector &burst, int sps,
Tom Tsou7fec3032016-03-06 22:33:20 -08001793 complex chan, float toa)
1794{
1795 signalVector *delay, *dec;
1796
1797 if ((sps != 1) && (sps != 4))
1798 return NULL;
1799
Tom Tsou7fec3032016-03-06 22:33:20 -08001800 delay = delayVector(&burst, NULL, -toa * (float) sps);
Alexander Chemerise0c12182017-03-18 13:27:48 -07001801 scaleVector(*delay, (complex) 1.0 / chan);
Tom Tsou7fec3032016-03-06 22:33:20 -08001802
1803 if (sps == 1)
1804 return delay;
1805
1806 dec = downsampleBurst(*delay);
1807
1808 delete delay;
1809 return dec;
1810}
1811
1812/*
Tom Tsoud3253432016-03-06 03:08:01 -08001813 * Demodulate GSMK burst. Prior to symbol rotation, operate at
1814 * 4 SPS (if activated) to minimize distortion through the fractional
1815 * delay filters. Symbol rotation and after always operates at 1 SPS.
1816 */
Tom Tsou70134a02017-06-12 14:23:53 -07001817static SoftVector *demodGmskBurst(const signalVector &rxBurst,
1818 int sps, complex channel, float TOA)
dburgessb3a0ca42011-10-12 07:44:40 +00001819{
Thomas Tsou94edaae2013-11-09 22:19:19 -05001820 SoftVector *bits;
Tom Tsou7fec3032016-03-06 22:33:20 -08001821 signalVector *dec;
dburgessb3a0ca42011-10-12 07:44:40 +00001822
Tom Tsou7fec3032016-03-06 22:33:20 -08001823 dec = demodCommon(rxBurst, sps, channel, TOA);
1824 if (!dec)
1825 return NULL;
dburgessb3a0ca42011-10-12 07:44:40 +00001826
Tom Tsoud3253432016-03-06 03:08:01 -08001827 /* Shift up by a quarter of a frequency */
1828 GMSKReverseRotate(*dec, 1);
Alexander Chemeris132fb242017-03-17 17:22:33 -07001829 /* Take real part of the signal */
1830 bits = signalToSoftVector(dec);
Thomas Tsou94edaae2013-11-09 22:19:19 -05001831 delete dec;
dburgessb3a0ca42011-10-12 07:44:40 +00001832
Thomas Tsou94edaae2013-11-09 22:19:19 -05001833 return bits;
dburgessb3a0ca42011-10-12 07:44:40 +00001834}
Thomas Tsou94edaae2013-11-09 22:19:19 -05001835
Tom Tsoud3253432016-03-06 03:08:01 -08001836/*
1837 * Demodulate an 8-PSK burst. Prior to symbol rotation, operate at
1838 * 4 SPS (if activated) to minimize distortion through the fractional
1839 * delay filters. Symbol rotation and after always operates at 1 SPS.
1840 *
1841 * Allow 1 SPS demodulation here, but note that other parts of the
1842 * transceiver restrict EDGE operatoin to 4 SPS - 8-PSK distortion
1843 * through the fractional delay filters at 1 SPS renders signal
1844 * nearly unrecoverable.
1845 */
Tom Tsou70134a02017-06-12 14:23:53 -07001846static SoftVector *demodEdgeBurst(const signalVector &burst,
1847 int sps, complex chan, float toa)
Tom Tsoud3253432016-03-06 03:08:01 -08001848{
1849 SoftVector *bits;
Tom Tsou7fec3032016-03-06 22:33:20 -08001850 signalVector *dec, *rot, *eq;
Tom Tsoud3253432016-03-06 03:08:01 -08001851
Tom Tsou7fec3032016-03-06 22:33:20 -08001852 dec = demodCommon(burst, sps, chan, toa);
1853 if (!dec)
Tom Tsoud3253432016-03-06 03:08:01 -08001854 return NULL;
1855
Tom Tsou7fec3032016-03-06 22:33:20 -08001856 /* Equalize and derotate */
Tom Tsoud3253432016-03-06 03:08:01 -08001857 eq = convolve(dec, GSMPulse4->c0_inv, NULL, NO_DELAY);
1858 rot = derotateEdgeBurst(*eq, 1);
1859
Tom Tsou7fec3032016-03-06 22:33:20 -08001860 /* Soft slice and normalize */
Tom Tsou04795622016-04-26 21:17:36 -07001861 bits = softSliceEdgeBurst(*rot);
Tom Tsoud3253432016-03-06 03:08:01 -08001862
1863 delete dec;
1864 delete eq;
1865 delete rot;
1866
1867 return bits;
1868}
1869
Alexander Chemerise0c12182017-03-18 13:27:48 -07001870SoftVector *demodAnyBurst(const signalVector &burst, int sps, complex amp,
Alexander Chemeris6e1dffd2017-03-17 16:13:51 -07001871 float toa, CorrType type)
1872{
1873 if (type == EDGE)
1874 return demodEdgeBurst(burst, sps, amp, toa);
1875 else
1876 return demodGmskBurst(burst, sps, amp, toa);
1877}
1878
Tom Tsou2079a3c2016-03-06 00:58:56 -08001879bool sigProcLibSetup()
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001880{
Thomas Tsou0e0e1f42013-11-09 22:08:51 -05001881 generateSincTable();
Tom Tsou2079a3c2016-03-06 00:58:56 -08001882 initGMSKRotationTables();
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001883
Tom Tsou2079a3c2016-03-06 00:58:56 -08001884 GSMPulse1 = generateGSMPulse(1);
1885 GSMPulse4 = generateGSMPulse(4);
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001886
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001887 generateRACHSequence(&gRACHSequences[0], gRACHSynchSequenceTS0, 1);
1888 generateRACHSequence(&gRACHSequences[1], gRACHSynchSequenceTS1, 1);
1889 generateRACHSequence(&gRACHSequences[2], gRACHSynchSequenceTS2, 1);
1890
Tom Tsoud3253432016-03-06 03:08:01 -08001891 for (int tsc = 0; tsc < 8; tsc++) {
Tom Tsou2079a3c2016-03-06 00:58:56 -08001892 generateMidamble(1, tsc);
Tom Tsoud3253432016-03-06 03:08:01 -08001893 gEdgeMidambles[tsc] = generateEdgeMidamble(tsc);
1894 }
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001895
Thomas Tsouf79c4d02013-11-09 15:51:56 -06001896 generateDelayFilters();
1897
Tom Tsoud3253432016-03-06 03:08:01 -08001898 dnsampler = new Resampler(1, 4);
1899 if (!dnsampler->init()) {
1900 LOG(ALERT) << "Rx resampler failed to initialize";
1901 goto fail;
1902 }
1903
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001904 return true;
Tom Tsoud3253432016-03-06 03:08:01 -08001905
1906fail:
1907 sigProcLibDestroy();
1908 return false;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001909}