blob: f720828c17ae7d1eb97436e802c1a33ff25ca4b0 [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 {
Pau Espin Pedrolf7331762018-12-03 17:46:04 +010087 CorrelationSequence() : sequence(NULL)
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,
288 size_t start = 0, size_t len = 0,
289 size_t step = 1, int offset = 0)
dburgessb3a0ca42011-10-12 07:44:40 +0000290{
Thomas Tsou3f32ab52013-11-15 16:32:54 -0500291 int rc;
292 size_t head = 0, tail = 0;
Thomas Tsou3eaae802013-08-20 19:31:14 -0400293 bool alloc = false, append = false;
294 const signalVector *_x = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000295
Thomas Tsou3eaae802013-08-20 19:31:14 -0400296 if (!x || !h)
dburgessb3a0ca42011-10-12 07:44:40 +0000297 return NULL;
298
Thomas Tsou3eaae802013-08-20 19:31:14 -0400299 switch (spanType) {
300 case START_ONLY:
301 start = 0;
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500302 head = h->size() - 1;
Thomas Tsou3eaae802013-08-20 19:31:14 -0400303 len = x->size();
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500304
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500305 if (x->getStart() < head)
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500306 append = true;
dburgessb3a0ca42011-10-12 07:44:40 +0000307 break;
Thomas Tsou3eaae802013-08-20 19:31:14 -0400308 case NO_DELAY:
309 start = h->size() / 2;
310 head = start;
311 tail = start;
312 len = x->size();
313 append = true;
314 break;
315 case CUSTOM:
316 if (start < h->size() - 1) {
317 head = h->size() - start;
318 append = true;
319 }
320 if (start + len > x->size()) {
321 tail = start + len - x->size();
322 append = true;
dburgessb3a0ca42011-10-12 07:44:40 +0000323 }
324 break;
325 default:
326 return NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000327 }
dburgessb3a0ca42011-10-12 07:44:40 +0000328
Thomas Tsou3eaae802013-08-20 19:31:14 -0400329 /*
330 * Error if the output vector is too small. Create the output vector
331 * if the pointer is NULL.
332 */
333 if (y && (len > y->size()))
334 return NULL;
335 if (!y) {
Pau Espin Pedrolf7331762018-12-03 17:46:04 +0100336 y = new signalVector(len, convolve_h_alloc, free);
Thomas Tsou3eaae802013-08-20 19:31:14 -0400337 alloc = true;
338 }
339
340 /* Prepend or post-pend the input vector if the parameters require it */
341 if (append)
342 _x = new signalVector(*x, head, tail);
343 else
344 _x = x;
345
346 /*
347 * Four convovle types:
348 * 1. Complex-Real (aligned)
349 * 2. Complex-Complex (aligned)
350 * 3. Complex-Real (!aligned)
351 * 4. Complex-Complex (!aligned)
352 */
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500353 if (h->isReal() && h->isAligned()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -0400354 rc = convolve_real((float *) _x->begin(), _x->size(),
355 (float *) h->begin(), h->size(),
356 (float *) y->begin(), y->size(),
357 start, len, step, offset);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500358 } else if (!h->isReal() && h->isAligned()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -0400359 rc = convolve_complex((float *) _x->begin(), _x->size(),
360 (float *) h->begin(), h->size(),
361 (float *) y->begin(), y->size(),
362 start, len, step, offset);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500363 } else if (h->isReal() && !h->isAligned()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -0400364 rc = base_convolve_real((float *) _x->begin(), _x->size(),
365 (float *) h->begin(), h->size(),
366 (float *) y->begin(), y->size(),
367 start, len, step, offset);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500368 } else if (!h->isReal() && !h->isAligned()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -0400369 rc = base_convolve_complex((float *) _x->begin(), _x->size(),
370 (float *) h->begin(), h->size(),
371 (float *) y->begin(), y->size(),
372 start, len, step, offset);
373 } else {
374 rc = -1;
375 }
376
377 if (append)
378 delete _x;
379
380 if (rc < 0) {
381 if (alloc)
382 delete y;
383 return NULL;
384 }
385
386 return y;
387}
dburgessb3a0ca42011-10-12 07:44:40 +0000388
Tom Tsoud3253432016-03-06 03:08:01 -0800389/*
390 * Generate static EDGE linear equalizer. This equalizer is not adaptive.
391 * Filter taps are generated from the inverted 1 SPS impulse response of
392 * the EDGE pulse shape captured after the downsampling filter.
393 */
394static bool generateInvertC0Pulse(PulseSequence *pulse)
395{
396 if (!pulse)
397 return false;
398
Pau Espin Pedrolf7331762018-12-03 17:46:04 +0100399 pulse->c0_inv = new signalVector((complex *) convolve_h_alloc(5), 0, 5, convolve_h_alloc, free);
Tom Tsoud3253432016-03-06 03:08:01 -0800400 pulse->c0_inv->isReal(true);
401 pulse->c0_inv->setAligned(false);
402
403 signalVector::iterator xP = pulse->c0_inv->begin();
404 *xP++ = 0.15884;
405 *xP++ = -0.43176;
406 *xP++ = 1.00000;
407 *xP++ = -0.42608;
408 *xP++ = 0.14882;
409
410 return true;
411}
412
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400413static bool generateC1Pulse(int sps, PulseSequence *pulse)
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800414{
415 int len;
416
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400417 if (!pulse)
418 return false;
419
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800420 switch (sps) {
421 case 4:
422 len = 8;
423 break;
424 default:
425 return false;
426 }
427
Pau Espin Pedrolf7331762018-12-03 17:46:04 +0100428 pulse->c1 = new signalVector((complex *) convolve_h_alloc(len), 0, len, convolve_h_alloc, free);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500429 pulse->c1->isReal(true);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800430
431 /* Enable alignment for SSE usage */
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400432 pulse->c1->setAligned(true);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800433
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400434 signalVector::iterator xP = pulse->c1->begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800435
436 switch (sps) {
437 case 4:
438 /* BT = 0.30 */
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400439 *xP++ = 0.0;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800440 *xP++ = 8.16373112e-03;
441 *xP++ = 2.84385729e-02;
442 *xP++ = 5.64158904e-02;
443 *xP++ = 7.05463553e-02;
444 *xP++ = 5.64158904e-02;
445 *xP++ = 2.84385729e-02;
446 *xP++ = 8.16373112e-03;
447 }
448
449 return true;
450}
451
Tom Tsou2079a3c2016-03-06 00:58:56 -0800452static PulseSequence *generateGSMPulse(int sps)
dburgessb3a0ca42011-10-12 07:44:40 +0000453{
Thomas Tsou83e06892013-08-20 16:10:01 -0400454 int len;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800455 float arg, avg, center;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400456 PulseSequence *pulse;
Thomas Tsou83e06892013-08-20 16:10:01 -0400457
Tom Tsoud3253432016-03-06 03:08:01 -0800458 if ((sps != 1) && (sps != 4))
459 return NULL;
460
Thomas Tsou83e06892013-08-20 16:10:01 -0400461 /* Store a single tap filter used for correlation sequence generation */
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400462 pulse = new PulseSequence();
463 pulse->empty = new signalVector(1);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500464 pulse->empty->isReal(true);
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400465 *(pulse->empty->begin()) = 1.0f;
Thomas Tsou83e06892013-08-20 16:10:01 -0400466
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400467 /*
468 * For 4 samples-per-symbol use a precomputed single pulse Laurent
469 * approximation. This should yields below 2 degrees of phase error at
470 * the modulator output. Use the existing pulse approximation for all
471 * other oversampling factors.
472 */
473 switch (sps) {
474 case 4:
475 len = 16;
476 break;
Tom Tsoud3253432016-03-06 03:08:01 -0800477 case 1:
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400478 default:
Tom Tsou2079a3c2016-03-06 00:58:56 -0800479 len = 4;
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400480 }
Thomas Tsou3eaae802013-08-20 19:31:14 -0400481
Pau Espin Pedrolf7331762018-12-03 17:46:04 +0100482 pulse->c0 = new signalVector((complex *) convolve_h_alloc(len), 0, len, convolve_h_alloc, free);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500483 pulse->c0->isReal(true);
Thomas Tsou3eaae802013-08-20 19:31:14 -0400484
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800485 /* Enable alingnment for SSE usage */
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400486 pulse->c0->setAligned(true);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800487
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400488 signalVector::iterator xP = pulse->c0->begin();
Thomas Tsou83e06892013-08-20 16:10:01 -0400489
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400490 if (sps == 4) {
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800491 *xP++ = 0.0;
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400492 *xP++ = 4.46348606e-03;
493 *xP++ = 2.84385729e-02;
494 *xP++ = 1.03184855e-01;
495 *xP++ = 2.56065552e-01;
496 *xP++ = 4.76375085e-01;
497 *xP++ = 7.05961177e-01;
498 *xP++ = 8.71291644e-01;
499 *xP++ = 9.29453645e-01;
500 *xP++ = 8.71291644e-01;
501 *xP++ = 7.05961177e-01;
502 *xP++ = 4.76375085e-01;
503 *xP++ = 2.56065552e-01;
504 *xP++ = 1.03184855e-01;
505 *xP++ = 2.84385729e-02;
506 *xP++ = 4.46348606e-03;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400507 generateC1Pulse(sps, pulse);
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400508 } else {
509 center = (float) (len - 1.0) / 2.0;
Thomas Tsou83e06892013-08-20 16:10:01 -0400510
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400511 /* GSM pulse approximation */
512 for (int i = 0; i < len; i++) {
513 arg = ((float) i - center) / (float) sps;
514 *xP++ = 0.96 * exp(-1.1380 * arg * arg -
515 0.527 * arg * arg * arg * arg);
516 }
dburgessb3a0ca42011-10-12 07:44:40 +0000517
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400518 avg = sqrtf(vectorNorm2(*pulse->c0) / sps);
519 xP = pulse->c0->begin();
520 for (int i = 0; i < len; i++)
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800521 *xP++ /= avg;
522 }
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400523
Tom Tsoud3253432016-03-06 03:08:01 -0800524 /*
525 * Current form of the EDGE equalization filter non-realizable at 4 SPS.
526 * Load the onto both 1 SPS and 4 SPS objects for convenience. Note that
527 * the EDGE demodulator downsamples to 1 SPS prior to equalization.
528 */
529 generateInvertC0Pulse(pulse);
530
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400531 return pulse;
dburgessb3a0ca42011-10-12 07:44:40 +0000532}
533
Tom Tsoud3253432016-03-06 03:08:01 -0800534bool vectorSlicer(SoftVector *x)
535{
536 SoftVector::iterator xP = x->begin();
537 SoftVector::iterator xPEnd = x->end();
538 while (xP < xPEnd) {
539 *xP = 0.5 * (*xP + 1.0f);
540 if (*xP > 1.0)
541 *xP = 1.0;
542 if (*xP < 0.0)
543 *xP = 0.0;
544 xP++;
545 }
546 return true;
547}
548
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800549static signalVector *rotateBurst(const BitVector &wBurst,
550 int guardPeriodLength, int sps)
551{
552 int burst_len;
Tom Tsou7278a872017-06-14 14:50:39 -0700553 signalVector *pulse, rotated;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800554 signalVector::iterator itr;
555
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400556 pulse = GSMPulse1->empty;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800557 burst_len = sps * (wBurst.size() + guardPeriodLength);
558 rotated = signalVector(burst_len);
559 itr = rotated.begin();
560
561 for (unsigned i = 0; i < wBurst.size(); i++) {
562 *itr = 2.0 * (wBurst[i] & 0x01) - 1.0;
563 itr += sps;
564 }
565
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400566 GMSKRotate(rotated, sps);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500567 rotated.isReal(false);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800568
569 /* Dummy filter operation */
Tom Tsou7278a872017-06-14 14:50:39 -0700570 return convolve(&rotated, pulse, NULL, START_ONLY);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800571}
572
Tom Tsoud3253432016-03-06 03:08:01 -0800573static void rotateBurst2(signalVector &burst, double phase)
574{
575 Complex<float> rot = Complex<float>(cos(phase), sin(phase));
576
577 for (size_t i = 0; i < burst.size(); i++)
578 burst[i] = burst[i] * rot;
579}
580
Tom Tsou4dfd64a2016-03-06 20:31:51 -0800581/*
582 * Ignore the guard length argument in the GMSK modulator interface
583 * because it results in 624/628 sized bursts instead of the preferred
584 * burst length of 625. Only 4 SPS is supported.
585 */
586static signalVector *modulateBurstLaurent(const BitVector &bits)
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800587{
Tom Tsou4dfd64a2016-03-06 20:31:51 -0800588 int burst_len, sps = 4;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800589 float phase;
Tom Tsou7278a872017-06-14 14:50:39 -0700590 signalVector *c0_pulse, *c1_pulse, *c0_shaped, *c1_shaped;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800591 signalVector::iterator c0_itr, c1_itr;
592
Tom Tsou2079a3c2016-03-06 00:58:56 -0800593 c0_pulse = GSMPulse4->c0;
594 c1_pulse = GSMPulse4->c1;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800595
Tom Tsou4dfd64a2016-03-06 20:31:51 -0800596 if (bits.size() > 156)
597 return NULL;
598
599 burst_len = 625;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800600
Tom Tsou7278a872017-06-14 14:50:39 -0700601 signalVector c0_burst(burst_len, c0_pulse->size());
602 c0_burst.isReal(true);
603 c0_itr = c0_burst.begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800604
Tom Tsou7278a872017-06-14 14:50:39 -0700605 signalVector c1_burst(burst_len, c1_pulse->size());
606 c1_itr = c1_burst.begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800607
Tom Tsouaa15d622016-08-11 14:36:23 -0700608 /* Padded differential tail bits */
609 *c0_itr = 2.0 * (0x00 & 0x01) - 1.0;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800610 c0_itr += sps;
611
612 /* Main burst bits */
613 for (unsigned i = 0; i < bits.size(); i++) {
614 *c0_itr = 2.0 * (bits[i] & 0x01) - 1.0;
615 c0_itr += sps;
616 }
617
Tom Tsouaa15d622016-08-11 14:36:23 -0700618 /* Padded differential tail bits */
619 *c0_itr = 2.0 * (0x00 & 0x01) - 1.0;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800620
621 /* Generate C0 phase coefficients */
Tom Tsou7278a872017-06-14 14:50:39 -0700622 GMSKRotate(c0_burst, sps);
623 c0_burst.isReal(false);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800624
Tom Tsou7278a872017-06-14 14:50:39 -0700625 c0_itr = c0_burst.begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800626 c0_itr += sps * 2;
627 c1_itr += sps * 2;
628
629 /* Start magic */
630 phase = 2.0 * ((0x01 & 0x01) ^ (0x01 & 0x01)) - 1.0;
631 *c1_itr = *c0_itr * Complex<float>(0, phase);
632 c0_itr += sps;
633 c1_itr += sps;
634
635 /* Generate C1 phase coefficients */
636 for (unsigned i = 2; i < bits.size(); i++) {
637 phase = 2.0 * ((bits[i - 1] & 0x01) ^ (bits[i - 2] & 0x01)) - 1.0;
638 *c1_itr = *c0_itr * Complex<float>(0, phase);
639
640 c0_itr += sps;
641 c1_itr += sps;
642 }
643
644 /* End magic */
645 int i = bits.size();
646 phase = 2.0 * ((bits[i-1] & 0x01) ^ (bits[i-2] & 0x01)) - 1.0;
647 *c1_itr = *c0_itr * Complex<float>(0, phase);
648
649 /* Primary (C0) and secondary (C1) pulse shaping */
Tom Tsou7278a872017-06-14 14:50:39 -0700650 c0_shaped = convolve(&c0_burst, c0_pulse, NULL, START_ONLY);
651 c1_shaped = convolve(&c1_burst, c1_pulse, NULL, START_ONLY);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800652
653 /* Sum shaped outputs into C0 */
654 c0_itr = c0_shaped->begin();
655 c1_itr = c1_shaped->begin();
656 for (unsigned i = 0; i < c0_shaped->size(); i++ )
657 *c0_itr++ += *c1_itr++;
658
659 delete c1_shaped;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800660 return c0_shaped;
661}
662
Tom Tsoud3253432016-03-06 03:08:01 -0800663static signalVector *rotateEdgeBurst(const signalVector &symbols, int sps)
664{
665 signalVector *burst;
666 signalVector::iterator burst_itr;
667
668 burst = new signalVector(symbols.size() * sps);
669 burst_itr = burst->begin();
670
671 for (size_t i = 0; i < symbols.size(); i++) {
672 float phase = i * 3.0f * M_PI / 8.0f;
673 Complex<float> rot = Complex<float>(cos(phase), sin(phase));
674
675 *burst_itr = symbols[i] * rot;
676 burst_itr += sps;
677 }
678
679 return burst;
680}
681
682static signalVector *derotateEdgeBurst(const signalVector &symbols, int sps)
683{
684 signalVector *burst;
685 signalVector::iterator burst_itr;
686
687 if (symbols.size() % sps)
688 return NULL;
689
690 burst = new signalVector(symbols.size() / sps);
691 burst_itr = burst->begin();
692
693 for (size_t i = 0; i < burst->size(); i++) {
694 float phase = (float) (i % 16) * 3.0f * M_PI / 8.0f;
695 Complex<float> rot = Complex<float>(cosf(phase), -sinf(phase));
696
697 *burst_itr = symbols[sps * i] * rot;
698 burst_itr++;
699 }
700
701 return burst;
702}
703
704static signalVector *mapEdgeSymbols(const BitVector &bits)
705{
706 if (bits.size() % 3)
707 return NULL;
708
709 signalVector *symbols = new signalVector(bits.size() / 3);
710
711 for (size_t i = 0; i < symbols->size(); i++) {
712 unsigned index = (((unsigned) bits[3 * i + 0] & 0x01) << 0) |
713 (((unsigned) bits[3 * i + 1] & 0x01) << 1) |
714 (((unsigned) bits[3 * i + 2] & 0x01) << 2);
715
716 (*symbols)[i] = psk8_table[index];
717 }
718
719 return symbols;
720}
721
Tom Tsoud2b07032016-04-26 19:28:59 -0700722/*
723 * EDGE 8-PSK rotate and pulse shape
724 *
725 * Delay the EDGE downlink bursts by one symbol in order to match GMSK pulse
726 * shaping group delay. The difference in group delay arises from the dual
727 * pulse filter combination of the GMSK Laurent represenation whereas 8-PSK
728 * uses a single pulse linear filter.
729 */
Tom Tsoud3253432016-03-06 03:08:01 -0800730static signalVector *shapeEdgeBurst(const signalVector &symbols)
731{
Tom Tsoud2b07032016-04-26 19:28:59 -0700732 size_t nsyms, nsamps = 625, sps = 4;
Tom Tsoud3253432016-03-06 03:08:01 -0800733 signalVector::iterator burst_itr;
734
735 nsyms = symbols.size();
736
Tom Tsoud2b07032016-04-26 19:28:59 -0700737 if (nsyms * sps > nsamps)
Tom Tsoud3253432016-03-06 03:08:01 -0800738 nsyms = 156;
739
Tom Tsou7278a872017-06-14 14:50:39 -0700740 signalVector burst(nsamps, GSMPulse4->c0->size());
Tom Tsoud3253432016-03-06 03:08:01 -0800741
Tom Tsoud2b07032016-04-26 19:28:59 -0700742 /* Delay burst by 1 symbol */
Tom Tsou7278a872017-06-14 14:50:39 -0700743 burst_itr = burst.begin() + sps;
Tom Tsou06676ea2016-07-19 12:50:21 -0700744 for (size_t i = 0; i < nsyms; i++) {
Tom Tsoud3253432016-03-06 03:08:01 -0800745 float phase = i * 3.0f * M_PI / 8.0f;
746 Complex<float> rot = Complex<float>(cos(phase), sin(phase));
747
748 *burst_itr = symbols[i] * rot;
Tom Tsoud2b07032016-04-26 19:28:59 -0700749 burst_itr += sps;
Tom Tsoud3253432016-03-06 03:08:01 -0800750 }
751
752 /* Single Gaussian pulse approximation shaping */
Tom Tsou7278a872017-06-14 14:50:39 -0700753 return convolve(&burst, GSMPulse4->c0, NULL, START_ONLY);
Tom Tsoud3253432016-03-06 03:08:01 -0800754}
755
756/*
Tom Tsou8ee2f382016-03-06 20:57:34 -0800757 * Generate a random GSM normal burst.
758 */
759signalVector *genRandNormalBurst(int tsc, int sps, int tn)
760{
761 if ((tsc < 0) || (tsc > 7) || (tn < 0) || (tn > 7))
762 return NULL;
763 if ((sps != 1) && (sps != 4))
764 return NULL;
765
766 int i = 0;
Tom Tsou7278a872017-06-14 14:50:39 -0700767 BitVector bits(148);
Tom Tsou8ee2f382016-03-06 20:57:34 -0800768
769 /* Tail bits */
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700770 for (; i < 3; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700771 bits[i] = 0;
Tom Tsou8ee2f382016-03-06 20:57:34 -0800772
773 /* Random bits */
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700774 for (; i < 60; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700775 bits[i] = rand() % 2;
Tom Tsou8ee2f382016-03-06 20:57:34 -0800776
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700777 /* Stealing bit */
Tom Tsou7278a872017-06-14 14:50:39 -0700778 bits[i++] = 0;
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700779
Tom Tsou8ee2f382016-03-06 20:57:34 -0800780 /* Training sequence */
781 for (int n = 0; i < 87; i++, n++)
Tom Tsou7278a872017-06-14 14:50:39 -0700782 bits[i] = gTrainingSequence[tsc][n];
Tom Tsou8ee2f382016-03-06 20:57:34 -0800783
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700784 /* Stealing bit */
Tom Tsou7278a872017-06-14 14:50:39 -0700785 bits[i++] = 0;
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700786
Tom Tsou8ee2f382016-03-06 20:57:34 -0800787 /* Random bits */
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700788 for (; i < 145; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700789 bits[i] = rand() % 2;
Tom Tsou8ee2f382016-03-06 20:57:34 -0800790
791 /* Tail bits */
792 for (; i < 148; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700793 bits[i] = 0;
Tom Tsou8ee2f382016-03-06 20:57:34 -0800794
795 int guard = 8 + !(tn % 4);
Tom Tsou7278a872017-06-14 14:50:39 -0700796 return modulateBurst(bits, guard, sps);
Tom Tsou8ee2f382016-03-06 20:57:34 -0800797}
798
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300799/*
800 * Generate a random GSM access burst.
801 */
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300802signalVector *genRandAccessBurst(int delay, int sps, int tn)
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300803{
804 if ((tn < 0) || (tn > 7))
805 return NULL;
806 if ((sps != 1) && (sps != 4))
807 return NULL;
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300808 if (delay > 68)
809 return NULL;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300810
811 int i = 0;
Tom Tsou7278a872017-06-14 14:50:39 -0700812 BitVector bits(88 + delay);
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300813
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300814 /* delay */
815 for (; i < delay; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700816 bits[i] = 0;
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300817
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300818 /* head and synch bits */
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300819 for (int n = 0; i < 49+delay; i++, n++)
Tom Tsou7278a872017-06-14 14:50:39 -0700820 bits[i] = gRACHBurst[n];
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300821
822 /* Random bits */
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300823 for (; i < 85+delay; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700824 bits[i] = rand() % 2;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300825
826 /* Tail bits */
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300827 for (; i < 88+delay; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700828 bits[i] = 0;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300829
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300830 int guard = 68-delay + !(tn % 4);
Tom Tsou7278a872017-06-14 14:50:39 -0700831 return modulateBurst(bits, guard, sps);
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300832}
833
Tom Tsou8ee2f382016-03-06 20:57:34 -0800834signalVector *generateEmptyBurst(int sps, int tn)
835{
836 if ((tn < 0) || (tn > 7))
837 return NULL;
838
839 if (sps == 4)
840 return new signalVector(625);
841 else if (sps == 1)
842 return new signalVector(148 + 8 + !(tn % 4));
843 else
844 return NULL;
845}
846
847signalVector *generateDummyBurst(int sps, int tn)
848{
849 if (((sps != 1) && (sps != 4)) || (tn < 0) || (tn > 7))
850 return NULL;
851
852 return modulateBurst(gDummyBurst, 8 + !(tn % 4), sps);
853}
854
855/*
Tom Tsoud3253432016-03-06 03:08:01 -0800856 * Generate a random 8-PSK EDGE burst. Only 4 SPS is supported with
857 * the returned burst being 625 samples in length.
858 */
859signalVector *generateEdgeBurst(int tsc)
860{
861 int tail = 9 / 3;
862 int data = 174 / 3;
863 int train = 78 / 3;
864
865 if ((tsc < 0) || (tsc > 7))
866 return NULL;
867
Tom Tsou7278a872017-06-14 14:50:39 -0700868 signalVector burst(148);
Tom Tsoud3253432016-03-06 03:08:01 -0800869 const BitVector *midamble = &gEdgeTrainingSequence[tsc];
870
871 /* Tail */
872 int n, i = 0;
873 for (; i < tail; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700874 burst[i] = psk8_table[7];
Tom Tsoud3253432016-03-06 03:08:01 -0800875
876 /* Body */
877 for (; i < tail + data; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700878 burst[i] = psk8_table[rand() % 8];
Tom Tsoud3253432016-03-06 03:08:01 -0800879
880 /* TSC */
881 for (n = 0; i < tail + data + train; i++, n++) {
882 unsigned index = (((unsigned) (*midamble)[3 * n + 0] & 0x01) << 0) |
883 (((unsigned) (*midamble)[3 * n + 1] & 0x01) << 1) |
884 (((unsigned) (*midamble)[3 * n + 2] & 0x01) << 2);
885
Tom Tsou7278a872017-06-14 14:50:39 -0700886 burst[i] = psk8_table[index];
Tom Tsoud3253432016-03-06 03:08:01 -0800887 }
888
889 /* Body */
890 for (; i < tail + data + train + data; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700891 burst[i] = psk8_table[rand() % 8];
Tom Tsoud3253432016-03-06 03:08:01 -0800892
893 /* Tail */
894 for (; i < tail + data + train + data + tail; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700895 burst[i] = psk8_table[7];
Tom Tsoud3253432016-03-06 03:08:01 -0800896
Tom Tsou7278a872017-06-14 14:50:39 -0700897 return shapeEdgeBurst(burst);
Tom Tsoud3253432016-03-06 03:08:01 -0800898}
899
900/*
901 * Modulate 8-PSK burst. When empty pulse shaping (rotation only)
902 * is enabled, the output vector length will be bit sequence length
903 * times the SPS value. When pulse shaping is enabled, the output
Alexander Chemeris9270a5a2017-03-17 13:03:41 -0700904 * vector length is fixed at 625 samples (156.25 symbols at 4 SPS).
Tom Tsoud3253432016-03-06 03:08:01 -0800905 * Pulse shaped bit sequences that go beyond one burst are truncated.
906 * Pulse shaping at anything but 4 SPS is not supported.
907 */
908signalVector *modulateEdgeBurst(const BitVector &bits,
909 int sps, bool empty)
910{
911 signalVector *shape, *burst;
912
913 if ((sps != 4) && !empty)
914 return NULL;
915
916 burst = mapEdgeSymbols(bits);
917 if (!burst)
918 return NULL;
919
920 if (empty)
921 shape = rotateEdgeBurst(*burst, sps);
922 else
923 shape = shapeEdgeBurst(*burst);
924
925 delete burst;
926 return shape;
927}
928
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800929static signalVector *modulateBurstBasic(const BitVector &bits,
930 int guard_len, int sps)
931{
932 int burst_len;
Tom Tsou7278a872017-06-14 14:50:39 -0700933 signalVector *pulse;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800934 signalVector::iterator burst_itr;
935
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400936 if (sps == 1)
937 pulse = GSMPulse1->c0;
938 else
Tom Tsou2079a3c2016-03-06 00:58:56 -0800939 pulse = GSMPulse4->c0;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400940
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800941 burst_len = sps * (bits.size() + guard_len);
942
Tom Tsou7278a872017-06-14 14:50:39 -0700943 signalVector burst(burst_len, pulse->size());
944 burst.isReal(true);
945 burst_itr = burst.begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800946
947 /* Raw bits are not differentially encoded */
948 for (unsigned i = 0; i < bits.size(); i++) {
949 *burst_itr = 2.0 * (bits[i] & 0x01) - 1.0;
950 burst_itr += sps;
951 }
952
Tom Tsou7278a872017-06-14 14:50:39 -0700953 GMSKRotate(burst, sps);
954 burst.isReal(false);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800955
956 /* Single Gaussian pulse approximation shaping */
Tom Tsou7278a872017-06-14 14:50:39 -0700957 return convolve(&burst, pulse, NULL, START_ONLY);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800958}
959
Thomas Tsou3eaae802013-08-20 19:31:14 -0400960/* Assume input bits are not differentially encoded */
Thomas Tsou83e06892013-08-20 16:10:01 -0400961signalVector *modulateBurst(const BitVector &wBurst, int guardPeriodLength,
962 int sps, bool emptyPulse)
dburgessb3a0ca42011-10-12 07:44:40 +0000963{
Thomas Tsou83e06892013-08-20 16:10:01 -0400964 if (emptyPulse)
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800965 return rotateBurst(wBurst, guardPeriodLength, sps);
966 else if (sps == 4)
Tom Tsou4dfd64a2016-03-06 20:31:51 -0800967 return modulateBurstLaurent(wBurst);
Thomas Tsou83e06892013-08-20 16:10:01 -0400968 else
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800969 return modulateBurstBasic(wBurst, guardPeriodLength, sps);
dburgessb3a0ca42011-10-12 07:44:40 +0000970}
971
Tom Tsou2079a3c2016-03-06 00:58:56 -0800972static void generateSincTable()
Thomas Tsou0e0e1f42013-11-09 22:08:51 -0500973{
Thomas Tsou0e0e1f42013-11-09 22:08:51 -0500974 for (int i = 0; i < TABLESIZE; i++) {
Tom Tsoua3dce852017-06-16 17:14:31 -0700975 auto x = (double) i / TABLESIZE * 8 * M_PI;
976 auto y = sin(x) / x;
Tom Tsou35474132017-06-19 16:00:34 -0700977 sincTable[i] = std::isnan(y) ? 1.0 : y;
Thomas Tsou0e0e1f42013-11-09 22:08:51 -0500978 }
979}
980
Tom Tsou70134a02017-06-12 14:23:53 -0700981static float sinc(float x)
dburgessb3a0ca42011-10-12 07:44:40 +0000982{
Thomas Tsou0e0e1f42013-11-09 22:08:51 -0500983 if (fabs(x) >= 8 * M_PI)
984 return 0.0;
985
986 int index = (int) floorf(fabs(x) / (8 * M_PI) * TABLESIZE);
987
988 return sincTable[index];
dburgessb3a0ca42011-10-12 07:44:40 +0000989}
990
Thomas Tsouf79c4d02013-11-09 15:51:56 -0600991/*
992 * Create fractional delay filterbank with Blackman-harris windowed
993 * sinc function generator. The number of filters generated is specified
994 * by the DELAYFILTS value.
995 */
Tom Tsou70134a02017-06-12 14:23:53 -0700996static void generateDelayFilters()
Thomas Tsouf79c4d02013-11-09 15:51:56 -0600997{
998 int h_len = 20;
999 complex *data;
1000 signalVector *h;
1001 signalVector::iterator itr;
1002
1003 float k, sum;
1004 float a0 = 0.35875;
1005 float a1 = 0.48829;
1006 float a2 = 0.14128;
1007 float a3 = 0.01168;
1008
1009 for (int i = 0; i < DELAYFILTS; i++) {
1010 data = (complex *) convolve_h_alloc(h_len);
Pau Espin Pedrolf7331762018-12-03 17:46:04 +01001011 h = new signalVector(data, 0, h_len, convolve_h_alloc, free);
Thomas Tsouf79c4d02013-11-09 15:51:56 -06001012 h->setAligned(true);
1013 h->isReal(true);
1014
1015 sum = 0.0;
1016 itr = h->end();
1017 for (int n = 0; n < h_len; n++) {
1018 k = (float) n;
1019 *--itr = (complex) sinc(M_PI_F *
1020 (k - (float) h_len / 2.0 - (float) i / DELAYFILTS));
1021 *itr *= a0 -
1022 a1 * cos(2 * M_PI * n / (h_len - 1)) +
1023 a2 * cos(4 * M_PI * n / (h_len - 1)) -
1024 a3 * cos(6 * M_PI * n / (h_len - 1));
1025
1026 sum += itr->real();
1027 }
1028
1029 itr = h->begin();
1030 for (int n = 0; n < h_len; n++)
1031 *itr++ /= sum;
1032
1033 delayFilters[i] = h;
1034 }
1035}
1036
Alexander Chemerise0c12182017-03-18 13:27:48 -07001037signalVector *delayVector(const signalVector *in, signalVector *out, float delay)
dburgessb3a0ca42011-10-12 07:44:40 +00001038{
Thomas Tsouf79c4d02013-11-09 15:51:56 -06001039 int whole, index;
Thomas Tsou2c282f52013-10-08 21:34:35 -04001040 float frac;
Thomas Tsou94edaae2013-11-09 22:19:19 -05001041 signalVector *h, *shift, *fshift = NULL;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001042
Thomas Tsou2c282f52013-10-08 21:34:35 -04001043 whole = floor(delay);
1044 frac = delay - whole;
1045
1046 /* Sinc interpolated fractional shift (if allowable) */
1047 if (fabs(frac) > 1e-2) {
Thomas Tsouf79c4d02013-11-09 15:51:56 -06001048 index = floorf(frac * (float) DELAYFILTS);
1049 h = delayFilters[index];
Thomas Tsou2c282f52013-10-08 21:34:35 -04001050
Thomas Tsou94edaae2013-11-09 22:19:19 -05001051 fshift = convolve(in, h, NULL, NO_DELAY);
1052 if (!fshift)
1053 return NULL;
dburgessb3a0ca42011-10-12 07:44:40 +00001054 }
1055
Thomas Tsou94edaae2013-11-09 22:19:19 -05001056 if (!fshift)
1057 shift = new signalVector(*in);
1058 else
1059 shift = fshift;
1060
Thomas Tsou2c282f52013-10-08 21:34:35 -04001061 /* Integer sample shift */
1062 if (whole < 0) {
1063 whole = -whole;
Thomas Tsou94edaae2013-11-09 22:19:19 -05001064 signalVector::iterator wBurstItr = shift->begin();
1065 signalVector::iterator shiftedItr = shift->begin() + whole;
Thomas Tsou2c282f52013-10-08 21:34:35 -04001066
Thomas Tsou94edaae2013-11-09 22:19:19 -05001067 while (shiftedItr < shift->end())
dburgessb3a0ca42011-10-12 07:44:40 +00001068 *wBurstItr++ = *shiftedItr++;
Thomas Tsou2c282f52013-10-08 21:34:35 -04001069
Thomas Tsou94edaae2013-11-09 22:19:19 -05001070 while (wBurstItr < shift->end())
1071 *wBurstItr++ = 0.0;
1072 } else if (whole >= 0) {
1073 signalVector::iterator wBurstItr = shift->end() - 1;
1074 signalVector::iterator shiftedItr = shift->end() - 1 - whole;
1075
1076 while (shiftedItr >= shift->begin())
dburgessb3a0ca42011-10-12 07:44:40 +00001077 *wBurstItr-- = *shiftedItr--;
Thomas Tsou94edaae2013-11-09 22:19:19 -05001078
1079 while (wBurstItr >= shift->begin())
dburgessb3a0ca42011-10-12 07:44:40 +00001080 *wBurstItr-- = 0.0;
1081 }
Thomas Tsou2c282f52013-10-08 21:34:35 -04001082
Thomas Tsou94edaae2013-11-09 22:19:19 -05001083 if (!out)
1084 return shift;
1085
1086 out->clone(*shift);
1087 delete shift;
1088 return out;
dburgessb3a0ca42011-10-12 07:44:40 +00001089}
Thomas Tsou2c282f52013-10-08 21:34:35 -04001090
Tom Tsou70134a02017-06-12 14:23:53 -07001091static complex interpolatePoint(const signalVector &inSig, float ix)
dburgessb3a0ca42011-10-12 07:44:40 +00001092{
dburgessb3a0ca42011-10-12 07:44:40 +00001093 int start = (int) (floor(ix) - 10);
1094 if (start < 0) start = 0;
1095 int end = (int) (floor(ix) + 11);
1096 if ((unsigned) end > inSig.size()-1) end = inSig.size()-1;
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001097
dburgessb3a0ca42011-10-12 07:44:40 +00001098 complex pVal = 0.0;
Thomas Tsou20eb6d62013-11-09 14:30:41 -05001099 if (!inSig.isReal()) {
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001100 for (int i = start; i < end; i++)
dburgessb3a0ca42011-10-12 07:44:40 +00001101 pVal += inSig[i] * sinc(M_PI_F*(i-ix));
1102 }
1103 else {
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001104 for (int i = start; i < end; i++)
dburgessb3a0ca42011-10-12 07:44:40 +00001105 pVal += inSig[i].real() * sinc(M_PI_F*(i-ix));
1106 }
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001107
dburgessb3a0ca42011-10-12 07:44:40 +00001108 return pVal;
1109}
1110
Thomas Tsou8181b012013-08-20 21:17:19 -04001111static complex fastPeakDetect(const signalVector &rxBurst, float *index)
1112{
1113 float val, max = 0.0f;
1114 complex amp;
1115 int _index = -1;
1116
Thomas Tsou3f32ab52013-11-15 16:32:54 -05001117 for (size_t i = 0; i < rxBurst.size(); i++) {
Thomas Tsou8181b012013-08-20 21:17:19 -04001118 val = rxBurst[i].norm2();
1119 if (val > max) {
1120 max = val;
1121 _index = i;
1122 amp = rxBurst[i];
1123 }
1124 }
1125
1126 if (index)
1127 *index = (float) _index;
1128
1129 return amp;
1130}
1131
Tom Tsou70134a02017-06-12 14:23:53 -07001132static complex peakDetect(const signalVector &rxBurst,
1133 float *peakIndex, float *avgPwr)
dburgessb3a0ca42011-10-12 07:44:40 +00001134{
dburgessb3a0ca42011-10-12 07:44:40 +00001135 complex maxVal = 0.0;
1136 float maxIndex = -1;
1137 float sumPower = 0.0;
1138
1139 for (unsigned int i = 0; i < rxBurst.size(); i++) {
1140 float samplePower = rxBurst[i].norm2();
1141 if (samplePower > maxVal.real()) {
1142 maxVal = samplePower;
1143 maxIndex = i;
1144 }
1145 sumPower += samplePower;
1146 }
1147
1148 // interpolate around the peak
1149 // to save computation, we'll use early-late balancing
1150 float earlyIndex = maxIndex-1;
1151 float lateIndex = maxIndex+1;
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001152
dburgessb3a0ca42011-10-12 07:44:40 +00001153 float incr = 0.5;
1154 while (incr > 1.0/1024.0) {
1155 complex earlyP = interpolatePoint(rxBurst,earlyIndex);
1156 complex lateP = interpolatePoint(rxBurst,lateIndex);
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001157 if (earlyP < lateP)
dburgessb3a0ca42011-10-12 07:44:40 +00001158 earlyIndex += incr;
1159 else if (earlyP > lateP)
1160 earlyIndex -= incr;
1161 else break;
1162 incr /= 2.0;
1163 lateIndex = earlyIndex + 2.0;
1164 }
1165
1166 maxIndex = earlyIndex + 1.0;
1167 maxVal = interpolatePoint(rxBurst,maxIndex);
1168
1169 if (peakIndex!=NULL)
1170 *peakIndex = maxIndex;
1171
1172 if (avgPwr!=NULL)
1173 *avgPwr = (sumPower-maxVal.norm2()) / (rxBurst.size()-1);
1174
1175 return maxVal;
1176
1177}
1178
1179void scaleVector(signalVector &x,
1180 complex scale)
1181{
Thomas Tsou7e4e5362013-10-30 21:18:55 -04001182#ifdef HAVE_NEON
1183 int len = x.size();
1184
1185 scale_complex((float *) x.begin(),
1186 (float *) x.begin(),
1187 (float *) &scale, len);
1188#else
dburgessb3a0ca42011-10-12 07:44:40 +00001189 signalVector::iterator xP = x.begin();
1190 signalVector::iterator xPEnd = x.end();
Thomas Tsou20eb6d62013-11-09 14:30:41 -05001191 if (!x.isReal()) {
dburgessb3a0ca42011-10-12 07:44:40 +00001192 while (xP < xPEnd) {
1193 *xP = *xP * scale;
1194 xP++;
1195 }
1196 }
1197 else {
1198 while (xP < xPEnd) {
1199 *xP = xP->real() * scale;
1200 xP++;
1201 }
1202 }
Thomas Tsou7e4e5362013-10-30 21:18:55 -04001203#endif
dburgessb3a0ca42011-10-12 07:44:40 +00001204}
1205
1206/** in-place conjugation */
Tom Tsou70134a02017-06-12 14:23:53 -07001207static void conjugateVector(signalVector &x)
dburgessb3a0ca42011-10-12 07:44:40 +00001208{
Thomas Tsou20eb6d62013-11-09 14:30:41 -05001209 if (x.isReal()) return;
dburgessb3a0ca42011-10-12 07:44:40 +00001210 signalVector::iterator xP = x.begin();
1211 signalVector::iterator xPEnd = x.end();
1212 while (xP < xPEnd) {
1213 *xP = xP->conj();
1214 xP++;
1215 }
1216}
1217
Tom Tsou2079a3c2016-03-06 00:58:56 -08001218static bool generateMidamble(int sps, int tsc)
dburgessb3a0ca42011-10-12 07:44:40 +00001219{
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001220 bool status = true;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001221 float toa;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001222 complex *data = NULL;
1223 signalVector *autocorr = NULL, *midamble = NULL;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001224 signalVector *midMidamble = NULL, *_midMidamble = NULL;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001225
Thomas Tsou3eaae802013-08-20 19:31:14 -04001226 if ((tsc < 0) || (tsc > 7))
dburgessb3a0ca42011-10-12 07:44:40 +00001227 return false;
1228
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001229 delete gMidambles[tsc];
Thomas Tsou3eaae802013-08-20 19:31:14 -04001230
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001231 /* Use middle 16 bits of each TSC. Correlation sequence is not pulse shaped */
1232 midMidamble = modulateBurst(gTrainingSequence[tsc].segment(5,16), 0, sps, true);
1233 if (!midMidamble)
1234 return false;
1235
Thomas Tsou3eaae802013-08-20 19:31:14 -04001236 /* Simulated receive sequence is pulse shaped */
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001237 midamble = modulateBurst(gTrainingSequence[tsc], 0, sps, false);
1238 if (!midamble) {
1239 status = false;
1240 goto release;
dburgessb3a0ca42011-10-12 07:44:40 +00001241 }
Thomas Tsou3eaae802013-08-20 19:31:14 -04001242
dburgessb3a0ca42011-10-12 07:44:40 +00001243 // NOTE: Because ideal TSC 16-bit midamble is 66 symbols into burst,
1244 // the ideal TSC has an + 180 degree phase shift,
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001245 // due to the pi/2 frequency shift, that
dburgessb3a0ca42011-10-12 07:44:40 +00001246 // needs to be accounted for.
1247 // 26-midamble is 61 symbols into burst, has +90 degree phase shift.
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001248 scaleVector(*midMidamble, complex(-1.0, 0.0));
1249 scaleVector(*midamble, complex(0.0, 1.0));
dburgessb3a0ca42011-10-12 07:44:40 +00001250
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001251 conjugateVector(*midMidamble);
dburgessb3a0ca42011-10-12 07:44:40 +00001252
Thomas Tsou3eaae802013-08-20 19:31:14 -04001253 /* For SSE alignment, reallocate the midamble sequence on 16-byte boundary */
1254 data = (complex *) convolve_h_alloc(midMidamble->size());
Pau Espin Pedrolf7331762018-12-03 17:46:04 +01001255 _midMidamble = new signalVector(data, 0, midMidamble->size(), convolve_h_alloc, free);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001256 _midMidamble->setAligned(true);
Pau Espin Pedrol5e68cde2018-08-30 20:45:14 +02001257 midMidamble->copyTo(*_midMidamble);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001258
1259 autocorr = convolve(midamble, _midMidamble, NULL, NO_DELAY);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001260 if (!autocorr) {
1261 status = false;
1262 goto release;
1263 }
dburgessb3a0ca42011-10-12 07:44:40 +00001264
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001265 gMidambles[tsc] = new CorrelationSequence;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001266 gMidambles[tsc]->sequence = _midMidamble;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001267 gMidambles[tsc]->gain = peakDetect(*autocorr, &toa, NULL);
1268
1269 /* For 1 sps only
1270 * (Half of correlation length - 1) + midpoint of pulse shape + remainder
1271 * 13.5 = (16 / 2 - 1) + 1.5 + (26 - 10) / 2
1272 */
1273 if (sps == 1)
1274 gMidambles[tsc]->toa = toa - 13.5;
1275 else
1276 gMidambles[tsc]->toa = 0;
dburgessb3a0ca42011-10-12 07:44:40 +00001277
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001278release:
dburgessb3a0ca42011-10-12 07:44:40 +00001279 delete autocorr;
1280 delete midamble;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001281 delete midMidamble;
dburgessb3a0ca42011-10-12 07:44:40 +00001282
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001283 if (!status) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001284 delete _midMidamble;
1285 free(data);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001286 gMidambles[tsc] = NULL;
1287 }
1288
1289 return status;
dburgessb3a0ca42011-10-12 07:44:40 +00001290}
1291
Tom Tsou70134a02017-06-12 14:23:53 -07001292static CorrelationSequence *generateEdgeMidamble(int tsc)
Tom Tsoud3253432016-03-06 03:08:01 -08001293{
1294 complex *data = NULL;
1295 signalVector *midamble = NULL, *_midamble = NULL;
1296 CorrelationSequence *seq;
1297
1298 if ((tsc < 0) || (tsc > 7))
1299 return NULL;
1300
1301 /* Use middle 48 bits of each TSC. Correlation sequence is not pulse shaped */
1302 const BitVector *bits = &gEdgeTrainingSequence[tsc];
1303 midamble = modulateEdgeBurst(bits->segment(15, 48), 1, true);
1304 if (!midamble)
1305 return NULL;
1306
1307 conjugateVector(*midamble);
1308
1309 data = (complex *) convolve_h_alloc(midamble->size());
Pau Espin Pedrolf7331762018-12-03 17:46:04 +01001310 _midamble = new signalVector(data, 0, midamble->size(), convolve_h_alloc, free);
Tom Tsoud3253432016-03-06 03:08:01 -08001311 _midamble->setAligned(true);
Pau Espin Pedrol5e68cde2018-08-30 20:45:14 +02001312 midamble->copyTo(*_midamble);
Tom Tsoud3253432016-03-06 03:08:01 -08001313
1314 /* Channel gain is an empirically measured value */
1315 seq = new CorrelationSequence;
Tom Tsoud3253432016-03-06 03:08:01 -08001316 seq->sequence = _midamble;
1317 seq->gain = Complex<float>(-19.6432, 19.5006) / 1.18;
1318 seq->toa = 0;
1319
1320 delete midamble;
1321
1322 return seq;
1323}
1324
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001325static bool generateRACHSequence(CorrelationSequence **seq, const BitVector &bv, int sps)
dburgessb3a0ca42011-10-12 07:44:40 +00001326{
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001327 bool status = true;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001328 float toa;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001329 complex *data = NULL;
1330 signalVector *autocorr = NULL;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001331 signalVector *seq0 = NULL, *seq1 = NULL, *_seq1 = NULL;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001332
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001333 if (*seq != NULL)
1334 delete *seq;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001335
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001336 seq0 = modulateBurst(bv, 0, sps, false);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001337 if (!seq0)
1338 return false;
1339
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001340 seq1 = modulateBurst(bv.segment(0, 40), 0, sps, true);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001341 if (!seq1) {
1342 status = false;
1343 goto release;
dburgessb3a0ca42011-10-12 07:44:40 +00001344 }
1345
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001346 conjugateVector(*seq1);
dburgessb3a0ca42011-10-12 07:44:40 +00001347
Thomas Tsou3eaae802013-08-20 19:31:14 -04001348 /* For SSE alignment, reallocate the midamble sequence on 16-byte boundary */
1349 data = (complex *) convolve_h_alloc(seq1->size());
Pau Espin Pedrolf7331762018-12-03 17:46:04 +01001350 _seq1 = new signalVector(data, 0, seq1->size(), convolve_h_alloc, free);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001351 _seq1->setAligned(true);
Pau Espin Pedrol5e68cde2018-08-30 20:45:14 +02001352 seq1->copyTo(*_seq1);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001353
1354 autocorr = convolve(seq0, _seq1, autocorr, NO_DELAY);
1355 if (!autocorr) {
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001356 status = false;
1357 goto release;
1358 }
dburgessb3a0ca42011-10-12 07:44:40 +00001359
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001360 *seq = new CorrelationSequence;
1361 (*seq)->sequence = _seq1;
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001362 (*seq)->gain = peakDetect(*autocorr, &toa, NULL);
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001363
1364 /* For 1 sps only
1365 * (Half of correlation length - 1) + midpoint of pulse shaping filer
1366 * 20.5 = (40 / 2 - 1) + 1.5
1367 */
1368 if (sps == 1)
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001369 (*seq)->toa = toa - 20.5;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001370 else
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001371 (*seq)->toa = 0.0;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001372
1373release:
dburgessb3a0ca42011-10-12 07:44:40 +00001374 delete autocorr;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001375 delete seq0;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001376 delete seq1;
dburgessb3a0ca42011-10-12 07:44:40 +00001377
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001378 if (!status) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001379 delete _seq1;
1380 free(data);
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001381 *seq = NULL;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001382 }
dburgessb3a0ca42011-10-12 07:44:40 +00001383
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001384 return status;
dburgessb3a0ca42011-10-12 07:44:40 +00001385}
Thomas Tsou3eaae802013-08-20 19:31:14 -04001386
Tom Tsoua84e1622016-06-29 14:50:25 -07001387/*
1388 * Peak-to-average computation +/- range from peak in symbols
1389 */
1390#define COMPUTE_PEAK_MIN 2
1391#define COMPUTE_PEAK_MAX 5
1392
1393/*
1394 * Minimum number of values needed to compute peak-to-average
1395 */
1396#define COMPUTE_PEAK_CNT 5
1397
Thomas Tsou865bca42013-08-21 20:58:00 -04001398static float computePeakRatio(signalVector *corr,
1399 int sps, float toa, complex amp)
dburgessb3a0ca42011-10-12 07:44:40 +00001400{
Thomas Tsou865bca42013-08-21 20:58:00 -04001401 int num = 0;
1402 complex *peak;
1403 float rms, avg = 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001404
Thomas Tsou865bca42013-08-21 20:58:00 -04001405 /* Check for bogus results */
1406 if ((toa < 0.0) || (toa > corr->size()))
1407 return 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001408
Alexander Chemeris1e9b4d52015-06-04 19:05:28 -04001409 peak = corr->begin() + (int) rint(toa);
1410
Tom Tsoua84e1622016-06-29 14:50:25 -07001411 for (int i = COMPUTE_PEAK_MIN * sps; i <= COMPUTE_PEAK_MAX * sps; i++) {
Thomas Tsou865bca42013-08-21 20:58:00 -04001412 if (peak - i >= corr->begin()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001413 avg += (peak - i)->norm2();
1414 num++;
1415 }
Thomas Tsou865bca42013-08-21 20:58:00 -04001416 if (peak + i < corr->end()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001417 avg += (peak + i)->norm2();
1418 num++;
1419 }
dburgessb3a0ca42011-10-12 07:44:40 +00001420 }
1421
Tom Tsoua84e1622016-06-29 14:50:25 -07001422 if (num < COMPUTE_PEAK_CNT)
Thomas Tsou865bca42013-08-21 20:58:00 -04001423 return 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001424
Thomas Tsou3eaae802013-08-20 19:31:14 -04001425 rms = sqrtf(avg / (float) num) + 0.00001;
dburgessb3a0ca42011-10-12 07:44:40 +00001426
Thomas Tsou865bca42013-08-21 20:58:00 -04001427 return (amp.abs()) / rms;
dburgessb3a0ca42011-10-12 07:44:40 +00001428}
1429
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001430float energyDetect(const signalVector &rxBurst, unsigned windowLength)
dburgessb3a0ca42011-10-12 07:44:40 +00001431{
1432
1433 signalVector::const_iterator windowItr = rxBurst.begin(); //+rxBurst.size()/2 - 5*windowLength/2;
1434 float energy = 0.0;
Tom Tsou2af14402017-03-23 14:54:00 -07001435 if (windowLength == 0) return 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001436 if (windowLength > rxBurst.size()) windowLength = rxBurst.size();
1437 for (unsigned i = 0; i < windowLength; i++) {
1438 energy += windowItr->norm2();
1439 windowItr+=4;
1440 }
Alexander Chemeris1dd05cf2017-03-15 23:23:36 +03001441 return energy/windowLength;
dburgessb3a0ca42011-10-12 07:44:40 +00001442}
dburgessb3a0ca42011-10-12 07:44:40 +00001443
Tom Tsou70134a02017-06-12 14:23:53 -07001444static signalVector *downsampleBurst(const signalVector &burst)
1445{
Tom Tsou7278a872017-06-14 14:50:39 -07001446 signalVector in(DOWNSAMPLE_IN_LEN, dnsampler->len());
1447 signalVector *out = new signalVector(DOWNSAMPLE_OUT_LEN);
Pau Espin Pedrol5e68cde2018-08-30 20:45:14 +02001448 burst.copyToSegment(in, 0, DOWNSAMPLE_IN_LEN);
Tom Tsou70134a02017-06-12 14:23:53 -07001449
Tom Tsou7278a872017-06-14 14:50:39 -07001450 if (dnsampler->rotate((float *) in.begin(), DOWNSAMPLE_IN_LEN,
Tom Tsou70134a02017-06-12 14:23:53 -07001451 (float *) out->begin(), DOWNSAMPLE_OUT_LEN) < 0) {
1452 delete out;
1453 out = NULL;
1454 }
1455
Tom Tsou70134a02017-06-12 14:23:53 -07001456 return out;
1457};
1458
Thomas Tsou865bca42013-08-21 20:58:00 -04001459/*
1460 * Detect a burst based on correlation and peak-to-average ratio
1461 *
1462 * For one sampler-per-symbol, perform fast peak detection (no interpolation)
1463 * for initial gating. We do this because energy detection should be disabled.
1464 * For higher oversampling values, we assume the energy detector is in place
1465 * and we run full interpolating peak detection.
1466 */
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001467static int detectBurst(const signalVector &burst,
Thomas Tsou865bca42013-08-21 20:58:00 -04001468 signalVector &corr, CorrelationSequence *sync,
1469 float thresh, int sps, complex *amp, float *toa,
1470 int start, int len)
dburgessb3a0ca42011-10-12 07:44:40 +00001471{
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001472 const signalVector *corr_in;
1473 signalVector *dec = NULL;
Tom Tsoud3253432016-03-06 03:08:01 -08001474
1475 if (sps == 4) {
1476 dec = downsampleBurst(burst);
1477 corr_in = dec;
1478 sps = 1;
1479 } else {
1480 corr_in = &burst;
1481 }
1482
Thomas Tsou865bca42013-08-21 20:58:00 -04001483 /* Correlate */
Tom Tsoud3253432016-03-06 03:08:01 -08001484 if (!convolve(corr_in, sync->sequence, &corr,
1485 CUSTOM, start, len, 1, 0)) {
1486 delete dec;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001487 return -1;
dburgessb3a0ca42011-10-12 07:44:40 +00001488 }
1489
Tom Tsoud3253432016-03-06 03:08:01 -08001490 delete dec;
1491
1492 /* Running at the downsampled rate at this point */
1493 sps = 1;
1494
Thomas Tsou865bca42013-08-21 20:58:00 -04001495 /* Peak detection - place restrictions at correlation edges */
1496 *amp = fastPeakDetect(corr, toa);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001497
Thomas Tsou865bca42013-08-21 20:58:00 -04001498 if ((*toa < 3 * sps) || (*toa > len - 3 * sps))
1499 return 0;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001500
Thomas Tsou865bca42013-08-21 20:58:00 -04001501 /* Peak -to-average ratio */
1502 if (computePeakRatio(&corr, sps, *toa, *amp) < thresh)
1503 return 0;
1504
1505 /* Compute peak-to-average ratio. Reject if we don't have enough values */
1506 *amp = peakDetect(corr, toa, NULL);
1507
1508 /* Normalize our channel gain */
1509 *amp = *amp / sync->gain;
1510
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001511 /* Compensate for residuate time lag */
1512 *toa = *toa - sync->toa;
1513
Thomas Tsou865bca42013-08-21 20:58:00 -04001514 return 1;
1515}
1516
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001517static float maxAmplitude(const signalVector &burst)
Tom Tsou577cd022015-05-18 13:57:54 -07001518{
Alexander Chemeris954b1182015-06-04 15:39:41 -04001519 float max = 0.0;
1520 for (size_t i = 0; i < burst.size(); i++) {
1521 if (fabs(burst[i].real()) > max)
1522 max = fabs(burst[i].real());
1523 if (fabs(burst[i].imag()) > max)
1524 max = fabs(burst[i].imag());
1525 }
Tom Tsou577cd022015-05-18 13:57:54 -07001526
Alexander Chemeris954b1182015-06-04 15:39:41 -04001527 return max;
Tom Tsou577cd022015-05-18 13:57:54 -07001528}
1529
Alexander Chemeris130a8002015-06-09 20:52:11 -04001530/*
1531 * RACH/Normal burst detection with clipping detection
Thomas Tsou865bca42013-08-21 20:58:00 -04001532 *
1533 * Correlation window parameters:
Alexander Chemeris130a8002015-06-09 20:52:11 -04001534 * target: Tail bits + burst length
1535 * head: Search symbols before target
1536 * tail: Search symbols after target
Thomas Tsou865bca42013-08-21 20:58:00 -04001537 */
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001538static int detectGeneralBurst(const signalVector &rxBurst,
1539 float thresh,
1540 int sps,
1541 complex &amp,
1542 float &toa,
1543 int target, int head, int tail,
1544 CorrelationSequence *sync)
Thomas Tsou865bca42013-08-21 20:58:00 -04001545{
Alexander Chemeris130a8002015-06-09 20:52:11 -04001546 int rc, start, len;
Alexander Chemeris954b1182015-06-04 15:39:41 -04001547 bool clipping = false;
Thomas Tsou865bca42013-08-21 20:58:00 -04001548
1549 if ((sps != 1) && (sps != 4))
Tom Tsou577cd022015-05-18 13:57:54 -07001550 return -SIGERR_UNSUPPORTED;
1551
Alexander Chemeris954b1182015-06-04 15:39:41 -04001552 // Detect potential clipping
1553 // We still may be able to demod the burst, so we'll give it a try
1554 // and only report clipping if we can't demod.
1555 float maxAmpl = maxAmplitude(rxBurst);
1556 if (maxAmpl > CLIP_THRESH) {
1557 LOG(DEBUG) << "max burst amplitude: " << maxAmpl << " is above the clipping threshold: " << CLIP_THRESH << std::endl;
1558 clipping = true;
1559 }
Thomas Tsou865bca42013-08-21 20:58:00 -04001560
Tom Tsoud3253432016-03-06 03:08:01 -08001561 start = target - head - 1;
1562 len = head + tail;
Tom Tsou7278a872017-06-14 14:50:39 -07001563 signalVector corr(len);
Thomas Tsou865bca42013-08-21 20:58:00 -04001564
Tom Tsou7278a872017-06-14 14:50:39 -07001565 rc = detectBurst(rxBurst, corr, sync,
Alexander Chemeris130a8002015-06-09 20:52:11 -04001566 thresh, sps, &amp, &toa, start, len);
Thomas Tsou865bca42013-08-21 20:58:00 -04001567 if (rc < 0) {
Tom Tsou577cd022015-05-18 13:57:54 -07001568 return -SIGERR_INTERNAL;
Thomas Tsou865bca42013-08-21 20:58:00 -04001569 } else if (!rc) {
Alexander Chemeris130a8002015-06-09 20:52:11 -04001570 amp = 0.0f;
1571 toa = 0.0f;
Alexander Chemeris954b1182015-06-04 15:39:41 -04001572 return clipping?-SIGERR_CLIP:SIGERR_NONE;
dburgessb3a0ca42011-10-12 07:44:40 +00001573 }
1574
Thomas Tsou865bca42013-08-21 20:58:00 -04001575 /* Subtract forward search bits from delay */
Tom Tsoud3253432016-03-06 03:08:01 -08001576 toa -= head;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001577
Thomas Tsou865bca42013-08-21 20:58:00 -04001578 return 1;
1579}
Thomas Tsou3eaae802013-08-20 19:31:14 -04001580
Alexander Chemeris130a8002015-06-09 20:52:11 -04001581
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001582/*
Alexander Chemeris130a8002015-06-09 20:52:11 -04001583 * RACH burst detection
1584 *
1585 * Correlation window parameters:
1586 * target: Tail bits + RACH length (reduced from 41 to a multiple of 4)
Tom Tsoue90c24c2016-06-21 16:14:39 -07001587 * head: Search 8 symbols before target
1588 * tail: Search 8 symbols + maximum expected delay
Alexander Chemeris130a8002015-06-09 20:52:11 -04001589 */
Tom Tsou70134a02017-06-12 14:23:53 -07001590static int detectRACHBurst(const signalVector &burst, float threshold, int sps,
Vadim Yanitskiy444ff342018-10-22 02:25:23 +02001591 complex &amplitude, float &toa, unsigned max_toa, bool ext)
Alexander Chemeris130a8002015-06-09 20:52:11 -04001592{
1593 int rc, target, head, tail;
Vadim Yanitskiy444ff342018-10-22 02:25:23 +02001594 int i, num_seq;
Alexander Chemeris130a8002015-06-09 20:52:11 -04001595
1596 target = 8 + 40;
Tom Tsoue90c24c2016-06-21 16:14:39 -07001597 head = 8;
Alexander Chemeris14d13b62017-03-17 15:12:17 -07001598 tail = 8 + max_toa;
Vadim Yanitskiy444ff342018-10-22 02:25:23 +02001599 num_seq = ext ? 3 : 1;
Alexander Chemeris130a8002015-06-09 20:52:11 -04001600
Vadim Yanitskiy444ff342018-10-22 02:25:23 +02001601 for (i = 0; i < num_seq; i++) {
1602 rc = detectGeneralBurst(burst, threshold, sps, amplitude, toa,
1603 target, head, tail, gRACHSequences[i]);
1604 if (rc > 0)
1605 break;
1606 }
Alexander Chemeris130a8002015-06-09 20:52:11 -04001607
1608 return rc;
1609}
1610
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001611/*
Thomas Tsou865bca42013-08-21 20:58:00 -04001612 * Normal burst detection
1613 *
1614 * Correlation window parameters:
1615 * target: Tail + data + mid-midamble + 1/2 remaining midamblebits
Tom Tsoue90c24c2016-06-21 16:14:39 -07001616 * head: Search 6 symbols before target
1617 * tail: Search 6 symbols + maximum expected delay
Thomas Tsou865bca42013-08-21 20:58:00 -04001618 */
Tom Tsou70134a02017-06-12 14:23:53 -07001619static int analyzeTrafficBurst(const signalVector &burst, unsigned tsc, float threshold,
1620 int sps, complex &amplitude, float &toa, unsigned max_toa)
Thomas Tsou865bca42013-08-21 20:58:00 -04001621{
Alexander Chemeris130a8002015-06-09 20:52:11 -04001622 int rc, target, head, tail;
Thomas Tsou865bca42013-08-21 20:58:00 -04001623 CorrelationSequence *sync;
1624
Tom Tsouae91f132017-03-28 14:40:38 -07001625 if (tsc > 7)
Tom Tsou577cd022015-05-18 13:57:54 -07001626 return -SIGERR_UNSUPPORTED;
1627
Thomas Tsou865bca42013-08-21 20:58:00 -04001628 target = 3 + 58 + 16 + 5;
Tom Tsoue90c24c2016-06-21 16:14:39 -07001629 head = 6;
1630 tail = 6 + max_toa;
Thomas Tsou865bca42013-08-21 20:58:00 -04001631 sync = gMidambles[tsc];
Thomas Tsou865bca42013-08-21 20:58:00 -04001632
Alexander Chemeris14d13b62017-03-17 15:12:17 -07001633 rc = detectGeneralBurst(burst, threshold, sps, amplitude, toa,
Alexander Chemeris130a8002015-06-09 20:52:11 -04001634 target, head, tail, sync);
Alexander Chemeris130a8002015-06-09 20:52:11 -04001635 return rc;
dburgessb3a0ca42011-10-12 07:44:40 +00001636}
1637
Tom Tsou70134a02017-06-12 14:23:53 -07001638static int detectEdgeBurst(const signalVector &burst, unsigned tsc, float threshold,
1639 int sps, complex &amplitude, float &toa, unsigned max_toa)
Tom Tsoud3253432016-03-06 03:08:01 -08001640{
1641 int rc, target, head, tail;
1642 CorrelationSequence *sync;
1643
Tom Tsouae91f132017-03-28 14:40:38 -07001644 if (tsc > 7)
Tom Tsoud3253432016-03-06 03:08:01 -08001645 return -SIGERR_UNSUPPORTED;
1646
1647 target = 3 + 58 + 16 + 5;
Tom Tsoue90c24c2016-06-21 16:14:39 -07001648 head = 6;
1649 tail = 6 + max_toa;
Tom Tsoud3253432016-03-06 03:08:01 -08001650 sync = gEdgeMidambles[tsc];
1651
Alexander Chemeris14d13b62017-03-17 15:12:17 -07001652 rc = detectGeneralBurst(burst, threshold, sps, amplitude, toa,
Tom Tsoud3253432016-03-06 03:08:01 -08001653 target, head, tail, sync);
1654 return rc;
1655}
1656
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001657int detectAnyBurst(const signalVector &burst, unsigned tsc, float threshold,
Alexander Chemeris4e6c9382017-03-17 15:24:18 -07001658 int sps, CorrType type, complex &amp, float &toa,
1659 unsigned max_toa)
1660{
1661 int rc = 0;
1662
1663 switch (type) {
1664 case EDGE:
1665 rc = detectEdgeBurst(burst, tsc, threshold, sps,
1666 amp, toa, max_toa);
1667 if (rc > 0)
1668 break;
1669 else
1670 type = TSC;
1671 case TSC:
1672 rc = analyzeTrafficBurst(burst, tsc, threshold, sps,
1673 amp, toa, max_toa);
1674 break;
Vadim Yanitskiy444ff342018-10-22 02:25:23 +02001675 case EXT_RACH:
Alexander Chemeris4e6c9382017-03-17 15:24:18 -07001676 case RACH:
1677 rc = detectRACHBurst(burst, threshold, sps, amp, toa,
Vadim Yanitskiy444ff342018-10-22 02:25:23 +02001678 max_toa, type == EXT_RACH);
Alexander Chemeris4e6c9382017-03-17 15:24:18 -07001679 break;
1680 default:
1681 LOG(ERR) << "Invalid correlation type";
1682 }
1683
1684 if (rc > 0)
1685 return type;
1686
1687 return rc;
1688}
1689
Tom Tsoud3253432016-03-06 03:08:01 -08001690/*
1691 * Soft 8-PSK decoding using Manhattan distance metric
1692 */
1693static SoftVector *softSliceEdgeBurst(signalVector &burst)
1694{
1695 size_t nsyms = 148;
1696
1697 if (burst.size() < nsyms)
1698 return NULL;
1699
1700 signalVector::iterator itr;
1701 SoftVector *bits = new SoftVector(nsyms * 3);
1702
1703 /*
1704 * Bits 0 and 1 - First and second bits of the symbol respectively
1705 */
1706 rotateBurst2(burst, -M_PI / 8.0);
1707 itr = burst.begin();
1708 for (size_t i = 0; i < nsyms; i++) {
1709 (*bits)[3 * i + 0] = -itr->imag();
1710 (*bits)[3 * i + 1] = itr->real();
1711 itr++;
1712 }
1713
1714 /*
1715 * Bit 2 - Collapse symbols into quadrant 0 (positive X and Y).
1716 * Decision area is then simplified to X=Y axis. Rotate again to
1717 * place decision boundary on X-axis.
1718 */
1719 itr = burst.begin();
1720 for (size_t i = 0; i < burst.size(); i++) {
1721 burst[i] = Complex<float>(fabs(itr->real()), fabs(itr->imag()));
1722 itr++;
1723 }
1724
1725 rotateBurst2(burst, -M_PI / 4.0);
1726 itr = burst.begin();
1727 for (size_t i = 0; i < nsyms; i++) {
1728 (*bits)[3 * i + 2] = -itr->imag();
1729 itr++;
1730 }
1731
1732 signalVector soft(bits->size());
1733 for (size_t i = 0; i < bits->size(); i++)
1734 soft[i] = (*bits)[i];
1735
1736 return bits;
1737}
1738
1739/*
Alexander Chemeris132fb242017-03-17 17:22:33 -07001740 * Convert signalVector to SoftVector by taking real part of the signal.
1741 */
1742static SoftVector *signalToSoftVector(signalVector *dec)
1743{
1744 SoftVector *bits = new SoftVector(dec->size());
1745
1746 SoftVector::iterator bit_itr = bits->begin();
1747 signalVector::iterator burst_itr = dec->begin();
1748
1749 for (; burst_itr < dec->end(); burst_itr++)
1750 *bit_itr++ = burst_itr->real();
1751
1752 return bits;
1753}
1754
1755/*
Tom Tsou7fec3032016-03-06 22:33:20 -08001756 * Shared portion of GMSK and EDGE demodulators consisting of timing
1757 * recovery and single tap channel correction. For 4 SPS (if activated),
1758 * the output is downsampled prior to the 1 SPS modulation specific
1759 * stages.
1760 */
Alexander Chemerise0c12182017-03-18 13:27:48 -07001761static signalVector *demodCommon(const signalVector &burst, int sps,
Tom Tsou7fec3032016-03-06 22:33:20 -08001762 complex chan, float toa)
1763{
1764 signalVector *delay, *dec;
1765
1766 if ((sps != 1) && (sps != 4))
1767 return NULL;
1768
Tom Tsou7fec3032016-03-06 22:33:20 -08001769 delay = delayVector(&burst, NULL, -toa * (float) sps);
Alexander Chemerise0c12182017-03-18 13:27:48 -07001770 scaleVector(*delay, (complex) 1.0 / chan);
Tom Tsou7fec3032016-03-06 22:33:20 -08001771
1772 if (sps == 1)
1773 return delay;
1774
1775 dec = downsampleBurst(*delay);
1776
1777 delete delay;
1778 return dec;
1779}
1780
1781/*
Tom Tsoud3253432016-03-06 03:08:01 -08001782 * Demodulate GSMK burst. Prior to symbol rotation, operate at
1783 * 4 SPS (if activated) to minimize distortion through the fractional
1784 * delay filters. Symbol rotation and after always operates at 1 SPS.
1785 */
Tom Tsou70134a02017-06-12 14:23:53 -07001786static SoftVector *demodGmskBurst(const signalVector &rxBurst,
1787 int sps, complex channel, float TOA)
dburgessb3a0ca42011-10-12 07:44:40 +00001788{
Thomas Tsou94edaae2013-11-09 22:19:19 -05001789 SoftVector *bits;
Tom Tsou7fec3032016-03-06 22:33:20 -08001790 signalVector *dec;
dburgessb3a0ca42011-10-12 07:44:40 +00001791
Tom Tsou7fec3032016-03-06 22:33:20 -08001792 dec = demodCommon(rxBurst, sps, channel, TOA);
1793 if (!dec)
1794 return NULL;
dburgessb3a0ca42011-10-12 07:44:40 +00001795
Tom Tsoud3253432016-03-06 03:08:01 -08001796 /* Shift up by a quarter of a frequency */
1797 GMSKReverseRotate(*dec, 1);
Alexander Chemeris132fb242017-03-17 17:22:33 -07001798 /* Take real part of the signal */
1799 bits = signalToSoftVector(dec);
Thomas Tsou94edaae2013-11-09 22:19:19 -05001800 delete dec;
dburgessb3a0ca42011-10-12 07:44:40 +00001801
Thomas Tsou94edaae2013-11-09 22:19:19 -05001802 return bits;
dburgessb3a0ca42011-10-12 07:44:40 +00001803}
Thomas Tsou94edaae2013-11-09 22:19:19 -05001804
Tom Tsoud3253432016-03-06 03:08:01 -08001805/*
1806 * Demodulate an 8-PSK burst. Prior to symbol rotation, operate at
1807 * 4 SPS (if activated) to minimize distortion through the fractional
1808 * delay filters. Symbol rotation and after always operates at 1 SPS.
1809 *
1810 * Allow 1 SPS demodulation here, but note that other parts of the
1811 * transceiver restrict EDGE operatoin to 4 SPS - 8-PSK distortion
1812 * through the fractional delay filters at 1 SPS renders signal
1813 * nearly unrecoverable.
1814 */
Tom Tsou70134a02017-06-12 14:23:53 -07001815static SoftVector *demodEdgeBurst(const signalVector &burst,
1816 int sps, complex chan, float toa)
Tom Tsoud3253432016-03-06 03:08:01 -08001817{
1818 SoftVector *bits;
Tom Tsou7fec3032016-03-06 22:33:20 -08001819 signalVector *dec, *rot, *eq;
Tom Tsoud3253432016-03-06 03:08:01 -08001820
Tom Tsou7fec3032016-03-06 22:33:20 -08001821 dec = demodCommon(burst, sps, chan, toa);
1822 if (!dec)
Tom Tsoud3253432016-03-06 03:08:01 -08001823 return NULL;
1824
Tom Tsou7fec3032016-03-06 22:33:20 -08001825 /* Equalize and derotate */
Tom Tsoud3253432016-03-06 03:08:01 -08001826 eq = convolve(dec, GSMPulse4->c0_inv, NULL, NO_DELAY);
1827 rot = derotateEdgeBurst(*eq, 1);
1828
Tom Tsou7fec3032016-03-06 22:33:20 -08001829 /* Soft slice and normalize */
Tom Tsou04795622016-04-26 21:17:36 -07001830 bits = softSliceEdgeBurst(*rot);
Tom Tsoud3253432016-03-06 03:08:01 -08001831
1832 delete dec;
1833 delete eq;
1834 delete rot;
1835
1836 return bits;
1837}
1838
Alexander Chemerise0c12182017-03-18 13:27:48 -07001839SoftVector *demodAnyBurst(const signalVector &burst, int sps, complex amp,
Alexander Chemeris6e1dffd2017-03-17 16:13:51 -07001840 float toa, CorrType type)
1841{
1842 if (type == EDGE)
1843 return demodEdgeBurst(burst, sps, amp, toa);
1844 else
1845 return demodGmskBurst(burst, sps, amp, toa);
1846}
1847
Tom Tsou2079a3c2016-03-06 00:58:56 -08001848bool sigProcLibSetup()
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001849{
Thomas Tsou0e0e1f42013-11-09 22:08:51 -05001850 generateSincTable();
Tom Tsou2079a3c2016-03-06 00:58:56 -08001851 initGMSKRotationTables();
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001852
Tom Tsou2079a3c2016-03-06 00:58:56 -08001853 GSMPulse1 = generateGSMPulse(1);
1854 GSMPulse4 = generateGSMPulse(4);
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001855
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001856 generateRACHSequence(&gRACHSequences[0], gRACHSynchSequenceTS0, 1);
1857 generateRACHSequence(&gRACHSequences[1], gRACHSynchSequenceTS1, 1);
1858 generateRACHSequence(&gRACHSequences[2], gRACHSynchSequenceTS2, 1);
1859
Tom Tsoud3253432016-03-06 03:08:01 -08001860 for (int tsc = 0; tsc < 8; tsc++) {
Tom Tsou2079a3c2016-03-06 00:58:56 -08001861 generateMidamble(1, tsc);
Tom Tsoud3253432016-03-06 03:08:01 -08001862 gEdgeMidambles[tsc] = generateEdgeMidamble(tsc);
1863 }
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001864
Thomas Tsouf79c4d02013-11-09 15:51:56 -06001865 generateDelayFilters();
1866
Tom Tsoud3253432016-03-06 03:08:01 -08001867 dnsampler = new Resampler(1, 4);
1868 if (!dnsampler->init()) {
1869 LOG(ALERT) << "Rx resampler failed to initialize";
1870 goto fail;
1871 }
1872
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001873 return true;
Tom Tsoud3253432016-03-06 03:08:01 -08001874
1875fail:
1876 sigProcLibDestroy();
1877 return false;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001878}