blob: 28c4ded84ea9d8a86ad2bd690b4d5ae89873eec9 [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 {
Thomas Tsou3eaae802013-08-20 19:31:14 -040087 CorrelationSequence() : sequence(NULL), buffer(NULL)
Thomas Tsoue5dcfc42013-08-20 16:27:12 -040088 {
89 }
90
91 ~CorrelationSequence()
92 {
93 delete sequence;
Thomas Tsou3eaae802013-08-20 19:31:14 -040094 free(buffer);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -040095 }
96
dburgessb3a0ca42011-10-12 07:44:40 +000097 signalVector *sequence;
Thomas Tsou3eaae802013-08-20 19:31:14 -040098 void *buffer;
Thomas Tsouc1f7c422013-10-11 13:49:55 -040099 float toa;
dburgessb3a0ca42011-10-12 07:44:40 +0000100 complex gain;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400101};
dburgessb3a0ca42011-10-12 07:44:40 +0000102
Thomas Tsou83e06892013-08-20 16:10:01 -0400103/*
Thomas Tsou3eaae802013-08-20 19:31:14 -0400104 * Gaussian and empty modulation pulses. Like the correlation sequences,
105 * store the runtime (Gaussian) buffer separately because of needed alignment
106 * for SSE instructions.
Thomas Tsou83e06892013-08-20 16:10:01 -0400107 */
108struct PulseSequence {
Tom Tsoud3253432016-03-06 03:08:01 -0800109 PulseSequence() : c0(NULL), c1(NULL), c0_inv(NULL), empty(NULL),
110 c0_buffer(NULL), c1_buffer(NULL), c0_inv_buffer(NULL)
Thomas Tsou83e06892013-08-20 16:10:01 -0400111 {
112 }
113
114 ~PulseSequence()
115 {
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800116 delete c0;
117 delete c1;
Tom Tsoud3253432016-03-06 03:08:01 -0800118 delete c0_inv;
Thomas Tsou83e06892013-08-20 16:10:01 -0400119 delete empty;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800120 free(c0_buffer);
121 free(c1_buffer);
Thomas Tsou83e06892013-08-20 16:10:01 -0400122 }
123
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800124 signalVector *c0;
125 signalVector *c1;
Tom Tsoud3253432016-03-06 03:08:01 -0800126 signalVector *c0_inv;
Thomas Tsou83e06892013-08-20 16:10:01 -0400127 signalVector *empty;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800128 void *c0_buffer;
129 void *c1_buffer;
Tom Tsoud3253432016-03-06 03:08:01 -0800130 void *c0_inv_buffer;
Thomas Tsou83e06892013-08-20 16:10:01 -0400131};
132
Tom Tsoud3253432016-03-06 03:08:01 -0800133static CorrelationSequence *gMidambles[] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
134static CorrelationSequence *gEdgeMidambles[] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
Vadim Yanitskiya79bc702018-10-17 11:01:58 +0200135static CorrelationSequence *gRACHSequences[] = {NULL,NULL,NULL};
Tom Tsoud3253432016-03-06 03:08:01 -0800136static PulseSequence *GSMPulse1 = NULL;
137static PulseSequence *GSMPulse4 = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000138
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400139void sigProcLibDestroy()
140{
dburgessb3a0ca42011-10-12 07:44:40 +0000141 for (int i = 0; i < 8; i++) {
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400142 delete gMidambles[i];
Tom Tsoud3253432016-03-06 03:08:01 -0800143 delete gEdgeMidambles[i];
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400144 gMidambles[i] = NULL;
Tom Tsoud3253432016-03-06 03:08:01 -0800145 gEdgeMidambles[i] = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000146 }
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400147
Thomas Tsouf79c4d02013-11-09 15:51:56 -0600148 for (int i = 0; i < DELAYFILTS; i++) {
149 delete delayFilters[i];
150 delayFilters[i] = NULL;
151 }
152
Vadim Yanitskiya79bc702018-10-17 11:01:58 +0200153 for (int i = 0; i < 3; i++) {
154 delete gRACHSequences[i];
155 gRACHSequences[i] = NULL;
156 }
157
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400158 delete GMSKRotation1;
159 delete GMSKReverseRotation1;
Tom Tsou2079a3c2016-03-06 00:58:56 -0800160 delete GMSKRotation4;
161 delete GMSKReverseRotation4;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400162 delete GSMPulse1;
Tom Tsou2079a3c2016-03-06 00:58:56 -0800163 delete GSMPulse4;
Tom Tsoud3253432016-03-06 03:08:01 -0800164 delete dnsampler;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400165
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400166 GMSKRotation1 = NULL;
Tom Tsou2079a3c2016-03-06 00:58:56 -0800167 GMSKRotation4 = NULL;
168 GMSKReverseRotation4 = NULL;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400169 GMSKReverseRotation1 = NULL;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400170 GSMPulse1 = NULL;
Tom Tsou2079a3c2016-03-06 00:58:56 -0800171 GSMPulse4 = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000172}
173
Tom Tsou70134a02017-06-12 14:23:53 -0700174static float vectorNorm2(const signalVector &x)
dburgessb3a0ca42011-10-12 07:44:40 +0000175{
176 signalVector::const_iterator xPtr = x.begin();
177 float Energy = 0.0;
178 for (;xPtr != x.end();xPtr++) {
179 Energy += xPtr->norm2();
180 }
181 return Energy;
182}
183
Tom Tsou2079a3c2016-03-06 00:58:56 -0800184/*
185 * Initialize 4 sps and 1 sps rotation tables
186 */
187static void initGMSKRotationTables()
Thomas Tsoud24cc2c2013-08-20 15:41:45 -0400188{
Tom Tsou2079a3c2016-03-06 00:58:56 -0800189 size_t len1 = 157, len4 = 625;
190
191 GMSKRotation4 = new signalVector(len4);
192 GMSKReverseRotation4 = new signalVector(len4);
193 signalVector::iterator rotPtr = GMSKRotation4->begin();
194 signalVector::iterator revPtr = GMSKReverseRotation4->begin();
Tom Tsoubb0c68a2017-06-16 17:08:40 -0700195 auto phase = 0.0;
Tom Tsou2079a3c2016-03-06 00:58:56 -0800196 while (rotPtr != GMSKRotation4->end()) {
Tom Tsoubb0c68a2017-06-16 17:08:40 -0700197 *rotPtr++ = complex(cos(phase), sin(phase));
198 *revPtr++ = complex(cos(-phase), sin(-phase));
199 phase += M_PI / 2.0 / 4.0;
dburgessb3a0ca42011-10-12 07:44:40 +0000200 }
dburgessb3a0ca42011-10-12 07:44:40 +0000201
Tom Tsou2079a3c2016-03-06 00:58:56 -0800202 GMSKRotation1 = new signalVector(len1);
203 GMSKReverseRotation1 = new signalVector(len1);
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400204 rotPtr = GMSKRotation1->begin();
205 revPtr = GMSKReverseRotation1->begin();
206 phase = 0.0;
207 while (rotPtr != GMSKRotation1->end()) {
Tom Tsoubb0c68a2017-06-16 17:08:40 -0700208 *rotPtr++ = complex(cos(phase), sin(phase));
209 *revPtr++ = complex(cos(-phase), sin(-phase));
210 phase += M_PI / 2.0;
Thomas Tsoue57004d2013-08-20 18:55:33 -0400211 }
dburgessb3a0ca42011-10-12 07:44:40 +0000212}
213
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400214static void GMSKRotate(signalVector &x, int sps)
215{
Thomas Tsou0a3dc4c2013-11-09 02:29:55 -0500216#if HAVE_NEON
217 size_t len;
218 signalVector *a, *b, *out;
219
220 a = &x;
221 out = &x;
222 len = out->size();
223
224 if (len == 157)
225 len--;
226
227 if (sps == 1)
228 b = GMSKRotation1;
229 else
Tom Tsou2079a3c2016-03-06 00:58:56 -0800230 b = GMSKRotation4;
Thomas Tsou0a3dc4c2013-11-09 02:29:55 -0500231
232 mul_complex((float *) out->begin(),
233 (float *) a->begin(),
234 (float *) b->begin(), len);
235#else
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400236 signalVector::iterator rotPtr, xPtr = x.begin();
237
238 if (sps == 1)
239 rotPtr = GMSKRotation1->begin();
240 else
Tom Tsou2079a3c2016-03-06 00:58:56 -0800241 rotPtr = GMSKRotation4->begin();
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400242
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500243 if (x.isReal()) {
dburgessb3a0ca42011-10-12 07:44:40 +0000244 while (xPtr < x.end()) {
245 *xPtr = *rotPtr++ * (xPtr->real());
246 xPtr++;
247 }
248 }
249 else {
250 while (xPtr < x.end()) {
251 *xPtr = *rotPtr++ * (*xPtr);
252 xPtr++;
253 }
254 }
Thomas Tsou0a3dc4c2013-11-09 02:29:55 -0500255#endif
dburgessb3a0ca42011-10-12 07:44:40 +0000256}
257
Tom Tsou2079a3c2016-03-06 00:58:56 -0800258static bool GMSKReverseRotate(signalVector &x, int sps)
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400259{
260 signalVector::iterator rotPtr, xPtr= x.begin();
261
262 if (sps == 1)
263 rotPtr = GMSKReverseRotation1->begin();
Tom Tsou2079a3c2016-03-06 00:58:56 -0800264 else if (sps == 4)
265 rotPtr = GMSKReverseRotation4->begin();
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400266 else
Tom Tsou2079a3c2016-03-06 00:58:56 -0800267 return false;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400268
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500269 if (x.isReal()) {
dburgessb3a0ca42011-10-12 07:44:40 +0000270 while (xPtr < x.end()) {
271 *xPtr = *rotPtr++ * (xPtr->real());
272 xPtr++;
273 }
274 }
275 else {
276 while (xPtr < x.end()) {
277 *xPtr = *rotPtr++ * (*xPtr);
278 xPtr++;
279 }
280 }
Tom Tsou2079a3c2016-03-06 00:58:56 -0800281
282 return true;
dburgessb3a0ca42011-10-12 07:44:40 +0000283}
284
Tom Tsou70134a02017-06-12 14:23:53 -0700285/** Convolution type indicator */
286enum ConvType {
287 START_ONLY,
288 NO_DELAY,
289 CUSTOM,
290 UNDEFINED,
291};
292
293static signalVector *convolve(const signalVector *x, const signalVector *h,
294 signalVector *y, ConvType spanType,
295 size_t start = 0, size_t len = 0,
296 size_t step = 1, int offset = 0)
dburgessb3a0ca42011-10-12 07:44:40 +0000297{
Thomas Tsou3f32ab52013-11-15 16:32:54 -0500298 int rc;
299 size_t head = 0, tail = 0;
Thomas Tsou3eaae802013-08-20 19:31:14 -0400300 bool alloc = false, append = false;
301 const signalVector *_x = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000302
Thomas Tsou3eaae802013-08-20 19:31:14 -0400303 if (!x || !h)
dburgessb3a0ca42011-10-12 07:44:40 +0000304 return NULL;
305
Thomas Tsou3eaae802013-08-20 19:31:14 -0400306 switch (spanType) {
307 case START_ONLY:
308 start = 0;
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500309 head = h->size() - 1;
Thomas Tsou3eaae802013-08-20 19:31:14 -0400310 len = x->size();
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500311
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500312 if (x->getStart() < head)
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500313 append = true;
dburgessb3a0ca42011-10-12 07:44:40 +0000314 break;
Thomas Tsou3eaae802013-08-20 19:31:14 -0400315 case NO_DELAY:
316 start = h->size() / 2;
317 head = start;
318 tail = start;
319 len = x->size();
320 append = true;
321 break;
322 case CUSTOM:
323 if (start < h->size() - 1) {
324 head = h->size() - start;
325 append = true;
326 }
327 if (start + len > x->size()) {
328 tail = start + len - x->size();
329 append = true;
dburgessb3a0ca42011-10-12 07:44:40 +0000330 }
331 break;
332 default:
333 return NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000334 }
dburgessb3a0ca42011-10-12 07:44:40 +0000335
Thomas Tsou3eaae802013-08-20 19:31:14 -0400336 /*
337 * Error if the output vector is too small. Create the output vector
338 * if the pointer is NULL.
339 */
340 if (y && (len > y->size()))
341 return NULL;
342 if (!y) {
343 y = new signalVector(len);
344 alloc = true;
345 }
346
347 /* Prepend or post-pend the input vector if the parameters require it */
348 if (append)
349 _x = new signalVector(*x, head, tail);
350 else
351 _x = x;
352
353 /*
354 * Four convovle types:
355 * 1. Complex-Real (aligned)
356 * 2. Complex-Complex (aligned)
357 * 3. Complex-Real (!aligned)
358 * 4. Complex-Complex (!aligned)
359 */
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500360 if (h->isReal() && h->isAligned()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -0400361 rc = convolve_real((float *) _x->begin(), _x->size(),
362 (float *) h->begin(), h->size(),
363 (float *) y->begin(), y->size(),
364 start, len, step, offset);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500365 } else if (!h->isReal() && h->isAligned()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -0400366 rc = convolve_complex((float *) _x->begin(), _x->size(),
367 (float *) h->begin(), h->size(),
368 (float *) y->begin(), y->size(),
369 start, len, step, offset);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500370 } else if (h->isReal() && !h->isAligned()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -0400371 rc = base_convolve_real((float *) _x->begin(), _x->size(),
372 (float *) h->begin(), h->size(),
373 (float *) y->begin(), y->size(),
374 start, len, step, offset);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500375 } else if (!h->isReal() && !h->isAligned()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -0400376 rc = base_convolve_complex((float *) _x->begin(), _x->size(),
377 (float *) h->begin(), h->size(),
378 (float *) y->begin(), y->size(),
379 start, len, step, offset);
380 } else {
381 rc = -1;
382 }
383
384 if (append)
385 delete _x;
386
387 if (rc < 0) {
388 if (alloc)
389 delete y;
390 return NULL;
391 }
392
393 return y;
394}
dburgessb3a0ca42011-10-12 07:44:40 +0000395
Tom Tsoud3253432016-03-06 03:08:01 -0800396/*
397 * Generate static EDGE linear equalizer. This equalizer is not adaptive.
398 * Filter taps are generated from the inverted 1 SPS impulse response of
399 * the EDGE pulse shape captured after the downsampling filter.
400 */
401static bool generateInvertC0Pulse(PulseSequence *pulse)
402{
403 if (!pulse)
404 return false;
405
406 pulse->c0_inv_buffer = convolve_h_alloc(5);
407 pulse->c0_inv = new signalVector((complex *) pulse->c0_inv_buffer, 0, 5);
408 pulse->c0_inv->isReal(true);
409 pulse->c0_inv->setAligned(false);
410
411 signalVector::iterator xP = pulse->c0_inv->begin();
412 *xP++ = 0.15884;
413 *xP++ = -0.43176;
414 *xP++ = 1.00000;
415 *xP++ = -0.42608;
416 *xP++ = 0.14882;
417
418 return true;
419}
420
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400421static bool generateC1Pulse(int sps, PulseSequence *pulse)
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800422{
423 int len;
424
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400425 if (!pulse)
426 return false;
427
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800428 switch (sps) {
429 case 4:
430 len = 8;
431 break;
432 default:
433 return false;
434 }
435
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400436 pulse->c1_buffer = convolve_h_alloc(len);
437 pulse->c1 = new signalVector((complex *)
438 pulse->c1_buffer, 0, len);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500439 pulse->c1->isReal(true);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800440
441 /* Enable alignment for SSE usage */
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400442 pulse->c1->setAligned(true);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800443
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400444 signalVector::iterator xP = pulse->c1->begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800445
446 switch (sps) {
447 case 4:
448 /* BT = 0.30 */
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400449 *xP++ = 0.0;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800450 *xP++ = 8.16373112e-03;
451 *xP++ = 2.84385729e-02;
452 *xP++ = 5.64158904e-02;
453 *xP++ = 7.05463553e-02;
454 *xP++ = 5.64158904e-02;
455 *xP++ = 2.84385729e-02;
456 *xP++ = 8.16373112e-03;
457 }
458
459 return true;
460}
461
Tom Tsou2079a3c2016-03-06 00:58:56 -0800462static PulseSequence *generateGSMPulse(int sps)
dburgessb3a0ca42011-10-12 07:44:40 +0000463{
Thomas Tsou83e06892013-08-20 16:10:01 -0400464 int len;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800465 float arg, avg, center;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400466 PulseSequence *pulse;
Thomas Tsou83e06892013-08-20 16:10:01 -0400467
Tom Tsoud3253432016-03-06 03:08:01 -0800468 if ((sps != 1) && (sps != 4))
469 return NULL;
470
Thomas Tsou83e06892013-08-20 16:10:01 -0400471 /* Store a single tap filter used for correlation sequence generation */
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400472 pulse = new PulseSequence();
473 pulse->empty = new signalVector(1);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500474 pulse->empty->isReal(true);
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400475 *(pulse->empty->begin()) = 1.0f;
Thomas Tsou83e06892013-08-20 16:10:01 -0400476
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400477 /*
478 * For 4 samples-per-symbol use a precomputed single pulse Laurent
479 * approximation. This should yields below 2 degrees of phase error at
480 * the modulator output. Use the existing pulse approximation for all
481 * other oversampling factors.
482 */
483 switch (sps) {
484 case 4:
485 len = 16;
486 break;
Tom Tsoud3253432016-03-06 03:08:01 -0800487 case 1:
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400488 default:
Tom Tsou2079a3c2016-03-06 00:58:56 -0800489 len = 4;
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400490 }
Thomas Tsou3eaae802013-08-20 19:31:14 -0400491
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400492 pulse->c0_buffer = convolve_h_alloc(len);
493 pulse->c0 = new signalVector((complex *) pulse->c0_buffer, 0, len);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500494 pulse->c0->isReal(true);
Thomas Tsou3eaae802013-08-20 19:31:14 -0400495
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800496 /* Enable alingnment for SSE usage */
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400497 pulse->c0->setAligned(true);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800498
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400499 signalVector::iterator xP = pulse->c0->begin();
Thomas Tsou83e06892013-08-20 16:10:01 -0400500
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400501 if (sps == 4) {
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800502 *xP++ = 0.0;
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400503 *xP++ = 4.46348606e-03;
504 *xP++ = 2.84385729e-02;
505 *xP++ = 1.03184855e-01;
506 *xP++ = 2.56065552e-01;
507 *xP++ = 4.76375085e-01;
508 *xP++ = 7.05961177e-01;
509 *xP++ = 8.71291644e-01;
510 *xP++ = 9.29453645e-01;
511 *xP++ = 8.71291644e-01;
512 *xP++ = 7.05961177e-01;
513 *xP++ = 4.76375085e-01;
514 *xP++ = 2.56065552e-01;
515 *xP++ = 1.03184855e-01;
516 *xP++ = 2.84385729e-02;
517 *xP++ = 4.46348606e-03;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400518 generateC1Pulse(sps, pulse);
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400519 } else {
520 center = (float) (len - 1.0) / 2.0;
Thomas Tsou83e06892013-08-20 16:10:01 -0400521
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400522 /* GSM pulse approximation */
523 for (int i = 0; i < len; i++) {
524 arg = ((float) i - center) / (float) sps;
525 *xP++ = 0.96 * exp(-1.1380 * arg * arg -
526 0.527 * arg * arg * arg * arg);
527 }
dburgessb3a0ca42011-10-12 07:44:40 +0000528
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400529 avg = sqrtf(vectorNorm2(*pulse->c0) / sps);
530 xP = pulse->c0->begin();
531 for (int i = 0; i < len; i++)
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800532 *xP++ /= avg;
533 }
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400534
Tom Tsoud3253432016-03-06 03:08:01 -0800535 /*
536 * Current form of the EDGE equalization filter non-realizable at 4 SPS.
537 * Load the onto both 1 SPS and 4 SPS objects for convenience. Note that
538 * the EDGE demodulator downsamples to 1 SPS prior to equalization.
539 */
540 generateInvertC0Pulse(pulse);
541
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400542 return pulse;
dburgessb3a0ca42011-10-12 07:44:40 +0000543}
544
Tom Tsoud3253432016-03-06 03:08:01 -0800545bool vectorSlicer(SoftVector *x)
546{
547 SoftVector::iterator xP = x->begin();
548 SoftVector::iterator xPEnd = x->end();
549 while (xP < xPEnd) {
550 *xP = 0.5 * (*xP + 1.0f);
551 if (*xP > 1.0)
552 *xP = 1.0;
553 if (*xP < 0.0)
554 *xP = 0.0;
555 xP++;
556 }
557 return true;
558}
559
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800560static signalVector *rotateBurst(const BitVector &wBurst,
561 int guardPeriodLength, int sps)
562{
563 int burst_len;
Tom Tsou7278a872017-06-14 14:50:39 -0700564 signalVector *pulse, rotated;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800565 signalVector::iterator itr;
566
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400567 pulse = GSMPulse1->empty;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800568 burst_len = sps * (wBurst.size() + guardPeriodLength);
569 rotated = signalVector(burst_len);
570 itr = rotated.begin();
571
572 for (unsigned i = 0; i < wBurst.size(); i++) {
573 *itr = 2.0 * (wBurst[i] & 0x01) - 1.0;
574 itr += sps;
575 }
576
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400577 GMSKRotate(rotated, sps);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500578 rotated.isReal(false);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800579
580 /* Dummy filter operation */
Tom Tsou7278a872017-06-14 14:50:39 -0700581 return convolve(&rotated, pulse, NULL, START_ONLY);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800582}
583
Tom Tsoud3253432016-03-06 03:08:01 -0800584static void rotateBurst2(signalVector &burst, double phase)
585{
586 Complex<float> rot = Complex<float>(cos(phase), sin(phase));
587
588 for (size_t i = 0; i < burst.size(); i++)
589 burst[i] = burst[i] * rot;
590}
591
Tom Tsou4dfd64a2016-03-06 20:31:51 -0800592/*
593 * Ignore the guard length argument in the GMSK modulator interface
594 * because it results in 624/628 sized bursts instead of the preferred
595 * burst length of 625. Only 4 SPS is supported.
596 */
597static signalVector *modulateBurstLaurent(const BitVector &bits)
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800598{
Tom Tsou4dfd64a2016-03-06 20:31:51 -0800599 int burst_len, sps = 4;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800600 float phase;
Tom Tsou7278a872017-06-14 14:50:39 -0700601 signalVector *c0_pulse, *c1_pulse, *c0_shaped, *c1_shaped;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800602 signalVector::iterator c0_itr, c1_itr;
603
Tom Tsou2079a3c2016-03-06 00:58:56 -0800604 c0_pulse = GSMPulse4->c0;
605 c1_pulse = GSMPulse4->c1;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800606
Tom Tsou4dfd64a2016-03-06 20:31:51 -0800607 if (bits.size() > 156)
608 return NULL;
609
610 burst_len = 625;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800611
Tom Tsou7278a872017-06-14 14:50:39 -0700612 signalVector c0_burst(burst_len, c0_pulse->size());
613 c0_burst.isReal(true);
614 c0_itr = c0_burst.begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800615
Tom Tsou7278a872017-06-14 14:50:39 -0700616 signalVector c1_burst(burst_len, c1_pulse->size());
617 c1_itr = c1_burst.begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800618
Tom Tsouaa15d622016-08-11 14:36:23 -0700619 /* Padded differential tail bits */
620 *c0_itr = 2.0 * (0x00 & 0x01) - 1.0;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800621 c0_itr += sps;
622
623 /* Main burst bits */
624 for (unsigned i = 0; i < bits.size(); i++) {
625 *c0_itr = 2.0 * (bits[i] & 0x01) - 1.0;
626 c0_itr += sps;
627 }
628
Tom Tsouaa15d622016-08-11 14:36:23 -0700629 /* Padded differential tail bits */
630 *c0_itr = 2.0 * (0x00 & 0x01) - 1.0;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800631
632 /* Generate C0 phase coefficients */
Tom Tsou7278a872017-06-14 14:50:39 -0700633 GMSKRotate(c0_burst, sps);
634 c0_burst.isReal(false);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800635
Tom Tsou7278a872017-06-14 14:50:39 -0700636 c0_itr = c0_burst.begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800637 c0_itr += sps * 2;
638 c1_itr += sps * 2;
639
640 /* Start magic */
641 phase = 2.0 * ((0x01 & 0x01) ^ (0x01 & 0x01)) - 1.0;
642 *c1_itr = *c0_itr * Complex<float>(0, phase);
643 c0_itr += sps;
644 c1_itr += sps;
645
646 /* Generate C1 phase coefficients */
647 for (unsigned i = 2; i < bits.size(); i++) {
648 phase = 2.0 * ((bits[i - 1] & 0x01) ^ (bits[i - 2] & 0x01)) - 1.0;
649 *c1_itr = *c0_itr * Complex<float>(0, phase);
650
651 c0_itr += sps;
652 c1_itr += sps;
653 }
654
655 /* End magic */
656 int i = bits.size();
657 phase = 2.0 * ((bits[i-1] & 0x01) ^ (bits[i-2] & 0x01)) - 1.0;
658 *c1_itr = *c0_itr * Complex<float>(0, phase);
659
660 /* Primary (C0) and secondary (C1) pulse shaping */
Tom Tsou7278a872017-06-14 14:50:39 -0700661 c0_shaped = convolve(&c0_burst, c0_pulse, NULL, START_ONLY);
662 c1_shaped = convolve(&c1_burst, c1_pulse, NULL, START_ONLY);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800663
664 /* Sum shaped outputs into C0 */
665 c0_itr = c0_shaped->begin();
666 c1_itr = c1_shaped->begin();
667 for (unsigned i = 0; i < c0_shaped->size(); i++ )
668 *c0_itr++ += *c1_itr++;
669
670 delete c1_shaped;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800671 return c0_shaped;
672}
673
Tom Tsoud3253432016-03-06 03:08:01 -0800674static signalVector *rotateEdgeBurst(const signalVector &symbols, int sps)
675{
676 signalVector *burst;
677 signalVector::iterator burst_itr;
678
679 burst = new signalVector(symbols.size() * sps);
680 burst_itr = burst->begin();
681
682 for (size_t i = 0; i < symbols.size(); i++) {
683 float phase = i * 3.0f * M_PI / 8.0f;
684 Complex<float> rot = Complex<float>(cos(phase), sin(phase));
685
686 *burst_itr = symbols[i] * rot;
687 burst_itr += sps;
688 }
689
690 return burst;
691}
692
693static signalVector *derotateEdgeBurst(const signalVector &symbols, int sps)
694{
695 signalVector *burst;
696 signalVector::iterator burst_itr;
697
698 if (symbols.size() % sps)
699 return NULL;
700
701 burst = new signalVector(symbols.size() / sps);
702 burst_itr = burst->begin();
703
704 for (size_t i = 0; i < burst->size(); i++) {
705 float phase = (float) (i % 16) * 3.0f * M_PI / 8.0f;
706 Complex<float> rot = Complex<float>(cosf(phase), -sinf(phase));
707
708 *burst_itr = symbols[sps * i] * rot;
709 burst_itr++;
710 }
711
712 return burst;
713}
714
715static signalVector *mapEdgeSymbols(const BitVector &bits)
716{
717 if (bits.size() % 3)
718 return NULL;
719
720 signalVector *symbols = new signalVector(bits.size() / 3);
721
722 for (size_t i = 0; i < symbols->size(); i++) {
723 unsigned index = (((unsigned) bits[3 * i + 0] & 0x01) << 0) |
724 (((unsigned) bits[3 * i + 1] & 0x01) << 1) |
725 (((unsigned) bits[3 * i + 2] & 0x01) << 2);
726
727 (*symbols)[i] = psk8_table[index];
728 }
729
730 return symbols;
731}
732
Tom Tsoud2b07032016-04-26 19:28:59 -0700733/*
734 * EDGE 8-PSK rotate and pulse shape
735 *
736 * Delay the EDGE downlink bursts by one symbol in order to match GMSK pulse
737 * shaping group delay. The difference in group delay arises from the dual
738 * pulse filter combination of the GMSK Laurent represenation whereas 8-PSK
739 * uses a single pulse linear filter.
740 */
Tom Tsoud3253432016-03-06 03:08:01 -0800741static signalVector *shapeEdgeBurst(const signalVector &symbols)
742{
Tom Tsoud2b07032016-04-26 19:28:59 -0700743 size_t nsyms, nsamps = 625, sps = 4;
Tom Tsoud3253432016-03-06 03:08:01 -0800744 signalVector::iterator burst_itr;
745
746 nsyms = symbols.size();
747
Tom Tsoud2b07032016-04-26 19:28:59 -0700748 if (nsyms * sps > nsamps)
Tom Tsoud3253432016-03-06 03:08:01 -0800749 nsyms = 156;
750
Tom Tsou7278a872017-06-14 14:50:39 -0700751 signalVector burst(nsamps, GSMPulse4->c0->size());
Tom Tsoud3253432016-03-06 03:08:01 -0800752
Tom Tsoud2b07032016-04-26 19:28:59 -0700753 /* Delay burst by 1 symbol */
Tom Tsou7278a872017-06-14 14:50:39 -0700754 burst_itr = burst.begin() + sps;
Tom Tsou06676ea2016-07-19 12:50:21 -0700755 for (size_t i = 0; i < nsyms; i++) {
Tom Tsoud3253432016-03-06 03:08:01 -0800756 float phase = i * 3.0f * M_PI / 8.0f;
757 Complex<float> rot = Complex<float>(cos(phase), sin(phase));
758
759 *burst_itr = symbols[i] * rot;
Tom Tsoud2b07032016-04-26 19:28:59 -0700760 burst_itr += sps;
Tom Tsoud3253432016-03-06 03:08:01 -0800761 }
762
763 /* Single Gaussian pulse approximation shaping */
Tom Tsou7278a872017-06-14 14:50:39 -0700764 return convolve(&burst, GSMPulse4->c0, NULL, START_ONLY);
Tom Tsoud3253432016-03-06 03:08:01 -0800765}
766
767/*
Tom Tsou8ee2f382016-03-06 20:57:34 -0800768 * Generate a random GSM normal burst.
769 */
770signalVector *genRandNormalBurst(int tsc, int sps, int tn)
771{
772 if ((tsc < 0) || (tsc > 7) || (tn < 0) || (tn > 7))
773 return NULL;
774 if ((sps != 1) && (sps != 4))
775 return NULL;
776
777 int i = 0;
Tom Tsou7278a872017-06-14 14:50:39 -0700778 BitVector bits(148);
Tom Tsou8ee2f382016-03-06 20:57:34 -0800779
780 /* Tail bits */
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700781 for (; i < 3; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700782 bits[i] = 0;
Tom Tsou8ee2f382016-03-06 20:57:34 -0800783
784 /* Random bits */
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700785 for (; i < 60; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700786 bits[i] = rand() % 2;
Tom Tsou8ee2f382016-03-06 20:57:34 -0800787
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700788 /* Stealing bit */
Tom Tsou7278a872017-06-14 14:50:39 -0700789 bits[i++] = 0;
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700790
Tom Tsou8ee2f382016-03-06 20:57:34 -0800791 /* Training sequence */
792 for (int n = 0; i < 87; i++, n++)
Tom Tsou7278a872017-06-14 14:50:39 -0700793 bits[i] = gTrainingSequence[tsc][n];
Tom Tsou8ee2f382016-03-06 20:57:34 -0800794
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700795 /* Stealing bit */
Tom Tsou7278a872017-06-14 14:50:39 -0700796 bits[i++] = 0;
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700797
Tom Tsou8ee2f382016-03-06 20:57:34 -0800798 /* Random bits */
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700799 for (; i < 145; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700800 bits[i] = rand() % 2;
Tom Tsou8ee2f382016-03-06 20:57:34 -0800801
802 /* Tail bits */
803 for (; i < 148; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700804 bits[i] = 0;
Tom Tsou8ee2f382016-03-06 20:57:34 -0800805
806 int guard = 8 + !(tn % 4);
Tom Tsou7278a872017-06-14 14:50:39 -0700807 return modulateBurst(bits, guard, sps);
Tom Tsou8ee2f382016-03-06 20:57:34 -0800808}
809
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300810/*
811 * Generate a random GSM access burst.
812 */
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300813signalVector *genRandAccessBurst(int delay, int sps, int tn)
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300814{
815 if ((tn < 0) || (tn > 7))
816 return NULL;
817 if ((sps != 1) && (sps != 4))
818 return NULL;
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300819 if (delay > 68)
820 return NULL;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300821
822 int i = 0;
Tom Tsou7278a872017-06-14 14:50:39 -0700823 BitVector bits(88 + delay);
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300824
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300825 /* delay */
826 for (; i < delay; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700827 bits[i] = 0;
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300828
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300829 /* head and synch bits */
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300830 for (int n = 0; i < 49+delay; i++, n++)
Tom Tsou7278a872017-06-14 14:50:39 -0700831 bits[i] = gRACHBurst[n];
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300832
833 /* Random bits */
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300834 for (; i < 85+delay; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700835 bits[i] = rand() % 2;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300836
837 /* Tail bits */
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300838 for (; i < 88+delay; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700839 bits[i] = 0;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300840
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300841 int guard = 68-delay + !(tn % 4);
Tom Tsou7278a872017-06-14 14:50:39 -0700842 return modulateBurst(bits, guard, sps);
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300843}
844
Tom Tsou8ee2f382016-03-06 20:57:34 -0800845signalVector *generateEmptyBurst(int sps, int tn)
846{
847 if ((tn < 0) || (tn > 7))
848 return NULL;
849
850 if (sps == 4)
851 return new signalVector(625);
852 else if (sps == 1)
853 return new signalVector(148 + 8 + !(tn % 4));
854 else
855 return NULL;
856}
857
858signalVector *generateDummyBurst(int sps, int tn)
859{
860 if (((sps != 1) && (sps != 4)) || (tn < 0) || (tn > 7))
861 return NULL;
862
863 return modulateBurst(gDummyBurst, 8 + !(tn % 4), sps);
864}
865
866/*
Tom Tsoud3253432016-03-06 03:08:01 -0800867 * Generate a random 8-PSK EDGE burst. Only 4 SPS is supported with
868 * the returned burst being 625 samples in length.
869 */
870signalVector *generateEdgeBurst(int tsc)
871{
872 int tail = 9 / 3;
873 int data = 174 / 3;
874 int train = 78 / 3;
875
876 if ((tsc < 0) || (tsc > 7))
877 return NULL;
878
Tom Tsou7278a872017-06-14 14:50:39 -0700879 signalVector burst(148);
Tom Tsoud3253432016-03-06 03:08:01 -0800880 const BitVector *midamble = &gEdgeTrainingSequence[tsc];
881
882 /* Tail */
883 int n, i = 0;
884 for (; i < tail; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700885 burst[i] = psk8_table[7];
Tom Tsoud3253432016-03-06 03:08:01 -0800886
887 /* Body */
888 for (; i < tail + data; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700889 burst[i] = psk8_table[rand() % 8];
Tom Tsoud3253432016-03-06 03:08:01 -0800890
891 /* TSC */
892 for (n = 0; i < tail + data + train; i++, n++) {
893 unsigned index = (((unsigned) (*midamble)[3 * n + 0] & 0x01) << 0) |
894 (((unsigned) (*midamble)[3 * n + 1] & 0x01) << 1) |
895 (((unsigned) (*midamble)[3 * n + 2] & 0x01) << 2);
896
Tom Tsou7278a872017-06-14 14:50:39 -0700897 burst[i] = psk8_table[index];
Tom Tsoud3253432016-03-06 03:08:01 -0800898 }
899
900 /* Body */
901 for (; i < tail + data + train + data; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700902 burst[i] = psk8_table[rand() % 8];
Tom Tsoud3253432016-03-06 03:08:01 -0800903
904 /* Tail */
905 for (; i < tail + data + train + data + tail; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700906 burst[i] = psk8_table[7];
Tom Tsoud3253432016-03-06 03:08:01 -0800907
Tom Tsou7278a872017-06-14 14:50:39 -0700908 return shapeEdgeBurst(burst);
Tom Tsoud3253432016-03-06 03:08:01 -0800909}
910
911/*
912 * Modulate 8-PSK burst. When empty pulse shaping (rotation only)
913 * is enabled, the output vector length will be bit sequence length
914 * times the SPS value. When pulse shaping is enabled, the output
Alexander Chemeris9270a5a2017-03-17 13:03:41 -0700915 * vector length is fixed at 625 samples (156.25 symbols at 4 SPS).
Tom Tsoud3253432016-03-06 03:08:01 -0800916 * Pulse shaped bit sequences that go beyond one burst are truncated.
917 * Pulse shaping at anything but 4 SPS is not supported.
918 */
919signalVector *modulateEdgeBurst(const BitVector &bits,
920 int sps, bool empty)
921{
922 signalVector *shape, *burst;
923
924 if ((sps != 4) && !empty)
925 return NULL;
926
927 burst = mapEdgeSymbols(bits);
928 if (!burst)
929 return NULL;
930
931 if (empty)
932 shape = rotateEdgeBurst(*burst, sps);
933 else
934 shape = shapeEdgeBurst(*burst);
935
936 delete burst;
937 return shape;
938}
939
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800940static signalVector *modulateBurstBasic(const BitVector &bits,
941 int guard_len, int sps)
942{
943 int burst_len;
Tom Tsou7278a872017-06-14 14:50:39 -0700944 signalVector *pulse;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800945 signalVector::iterator burst_itr;
946
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400947 if (sps == 1)
948 pulse = GSMPulse1->c0;
949 else
Tom Tsou2079a3c2016-03-06 00:58:56 -0800950 pulse = GSMPulse4->c0;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400951
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800952 burst_len = sps * (bits.size() + guard_len);
953
Tom Tsou7278a872017-06-14 14:50:39 -0700954 signalVector burst(burst_len, pulse->size());
955 burst.isReal(true);
956 burst_itr = burst.begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800957
958 /* Raw bits are not differentially encoded */
959 for (unsigned i = 0; i < bits.size(); i++) {
960 *burst_itr = 2.0 * (bits[i] & 0x01) - 1.0;
961 burst_itr += sps;
962 }
963
Tom Tsou7278a872017-06-14 14:50:39 -0700964 GMSKRotate(burst, sps);
965 burst.isReal(false);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800966
967 /* Single Gaussian pulse approximation shaping */
Tom Tsou7278a872017-06-14 14:50:39 -0700968 return convolve(&burst, pulse, NULL, START_ONLY);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800969}
970
Thomas Tsou3eaae802013-08-20 19:31:14 -0400971/* Assume input bits are not differentially encoded */
Thomas Tsou83e06892013-08-20 16:10:01 -0400972signalVector *modulateBurst(const BitVector &wBurst, int guardPeriodLength,
973 int sps, bool emptyPulse)
dburgessb3a0ca42011-10-12 07:44:40 +0000974{
Thomas Tsou83e06892013-08-20 16:10:01 -0400975 if (emptyPulse)
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800976 return rotateBurst(wBurst, guardPeriodLength, sps);
977 else if (sps == 4)
Tom Tsou4dfd64a2016-03-06 20:31:51 -0800978 return modulateBurstLaurent(wBurst);
Thomas Tsou83e06892013-08-20 16:10:01 -0400979 else
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800980 return modulateBurstBasic(wBurst, guardPeriodLength, sps);
dburgessb3a0ca42011-10-12 07:44:40 +0000981}
982
Tom Tsou2079a3c2016-03-06 00:58:56 -0800983static void generateSincTable()
Thomas Tsou0e0e1f42013-11-09 22:08:51 -0500984{
Thomas Tsou0e0e1f42013-11-09 22:08:51 -0500985 for (int i = 0; i < TABLESIZE; i++) {
Tom Tsoua3dce852017-06-16 17:14:31 -0700986 auto x = (double) i / TABLESIZE * 8 * M_PI;
987 auto y = sin(x) / x;
Tom Tsou35474132017-06-19 16:00:34 -0700988 sincTable[i] = std::isnan(y) ? 1.0 : y;
Thomas Tsou0e0e1f42013-11-09 22:08:51 -0500989 }
990}
991
Tom Tsou70134a02017-06-12 14:23:53 -0700992static float sinc(float x)
dburgessb3a0ca42011-10-12 07:44:40 +0000993{
Thomas Tsou0e0e1f42013-11-09 22:08:51 -0500994 if (fabs(x) >= 8 * M_PI)
995 return 0.0;
996
997 int index = (int) floorf(fabs(x) / (8 * M_PI) * TABLESIZE);
998
999 return sincTable[index];
dburgessb3a0ca42011-10-12 07:44:40 +00001000}
1001
Thomas Tsouf79c4d02013-11-09 15:51:56 -06001002/*
1003 * Create fractional delay filterbank with Blackman-harris windowed
1004 * sinc function generator. The number of filters generated is specified
1005 * by the DELAYFILTS value.
1006 */
Tom Tsou70134a02017-06-12 14:23:53 -07001007static void generateDelayFilters()
Thomas Tsouf79c4d02013-11-09 15:51:56 -06001008{
1009 int h_len = 20;
1010 complex *data;
1011 signalVector *h;
1012 signalVector::iterator itr;
1013
1014 float k, sum;
1015 float a0 = 0.35875;
1016 float a1 = 0.48829;
1017 float a2 = 0.14128;
1018 float a3 = 0.01168;
1019
1020 for (int i = 0; i < DELAYFILTS; i++) {
1021 data = (complex *) convolve_h_alloc(h_len);
1022 h = new signalVector(data, 0, h_len);
1023 h->setAligned(true);
1024 h->isReal(true);
1025
1026 sum = 0.0;
1027 itr = h->end();
1028 for (int n = 0; n < h_len; n++) {
1029 k = (float) n;
1030 *--itr = (complex) sinc(M_PI_F *
1031 (k - (float) h_len / 2.0 - (float) i / DELAYFILTS));
1032 *itr *= a0 -
1033 a1 * cos(2 * M_PI * n / (h_len - 1)) +
1034 a2 * cos(4 * M_PI * n / (h_len - 1)) -
1035 a3 * cos(6 * M_PI * n / (h_len - 1));
1036
1037 sum += itr->real();
1038 }
1039
1040 itr = h->begin();
1041 for (int n = 0; n < h_len; n++)
1042 *itr++ /= sum;
1043
1044 delayFilters[i] = h;
1045 }
1046}
1047
Alexander Chemerise0c12182017-03-18 13:27:48 -07001048signalVector *delayVector(const signalVector *in, signalVector *out, float delay)
dburgessb3a0ca42011-10-12 07:44:40 +00001049{
Thomas Tsouf79c4d02013-11-09 15:51:56 -06001050 int whole, index;
Thomas Tsou2c282f52013-10-08 21:34:35 -04001051 float frac;
Thomas Tsou94edaae2013-11-09 22:19:19 -05001052 signalVector *h, *shift, *fshift = NULL;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001053
Thomas Tsou2c282f52013-10-08 21:34:35 -04001054 whole = floor(delay);
1055 frac = delay - whole;
1056
1057 /* Sinc interpolated fractional shift (if allowable) */
1058 if (fabs(frac) > 1e-2) {
Thomas Tsouf79c4d02013-11-09 15:51:56 -06001059 index = floorf(frac * (float) DELAYFILTS);
1060 h = delayFilters[index];
Thomas Tsou2c282f52013-10-08 21:34:35 -04001061
Thomas Tsou94edaae2013-11-09 22:19:19 -05001062 fshift = convolve(in, h, NULL, NO_DELAY);
1063 if (!fshift)
1064 return NULL;
dburgessb3a0ca42011-10-12 07:44:40 +00001065 }
1066
Thomas Tsou94edaae2013-11-09 22:19:19 -05001067 if (!fshift)
1068 shift = new signalVector(*in);
1069 else
1070 shift = fshift;
1071
Thomas Tsou2c282f52013-10-08 21:34:35 -04001072 /* Integer sample shift */
1073 if (whole < 0) {
1074 whole = -whole;
Thomas Tsou94edaae2013-11-09 22:19:19 -05001075 signalVector::iterator wBurstItr = shift->begin();
1076 signalVector::iterator shiftedItr = shift->begin() + whole;
Thomas Tsou2c282f52013-10-08 21:34:35 -04001077
Thomas Tsou94edaae2013-11-09 22:19:19 -05001078 while (shiftedItr < shift->end())
dburgessb3a0ca42011-10-12 07:44:40 +00001079 *wBurstItr++ = *shiftedItr++;
Thomas Tsou2c282f52013-10-08 21:34:35 -04001080
Thomas Tsou94edaae2013-11-09 22:19:19 -05001081 while (wBurstItr < shift->end())
1082 *wBurstItr++ = 0.0;
1083 } else if (whole >= 0) {
1084 signalVector::iterator wBurstItr = shift->end() - 1;
1085 signalVector::iterator shiftedItr = shift->end() - 1 - whole;
1086
1087 while (shiftedItr >= shift->begin())
dburgessb3a0ca42011-10-12 07:44:40 +00001088 *wBurstItr-- = *shiftedItr--;
Thomas Tsou94edaae2013-11-09 22:19:19 -05001089
1090 while (wBurstItr >= shift->begin())
dburgessb3a0ca42011-10-12 07:44:40 +00001091 *wBurstItr-- = 0.0;
1092 }
Thomas Tsou2c282f52013-10-08 21:34:35 -04001093
Thomas Tsou94edaae2013-11-09 22:19:19 -05001094 if (!out)
1095 return shift;
1096
1097 out->clone(*shift);
1098 delete shift;
1099 return out;
dburgessb3a0ca42011-10-12 07:44:40 +00001100}
Thomas Tsou2c282f52013-10-08 21:34:35 -04001101
Tom Tsou70134a02017-06-12 14:23:53 -07001102static complex interpolatePoint(const signalVector &inSig, float ix)
dburgessb3a0ca42011-10-12 07:44:40 +00001103{
dburgessb3a0ca42011-10-12 07:44:40 +00001104 int start = (int) (floor(ix) - 10);
1105 if (start < 0) start = 0;
1106 int end = (int) (floor(ix) + 11);
1107 if ((unsigned) end > inSig.size()-1) end = inSig.size()-1;
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001108
dburgessb3a0ca42011-10-12 07:44:40 +00001109 complex pVal = 0.0;
Thomas Tsou20eb6d62013-11-09 14:30:41 -05001110 if (!inSig.isReal()) {
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001111 for (int i = start; i < end; i++)
dburgessb3a0ca42011-10-12 07:44:40 +00001112 pVal += inSig[i] * sinc(M_PI_F*(i-ix));
1113 }
1114 else {
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001115 for (int i = start; i < end; i++)
dburgessb3a0ca42011-10-12 07:44:40 +00001116 pVal += inSig[i].real() * sinc(M_PI_F*(i-ix));
1117 }
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001118
dburgessb3a0ca42011-10-12 07:44:40 +00001119 return pVal;
1120}
1121
Thomas Tsou8181b012013-08-20 21:17:19 -04001122static complex fastPeakDetect(const signalVector &rxBurst, float *index)
1123{
1124 float val, max = 0.0f;
1125 complex amp;
1126 int _index = -1;
1127
Thomas Tsou3f32ab52013-11-15 16:32:54 -05001128 for (size_t i = 0; i < rxBurst.size(); i++) {
Thomas Tsou8181b012013-08-20 21:17:19 -04001129 val = rxBurst[i].norm2();
1130 if (val > max) {
1131 max = val;
1132 _index = i;
1133 amp = rxBurst[i];
1134 }
1135 }
1136
1137 if (index)
1138 *index = (float) _index;
1139
1140 return amp;
1141}
1142
Tom Tsou70134a02017-06-12 14:23:53 -07001143static complex peakDetect(const signalVector &rxBurst,
1144 float *peakIndex, float *avgPwr)
dburgessb3a0ca42011-10-12 07:44:40 +00001145{
dburgessb3a0ca42011-10-12 07:44:40 +00001146 complex maxVal = 0.0;
1147 float maxIndex = -1;
1148 float sumPower = 0.0;
1149
1150 for (unsigned int i = 0; i < rxBurst.size(); i++) {
1151 float samplePower = rxBurst[i].norm2();
1152 if (samplePower > maxVal.real()) {
1153 maxVal = samplePower;
1154 maxIndex = i;
1155 }
1156 sumPower += samplePower;
1157 }
1158
1159 // interpolate around the peak
1160 // to save computation, we'll use early-late balancing
1161 float earlyIndex = maxIndex-1;
1162 float lateIndex = maxIndex+1;
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001163
dburgessb3a0ca42011-10-12 07:44:40 +00001164 float incr = 0.5;
1165 while (incr > 1.0/1024.0) {
1166 complex earlyP = interpolatePoint(rxBurst,earlyIndex);
1167 complex lateP = interpolatePoint(rxBurst,lateIndex);
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001168 if (earlyP < lateP)
dburgessb3a0ca42011-10-12 07:44:40 +00001169 earlyIndex += incr;
1170 else if (earlyP > lateP)
1171 earlyIndex -= incr;
1172 else break;
1173 incr /= 2.0;
1174 lateIndex = earlyIndex + 2.0;
1175 }
1176
1177 maxIndex = earlyIndex + 1.0;
1178 maxVal = interpolatePoint(rxBurst,maxIndex);
1179
1180 if (peakIndex!=NULL)
1181 *peakIndex = maxIndex;
1182
1183 if (avgPwr!=NULL)
1184 *avgPwr = (sumPower-maxVal.norm2()) / (rxBurst.size()-1);
1185
1186 return maxVal;
1187
1188}
1189
1190void scaleVector(signalVector &x,
1191 complex scale)
1192{
Thomas Tsou7e4e5362013-10-30 21:18:55 -04001193#ifdef HAVE_NEON
1194 int len = x.size();
1195
1196 scale_complex((float *) x.begin(),
1197 (float *) x.begin(),
1198 (float *) &scale, len);
1199#else
dburgessb3a0ca42011-10-12 07:44:40 +00001200 signalVector::iterator xP = x.begin();
1201 signalVector::iterator xPEnd = x.end();
Thomas Tsou20eb6d62013-11-09 14:30:41 -05001202 if (!x.isReal()) {
dburgessb3a0ca42011-10-12 07:44:40 +00001203 while (xP < xPEnd) {
1204 *xP = *xP * scale;
1205 xP++;
1206 }
1207 }
1208 else {
1209 while (xP < xPEnd) {
1210 *xP = xP->real() * scale;
1211 xP++;
1212 }
1213 }
Thomas Tsou7e4e5362013-10-30 21:18:55 -04001214#endif
dburgessb3a0ca42011-10-12 07:44:40 +00001215}
1216
1217/** in-place conjugation */
Tom Tsou70134a02017-06-12 14:23:53 -07001218static void conjugateVector(signalVector &x)
dburgessb3a0ca42011-10-12 07:44:40 +00001219{
Thomas Tsou20eb6d62013-11-09 14:30:41 -05001220 if (x.isReal()) return;
dburgessb3a0ca42011-10-12 07:44:40 +00001221 signalVector::iterator xP = x.begin();
1222 signalVector::iterator xPEnd = x.end();
1223 while (xP < xPEnd) {
1224 *xP = xP->conj();
1225 xP++;
1226 }
1227}
1228
Tom Tsou2079a3c2016-03-06 00:58:56 -08001229static bool generateMidamble(int sps, int tsc)
dburgessb3a0ca42011-10-12 07:44:40 +00001230{
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001231 bool status = true;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001232 float toa;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001233 complex *data = NULL;
1234 signalVector *autocorr = NULL, *midamble = NULL;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001235 signalVector *midMidamble = NULL, *_midMidamble = NULL;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001236
Thomas Tsou3eaae802013-08-20 19:31:14 -04001237 if ((tsc < 0) || (tsc > 7))
dburgessb3a0ca42011-10-12 07:44:40 +00001238 return false;
1239
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001240 delete gMidambles[tsc];
Thomas Tsou3eaae802013-08-20 19:31:14 -04001241
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001242 /* Use middle 16 bits of each TSC. Correlation sequence is not pulse shaped */
1243 midMidamble = modulateBurst(gTrainingSequence[tsc].segment(5,16), 0, sps, true);
1244 if (!midMidamble)
1245 return false;
1246
Thomas Tsou3eaae802013-08-20 19:31:14 -04001247 /* Simulated receive sequence is pulse shaped */
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001248 midamble = modulateBurst(gTrainingSequence[tsc], 0, sps, false);
1249 if (!midamble) {
1250 status = false;
1251 goto release;
dburgessb3a0ca42011-10-12 07:44:40 +00001252 }
Thomas Tsou3eaae802013-08-20 19:31:14 -04001253
dburgessb3a0ca42011-10-12 07:44:40 +00001254 // NOTE: Because ideal TSC 16-bit midamble is 66 symbols into burst,
1255 // the ideal TSC has an + 180 degree phase shift,
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001256 // due to the pi/2 frequency shift, that
dburgessb3a0ca42011-10-12 07:44:40 +00001257 // needs to be accounted for.
1258 // 26-midamble is 61 symbols into burst, has +90 degree phase shift.
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001259 scaleVector(*midMidamble, complex(-1.0, 0.0));
1260 scaleVector(*midamble, complex(0.0, 1.0));
dburgessb3a0ca42011-10-12 07:44:40 +00001261
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001262 conjugateVector(*midMidamble);
dburgessb3a0ca42011-10-12 07:44:40 +00001263
Thomas Tsou3eaae802013-08-20 19:31:14 -04001264 /* For SSE alignment, reallocate the midamble sequence on 16-byte boundary */
1265 data = (complex *) convolve_h_alloc(midMidamble->size());
1266 _midMidamble = new signalVector(data, 0, midMidamble->size());
1267 _midMidamble->setAligned(true);
Pau Espin Pedrol5e68cde2018-08-30 20:45:14 +02001268 midMidamble->copyTo(*_midMidamble);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001269
1270 autocorr = convolve(midamble, _midMidamble, NULL, NO_DELAY);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001271 if (!autocorr) {
1272 status = false;
1273 goto release;
1274 }
dburgessb3a0ca42011-10-12 07:44:40 +00001275
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001276 gMidambles[tsc] = new CorrelationSequence;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001277 gMidambles[tsc]->buffer = data;
1278 gMidambles[tsc]->sequence = _midMidamble;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001279 gMidambles[tsc]->gain = peakDetect(*autocorr, &toa, NULL);
1280
1281 /* For 1 sps only
1282 * (Half of correlation length - 1) + midpoint of pulse shape + remainder
1283 * 13.5 = (16 / 2 - 1) + 1.5 + (26 - 10) / 2
1284 */
1285 if (sps == 1)
1286 gMidambles[tsc]->toa = toa - 13.5;
1287 else
1288 gMidambles[tsc]->toa = 0;
dburgessb3a0ca42011-10-12 07:44:40 +00001289
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001290release:
dburgessb3a0ca42011-10-12 07:44:40 +00001291 delete autocorr;
1292 delete midamble;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001293 delete midMidamble;
dburgessb3a0ca42011-10-12 07:44:40 +00001294
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001295 if (!status) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001296 delete _midMidamble;
1297 free(data);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001298 gMidambles[tsc] = NULL;
1299 }
1300
1301 return status;
dburgessb3a0ca42011-10-12 07:44:40 +00001302}
1303
Tom Tsou70134a02017-06-12 14:23:53 -07001304static CorrelationSequence *generateEdgeMidamble(int tsc)
Tom Tsoud3253432016-03-06 03:08:01 -08001305{
1306 complex *data = NULL;
1307 signalVector *midamble = NULL, *_midamble = NULL;
1308 CorrelationSequence *seq;
1309
1310 if ((tsc < 0) || (tsc > 7))
1311 return NULL;
1312
1313 /* Use middle 48 bits of each TSC. Correlation sequence is not pulse shaped */
1314 const BitVector *bits = &gEdgeTrainingSequence[tsc];
1315 midamble = modulateEdgeBurst(bits->segment(15, 48), 1, true);
1316 if (!midamble)
1317 return NULL;
1318
1319 conjugateVector(*midamble);
1320
1321 data = (complex *) convolve_h_alloc(midamble->size());
1322 _midamble = new signalVector(data, 0, midamble->size());
1323 _midamble->setAligned(true);
Pau Espin Pedrol5e68cde2018-08-30 20:45:14 +02001324 midamble->copyTo(*_midamble);
Tom Tsoud3253432016-03-06 03:08:01 -08001325
1326 /* Channel gain is an empirically measured value */
1327 seq = new CorrelationSequence;
1328 seq->buffer = data;
1329 seq->sequence = _midamble;
1330 seq->gain = Complex<float>(-19.6432, 19.5006) / 1.18;
1331 seq->toa = 0;
1332
1333 delete midamble;
1334
1335 return seq;
1336}
1337
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001338static bool generateRACHSequence(CorrelationSequence **seq, const BitVector &bv, int sps)
dburgessb3a0ca42011-10-12 07:44:40 +00001339{
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001340 bool status = true;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001341 float toa;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001342 complex *data = NULL;
1343 signalVector *autocorr = NULL;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001344 signalVector *seq0 = NULL, *seq1 = NULL, *_seq1 = NULL;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001345
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001346 if (*seq != NULL)
1347 delete *seq;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001348
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001349 seq0 = modulateBurst(bv, 0, sps, false);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001350 if (!seq0)
1351 return false;
1352
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001353 seq1 = modulateBurst(bv.segment(0, 40), 0, sps, true);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001354 if (!seq1) {
1355 status = false;
1356 goto release;
dburgessb3a0ca42011-10-12 07:44:40 +00001357 }
1358
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001359 conjugateVector(*seq1);
dburgessb3a0ca42011-10-12 07:44:40 +00001360
Thomas Tsou3eaae802013-08-20 19:31:14 -04001361 /* For SSE alignment, reallocate the midamble sequence on 16-byte boundary */
1362 data = (complex *) convolve_h_alloc(seq1->size());
1363 _seq1 = new signalVector(data, 0, seq1->size());
1364 _seq1->setAligned(true);
Pau Espin Pedrol5e68cde2018-08-30 20:45:14 +02001365 seq1->copyTo(*_seq1);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001366
1367 autocorr = convolve(seq0, _seq1, autocorr, NO_DELAY);
1368 if (!autocorr) {
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001369 status = false;
1370 goto release;
1371 }
dburgessb3a0ca42011-10-12 07:44:40 +00001372
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001373 *seq = new CorrelationSequence;
1374 (*seq)->sequence = _seq1;
1375 (*seq)->buffer = data;
1376 (*seq)->gain = peakDetect(*autocorr, &toa, NULL);
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001377
1378 /* For 1 sps only
1379 * (Half of correlation length - 1) + midpoint of pulse shaping filer
1380 * 20.5 = (40 / 2 - 1) + 1.5
1381 */
1382 if (sps == 1)
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001383 (*seq)->toa = toa - 20.5;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001384 else
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001385 (*seq)->toa = 0.0;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001386
1387release:
dburgessb3a0ca42011-10-12 07:44:40 +00001388 delete autocorr;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001389 delete seq0;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001390 delete seq1;
dburgessb3a0ca42011-10-12 07:44:40 +00001391
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001392 if (!status) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001393 delete _seq1;
1394 free(data);
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001395 *seq = NULL;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001396 }
dburgessb3a0ca42011-10-12 07:44:40 +00001397
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001398 return status;
dburgessb3a0ca42011-10-12 07:44:40 +00001399}
Thomas Tsou3eaae802013-08-20 19:31:14 -04001400
Tom Tsoua84e1622016-06-29 14:50:25 -07001401/*
1402 * Peak-to-average computation +/- range from peak in symbols
1403 */
1404#define COMPUTE_PEAK_MIN 2
1405#define COMPUTE_PEAK_MAX 5
1406
1407/*
1408 * Minimum number of values needed to compute peak-to-average
1409 */
1410#define COMPUTE_PEAK_CNT 5
1411
Thomas Tsou865bca42013-08-21 20:58:00 -04001412static float computePeakRatio(signalVector *corr,
1413 int sps, float toa, complex amp)
dburgessb3a0ca42011-10-12 07:44:40 +00001414{
Thomas Tsou865bca42013-08-21 20:58:00 -04001415 int num = 0;
1416 complex *peak;
1417 float rms, avg = 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001418
Thomas Tsou865bca42013-08-21 20:58:00 -04001419 /* Check for bogus results */
1420 if ((toa < 0.0) || (toa > corr->size()))
1421 return 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001422
Alexander Chemeris1e9b4d52015-06-04 19:05:28 -04001423 peak = corr->begin() + (int) rint(toa);
1424
Tom Tsoua84e1622016-06-29 14:50:25 -07001425 for (int i = COMPUTE_PEAK_MIN * sps; i <= COMPUTE_PEAK_MAX * sps; i++) {
Thomas Tsou865bca42013-08-21 20:58:00 -04001426 if (peak - i >= corr->begin()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001427 avg += (peak - i)->norm2();
1428 num++;
1429 }
Thomas Tsou865bca42013-08-21 20:58:00 -04001430 if (peak + i < corr->end()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001431 avg += (peak + i)->norm2();
1432 num++;
1433 }
dburgessb3a0ca42011-10-12 07:44:40 +00001434 }
1435
Tom Tsoua84e1622016-06-29 14:50:25 -07001436 if (num < COMPUTE_PEAK_CNT)
Thomas Tsou865bca42013-08-21 20:58:00 -04001437 return 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001438
Thomas Tsou3eaae802013-08-20 19:31:14 -04001439 rms = sqrtf(avg / (float) num) + 0.00001;
dburgessb3a0ca42011-10-12 07:44:40 +00001440
Thomas Tsou865bca42013-08-21 20:58:00 -04001441 return (amp.abs()) / rms;
dburgessb3a0ca42011-10-12 07:44:40 +00001442}
1443
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001444float energyDetect(const signalVector &rxBurst, unsigned windowLength)
dburgessb3a0ca42011-10-12 07:44:40 +00001445{
1446
1447 signalVector::const_iterator windowItr = rxBurst.begin(); //+rxBurst.size()/2 - 5*windowLength/2;
1448 float energy = 0.0;
Tom Tsou2af14402017-03-23 14:54:00 -07001449 if (windowLength == 0) return 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001450 if (windowLength > rxBurst.size()) windowLength = rxBurst.size();
1451 for (unsigned i = 0; i < windowLength; i++) {
1452 energy += windowItr->norm2();
1453 windowItr+=4;
1454 }
Alexander Chemeris1dd05cf2017-03-15 23:23:36 +03001455 return energy/windowLength;
dburgessb3a0ca42011-10-12 07:44:40 +00001456}
dburgessb3a0ca42011-10-12 07:44:40 +00001457
Tom Tsou70134a02017-06-12 14:23:53 -07001458static signalVector *downsampleBurst(const signalVector &burst)
1459{
Tom Tsou7278a872017-06-14 14:50:39 -07001460 signalVector in(DOWNSAMPLE_IN_LEN, dnsampler->len());
1461 signalVector *out = new signalVector(DOWNSAMPLE_OUT_LEN);
Pau Espin Pedrol5e68cde2018-08-30 20:45:14 +02001462 burst.copyToSegment(in, 0, DOWNSAMPLE_IN_LEN);
Tom Tsou70134a02017-06-12 14:23:53 -07001463
Tom Tsou7278a872017-06-14 14:50:39 -07001464 if (dnsampler->rotate((float *) in.begin(), DOWNSAMPLE_IN_LEN,
Tom Tsou70134a02017-06-12 14:23:53 -07001465 (float *) out->begin(), DOWNSAMPLE_OUT_LEN) < 0) {
1466 delete out;
1467 out = NULL;
1468 }
1469
Tom Tsou70134a02017-06-12 14:23:53 -07001470 return out;
1471};
1472
Thomas Tsou865bca42013-08-21 20:58:00 -04001473/*
1474 * Detect a burst based on correlation and peak-to-average ratio
1475 *
1476 * For one sampler-per-symbol, perform fast peak detection (no interpolation)
1477 * for initial gating. We do this because energy detection should be disabled.
1478 * For higher oversampling values, we assume the energy detector is in place
1479 * and we run full interpolating peak detection.
1480 */
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001481static int detectBurst(const signalVector &burst,
Thomas Tsou865bca42013-08-21 20:58:00 -04001482 signalVector &corr, CorrelationSequence *sync,
1483 float thresh, int sps, complex *amp, float *toa,
1484 int start, int len)
dburgessb3a0ca42011-10-12 07:44:40 +00001485{
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001486 const signalVector *corr_in;
1487 signalVector *dec = NULL;
Tom Tsoud3253432016-03-06 03:08:01 -08001488
1489 if (sps == 4) {
1490 dec = downsampleBurst(burst);
1491 corr_in = dec;
1492 sps = 1;
1493 } else {
1494 corr_in = &burst;
1495 }
1496
Thomas Tsou865bca42013-08-21 20:58:00 -04001497 /* Correlate */
Tom Tsoud3253432016-03-06 03:08:01 -08001498 if (!convolve(corr_in, sync->sequence, &corr,
1499 CUSTOM, start, len, 1, 0)) {
1500 delete dec;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001501 return -1;
dburgessb3a0ca42011-10-12 07:44:40 +00001502 }
1503
Tom Tsoud3253432016-03-06 03:08:01 -08001504 delete dec;
1505
1506 /* Running at the downsampled rate at this point */
1507 sps = 1;
1508
Thomas Tsou865bca42013-08-21 20:58:00 -04001509 /* Peak detection - place restrictions at correlation edges */
1510 *amp = fastPeakDetect(corr, toa);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001511
Thomas Tsou865bca42013-08-21 20:58:00 -04001512 if ((*toa < 3 * sps) || (*toa > len - 3 * sps))
1513 return 0;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001514
Thomas Tsou865bca42013-08-21 20:58:00 -04001515 /* Peak -to-average ratio */
1516 if (computePeakRatio(&corr, sps, *toa, *amp) < thresh)
1517 return 0;
1518
1519 /* Compute peak-to-average ratio. Reject if we don't have enough values */
1520 *amp = peakDetect(corr, toa, NULL);
1521
1522 /* Normalize our channel gain */
1523 *amp = *amp / sync->gain;
1524
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001525 /* Compensate for residuate time lag */
1526 *toa = *toa - sync->toa;
1527
Thomas Tsou865bca42013-08-21 20:58:00 -04001528 return 1;
1529}
1530
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001531static float maxAmplitude(const signalVector &burst)
Tom Tsou577cd022015-05-18 13:57:54 -07001532{
Alexander Chemeris954b1182015-06-04 15:39:41 -04001533 float max = 0.0;
1534 for (size_t i = 0; i < burst.size(); i++) {
1535 if (fabs(burst[i].real()) > max)
1536 max = fabs(burst[i].real());
1537 if (fabs(burst[i].imag()) > max)
1538 max = fabs(burst[i].imag());
1539 }
Tom Tsou577cd022015-05-18 13:57:54 -07001540
Alexander Chemeris954b1182015-06-04 15:39:41 -04001541 return max;
Tom Tsou577cd022015-05-18 13:57:54 -07001542}
1543
Alexander Chemeris130a8002015-06-09 20:52:11 -04001544/*
1545 * RACH/Normal burst detection with clipping detection
Thomas Tsou865bca42013-08-21 20:58:00 -04001546 *
1547 * Correlation window parameters:
Alexander Chemeris130a8002015-06-09 20:52:11 -04001548 * target: Tail bits + burst length
1549 * head: Search symbols before target
1550 * tail: Search symbols after target
Thomas Tsou865bca42013-08-21 20:58:00 -04001551 */
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001552static int detectGeneralBurst(const signalVector &rxBurst,
1553 float thresh,
1554 int sps,
1555 complex &amp,
1556 float &toa,
1557 int target, int head, int tail,
1558 CorrelationSequence *sync)
Thomas Tsou865bca42013-08-21 20:58:00 -04001559{
Alexander Chemeris130a8002015-06-09 20:52:11 -04001560 int rc, start, len;
Alexander Chemeris954b1182015-06-04 15:39:41 -04001561 bool clipping = false;
Thomas Tsou865bca42013-08-21 20:58:00 -04001562
1563 if ((sps != 1) && (sps != 4))
Tom Tsou577cd022015-05-18 13:57:54 -07001564 return -SIGERR_UNSUPPORTED;
1565
Alexander Chemeris954b1182015-06-04 15:39:41 -04001566 // Detect potential clipping
1567 // We still may be able to demod the burst, so we'll give it a try
1568 // and only report clipping if we can't demod.
1569 float maxAmpl = maxAmplitude(rxBurst);
1570 if (maxAmpl > CLIP_THRESH) {
1571 LOG(DEBUG) << "max burst amplitude: " << maxAmpl << " is above the clipping threshold: " << CLIP_THRESH << std::endl;
1572 clipping = true;
1573 }
Thomas Tsou865bca42013-08-21 20:58:00 -04001574
Tom Tsoud3253432016-03-06 03:08:01 -08001575 start = target - head - 1;
1576 len = head + tail;
Tom Tsou7278a872017-06-14 14:50:39 -07001577 signalVector corr(len);
Thomas Tsou865bca42013-08-21 20:58:00 -04001578
Tom Tsou7278a872017-06-14 14:50:39 -07001579 rc = detectBurst(rxBurst, corr, sync,
Alexander Chemeris130a8002015-06-09 20:52:11 -04001580 thresh, sps, &amp, &toa, start, len);
Thomas Tsou865bca42013-08-21 20:58:00 -04001581 if (rc < 0) {
Tom Tsou577cd022015-05-18 13:57:54 -07001582 return -SIGERR_INTERNAL;
Thomas Tsou865bca42013-08-21 20:58:00 -04001583 } else if (!rc) {
Alexander Chemeris130a8002015-06-09 20:52:11 -04001584 amp = 0.0f;
1585 toa = 0.0f;
Alexander Chemeris954b1182015-06-04 15:39:41 -04001586 return clipping?-SIGERR_CLIP:SIGERR_NONE;
dburgessb3a0ca42011-10-12 07:44:40 +00001587 }
1588
Thomas Tsou865bca42013-08-21 20:58:00 -04001589 /* Subtract forward search bits from delay */
Tom Tsoud3253432016-03-06 03:08:01 -08001590 toa -= head;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001591
Thomas Tsou865bca42013-08-21 20:58:00 -04001592 return 1;
1593}
Thomas Tsou3eaae802013-08-20 19:31:14 -04001594
Alexander Chemeris130a8002015-06-09 20:52:11 -04001595
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001596/*
Alexander Chemeris130a8002015-06-09 20:52:11 -04001597 * RACH burst detection
1598 *
1599 * Correlation window parameters:
1600 * target: Tail bits + RACH length (reduced from 41 to a multiple of 4)
Tom Tsoue90c24c2016-06-21 16:14:39 -07001601 * head: Search 8 symbols before target
1602 * tail: Search 8 symbols + maximum expected delay
Alexander Chemeris130a8002015-06-09 20:52:11 -04001603 */
Tom Tsou70134a02017-06-12 14:23:53 -07001604static int detectRACHBurst(const signalVector &burst, float threshold, int sps,
Vadim Yanitskiy444ff342018-10-22 02:25:23 +02001605 complex &amplitude, float &toa, unsigned max_toa, bool ext)
Alexander Chemeris130a8002015-06-09 20:52:11 -04001606{
1607 int rc, target, head, tail;
Vadim Yanitskiy444ff342018-10-22 02:25:23 +02001608 int i, num_seq;
Alexander Chemeris130a8002015-06-09 20:52:11 -04001609
1610 target = 8 + 40;
Tom Tsoue90c24c2016-06-21 16:14:39 -07001611 head = 8;
Alexander Chemeris14d13b62017-03-17 15:12:17 -07001612 tail = 8 + max_toa;
Vadim Yanitskiy444ff342018-10-22 02:25:23 +02001613 num_seq = ext ? 3 : 1;
Alexander Chemeris130a8002015-06-09 20:52:11 -04001614
Vadim Yanitskiy444ff342018-10-22 02:25:23 +02001615 for (i = 0; i < num_seq; i++) {
1616 rc = detectGeneralBurst(burst, threshold, sps, amplitude, toa,
1617 target, head, tail, gRACHSequences[i]);
1618 if (rc > 0)
1619 break;
1620 }
Alexander Chemeris130a8002015-06-09 20:52:11 -04001621
1622 return rc;
1623}
1624
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001625/*
Thomas Tsou865bca42013-08-21 20:58:00 -04001626 * Normal burst detection
1627 *
1628 * Correlation window parameters:
1629 * target: Tail + data + mid-midamble + 1/2 remaining midamblebits
Tom Tsoue90c24c2016-06-21 16:14:39 -07001630 * head: Search 6 symbols before target
1631 * tail: Search 6 symbols + maximum expected delay
Thomas Tsou865bca42013-08-21 20:58:00 -04001632 */
Tom Tsou70134a02017-06-12 14:23:53 -07001633static int analyzeTrafficBurst(const signalVector &burst, unsigned tsc, float threshold,
1634 int sps, complex &amplitude, float &toa, unsigned max_toa)
Thomas Tsou865bca42013-08-21 20:58:00 -04001635{
Alexander Chemeris130a8002015-06-09 20:52:11 -04001636 int rc, target, head, tail;
Thomas Tsou865bca42013-08-21 20:58:00 -04001637 CorrelationSequence *sync;
1638
Tom Tsouae91f132017-03-28 14:40:38 -07001639 if (tsc > 7)
Tom Tsou577cd022015-05-18 13:57:54 -07001640 return -SIGERR_UNSUPPORTED;
1641
Thomas Tsou865bca42013-08-21 20:58:00 -04001642 target = 3 + 58 + 16 + 5;
Tom Tsoue90c24c2016-06-21 16:14:39 -07001643 head = 6;
1644 tail = 6 + max_toa;
Thomas Tsou865bca42013-08-21 20:58:00 -04001645 sync = gMidambles[tsc];
Thomas Tsou865bca42013-08-21 20:58:00 -04001646
Alexander Chemeris14d13b62017-03-17 15:12:17 -07001647 rc = detectGeneralBurst(burst, threshold, sps, amplitude, toa,
Alexander Chemeris130a8002015-06-09 20:52:11 -04001648 target, head, tail, sync);
Alexander Chemeris130a8002015-06-09 20:52:11 -04001649 return rc;
dburgessb3a0ca42011-10-12 07:44:40 +00001650}
1651
Tom Tsou70134a02017-06-12 14:23:53 -07001652static int detectEdgeBurst(const signalVector &burst, unsigned tsc, float threshold,
1653 int sps, complex &amplitude, float &toa, unsigned max_toa)
Tom Tsoud3253432016-03-06 03:08:01 -08001654{
1655 int rc, target, head, tail;
1656 CorrelationSequence *sync;
1657
Tom Tsouae91f132017-03-28 14:40:38 -07001658 if (tsc > 7)
Tom Tsoud3253432016-03-06 03:08:01 -08001659 return -SIGERR_UNSUPPORTED;
1660
1661 target = 3 + 58 + 16 + 5;
Tom Tsoue90c24c2016-06-21 16:14:39 -07001662 head = 6;
1663 tail = 6 + max_toa;
Tom Tsoud3253432016-03-06 03:08:01 -08001664 sync = gEdgeMidambles[tsc];
1665
Alexander Chemeris14d13b62017-03-17 15:12:17 -07001666 rc = detectGeneralBurst(burst, threshold, sps, amplitude, toa,
Tom Tsoud3253432016-03-06 03:08:01 -08001667 target, head, tail, sync);
1668 return rc;
1669}
1670
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001671int detectAnyBurst(const signalVector &burst, unsigned tsc, float threshold,
Alexander Chemeris4e6c9382017-03-17 15:24:18 -07001672 int sps, CorrType type, complex &amp, float &toa,
1673 unsigned max_toa)
1674{
1675 int rc = 0;
1676
1677 switch (type) {
1678 case EDGE:
1679 rc = detectEdgeBurst(burst, tsc, threshold, sps,
1680 amp, toa, max_toa);
1681 if (rc > 0)
1682 break;
1683 else
1684 type = TSC;
1685 case TSC:
1686 rc = analyzeTrafficBurst(burst, tsc, threshold, sps,
1687 amp, toa, max_toa);
1688 break;
Vadim Yanitskiy444ff342018-10-22 02:25:23 +02001689 case EXT_RACH:
Alexander Chemeris4e6c9382017-03-17 15:24:18 -07001690 case RACH:
1691 rc = detectRACHBurst(burst, threshold, sps, amp, toa,
Vadim Yanitskiy444ff342018-10-22 02:25:23 +02001692 max_toa, type == EXT_RACH);
Alexander Chemeris4e6c9382017-03-17 15:24:18 -07001693 break;
1694 default:
1695 LOG(ERR) << "Invalid correlation type";
1696 }
1697
1698 if (rc > 0)
1699 return type;
1700
1701 return rc;
1702}
1703
Tom Tsoud3253432016-03-06 03:08:01 -08001704/*
1705 * Soft 8-PSK decoding using Manhattan distance metric
1706 */
1707static SoftVector *softSliceEdgeBurst(signalVector &burst)
1708{
1709 size_t nsyms = 148;
1710
1711 if (burst.size() < nsyms)
1712 return NULL;
1713
1714 signalVector::iterator itr;
1715 SoftVector *bits = new SoftVector(nsyms * 3);
1716
1717 /*
1718 * Bits 0 and 1 - First and second bits of the symbol respectively
1719 */
1720 rotateBurst2(burst, -M_PI / 8.0);
1721 itr = burst.begin();
1722 for (size_t i = 0; i < nsyms; i++) {
1723 (*bits)[3 * i + 0] = -itr->imag();
1724 (*bits)[3 * i + 1] = itr->real();
1725 itr++;
1726 }
1727
1728 /*
1729 * Bit 2 - Collapse symbols into quadrant 0 (positive X and Y).
1730 * Decision area is then simplified to X=Y axis. Rotate again to
1731 * place decision boundary on X-axis.
1732 */
1733 itr = burst.begin();
1734 for (size_t i = 0; i < burst.size(); i++) {
1735 burst[i] = Complex<float>(fabs(itr->real()), fabs(itr->imag()));
1736 itr++;
1737 }
1738
1739 rotateBurst2(burst, -M_PI / 4.0);
1740 itr = burst.begin();
1741 for (size_t i = 0; i < nsyms; i++) {
1742 (*bits)[3 * i + 2] = -itr->imag();
1743 itr++;
1744 }
1745
1746 signalVector soft(bits->size());
1747 for (size_t i = 0; i < bits->size(); i++)
1748 soft[i] = (*bits)[i];
1749
1750 return bits;
1751}
1752
1753/*
Alexander Chemeris132fb242017-03-17 17:22:33 -07001754 * Convert signalVector to SoftVector by taking real part of the signal.
1755 */
1756static SoftVector *signalToSoftVector(signalVector *dec)
1757{
1758 SoftVector *bits = new SoftVector(dec->size());
1759
1760 SoftVector::iterator bit_itr = bits->begin();
1761 signalVector::iterator burst_itr = dec->begin();
1762
1763 for (; burst_itr < dec->end(); burst_itr++)
1764 *bit_itr++ = burst_itr->real();
1765
1766 return bits;
1767}
1768
1769/*
Tom Tsou7fec3032016-03-06 22:33:20 -08001770 * Shared portion of GMSK and EDGE demodulators consisting of timing
1771 * recovery and single tap channel correction. For 4 SPS (if activated),
1772 * the output is downsampled prior to the 1 SPS modulation specific
1773 * stages.
1774 */
Alexander Chemerise0c12182017-03-18 13:27:48 -07001775static signalVector *demodCommon(const signalVector &burst, int sps,
Tom Tsou7fec3032016-03-06 22:33:20 -08001776 complex chan, float toa)
1777{
1778 signalVector *delay, *dec;
1779
1780 if ((sps != 1) && (sps != 4))
1781 return NULL;
1782
Tom Tsou7fec3032016-03-06 22:33:20 -08001783 delay = delayVector(&burst, NULL, -toa * (float) sps);
Alexander Chemerise0c12182017-03-18 13:27:48 -07001784 scaleVector(*delay, (complex) 1.0 / chan);
Tom Tsou7fec3032016-03-06 22:33:20 -08001785
1786 if (sps == 1)
1787 return delay;
1788
1789 dec = downsampleBurst(*delay);
1790
1791 delete delay;
1792 return dec;
1793}
1794
1795/*
Tom Tsoud3253432016-03-06 03:08:01 -08001796 * Demodulate GSMK burst. Prior to symbol rotation, operate at
1797 * 4 SPS (if activated) to minimize distortion through the fractional
1798 * delay filters. Symbol rotation and after always operates at 1 SPS.
1799 */
Tom Tsou70134a02017-06-12 14:23:53 -07001800static SoftVector *demodGmskBurst(const signalVector &rxBurst,
1801 int sps, complex channel, float TOA)
dburgessb3a0ca42011-10-12 07:44:40 +00001802{
Thomas Tsou94edaae2013-11-09 22:19:19 -05001803 SoftVector *bits;
Tom Tsou7fec3032016-03-06 22:33:20 -08001804 signalVector *dec;
dburgessb3a0ca42011-10-12 07:44:40 +00001805
Tom Tsou7fec3032016-03-06 22:33:20 -08001806 dec = demodCommon(rxBurst, sps, channel, TOA);
1807 if (!dec)
1808 return NULL;
dburgessb3a0ca42011-10-12 07:44:40 +00001809
Tom Tsoud3253432016-03-06 03:08:01 -08001810 /* Shift up by a quarter of a frequency */
1811 GMSKReverseRotate(*dec, 1);
Alexander Chemeris132fb242017-03-17 17:22:33 -07001812 /* Take real part of the signal */
1813 bits = signalToSoftVector(dec);
Thomas Tsou94edaae2013-11-09 22:19:19 -05001814 delete dec;
dburgessb3a0ca42011-10-12 07:44:40 +00001815
Thomas Tsou94edaae2013-11-09 22:19:19 -05001816 return bits;
dburgessb3a0ca42011-10-12 07:44:40 +00001817}
Thomas Tsou94edaae2013-11-09 22:19:19 -05001818
Tom Tsoud3253432016-03-06 03:08:01 -08001819/*
1820 * Demodulate an 8-PSK burst. Prior to symbol rotation, operate at
1821 * 4 SPS (if activated) to minimize distortion through the fractional
1822 * delay filters. Symbol rotation and after always operates at 1 SPS.
1823 *
1824 * Allow 1 SPS demodulation here, but note that other parts of the
1825 * transceiver restrict EDGE operatoin to 4 SPS - 8-PSK distortion
1826 * through the fractional delay filters at 1 SPS renders signal
1827 * nearly unrecoverable.
1828 */
Tom Tsou70134a02017-06-12 14:23:53 -07001829static SoftVector *demodEdgeBurst(const signalVector &burst,
1830 int sps, complex chan, float toa)
Tom Tsoud3253432016-03-06 03:08:01 -08001831{
1832 SoftVector *bits;
Tom Tsou7fec3032016-03-06 22:33:20 -08001833 signalVector *dec, *rot, *eq;
Tom Tsoud3253432016-03-06 03:08:01 -08001834
Tom Tsou7fec3032016-03-06 22:33:20 -08001835 dec = demodCommon(burst, sps, chan, toa);
1836 if (!dec)
Tom Tsoud3253432016-03-06 03:08:01 -08001837 return NULL;
1838
Tom Tsou7fec3032016-03-06 22:33:20 -08001839 /* Equalize and derotate */
Tom Tsoud3253432016-03-06 03:08:01 -08001840 eq = convolve(dec, GSMPulse4->c0_inv, NULL, NO_DELAY);
1841 rot = derotateEdgeBurst(*eq, 1);
1842
Tom Tsou7fec3032016-03-06 22:33:20 -08001843 /* Soft slice and normalize */
Tom Tsou04795622016-04-26 21:17:36 -07001844 bits = softSliceEdgeBurst(*rot);
Tom Tsoud3253432016-03-06 03:08:01 -08001845
1846 delete dec;
1847 delete eq;
1848 delete rot;
1849
1850 return bits;
1851}
1852
Alexander Chemerise0c12182017-03-18 13:27:48 -07001853SoftVector *demodAnyBurst(const signalVector &burst, int sps, complex amp,
Alexander Chemeris6e1dffd2017-03-17 16:13:51 -07001854 float toa, CorrType type)
1855{
1856 if (type == EDGE)
1857 return demodEdgeBurst(burst, sps, amp, toa);
1858 else
1859 return demodGmskBurst(burst, sps, amp, toa);
1860}
1861
Tom Tsou2079a3c2016-03-06 00:58:56 -08001862bool sigProcLibSetup()
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001863{
Thomas Tsou0e0e1f42013-11-09 22:08:51 -05001864 generateSincTable();
Tom Tsou2079a3c2016-03-06 00:58:56 -08001865 initGMSKRotationTables();
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001866
Tom Tsou2079a3c2016-03-06 00:58:56 -08001867 GSMPulse1 = generateGSMPulse(1);
1868 GSMPulse4 = generateGSMPulse(4);
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001869
Vadim Yanitskiya79bc702018-10-17 11:01:58 +02001870 generateRACHSequence(&gRACHSequences[0], gRACHSynchSequenceTS0, 1);
1871 generateRACHSequence(&gRACHSequences[1], gRACHSynchSequenceTS1, 1);
1872 generateRACHSequence(&gRACHSequences[2], gRACHSynchSequenceTS2, 1);
1873
Tom Tsoud3253432016-03-06 03:08:01 -08001874 for (int tsc = 0; tsc < 8; tsc++) {
Tom Tsou2079a3c2016-03-06 00:58:56 -08001875 generateMidamble(1, tsc);
Tom Tsoud3253432016-03-06 03:08:01 -08001876 gEdgeMidambles[tsc] = generateEdgeMidamble(tsc);
1877 }
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001878
Thomas Tsouf79c4d02013-11-09 15:51:56 -06001879 generateDelayFilters();
1880
Tom Tsoud3253432016-03-06 03:08:01 -08001881 dnsampler = new Resampler(1, 4);
1882 if (!dnsampler->init()) {
1883 LOG(ALERT) << "Rx resampler failed to initialize";
1884 goto fail;
1885 }
1886
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001887 return true;
Tom Tsoud3253432016-03-06 03:08:01 -08001888
1889fail:
1890 sigProcLibDestroy();
1891 return false;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001892}