blob: 692fbe024c86e9ef9a7627b853f53d0df5d0107c [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};
135static CorrelationSequence *gRACHSequence = NULL;
136static 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
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400153 delete GMSKRotation1;
154 delete GMSKReverseRotation1;
Tom Tsou2079a3c2016-03-06 00:58:56 -0800155 delete GMSKRotation4;
156 delete GMSKReverseRotation4;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400157 delete gRACHSequence;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400158 delete GSMPulse1;
Tom Tsou2079a3c2016-03-06 00:58:56 -0800159 delete GSMPulse4;
Tom Tsoud3253432016-03-06 03:08:01 -0800160 delete dnsampler;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400161
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400162 GMSKRotation1 = NULL;
Tom Tsou2079a3c2016-03-06 00:58:56 -0800163 GMSKRotation4 = NULL;
164 GMSKReverseRotation4 = NULL;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400165 GMSKReverseRotation1 = NULL;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400166 gRACHSequence = NULL;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400167 GSMPulse1 = NULL;
Tom Tsou2079a3c2016-03-06 00:58:56 -0800168 GSMPulse4 = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000169}
170
Tom Tsou70134a02017-06-12 14:23:53 -0700171static float vectorNorm2(const signalVector &x)
dburgessb3a0ca42011-10-12 07:44:40 +0000172{
173 signalVector::const_iterator xPtr = x.begin();
174 float Energy = 0.0;
175 for (;xPtr != x.end();xPtr++) {
176 Energy += xPtr->norm2();
177 }
178 return Energy;
179}
180
Tom Tsou2079a3c2016-03-06 00:58:56 -0800181/*
182 * Initialize 4 sps and 1 sps rotation tables
183 */
184static void initGMSKRotationTables()
Thomas Tsoud24cc2c2013-08-20 15:41:45 -0400185{
Tom Tsou2079a3c2016-03-06 00:58:56 -0800186 size_t len1 = 157, len4 = 625;
187
188 GMSKRotation4 = new signalVector(len4);
189 GMSKReverseRotation4 = new signalVector(len4);
190 signalVector::iterator rotPtr = GMSKRotation4->begin();
191 signalVector::iterator revPtr = GMSKReverseRotation4->begin();
Tom Tsoubb0c68a2017-06-16 17:08:40 -0700192 auto phase = 0.0;
Tom Tsou2079a3c2016-03-06 00:58:56 -0800193 while (rotPtr != GMSKRotation4->end()) {
Tom Tsoubb0c68a2017-06-16 17:08:40 -0700194 *rotPtr++ = complex(cos(phase), sin(phase));
195 *revPtr++ = complex(cos(-phase), sin(-phase));
196 phase += M_PI / 2.0 / 4.0;
dburgessb3a0ca42011-10-12 07:44:40 +0000197 }
dburgessb3a0ca42011-10-12 07:44:40 +0000198
Tom Tsou2079a3c2016-03-06 00:58:56 -0800199 GMSKRotation1 = new signalVector(len1);
200 GMSKReverseRotation1 = new signalVector(len1);
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400201 rotPtr = GMSKRotation1->begin();
202 revPtr = GMSKReverseRotation1->begin();
203 phase = 0.0;
204 while (rotPtr != GMSKRotation1->end()) {
Tom Tsoubb0c68a2017-06-16 17:08:40 -0700205 *rotPtr++ = complex(cos(phase), sin(phase));
206 *revPtr++ = complex(cos(-phase), sin(-phase));
207 phase += M_PI / 2.0;
Thomas Tsoue57004d2013-08-20 18:55:33 -0400208 }
dburgessb3a0ca42011-10-12 07:44:40 +0000209}
210
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400211static void GMSKRotate(signalVector &x, int sps)
212{
Thomas Tsou0a3dc4c2013-11-09 02:29:55 -0500213#if HAVE_NEON
214 size_t len;
215 signalVector *a, *b, *out;
216
217 a = &x;
218 out = &x;
219 len = out->size();
220
221 if (len == 157)
222 len--;
223
224 if (sps == 1)
225 b = GMSKRotation1;
226 else
Tom Tsou2079a3c2016-03-06 00:58:56 -0800227 b = GMSKRotation4;
Thomas Tsou0a3dc4c2013-11-09 02:29:55 -0500228
229 mul_complex((float *) out->begin(),
230 (float *) a->begin(),
231 (float *) b->begin(), len);
232#else
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400233 signalVector::iterator rotPtr, xPtr = x.begin();
234
235 if (sps == 1)
236 rotPtr = GMSKRotation1->begin();
237 else
Tom Tsou2079a3c2016-03-06 00:58:56 -0800238 rotPtr = GMSKRotation4->begin();
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400239
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500240 if (x.isReal()) {
dburgessb3a0ca42011-10-12 07:44:40 +0000241 while (xPtr < x.end()) {
242 *xPtr = *rotPtr++ * (xPtr->real());
243 xPtr++;
244 }
245 }
246 else {
247 while (xPtr < x.end()) {
248 *xPtr = *rotPtr++ * (*xPtr);
249 xPtr++;
250 }
251 }
Thomas Tsou0a3dc4c2013-11-09 02:29:55 -0500252#endif
dburgessb3a0ca42011-10-12 07:44:40 +0000253}
254
Tom Tsou2079a3c2016-03-06 00:58:56 -0800255static bool GMSKReverseRotate(signalVector &x, int sps)
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400256{
257 signalVector::iterator rotPtr, xPtr= x.begin();
258
259 if (sps == 1)
260 rotPtr = GMSKReverseRotation1->begin();
Tom Tsou2079a3c2016-03-06 00:58:56 -0800261 else if (sps == 4)
262 rotPtr = GMSKReverseRotation4->begin();
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400263 else
Tom Tsou2079a3c2016-03-06 00:58:56 -0800264 return false;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400265
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500266 if (x.isReal()) {
dburgessb3a0ca42011-10-12 07:44:40 +0000267 while (xPtr < x.end()) {
268 *xPtr = *rotPtr++ * (xPtr->real());
269 xPtr++;
270 }
271 }
272 else {
273 while (xPtr < x.end()) {
274 *xPtr = *rotPtr++ * (*xPtr);
275 xPtr++;
276 }
277 }
Tom Tsou2079a3c2016-03-06 00:58:56 -0800278
279 return true;
dburgessb3a0ca42011-10-12 07:44:40 +0000280}
281
Tom Tsou70134a02017-06-12 14:23:53 -0700282/** Convolution type indicator */
283enum ConvType {
284 START_ONLY,
285 NO_DELAY,
286 CUSTOM,
287 UNDEFINED,
288};
289
290static signalVector *convolve(const signalVector *x, const signalVector *h,
291 signalVector *y, ConvType spanType,
292 size_t start = 0, size_t len = 0,
293 size_t step = 1, int offset = 0)
dburgessb3a0ca42011-10-12 07:44:40 +0000294{
Thomas Tsou3f32ab52013-11-15 16:32:54 -0500295 int rc;
296 size_t head = 0, tail = 0;
Thomas Tsou3eaae802013-08-20 19:31:14 -0400297 bool alloc = false, append = false;
298 const signalVector *_x = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000299
Thomas Tsou3eaae802013-08-20 19:31:14 -0400300 if (!x || !h)
dburgessb3a0ca42011-10-12 07:44:40 +0000301 return NULL;
302
Thomas Tsou3eaae802013-08-20 19:31:14 -0400303 switch (spanType) {
304 case START_ONLY:
305 start = 0;
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500306 head = h->size() - 1;
Thomas Tsou3eaae802013-08-20 19:31:14 -0400307 len = x->size();
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500308
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500309 if (x->getStart() < head)
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500310 append = true;
dburgessb3a0ca42011-10-12 07:44:40 +0000311 break;
Thomas Tsou3eaae802013-08-20 19:31:14 -0400312 case NO_DELAY:
313 start = h->size() / 2;
314 head = start;
315 tail = start;
316 len = x->size();
317 append = true;
318 break;
319 case CUSTOM:
320 if (start < h->size() - 1) {
321 head = h->size() - start;
322 append = true;
323 }
324 if (start + len > x->size()) {
325 tail = start + len - x->size();
326 append = true;
dburgessb3a0ca42011-10-12 07:44:40 +0000327 }
328 break;
329 default:
330 return NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000331 }
dburgessb3a0ca42011-10-12 07:44:40 +0000332
Thomas Tsou3eaae802013-08-20 19:31:14 -0400333 /*
334 * Error if the output vector is too small. Create the output vector
335 * if the pointer is NULL.
336 */
337 if (y && (len > y->size()))
338 return NULL;
339 if (!y) {
340 y = new signalVector(len);
341 alloc = true;
342 }
343
344 /* Prepend or post-pend the input vector if the parameters require it */
345 if (append)
346 _x = new signalVector(*x, head, tail);
347 else
348 _x = x;
349
350 /*
351 * Four convovle types:
352 * 1. Complex-Real (aligned)
353 * 2. Complex-Complex (aligned)
354 * 3. Complex-Real (!aligned)
355 * 4. Complex-Complex (!aligned)
356 */
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500357 if (h->isReal() && h->isAligned()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -0400358 rc = convolve_real((float *) _x->begin(), _x->size(),
359 (float *) h->begin(), h->size(),
360 (float *) y->begin(), y->size(),
361 start, len, step, offset);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500362 } else if (!h->isReal() && h->isAligned()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -0400363 rc = convolve_complex((float *) _x->begin(), _x->size(),
364 (float *) h->begin(), h->size(),
365 (float *) y->begin(), y->size(),
366 start, len, step, offset);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500367 } else if (h->isReal() && !h->isAligned()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -0400368 rc = base_convolve_real((float *) _x->begin(), _x->size(),
369 (float *) h->begin(), h->size(),
370 (float *) y->begin(), y->size(),
371 start, len, step, offset);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500372 } else if (!h->isReal() && !h->isAligned()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -0400373 rc = base_convolve_complex((float *) _x->begin(), _x->size(),
374 (float *) h->begin(), h->size(),
375 (float *) y->begin(), y->size(),
376 start, len, step, offset);
377 } else {
378 rc = -1;
379 }
380
381 if (append)
382 delete _x;
383
384 if (rc < 0) {
385 if (alloc)
386 delete y;
387 return NULL;
388 }
389
390 return y;
391}
dburgessb3a0ca42011-10-12 07:44:40 +0000392
Tom Tsoud3253432016-03-06 03:08:01 -0800393/*
394 * Generate static EDGE linear equalizer. This equalizer is not adaptive.
395 * Filter taps are generated from the inverted 1 SPS impulse response of
396 * the EDGE pulse shape captured after the downsampling filter.
397 */
398static bool generateInvertC0Pulse(PulseSequence *pulse)
399{
400 if (!pulse)
401 return false;
402
403 pulse->c0_inv_buffer = convolve_h_alloc(5);
404 pulse->c0_inv = new signalVector((complex *) pulse->c0_inv_buffer, 0, 5);
405 pulse->c0_inv->isReal(true);
406 pulse->c0_inv->setAligned(false);
407
408 signalVector::iterator xP = pulse->c0_inv->begin();
409 *xP++ = 0.15884;
410 *xP++ = -0.43176;
411 *xP++ = 1.00000;
412 *xP++ = -0.42608;
413 *xP++ = 0.14882;
414
415 return true;
416}
417
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400418static bool generateC1Pulse(int sps, PulseSequence *pulse)
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800419{
420 int len;
421
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400422 if (!pulse)
423 return false;
424
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800425 switch (sps) {
426 case 4:
427 len = 8;
428 break;
429 default:
430 return false;
431 }
432
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400433 pulse->c1_buffer = convolve_h_alloc(len);
434 pulse->c1 = new signalVector((complex *)
435 pulse->c1_buffer, 0, len);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500436 pulse->c1->isReal(true);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800437
438 /* Enable alignment for SSE usage */
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400439 pulse->c1->setAligned(true);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800440
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400441 signalVector::iterator xP = pulse->c1->begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800442
443 switch (sps) {
444 case 4:
445 /* BT = 0.30 */
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400446 *xP++ = 0.0;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800447 *xP++ = 8.16373112e-03;
448 *xP++ = 2.84385729e-02;
449 *xP++ = 5.64158904e-02;
450 *xP++ = 7.05463553e-02;
451 *xP++ = 5.64158904e-02;
452 *xP++ = 2.84385729e-02;
453 *xP++ = 8.16373112e-03;
454 }
455
456 return true;
457}
458
Tom Tsou2079a3c2016-03-06 00:58:56 -0800459static PulseSequence *generateGSMPulse(int sps)
dburgessb3a0ca42011-10-12 07:44:40 +0000460{
Thomas Tsou83e06892013-08-20 16:10:01 -0400461 int len;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800462 float arg, avg, center;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400463 PulseSequence *pulse;
Thomas Tsou83e06892013-08-20 16:10:01 -0400464
Tom Tsoud3253432016-03-06 03:08:01 -0800465 if ((sps != 1) && (sps != 4))
466 return NULL;
467
Thomas Tsou83e06892013-08-20 16:10:01 -0400468 /* Store a single tap filter used for correlation sequence generation */
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400469 pulse = new PulseSequence();
470 pulse->empty = new signalVector(1);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500471 pulse->empty->isReal(true);
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400472 *(pulse->empty->begin()) = 1.0f;
Thomas Tsou83e06892013-08-20 16:10:01 -0400473
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400474 /*
475 * For 4 samples-per-symbol use a precomputed single pulse Laurent
476 * approximation. This should yields below 2 degrees of phase error at
477 * the modulator output. Use the existing pulse approximation for all
478 * other oversampling factors.
479 */
480 switch (sps) {
481 case 4:
482 len = 16;
483 break;
Tom Tsoud3253432016-03-06 03:08:01 -0800484 case 1:
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400485 default:
Tom Tsou2079a3c2016-03-06 00:58:56 -0800486 len = 4;
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400487 }
Thomas Tsou3eaae802013-08-20 19:31:14 -0400488
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400489 pulse->c0_buffer = convolve_h_alloc(len);
490 pulse->c0 = new signalVector((complex *) pulse->c0_buffer, 0, len);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500491 pulse->c0->isReal(true);
Thomas Tsou3eaae802013-08-20 19:31:14 -0400492
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800493 /* Enable alingnment for SSE usage */
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400494 pulse->c0->setAligned(true);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800495
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400496 signalVector::iterator xP = pulse->c0->begin();
Thomas Tsou83e06892013-08-20 16:10:01 -0400497
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400498 if (sps == 4) {
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800499 *xP++ = 0.0;
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400500 *xP++ = 4.46348606e-03;
501 *xP++ = 2.84385729e-02;
502 *xP++ = 1.03184855e-01;
503 *xP++ = 2.56065552e-01;
504 *xP++ = 4.76375085e-01;
505 *xP++ = 7.05961177e-01;
506 *xP++ = 8.71291644e-01;
507 *xP++ = 9.29453645e-01;
508 *xP++ = 8.71291644e-01;
509 *xP++ = 7.05961177e-01;
510 *xP++ = 4.76375085e-01;
511 *xP++ = 2.56065552e-01;
512 *xP++ = 1.03184855e-01;
513 *xP++ = 2.84385729e-02;
514 *xP++ = 4.46348606e-03;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400515 generateC1Pulse(sps, pulse);
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400516 } else {
517 center = (float) (len - 1.0) / 2.0;
Thomas Tsou83e06892013-08-20 16:10:01 -0400518
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400519 /* GSM pulse approximation */
520 for (int i = 0; i < len; i++) {
521 arg = ((float) i - center) / (float) sps;
522 *xP++ = 0.96 * exp(-1.1380 * arg * arg -
523 0.527 * arg * arg * arg * arg);
524 }
dburgessb3a0ca42011-10-12 07:44:40 +0000525
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400526 avg = sqrtf(vectorNorm2(*pulse->c0) / sps);
527 xP = pulse->c0->begin();
528 for (int i = 0; i < len; i++)
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800529 *xP++ /= avg;
530 }
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400531
Tom Tsoud3253432016-03-06 03:08:01 -0800532 /*
533 * Current form of the EDGE equalization filter non-realizable at 4 SPS.
534 * Load the onto both 1 SPS and 4 SPS objects for convenience. Note that
535 * the EDGE demodulator downsamples to 1 SPS prior to equalization.
536 */
537 generateInvertC0Pulse(pulse);
538
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400539 return pulse;
dburgessb3a0ca42011-10-12 07:44:40 +0000540}
541
Tom Tsoud3253432016-03-06 03:08:01 -0800542bool vectorSlicer(SoftVector *x)
543{
544 SoftVector::iterator xP = x->begin();
545 SoftVector::iterator xPEnd = x->end();
546 while (xP < xPEnd) {
547 *xP = 0.5 * (*xP + 1.0f);
548 if (*xP > 1.0)
549 *xP = 1.0;
550 if (*xP < 0.0)
551 *xP = 0.0;
552 xP++;
553 }
554 return true;
555}
556
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800557static signalVector *rotateBurst(const BitVector &wBurst,
558 int guardPeriodLength, int sps)
559{
560 int burst_len;
Tom Tsou7278a872017-06-14 14:50:39 -0700561 signalVector *pulse, rotated;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800562 signalVector::iterator itr;
563
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400564 pulse = GSMPulse1->empty;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800565 burst_len = sps * (wBurst.size() + guardPeriodLength);
566 rotated = signalVector(burst_len);
567 itr = rotated.begin();
568
569 for (unsigned i = 0; i < wBurst.size(); i++) {
570 *itr = 2.0 * (wBurst[i] & 0x01) - 1.0;
571 itr += sps;
572 }
573
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400574 GMSKRotate(rotated, sps);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500575 rotated.isReal(false);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800576
577 /* Dummy filter operation */
Tom Tsou7278a872017-06-14 14:50:39 -0700578 return convolve(&rotated, pulse, NULL, START_ONLY);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800579}
580
Tom Tsoud3253432016-03-06 03:08:01 -0800581static void rotateBurst2(signalVector &burst, double phase)
582{
583 Complex<float> rot = Complex<float>(cos(phase), sin(phase));
584
585 for (size_t i = 0; i < burst.size(); i++)
586 burst[i] = burst[i] * rot;
587}
588
Tom Tsou4dfd64a2016-03-06 20:31:51 -0800589/*
590 * Ignore the guard length argument in the GMSK modulator interface
591 * because it results in 624/628 sized bursts instead of the preferred
592 * burst length of 625. Only 4 SPS is supported.
593 */
594static signalVector *modulateBurstLaurent(const BitVector &bits)
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800595{
Tom Tsou4dfd64a2016-03-06 20:31:51 -0800596 int burst_len, sps = 4;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800597 float phase;
Tom Tsou7278a872017-06-14 14:50:39 -0700598 signalVector *c0_pulse, *c1_pulse, *c0_shaped, *c1_shaped;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800599 signalVector::iterator c0_itr, c1_itr;
600
Tom Tsou2079a3c2016-03-06 00:58:56 -0800601 c0_pulse = GSMPulse4->c0;
602 c1_pulse = GSMPulse4->c1;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800603
Tom Tsou4dfd64a2016-03-06 20:31:51 -0800604 if (bits.size() > 156)
605 return NULL;
606
607 burst_len = 625;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800608
Tom Tsou7278a872017-06-14 14:50:39 -0700609 signalVector c0_burst(burst_len, c0_pulse->size());
610 c0_burst.isReal(true);
611 c0_itr = c0_burst.begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800612
Tom Tsou7278a872017-06-14 14:50:39 -0700613 signalVector c1_burst(burst_len, c1_pulse->size());
614 c1_itr = c1_burst.begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800615
Tom Tsouaa15d622016-08-11 14:36:23 -0700616 /* Padded differential tail bits */
617 *c0_itr = 2.0 * (0x00 & 0x01) - 1.0;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800618 c0_itr += sps;
619
620 /* Main burst bits */
621 for (unsigned i = 0; i < bits.size(); i++) {
622 *c0_itr = 2.0 * (bits[i] & 0x01) - 1.0;
623 c0_itr += sps;
624 }
625
Tom Tsouaa15d622016-08-11 14:36:23 -0700626 /* Padded differential tail bits */
627 *c0_itr = 2.0 * (0x00 & 0x01) - 1.0;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800628
629 /* Generate C0 phase coefficients */
Tom Tsou7278a872017-06-14 14:50:39 -0700630 GMSKRotate(c0_burst, sps);
631 c0_burst.isReal(false);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800632
Tom Tsou7278a872017-06-14 14:50:39 -0700633 c0_itr = c0_burst.begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800634 c0_itr += sps * 2;
635 c1_itr += sps * 2;
636
637 /* Start magic */
638 phase = 2.0 * ((0x01 & 0x01) ^ (0x01 & 0x01)) - 1.0;
639 *c1_itr = *c0_itr * Complex<float>(0, phase);
640 c0_itr += sps;
641 c1_itr += sps;
642
643 /* Generate C1 phase coefficients */
644 for (unsigned i = 2; i < bits.size(); i++) {
645 phase = 2.0 * ((bits[i - 1] & 0x01) ^ (bits[i - 2] & 0x01)) - 1.0;
646 *c1_itr = *c0_itr * Complex<float>(0, phase);
647
648 c0_itr += sps;
649 c1_itr += sps;
650 }
651
652 /* End magic */
653 int i = bits.size();
654 phase = 2.0 * ((bits[i-1] & 0x01) ^ (bits[i-2] & 0x01)) - 1.0;
655 *c1_itr = *c0_itr * Complex<float>(0, phase);
656
657 /* Primary (C0) and secondary (C1) pulse shaping */
Tom Tsou7278a872017-06-14 14:50:39 -0700658 c0_shaped = convolve(&c0_burst, c0_pulse, NULL, START_ONLY);
659 c1_shaped = convolve(&c1_burst, c1_pulse, NULL, START_ONLY);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800660
661 /* Sum shaped outputs into C0 */
662 c0_itr = c0_shaped->begin();
663 c1_itr = c1_shaped->begin();
664 for (unsigned i = 0; i < c0_shaped->size(); i++ )
665 *c0_itr++ += *c1_itr++;
666
667 delete c1_shaped;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800668 return c0_shaped;
669}
670
Tom Tsoud3253432016-03-06 03:08:01 -0800671static signalVector *rotateEdgeBurst(const signalVector &symbols, int sps)
672{
673 signalVector *burst;
674 signalVector::iterator burst_itr;
675
676 burst = new signalVector(symbols.size() * sps);
677 burst_itr = burst->begin();
678
679 for (size_t i = 0; i < symbols.size(); i++) {
680 float phase = i * 3.0f * M_PI / 8.0f;
681 Complex<float> rot = Complex<float>(cos(phase), sin(phase));
682
683 *burst_itr = symbols[i] * rot;
684 burst_itr += sps;
685 }
686
687 return burst;
688}
689
690static signalVector *derotateEdgeBurst(const signalVector &symbols, int sps)
691{
692 signalVector *burst;
693 signalVector::iterator burst_itr;
694
695 if (symbols.size() % sps)
696 return NULL;
697
698 burst = new signalVector(symbols.size() / sps);
699 burst_itr = burst->begin();
700
701 for (size_t i = 0; i < burst->size(); i++) {
702 float phase = (float) (i % 16) * 3.0f * M_PI / 8.0f;
703 Complex<float> rot = Complex<float>(cosf(phase), -sinf(phase));
704
705 *burst_itr = symbols[sps * i] * rot;
706 burst_itr++;
707 }
708
709 return burst;
710}
711
712static signalVector *mapEdgeSymbols(const BitVector &bits)
713{
714 if (bits.size() % 3)
715 return NULL;
716
717 signalVector *symbols = new signalVector(bits.size() / 3);
718
719 for (size_t i = 0; i < symbols->size(); i++) {
720 unsigned index = (((unsigned) bits[3 * i + 0] & 0x01) << 0) |
721 (((unsigned) bits[3 * i + 1] & 0x01) << 1) |
722 (((unsigned) bits[3 * i + 2] & 0x01) << 2);
723
724 (*symbols)[i] = psk8_table[index];
725 }
726
727 return symbols;
728}
729
Tom Tsoud2b07032016-04-26 19:28:59 -0700730/*
731 * EDGE 8-PSK rotate and pulse shape
732 *
733 * Delay the EDGE downlink bursts by one symbol in order to match GMSK pulse
734 * shaping group delay. The difference in group delay arises from the dual
735 * pulse filter combination of the GMSK Laurent represenation whereas 8-PSK
736 * uses a single pulse linear filter.
737 */
Tom Tsoud3253432016-03-06 03:08:01 -0800738static signalVector *shapeEdgeBurst(const signalVector &symbols)
739{
Tom Tsoud2b07032016-04-26 19:28:59 -0700740 size_t nsyms, nsamps = 625, sps = 4;
Tom Tsoud3253432016-03-06 03:08:01 -0800741 signalVector::iterator burst_itr;
742
743 nsyms = symbols.size();
744
Tom Tsoud2b07032016-04-26 19:28:59 -0700745 if (nsyms * sps > nsamps)
Tom Tsoud3253432016-03-06 03:08:01 -0800746 nsyms = 156;
747
Tom Tsou7278a872017-06-14 14:50:39 -0700748 signalVector burst(nsamps, GSMPulse4->c0->size());
Tom Tsoud3253432016-03-06 03:08:01 -0800749
Tom Tsoud2b07032016-04-26 19:28:59 -0700750 /* Delay burst by 1 symbol */
Tom Tsou7278a872017-06-14 14:50:39 -0700751 burst_itr = burst.begin() + sps;
Tom Tsou06676ea2016-07-19 12:50:21 -0700752 for (size_t i = 0; i < nsyms; i++) {
Tom Tsoud3253432016-03-06 03:08:01 -0800753 float phase = i * 3.0f * M_PI / 8.0f;
754 Complex<float> rot = Complex<float>(cos(phase), sin(phase));
755
756 *burst_itr = symbols[i] * rot;
Tom Tsoud2b07032016-04-26 19:28:59 -0700757 burst_itr += sps;
Tom Tsoud3253432016-03-06 03:08:01 -0800758 }
759
760 /* Single Gaussian pulse approximation shaping */
Tom Tsou7278a872017-06-14 14:50:39 -0700761 return convolve(&burst, GSMPulse4->c0, NULL, START_ONLY);
Tom Tsoud3253432016-03-06 03:08:01 -0800762}
763
764/*
Tom Tsou8ee2f382016-03-06 20:57:34 -0800765 * Generate a random GSM normal burst.
766 */
767signalVector *genRandNormalBurst(int tsc, int sps, int tn)
768{
769 if ((tsc < 0) || (tsc > 7) || (tn < 0) || (tn > 7))
770 return NULL;
771 if ((sps != 1) && (sps != 4))
772 return NULL;
773
774 int i = 0;
Tom Tsou7278a872017-06-14 14:50:39 -0700775 BitVector bits(148);
Tom Tsou8ee2f382016-03-06 20:57:34 -0800776
777 /* Tail bits */
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700778 for (; i < 3; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700779 bits[i] = 0;
Tom Tsou8ee2f382016-03-06 20:57:34 -0800780
781 /* Random bits */
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700782 for (; i < 60; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700783 bits[i] = rand() % 2;
Tom Tsou8ee2f382016-03-06 20:57:34 -0800784
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700785 /* Stealing bit */
Tom Tsou7278a872017-06-14 14:50:39 -0700786 bits[i++] = 0;
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700787
Tom Tsou8ee2f382016-03-06 20:57:34 -0800788 /* Training sequence */
789 for (int n = 0; i < 87; i++, n++)
Tom Tsou7278a872017-06-14 14:50:39 -0700790 bits[i] = gTrainingSequence[tsc][n];
Tom Tsou8ee2f382016-03-06 20:57:34 -0800791
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700792 /* Stealing bit */
Tom Tsou7278a872017-06-14 14:50:39 -0700793 bits[i++] = 0;
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700794
Tom Tsou8ee2f382016-03-06 20:57:34 -0800795 /* Random bits */
Alexander Chemeris5e65b532017-03-24 23:24:22 -0700796 for (; i < 145; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700797 bits[i] = rand() % 2;
Tom Tsou8ee2f382016-03-06 20:57:34 -0800798
799 /* Tail bits */
800 for (; i < 148; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700801 bits[i] = 0;
Tom Tsou8ee2f382016-03-06 20:57:34 -0800802
803 int guard = 8 + !(tn % 4);
Tom Tsou7278a872017-06-14 14:50:39 -0700804 return modulateBurst(bits, guard, sps);
Tom Tsou8ee2f382016-03-06 20:57:34 -0800805}
806
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300807/*
808 * Generate a random GSM access burst.
809 */
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300810signalVector *genRandAccessBurst(int delay, int sps, int tn)
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300811{
812 if ((tn < 0) || (tn > 7))
813 return NULL;
814 if ((sps != 1) && (sps != 4))
815 return NULL;
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300816 if (delay > 68)
817 return NULL;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300818
819 int i = 0;
Tom Tsou7278a872017-06-14 14:50:39 -0700820 BitVector bits(88 + delay);
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300821
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300822 /* delay */
823 for (; i < delay; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700824 bits[i] = 0;
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300825
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300826 /* head and synch bits */
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300827 for (int n = 0; i < 49+delay; i++, n++)
Tom Tsou7278a872017-06-14 14:50:39 -0700828 bits[i] = gRACHBurst[n];
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300829
830 /* Random bits */
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300831 for (; i < 85+delay; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700832 bits[i] = rand() % 2;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300833
834 /* Tail bits */
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300835 for (; i < 88+delay; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700836 bits[i] = 0;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300837
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300838 int guard = 68-delay + !(tn % 4);
Tom Tsou7278a872017-06-14 14:50:39 -0700839 return modulateBurst(bits, guard, sps);
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300840}
841
Tom Tsou8ee2f382016-03-06 20:57:34 -0800842signalVector *generateEmptyBurst(int sps, int tn)
843{
844 if ((tn < 0) || (tn > 7))
845 return NULL;
846
847 if (sps == 4)
848 return new signalVector(625);
849 else if (sps == 1)
850 return new signalVector(148 + 8 + !(tn % 4));
851 else
852 return NULL;
853}
854
855signalVector *generateDummyBurst(int sps, int tn)
856{
857 if (((sps != 1) && (sps != 4)) || (tn < 0) || (tn > 7))
858 return NULL;
859
860 return modulateBurst(gDummyBurst, 8 + !(tn % 4), sps);
861}
862
863/*
Tom Tsoud3253432016-03-06 03:08:01 -0800864 * Generate a random 8-PSK EDGE burst. Only 4 SPS is supported with
865 * the returned burst being 625 samples in length.
866 */
867signalVector *generateEdgeBurst(int tsc)
868{
869 int tail = 9 / 3;
870 int data = 174 / 3;
871 int train = 78 / 3;
872
873 if ((tsc < 0) || (tsc > 7))
874 return NULL;
875
Tom Tsou7278a872017-06-14 14:50:39 -0700876 signalVector burst(148);
Tom Tsoud3253432016-03-06 03:08:01 -0800877 const BitVector *midamble = &gEdgeTrainingSequence[tsc];
878
879 /* Tail */
880 int n, i = 0;
881 for (; i < tail; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700882 burst[i] = psk8_table[7];
Tom Tsoud3253432016-03-06 03:08:01 -0800883
884 /* Body */
885 for (; i < tail + data; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700886 burst[i] = psk8_table[rand() % 8];
Tom Tsoud3253432016-03-06 03:08:01 -0800887
888 /* TSC */
889 for (n = 0; i < tail + data + train; i++, n++) {
890 unsigned index = (((unsigned) (*midamble)[3 * n + 0] & 0x01) << 0) |
891 (((unsigned) (*midamble)[3 * n + 1] & 0x01) << 1) |
892 (((unsigned) (*midamble)[3 * n + 2] & 0x01) << 2);
893
Tom Tsou7278a872017-06-14 14:50:39 -0700894 burst[i] = psk8_table[index];
Tom Tsoud3253432016-03-06 03:08:01 -0800895 }
896
897 /* Body */
898 for (; i < tail + data + train + data; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700899 burst[i] = psk8_table[rand() % 8];
Tom Tsoud3253432016-03-06 03:08:01 -0800900
901 /* Tail */
902 for (; i < tail + data + train + data + tail; i++)
Tom Tsou7278a872017-06-14 14:50:39 -0700903 burst[i] = psk8_table[7];
Tom Tsoud3253432016-03-06 03:08:01 -0800904
Tom Tsou7278a872017-06-14 14:50:39 -0700905 return shapeEdgeBurst(burst);
Tom Tsoud3253432016-03-06 03:08:01 -0800906}
907
908/*
909 * Modulate 8-PSK burst. When empty pulse shaping (rotation only)
910 * is enabled, the output vector length will be bit sequence length
911 * times the SPS value. When pulse shaping is enabled, the output
Alexander Chemeris9270a5a2017-03-17 13:03:41 -0700912 * vector length is fixed at 625 samples (156.25 symbols at 4 SPS).
Tom Tsoud3253432016-03-06 03:08:01 -0800913 * Pulse shaped bit sequences that go beyond one burst are truncated.
914 * Pulse shaping at anything but 4 SPS is not supported.
915 */
916signalVector *modulateEdgeBurst(const BitVector &bits,
917 int sps, bool empty)
918{
919 signalVector *shape, *burst;
920
921 if ((sps != 4) && !empty)
922 return NULL;
923
924 burst = mapEdgeSymbols(bits);
925 if (!burst)
926 return NULL;
927
928 if (empty)
929 shape = rotateEdgeBurst(*burst, sps);
930 else
931 shape = shapeEdgeBurst(*burst);
932
933 delete burst;
934 return shape;
935}
936
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800937static signalVector *modulateBurstBasic(const BitVector &bits,
938 int guard_len, int sps)
939{
940 int burst_len;
Tom Tsou7278a872017-06-14 14:50:39 -0700941 signalVector *pulse;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800942 signalVector::iterator burst_itr;
943
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400944 if (sps == 1)
945 pulse = GSMPulse1->c0;
946 else
Tom Tsou2079a3c2016-03-06 00:58:56 -0800947 pulse = GSMPulse4->c0;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400948
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800949 burst_len = sps * (bits.size() + guard_len);
950
Tom Tsou7278a872017-06-14 14:50:39 -0700951 signalVector burst(burst_len, pulse->size());
952 burst.isReal(true);
953 burst_itr = burst.begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800954
955 /* Raw bits are not differentially encoded */
956 for (unsigned i = 0; i < bits.size(); i++) {
957 *burst_itr = 2.0 * (bits[i] & 0x01) - 1.0;
958 burst_itr += sps;
959 }
960
Tom Tsou7278a872017-06-14 14:50:39 -0700961 GMSKRotate(burst, sps);
962 burst.isReal(false);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800963
964 /* Single Gaussian pulse approximation shaping */
Tom Tsou7278a872017-06-14 14:50:39 -0700965 return convolve(&burst, pulse, NULL, START_ONLY);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800966}
967
Thomas Tsou3eaae802013-08-20 19:31:14 -0400968/* Assume input bits are not differentially encoded */
Thomas Tsou83e06892013-08-20 16:10:01 -0400969signalVector *modulateBurst(const BitVector &wBurst, int guardPeriodLength,
970 int sps, bool emptyPulse)
dburgessb3a0ca42011-10-12 07:44:40 +0000971{
Thomas Tsou83e06892013-08-20 16:10:01 -0400972 if (emptyPulse)
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800973 return rotateBurst(wBurst, guardPeriodLength, sps);
974 else if (sps == 4)
Tom Tsou4dfd64a2016-03-06 20:31:51 -0800975 return modulateBurstLaurent(wBurst);
Thomas Tsou83e06892013-08-20 16:10:01 -0400976 else
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800977 return modulateBurstBasic(wBurst, guardPeriodLength, sps);
dburgessb3a0ca42011-10-12 07:44:40 +0000978}
979
Tom Tsou2079a3c2016-03-06 00:58:56 -0800980static void generateSincTable()
Thomas Tsou0e0e1f42013-11-09 22:08:51 -0500981{
Thomas Tsou0e0e1f42013-11-09 22:08:51 -0500982 for (int i = 0; i < TABLESIZE; i++) {
Tom Tsoua3dce852017-06-16 17:14:31 -0700983 auto x = (double) i / TABLESIZE * 8 * M_PI;
984 auto y = sin(x) / x;
Tom Tsou35474132017-06-19 16:00:34 -0700985 sincTable[i] = std::isnan(y) ? 1.0 : y;
Thomas Tsou0e0e1f42013-11-09 22:08:51 -0500986 }
987}
988
Tom Tsou70134a02017-06-12 14:23:53 -0700989static float sinc(float x)
dburgessb3a0ca42011-10-12 07:44:40 +0000990{
Thomas Tsou0e0e1f42013-11-09 22:08:51 -0500991 if (fabs(x) >= 8 * M_PI)
992 return 0.0;
993
994 int index = (int) floorf(fabs(x) / (8 * M_PI) * TABLESIZE);
995
996 return sincTable[index];
dburgessb3a0ca42011-10-12 07:44:40 +0000997}
998
Thomas Tsouf79c4d02013-11-09 15:51:56 -0600999/*
1000 * Create fractional delay filterbank with Blackman-harris windowed
1001 * sinc function generator. The number of filters generated is specified
1002 * by the DELAYFILTS value.
1003 */
Tom Tsou70134a02017-06-12 14:23:53 -07001004static void generateDelayFilters()
Thomas Tsouf79c4d02013-11-09 15:51:56 -06001005{
1006 int h_len = 20;
1007 complex *data;
1008 signalVector *h;
1009 signalVector::iterator itr;
1010
1011 float k, sum;
1012 float a0 = 0.35875;
1013 float a1 = 0.48829;
1014 float a2 = 0.14128;
1015 float a3 = 0.01168;
1016
1017 for (int i = 0; i < DELAYFILTS; i++) {
1018 data = (complex *) convolve_h_alloc(h_len);
1019 h = new signalVector(data, 0, h_len);
1020 h->setAligned(true);
1021 h->isReal(true);
1022
1023 sum = 0.0;
1024 itr = h->end();
1025 for (int n = 0; n < h_len; n++) {
1026 k = (float) n;
1027 *--itr = (complex) sinc(M_PI_F *
1028 (k - (float) h_len / 2.0 - (float) i / DELAYFILTS));
1029 *itr *= a0 -
1030 a1 * cos(2 * M_PI * n / (h_len - 1)) +
1031 a2 * cos(4 * M_PI * n / (h_len - 1)) -
1032 a3 * cos(6 * M_PI * n / (h_len - 1));
1033
1034 sum += itr->real();
1035 }
1036
1037 itr = h->begin();
1038 for (int n = 0; n < h_len; n++)
1039 *itr++ /= sum;
1040
1041 delayFilters[i] = h;
1042 }
1043}
1044
Alexander Chemerise0c12182017-03-18 13:27:48 -07001045signalVector *delayVector(const signalVector *in, signalVector *out, float delay)
dburgessb3a0ca42011-10-12 07:44:40 +00001046{
Thomas Tsouf79c4d02013-11-09 15:51:56 -06001047 int whole, index;
Thomas Tsou2c282f52013-10-08 21:34:35 -04001048 float frac;
Thomas Tsou94edaae2013-11-09 22:19:19 -05001049 signalVector *h, *shift, *fshift = NULL;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001050
Thomas Tsou2c282f52013-10-08 21:34:35 -04001051 whole = floor(delay);
1052 frac = delay - whole;
1053
1054 /* Sinc interpolated fractional shift (if allowable) */
1055 if (fabs(frac) > 1e-2) {
Thomas Tsouf79c4d02013-11-09 15:51:56 -06001056 index = floorf(frac * (float) DELAYFILTS);
1057 h = delayFilters[index];
Thomas Tsou2c282f52013-10-08 21:34:35 -04001058
Thomas Tsou94edaae2013-11-09 22:19:19 -05001059 fshift = convolve(in, h, NULL, NO_DELAY);
1060 if (!fshift)
1061 return NULL;
dburgessb3a0ca42011-10-12 07:44:40 +00001062 }
1063
Thomas Tsou94edaae2013-11-09 22:19:19 -05001064 if (!fshift)
1065 shift = new signalVector(*in);
1066 else
1067 shift = fshift;
1068
Thomas Tsou2c282f52013-10-08 21:34:35 -04001069 /* Integer sample shift */
1070 if (whole < 0) {
1071 whole = -whole;
Thomas Tsou94edaae2013-11-09 22:19:19 -05001072 signalVector::iterator wBurstItr = shift->begin();
1073 signalVector::iterator shiftedItr = shift->begin() + whole;
Thomas Tsou2c282f52013-10-08 21:34:35 -04001074
Thomas Tsou94edaae2013-11-09 22:19:19 -05001075 while (shiftedItr < shift->end())
dburgessb3a0ca42011-10-12 07:44:40 +00001076 *wBurstItr++ = *shiftedItr++;
Thomas Tsou2c282f52013-10-08 21:34:35 -04001077
Thomas Tsou94edaae2013-11-09 22:19:19 -05001078 while (wBurstItr < shift->end())
1079 *wBurstItr++ = 0.0;
1080 } else if (whole >= 0) {
1081 signalVector::iterator wBurstItr = shift->end() - 1;
1082 signalVector::iterator shiftedItr = shift->end() - 1 - whole;
1083
1084 while (shiftedItr >= shift->begin())
dburgessb3a0ca42011-10-12 07:44:40 +00001085 *wBurstItr-- = *shiftedItr--;
Thomas Tsou94edaae2013-11-09 22:19:19 -05001086
1087 while (wBurstItr >= shift->begin())
dburgessb3a0ca42011-10-12 07:44:40 +00001088 *wBurstItr-- = 0.0;
1089 }
Thomas Tsou2c282f52013-10-08 21:34:35 -04001090
Thomas Tsou94edaae2013-11-09 22:19:19 -05001091 if (!out)
1092 return shift;
1093
1094 out->clone(*shift);
1095 delete shift;
1096 return out;
dburgessb3a0ca42011-10-12 07:44:40 +00001097}
Thomas Tsou2c282f52013-10-08 21:34:35 -04001098
Tom Tsou70134a02017-06-12 14:23:53 -07001099static complex interpolatePoint(const signalVector &inSig, float ix)
dburgessb3a0ca42011-10-12 07:44:40 +00001100{
dburgessb3a0ca42011-10-12 07:44:40 +00001101 int start = (int) (floor(ix) - 10);
1102 if (start < 0) start = 0;
1103 int end = (int) (floor(ix) + 11);
1104 if ((unsigned) end > inSig.size()-1) end = inSig.size()-1;
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001105
dburgessb3a0ca42011-10-12 07:44:40 +00001106 complex pVal = 0.0;
Thomas Tsou20eb6d62013-11-09 14:30:41 -05001107 if (!inSig.isReal()) {
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001108 for (int i = start; i < end; i++)
dburgessb3a0ca42011-10-12 07:44:40 +00001109 pVal += inSig[i] * sinc(M_PI_F*(i-ix));
1110 }
1111 else {
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001112 for (int i = start; i < end; i++)
dburgessb3a0ca42011-10-12 07:44:40 +00001113 pVal += inSig[i].real() * sinc(M_PI_F*(i-ix));
1114 }
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001115
dburgessb3a0ca42011-10-12 07:44:40 +00001116 return pVal;
1117}
1118
Thomas Tsou8181b012013-08-20 21:17:19 -04001119static complex fastPeakDetect(const signalVector &rxBurst, float *index)
1120{
1121 float val, max = 0.0f;
1122 complex amp;
1123 int _index = -1;
1124
Thomas Tsou3f32ab52013-11-15 16:32:54 -05001125 for (size_t i = 0; i < rxBurst.size(); i++) {
Thomas Tsou8181b012013-08-20 21:17:19 -04001126 val = rxBurst[i].norm2();
1127 if (val > max) {
1128 max = val;
1129 _index = i;
1130 amp = rxBurst[i];
1131 }
1132 }
1133
1134 if (index)
1135 *index = (float) _index;
1136
1137 return amp;
1138}
1139
Tom Tsou70134a02017-06-12 14:23:53 -07001140static complex peakDetect(const signalVector &rxBurst,
1141 float *peakIndex, float *avgPwr)
dburgessb3a0ca42011-10-12 07:44:40 +00001142{
dburgessb3a0ca42011-10-12 07:44:40 +00001143 complex maxVal = 0.0;
1144 float maxIndex = -1;
1145 float sumPower = 0.0;
1146
1147 for (unsigned int i = 0; i < rxBurst.size(); i++) {
1148 float samplePower = rxBurst[i].norm2();
1149 if (samplePower > maxVal.real()) {
1150 maxVal = samplePower;
1151 maxIndex = i;
1152 }
1153 sumPower += samplePower;
1154 }
1155
1156 // interpolate around the peak
1157 // to save computation, we'll use early-late balancing
1158 float earlyIndex = maxIndex-1;
1159 float lateIndex = maxIndex+1;
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001160
dburgessb3a0ca42011-10-12 07:44:40 +00001161 float incr = 0.5;
1162 while (incr > 1.0/1024.0) {
1163 complex earlyP = interpolatePoint(rxBurst,earlyIndex);
1164 complex lateP = interpolatePoint(rxBurst,lateIndex);
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001165 if (earlyP < lateP)
dburgessb3a0ca42011-10-12 07:44:40 +00001166 earlyIndex += incr;
1167 else if (earlyP > lateP)
1168 earlyIndex -= incr;
1169 else break;
1170 incr /= 2.0;
1171 lateIndex = earlyIndex + 2.0;
1172 }
1173
1174 maxIndex = earlyIndex + 1.0;
1175 maxVal = interpolatePoint(rxBurst,maxIndex);
1176
1177 if (peakIndex!=NULL)
1178 *peakIndex = maxIndex;
1179
1180 if (avgPwr!=NULL)
1181 *avgPwr = (sumPower-maxVal.norm2()) / (rxBurst.size()-1);
1182
1183 return maxVal;
1184
1185}
1186
1187void scaleVector(signalVector &x,
1188 complex scale)
1189{
Thomas Tsou7e4e5362013-10-30 21:18:55 -04001190#ifdef HAVE_NEON
1191 int len = x.size();
1192
1193 scale_complex((float *) x.begin(),
1194 (float *) x.begin(),
1195 (float *) &scale, len);
1196#else
dburgessb3a0ca42011-10-12 07:44:40 +00001197 signalVector::iterator xP = x.begin();
1198 signalVector::iterator xPEnd = x.end();
Thomas Tsou20eb6d62013-11-09 14:30:41 -05001199 if (!x.isReal()) {
dburgessb3a0ca42011-10-12 07:44:40 +00001200 while (xP < xPEnd) {
1201 *xP = *xP * scale;
1202 xP++;
1203 }
1204 }
1205 else {
1206 while (xP < xPEnd) {
1207 *xP = xP->real() * scale;
1208 xP++;
1209 }
1210 }
Thomas Tsou7e4e5362013-10-30 21:18:55 -04001211#endif
dburgessb3a0ca42011-10-12 07:44:40 +00001212}
1213
1214/** in-place conjugation */
Tom Tsou70134a02017-06-12 14:23:53 -07001215static void conjugateVector(signalVector &x)
dburgessb3a0ca42011-10-12 07:44:40 +00001216{
Thomas Tsou20eb6d62013-11-09 14:30:41 -05001217 if (x.isReal()) return;
dburgessb3a0ca42011-10-12 07:44:40 +00001218 signalVector::iterator xP = x.begin();
1219 signalVector::iterator xPEnd = x.end();
1220 while (xP < xPEnd) {
1221 *xP = xP->conj();
1222 xP++;
1223 }
1224}
1225
Tom Tsou2079a3c2016-03-06 00:58:56 -08001226static bool generateMidamble(int sps, int tsc)
dburgessb3a0ca42011-10-12 07:44:40 +00001227{
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001228 bool status = true;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001229 float toa;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001230 complex *data = NULL;
1231 signalVector *autocorr = NULL, *midamble = NULL;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001232 signalVector *midMidamble = NULL, *_midMidamble = NULL;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001233
Thomas Tsou3eaae802013-08-20 19:31:14 -04001234 if ((tsc < 0) || (tsc > 7))
dburgessb3a0ca42011-10-12 07:44:40 +00001235 return false;
1236
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001237 delete gMidambles[tsc];
Thomas Tsou3eaae802013-08-20 19:31:14 -04001238
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001239 /* Use middle 16 bits of each TSC. Correlation sequence is not pulse shaped */
1240 midMidamble = modulateBurst(gTrainingSequence[tsc].segment(5,16), 0, sps, true);
1241 if (!midMidamble)
1242 return false;
1243
Thomas Tsou3eaae802013-08-20 19:31:14 -04001244 /* Simulated receive sequence is pulse shaped */
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001245 midamble = modulateBurst(gTrainingSequence[tsc], 0, sps, false);
1246 if (!midamble) {
1247 status = false;
1248 goto release;
dburgessb3a0ca42011-10-12 07:44:40 +00001249 }
Thomas Tsou3eaae802013-08-20 19:31:14 -04001250
dburgessb3a0ca42011-10-12 07:44:40 +00001251 // NOTE: Because ideal TSC 16-bit midamble is 66 symbols into burst,
1252 // the ideal TSC has an + 180 degree phase shift,
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001253 // due to the pi/2 frequency shift, that
dburgessb3a0ca42011-10-12 07:44:40 +00001254 // needs to be accounted for.
1255 // 26-midamble is 61 symbols into burst, has +90 degree phase shift.
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001256 scaleVector(*midMidamble, complex(-1.0, 0.0));
1257 scaleVector(*midamble, complex(0.0, 1.0));
dburgessb3a0ca42011-10-12 07:44:40 +00001258
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001259 conjugateVector(*midMidamble);
dburgessb3a0ca42011-10-12 07:44:40 +00001260
Thomas Tsou3eaae802013-08-20 19:31:14 -04001261 /* For SSE alignment, reallocate the midamble sequence on 16-byte boundary */
1262 data = (complex *) convolve_h_alloc(midMidamble->size());
1263 _midMidamble = new signalVector(data, 0, midMidamble->size());
1264 _midMidamble->setAligned(true);
Pau Espin Pedrol5e68cde2018-08-30 20:45:14 +02001265 midMidamble->copyTo(*_midMidamble);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001266
1267 autocorr = convolve(midamble, _midMidamble, NULL, NO_DELAY);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001268 if (!autocorr) {
1269 status = false;
1270 goto release;
1271 }
dburgessb3a0ca42011-10-12 07:44:40 +00001272
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001273 gMidambles[tsc] = new CorrelationSequence;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001274 gMidambles[tsc]->buffer = data;
1275 gMidambles[tsc]->sequence = _midMidamble;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001276 gMidambles[tsc]->gain = peakDetect(*autocorr, &toa, NULL);
1277
1278 /* For 1 sps only
1279 * (Half of correlation length - 1) + midpoint of pulse shape + remainder
1280 * 13.5 = (16 / 2 - 1) + 1.5 + (26 - 10) / 2
1281 */
1282 if (sps == 1)
1283 gMidambles[tsc]->toa = toa - 13.5;
1284 else
1285 gMidambles[tsc]->toa = 0;
dburgessb3a0ca42011-10-12 07:44:40 +00001286
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001287release:
dburgessb3a0ca42011-10-12 07:44:40 +00001288 delete autocorr;
1289 delete midamble;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001290 delete midMidamble;
dburgessb3a0ca42011-10-12 07:44:40 +00001291
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001292 if (!status) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001293 delete _midMidamble;
1294 free(data);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001295 gMidambles[tsc] = NULL;
1296 }
1297
1298 return status;
dburgessb3a0ca42011-10-12 07:44:40 +00001299}
1300
Tom Tsou70134a02017-06-12 14:23:53 -07001301static CorrelationSequence *generateEdgeMidamble(int tsc)
Tom Tsoud3253432016-03-06 03:08:01 -08001302{
1303 complex *data = NULL;
1304 signalVector *midamble = NULL, *_midamble = NULL;
1305 CorrelationSequence *seq;
1306
1307 if ((tsc < 0) || (tsc > 7))
1308 return NULL;
1309
1310 /* Use middle 48 bits of each TSC. Correlation sequence is not pulse shaped */
1311 const BitVector *bits = &gEdgeTrainingSequence[tsc];
1312 midamble = modulateEdgeBurst(bits->segment(15, 48), 1, true);
1313 if (!midamble)
1314 return NULL;
1315
1316 conjugateVector(*midamble);
1317
1318 data = (complex *) convolve_h_alloc(midamble->size());
1319 _midamble = new signalVector(data, 0, midamble->size());
1320 _midamble->setAligned(true);
Pau Espin Pedrol5e68cde2018-08-30 20:45:14 +02001321 midamble->copyTo(*_midamble);
Tom Tsoud3253432016-03-06 03:08:01 -08001322
1323 /* Channel gain is an empirically measured value */
1324 seq = new CorrelationSequence;
1325 seq->buffer = data;
1326 seq->sequence = _midamble;
1327 seq->gain = Complex<float>(-19.6432, 19.5006) / 1.18;
1328 seq->toa = 0;
1329
1330 delete midamble;
1331
1332 return seq;
1333}
1334
Tom Tsou2079a3c2016-03-06 00:58:56 -08001335static bool generateRACHSequence(int sps)
dburgessb3a0ca42011-10-12 07:44:40 +00001336{
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001337 bool status = true;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001338 float toa;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001339 complex *data = NULL;
1340 signalVector *autocorr = NULL;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001341 signalVector *seq0 = NULL, *seq1 = NULL, *_seq1 = NULL;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001342
1343 delete gRACHSequence;
1344
1345 seq0 = modulateBurst(gRACHSynchSequence, 0, sps, false);
1346 if (!seq0)
1347 return false;
1348
1349 seq1 = modulateBurst(gRACHSynchSequence.segment(0, 40), 0, sps, true);
1350 if (!seq1) {
1351 status = false;
1352 goto release;
dburgessb3a0ca42011-10-12 07:44:40 +00001353 }
1354
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001355 conjugateVector(*seq1);
dburgessb3a0ca42011-10-12 07:44:40 +00001356
Thomas Tsou3eaae802013-08-20 19:31:14 -04001357 /* For SSE alignment, reallocate the midamble sequence on 16-byte boundary */
1358 data = (complex *) convolve_h_alloc(seq1->size());
1359 _seq1 = new signalVector(data, 0, seq1->size());
1360 _seq1->setAligned(true);
Pau Espin Pedrol5e68cde2018-08-30 20:45:14 +02001361 seq1->copyTo(*_seq1);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001362
1363 autocorr = convolve(seq0, _seq1, autocorr, NO_DELAY);
1364 if (!autocorr) {
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001365 status = false;
1366 goto release;
1367 }
dburgessb3a0ca42011-10-12 07:44:40 +00001368
1369 gRACHSequence = new CorrelationSequence;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001370 gRACHSequence->sequence = _seq1;
1371 gRACHSequence->buffer = data;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001372 gRACHSequence->gain = peakDetect(*autocorr, &toa, NULL);
1373
1374 /* For 1 sps only
1375 * (Half of correlation length - 1) + midpoint of pulse shaping filer
1376 * 20.5 = (40 / 2 - 1) + 1.5
1377 */
1378 if (sps == 1)
1379 gRACHSequence->toa = toa - 20.5;
1380 else
1381 gRACHSequence->toa = 0.0;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001382
1383release:
dburgessb3a0ca42011-10-12 07:44:40 +00001384 delete autocorr;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001385 delete seq0;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001386 delete seq1;
dburgessb3a0ca42011-10-12 07:44:40 +00001387
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001388 if (!status) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001389 delete _seq1;
1390 free(data);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001391 gRACHSequence = NULL;
1392 }
dburgessb3a0ca42011-10-12 07:44:40 +00001393
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001394 return status;
dburgessb3a0ca42011-10-12 07:44:40 +00001395}
Thomas Tsou3eaae802013-08-20 19:31:14 -04001396
Tom Tsoua84e1622016-06-29 14:50:25 -07001397/*
1398 * Peak-to-average computation +/- range from peak in symbols
1399 */
1400#define COMPUTE_PEAK_MIN 2
1401#define COMPUTE_PEAK_MAX 5
1402
1403/*
1404 * Minimum number of values needed to compute peak-to-average
1405 */
1406#define COMPUTE_PEAK_CNT 5
1407
Thomas Tsou865bca42013-08-21 20:58:00 -04001408static float computePeakRatio(signalVector *corr,
1409 int sps, float toa, complex amp)
dburgessb3a0ca42011-10-12 07:44:40 +00001410{
Thomas Tsou865bca42013-08-21 20:58:00 -04001411 int num = 0;
1412 complex *peak;
1413 float rms, avg = 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001414
Thomas Tsou865bca42013-08-21 20:58:00 -04001415 /* Check for bogus results */
1416 if ((toa < 0.0) || (toa > corr->size()))
1417 return 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001418
Alexander Chemeris1e9b4d52015-06-04 19:05:28 -04001419 peak = corr->begin() + (int) rint(toa);
1420
Tom Tsoua84e1622016-06-29 14:50:25 -07001421 for (int i = COMPUTE_PEAK_MIN * sps; i <= COMPUTE_PEAK_MAX * sps; i++) {
Thomas Tsou865bca42013-08-21 20:58:00 -04001422 if (peak - i >= corr->begin()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001423 avg += (peak - i)->norm2();
1424 num++;
1425 }
Thomas Tsou865bca42013-08-21 20:58:00 -04001426 if (peak + i < corr->end()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001427 avg += (peak + i)->norm2();
1428 num++;
1429 }
dburgessb3a0ca42011-10-12 07:44:40 +00001430 }
1431
Tom Tsoua84e1622016-06-29 14:50:25 -07001432 if (num < COMPUTE_PEAK_CNT)
Thomas Tsou865bca42013-08-21 20:58:00 -04001433 return 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001434
Thomas Tsou3eaae802013-08-20 19:31:14 -04001435 rms = sqrtf(avg / (float) num) + 0.00001;
dburgessb3a0ca42011-10-12 07:44:40 +00001436
Thomas Tsou865bca42013-08-21 20:58:00 -04001437 return (amp.abs()) / rms;
dburgessb3a0ca42011-10-12 07:44:40 +00001438}
1439
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001440float energyDetect(const signalVector &rxBurst, unsigned windowLength)
dburgessb3a0ca42011-10-12 07:44:40 +00001441{
1442
1443 signalVector::const_iterator windowItr = rxBurst.begin(); //+rxBurst.size()/2 - 5*windowLength/2;
1444 float energy = 0.0;
Tom Tsou2af14402017-03-23 14:54:00 -07001445 if (windowLength == 0) return 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001446 if (windowLength > rxBurst.size()) windowLength = rxBurst.size();
1447 for (unsigned i = 0; i < windowLength; i++) {
1448 energy += windowItr->norm2();
1449 windowItr+=4;
1450 }
Alexander Chemeris1dd05cf2017-03-15 23:23:36 +03001451 return energy/windowLength;
dburgessb3a0ca42011-10-12 07:44:40 +00001452}
dburgessb3a0ca42011-10-12 07:44:40 +00001453
Tom Tsou70134a02017-06-12 14:23:53 -07001454static signalVector *downsampleBurst(const signalVector &burst)
1455{
Tom Tsou7278a872017-06-14 14:50:39 -07001456 signalVector in(DOWNSAMPLE_IN_LEN, dnsampler->len());
1457 signalVector *out = new signalVector(DOWNSAMPLE_OUT_LEN);
Pau Espin Pedrol5e68cde2018-08-30 20:45:14 +02001458 burst.copyToSegment(in, 0, DOWNSAMPLE_IN_LEN);
Tom Tsou70134a02017-06-12 14:23:53 -07001459
Tom Tsou7278a872017-06-14 14:50:39 -07001460 if (dnsampler->rotate((float *) in.begin(), DOWNSAMPLE_IN_LEN,
Tom Tsou70134a02017-06-12 14:23:53 -07001461 (float *) out->begin(), DOWNSAMPLE_OUT_LEN) < 0) {
1462 delete out;
1463 out = NULL;
1464 }
1465
Tom Tsou70134a02017-06-12 14:23:53 -07001466 return out;
1467};
1468
Thomas Tsou865bca42013-08-21 20:58:00 -04001469/*
1470 * Detect a burst based on correlation and peak-to-average ratio
1471 *
1472 * For one sampler-per-symbol, perform fast peak detection (no interpolation)
1473 * for initial gating. We do this because energy detection should be disabled.
1474 * For higher oversampling values, we assume the energy detector is in place
1475 * and we run full interpolating peak detection.
1476 */
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001477static int detectBurst(const signalVector &burst,
Thomas Tsou865bca42013-08-21 20:58:00 -04001478 signalVector &corr, CorrelationSequence *sync,
1479 float thresh, int sps, complex *amp, float *toa,
1480 int start, int len)
dburgessb3a0ca42011-10-12 07:44:40 +00001481{
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001482 const signalVector *corr_in;
1483 signalVector *dec = NULL;
Tom Tsoud3253432016-03-06 03:08:01 -08001484
1485 if (sps == 4) {
1486 dec = downsampleBurst(burst);
1487 corr_in = dec;
1488 sps = 1;
1489 } else {
1490 corr_in = &burst;
1491 }
1492
Thomas Tsou865bca42013-08-21 20:58:00 -04001493 /* Correlate */
Tom Tsoud3253432016-03-06 03:08:01 -08001494 if (!convolve(corr_in, sync->sequence, &corr,
1495 CUSTOM, start, len, 1, 0)) {
1496 delete dec;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001497 return -1;
dburgessb3a0ca42011-10-12 07:44:40 +00001498 }
1499
Tom Tsoud3253432016-03-06 03:08:01 -08001500 delete dec;
1501
1502 /* Running at the downsampled rate at this point */
1503 sps = 1;
1504
Thomas Tsou865bca42013-08-21 20:58:00 -04001505 /* Peak detection - place restrictions at correlation edges */
1506 *amp = fastPeakDetect(corr, toa);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001507
Thomas Tsou865bca42013-08-21 20:58:00 -04001508 if ((*toa < 3 * sps) || (*toa > len - 3 * sps))
1509 return 0;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001510
Thomas Tsou865bca42013-08-21 20:58:00 -04001511 /* Peak -to-average ratio */
1512 if (computePeakRatio(&corr, sps, *toa, *amp) < thresh)
1513 return 0;
1514
1515 /* Compute peak-to-average ratio. Reject if we don't have enough values */
1516 *amp = peakDetect(corr, toa, NULL);
1517
1518 /* Normalize our channel gain */
1519 *amp = *amp / sync->gain;
1520
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001521 /* Compensate for residuate time lag */
1522 *toa = *toa - sync->toa;
1523
Thomas Tsou865bca42013-08-21 20:58:00 -04001524 return 1;
1525}
1526
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001527static float maxAmplitude(const signalVector &burst)
Tom Tsou577cd022015-05-18 13:57:54 -07001528{
Alexander Chemeris954b1182015-06-04 15:39:41 -04001529 float max = 0.0;
1530 for (size_t i = 0; i < burst.size(); i++) {
1531 if (fabs(burst[i].real()) > max)
1532 max = fabs(burst[i].real());
1533 if (fabs(burst[i].imag()) > max)
1534 max = fabs(burst[i].imag());
1535 }
Tom Tsou577cd022015-05-18 13:57:54 -07001536
Alexander Chemeris954b1182015-06-04 15:39:41 -04001537 return max;
Tom Tsou577cd022015-05-18 13:57:54 -07001538}
1539
Alexander Chemeris130a8002015-06-09 20:52:11 -04001540/*
1541 * RACH/Normal burst detection with clipping detection
Thomas Tsou865bca42013-08-21 20:58:00 -04001542 *
1543 * Correlation window parameters:
Alexander Chemeris130a8002015-06-09 20:52:11 -04001544 * target: Tail bits + burst length
1545 * head: Search symbols before target
1546 * tail: Search symbols after target
Thomas Tsou865bca42013-08-21 20:58:00 -04001547 */
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001548static int detectGeneralBurst(const signalVector &rxBurst,
1549 float thresh,
1550 int sps,
1551 complex &amp,
1552 float &toa,
1553 int target, int head, int tail,
1554 CorrelationSequence *sync)
Thomas Tsou865bca42013-08-21 20:58:00 -04001555{
Alexander Chemeris130a8002015-06-09 20:52:11 -04001556 int rc, start, len;
Alexander Chemeris954b1182015-06-04 15:39:41 -04001557 bool clipping = false;
Thomas Tsou865bca42013-08-21 20:58:00 -04001558
1559 if ((sps != 1) && (sps != 4))
Tom Tsou577cd022015-05-18 13:57:54 -07001560 return -SIGERR_UNSUPPORTED;
1561
Alexander Chemeris954b1182015-06-04 15:39:41 -04001562 // Detect potential clipping
1563 // We still may be able to demod the burst, so we'll give it a try
1564 // and only report clipping if we can't demod.
1565 float maxAmpl = maxAmplitude(rxBurst);
1566 if (maxAmpl > CLIP_THRESH) {
1567 LOG(DEBUG) << "max burst amplitude: " << maxAmpl << " is above the clipping threshold: " << CLIP_THRESH << std::endl;
1568 clipping = true;
1569 }
Thomas Tsou865bca42013-08-21 20:58:00 -04001570
Tom Tsoud3253432016-03-06 03:08:01 -08001571 start = target - head - 1;
1572 len = head + tail;
Tom Tsou7278a872017-06-14 14:50:39 -07001573 signalVector corr(len);
Thomas Tsou865bca42013-08-21 20:58:00 -04001574
Tom Tsou7278a872017-06-14 14:50:39 -07001575 rc = detectBurst(rxBurst, corr, sync,
Alexander Chemeris130a8002015-06-09 20:52:11 -04001576 thresh, sps, &amp, &toa, start, len);
Thomas Tsou865bca42013-08-21 20:58:00 -04001577 if (rc < 0) {
Tom Tsou577cd022015-05-18 13:57:54 -07001578 return -SIGERR_INTERNAL;
Thomas Tsou865bca42013-08-21 20:58:00 -04001579 } else if (!rc) {
Alexander Chemeris130a8002015-06-09 20:52:11 -04001580 amp = 0.0f;
1581 toa = 0.0f;
Alexander Chemeris954b1182015-06-04 15:39:41 -04001582 return clipping?-SIGERR_CLIP:SIGERR_NONE;
dburgessb3a0ca42011-10-12 07:44:40 +00001583 }
1584
Thomas Tsou865bca42013-08-21 20:58:00 -04001585 /* Subtract forward search bits from delay */
Tom Tsoud3253432016-03-06 03:08:01 -08001586 toa -= head;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001587
Thomas Tsou865bca42013-08-21 20:58:00 -04001588 return 1;
1589}
Thomas Tsou3eaae802013-08-20 19:31:14 -04001590
Alexander Chemeris130a8002015-06-09 20:52:11 -04001591
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001592/*
Alexander Chemeris130a8002015-06-09 20:52:11 -04001593 * RACH burst detection
1594 *
1595 * Correlation window parameters:
1596 * target: Tail bits + RACH length (reduced from 41 to a multiple of 4)
Tom Tsoue90c24c2016-06-21 16:14:39 -07001597 * head: Search 8 symbols before target
1598 * tail: Search 8 symbols + maximum expected delay
Alexander Chemeris130a8002015-06-09 20:52:11 -04001599 */
Tom Tsou70134a02017-06-12 14:23:53 -07001600static int detectRACHBurst(const signalVector &burst, float threshold, int sps,
1601 complex &amplitude, float &toa, unsigned max_toa)
Alexander Chemeris130a8002015-06-09 20:52:11 -04001602{
1603 int rc, target, head, tail;
1604 CorrelationSequence *sync;
1605
1606 target = 8 + 40;
Tom Tsoue90c24c2016-06-21 16:14:39 -07001607 head = 8;
Alexander Chemeris14d13b62017-03-17 15:12:17 -07001608 tail = 8 + max_toa;
Alexander Chemeris130a8002015-06-09 20:52:11 -04001609 sync = gRACHSequence;
1610
Alexander Chemeris14d13b62017-03-17 15:12:17 -07001611 rc = detectGeneralBurst(burst, threshold, sps, amplitude, toa,
Alexander Chemeris130a8002015-06-09 20:52:11 -04001612 target, head, tail, sync);
1613
1614 return rc;
1615}
1616
Pau Espin Pedrol21ce05c2018-08-30 20:47:20 +02001617/*
Thomas Tsou865bca42013-08-21 20:58:00 -04001618 * Normal burst detection
1619 *
1620 * Correlation window parameters:
1621 * target: Tail + data + mid-midamble + 1/2 remaining midamblebits
Tom Tsoue90c24c2016-06-21 16:14:39 -07001622 * head: Search 6 symbols before target
1623 * tail: Search 6 symbols + maximum expected delay
Thomas Tsou865bca42013-08-21 20:58:00 -04001624 */
Tom Tsou70134a02017-06-12 14:23:53 -07001625static int analyzeTrafficBurst(const signalVector &burst, unsigned tsc, float threshold,
1626 int sps, complex &amplitude, float &toa, unsigned max_toa)
Thomas Tsou865bca42013-08-21 20:58:00 -04001627{
Alexander Chemeris130a8002015-06-09 20:52:11 -04001628 int rc, target, head, tail;
Thomas Tsou865bca42013-08-21 20:58:00 -04001629 CorrelationSequence *sync;
1630
Tom Tsouae91f132017-03-28 14:40:38 -07001631 if (tsc > 7)
Tom Tsou577cd022015-05-18 13:57:54 -07001632 return -SIGERR_UNSUPPORTED;
1633
Thomas Tsou865bca42013-08-21 20:58:00 -04001634 target = 3 + 58 + 16 + 5;
Tom Tsoue90c24c2016-06-21 16:14:39 -07001635 head = 6;
1636 tail = 6 + max_toa;
Thomas Tsou865bca42013-08-21 20:58:00 -04001637 sync = gMidambles[tsc];
Thomas Tsou865bca42013-08-21 20:58:00 -04001638
Alexander Chemeris14d13b62017-03-17 15:12:17 -07001639 rc = detectGeneralBurst(burst, threshold, sps, amplitude, toa,
Alexander Chemeris130a8002015-06-09 20:52:11 -04001640 target, head, tail, sync);
Alexander Chemeris130a8002015-06-09 20:52:11 -04001641 return rc;
dburgessb3a0ca42011-10-12 07:44:40 +00001642}
1643
Tom Tsou70134a02017-06-12 14:23:53 -07001644static int detectEdgeBurst(const signalVector &burst, unsigned tsc, float threshold,
1645 int sps, complex &amplitude, float &toa, unsigned max_toa)
Tom Tsoud3253432016-03-06 03:08:01 -08001646{
1647 int rc, target, head, tail;
1648 CorrelationSequence *sync;
1649
Tom Tsouae91f132017-03-28 14:40:38 -07001650 if (tsc > 7)
Tom Tsoud3253432016-03-06 03:08:01 -08001651 return -SIGERR_UNSUPPORTED;
1652
1653 target = 3 + 58 + 16 + 5;
Tom Tsoue90c24c2016-06-21 16:14:39 -07001654 head = 6;
1655 tail = 6 + max_toa;
Tom Tsoud3253432016-03-06 03:08:01 -08001656 sync = gEdgeMidambles[tsc];
1657
Alexander Chemeris14d13b62017-03-17 15:12:17 -07001658 rc = detectGeneralBurst(burst, threshold, sps, amplitude, toa,
Tom Tsoud3253432016-03-06 03:08:01 -08001659 target, head, tail, sync);
1660 return rc;
1661}
1662
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001663int detectAnyBurst(const signalVector &burst, unsigned tsc, float threshold,
Alexander Chemeris4e6c9382017-03-17 15:24:18 -07001664 int sps, CorrType type, complex &amp, float &toa,
1665 unsigned max_toa)
1666{
1667 int rc = 0;
1668
1669 switch (type) {
1670 case EDGE:
1671 rc = detectEdgeBurst(burst, tsc, threshold, sps,
1672 amp, toa, max_toa);
1673 if (rc > 0)
1674 break;
1675 else
1676 type = TSC;
1677 case TSC:
1678 rc = analyzeTrafficBurst(burst, tsc, threshold, sps,
1679 amp, toa, max_toa);
1680 break;
1681 case RACH:
1682 rc = detectRACHBurst(burst, threshold, sps, amp, toa,
1683 max_toa);
1684 break;
1685 default:
1686 LOG(ERR) << "Invalid correlation type";
1687 }
1688
1689 if (rc > 0)
1690 return type;
1691
1692 return rc;
1693}
1694
Tom Tsoud3253432016-03-06 03:08:01 -08001695/*
1696 * Soft 8-PSK decoding using Manhattan distance metric
1697 */
1698static SoftVector *softSliceEdgeBurst(signalVector &burst)
1699{
1700 size_t nsyms = 148;
1701
1702 if (burst.size() < nsyms)
1703 return NULL;
1704
1705 signalVector::iterator itr;
1706 SoftVector *bits = new SoftVector(nsyms * 3);
1707
1708 /*
1709 * Bits 0 and 1 - First and second bits of the symbol respectively
1710 */
1711 rotateBurst2(burst, -M_PI / 8.0);
1712 itr = burst.begin();
1713 for (size_t i = 0; i < nsyms; i++) {
1714 (*bits)[3 * i + 0] = -itr->imag();
1715 (*bits)[3 * i + 1] = itr->real();
1716 itr++;
1717 }
1718
1719 /*
1720 * Bit 2 - Collapse symbols into quadrant 0 (positive X and Y).
1721 * Decision area is then simplified to X=Y axis. Rotate again to
1722 * place decision boundary on X-axis.
1723 */
1724 itr = burst.begin();
1725 for (size_t i = 0; i < burst.size(); i++) {
1726 burst[i] = Complex<float>(fabs(itr->real()), fabs(itr->imag()));
1727 itr++;
1728 }
1729
1730 rotateBurst2(burst, -M_PI / 4.0);
1731 itr = burst.begin();
1732 for (size_t i = 0; i < nsyms; i++) {
1733 (*bits)[3 * i + 2] = -itr->imag();
1734 itr++;
1735 }
1736
1737 signalVector soft(bits->size());
1738 for (size_t i = 0; i < bits->size(); i++)
1739 soft[i] = (*bits)[i];
1740
1741 return bits;
1742}
1743
1744/*
Alexander Chemeris132fb242017-03-17 17:22:33 -07001745 * Convert signalVector to SoftVector by taking real part of the signal.
1746 */
1747static SoftVector *signalToSoftVector(signalVector *dec)
1748{
1749 SoftVector *bits = new SoftVector(dec->size());
1750
1751 SoftVector::iterator bit_itr = bits->begin();
1752 signalVector::iterator burst_itr = dec->begin();
1753
1754 for (; burst_itr < dec->end(); burst_itr++)
1755 *bit_itr++ = burst_itr->real();
1756
1757 return bits;
1758}
1759
1760/*
Tom Tsou7fec3032016-03-06 22:33:20 -08001761 * Shared portion of GMSK and EDGE demodulators consisting of timing
1762 * recovery and single tap channel correction. For 4 SPS (if activated),
1763 * the output is downsampled prior to the 1 SPS modulation specific
1764 * stages.
1765 */
Alexander Chemerise0c12182017-03-18 13:27:48 -07001766static signalVector *demodCommon(const signalVector &burst, int sps,
Tom Tsou7fec3032016-03-06 22:33:20 -08001767 complex chan, float toa)
1768{
1769 signalVector *delay, *dec;
1770
1771 if ((sps != 1) && (sps != 4))
1772 return NULL;
1773
Tom Tsou7fec3032016-03-06 22:33:20 -08001774 delay = delayVector(&burst, NULL, -toa * (float) sps);
Alexander Chemerise0c12182017-03-18 13:27:48 -07001775 scaleVector(*delay, (complex) 1.0 / chan);
Tom Tsou7fec3032016-03-06 22:33:20 -08001776
1777 if (sps == 1)
1778 return delay;
1779
1780 dec = downsampleBurst(*delay);
1781
1782 delete delay;
1783 return dec;
1784}
1785
1786/*
Tom Tsoud3253432016-03-06 03:08:01 -08001787 * Demodulate GSMK burst. Prior to symbol rotation, operate at
1788 * 4 SPS (if activated) to minimize distortion through the fractional
1789 * delay filters. Symbol rotation and after always operates at 1 SPS.
1790 */
Tom Tsou70134a02017-06-12 14:23:53 -07001791static SoftVector *demodGmskBurst(const signalVector &rxBurst,
1792 int sps, complex channel, float TOA)
dburgessb3a0ca42011-10-12 07:44:40 +00001793{
Thomas Tsou94edaae2013-11-09 22:19:19 -05001794 SoftVector *bits;
Tom Tsou7fec3032016-03-06 22:33:20 -08001795 signalVector *dec;
dburgessb3a0ca42011-10-12 07:44:40 +00001796
Tom Tsou7fec3032016-03-06 22:33:20 -08001797 dec = demodCommon(rxBurst, sps, channel, TOA);
1798 if (!dec)
1799 return NULL;
dburgessb3a0ca42011-10-12 07:44:40 +00001800
Tom Tsoud3253432016-03-06 03:08:01 -08001801 /* Shift up by a quarter of a frequency */
1802 GMSKReverseRotate(*dec, 1);
Alexander Chemeris132fb242017-03-17 17:22:33 -07001803 /* Take real part of the signal */
1804 bits = signalToSoftVector(dec);
Thomas Tsou94edaae2013-11-09 22:19:19 -05001805 delete dec;
dburgessb3a0ca42011-10-12 07:44:40 +00001806
Thomas Tsou94edaae2013-11-09 22:19:19 -05001807 return bits;
dburgessb3a0ca42011-10-12 07:44:40 +00001808}
Thomas Tsou94edaae2013-11-09 22:19:19 -05001809
Tom Tsoud3253432016-03-06 03:08:01 -08001810/*
1811 * Demodulate an 8-PSK burst. Prior to symbol rotation, operate at
1812 * 4 SPS (if activated) to minimize distortion through the fractional
1813 * delay filters. Symbol rotation and after always operates at 1 SPS.
1814 *
1815 * Allow 1 SPS demodulation here, but note that other parts of the
1816 * transceiver restrict EDGE operatoin to 4 SPS - 8-PSK distortion
1817 * through the fractional delay filters at 1 SPS renders signal
1818 * nearly unrecoverable.
1819 */
Tom Tsou70134a02017-06-12 14:23:53 -07001820static SoftVector *demodEdgeBurst(const signalVector &burst,
1821 int sps, complex chan, float toa)
Tom Tsoud3253432016-03-06 03:08:01 -08001822{
1823 SoftVector *bits;
Tom Tsou7fec3032016-03-06 22:33:20 -08001824 signalVector *dec, *rot, *eq;
Tom Tsoud3253432016-03-06 03:08:01 -08001825
Tom Tsou7fec3032016-03-06 22:33:20 -08001826 dec = demodCommon(burst, sps, chan, toa);
1827 if (!dec)
Tom Tsoud3253432016-03-06 03:08:01 -08001828 return NULL;
1829
Tom Tsou7fec3032016-03-06 22:33:20 -08001830 /* Equalize and derotate */
Tom Tsoud3253432016-03-06 03:08:01 -08001831 eq = convolve(dec, GSMPulse4->c0_inv, NULL, NO_DELAY);
1832 rot = derotateEdgeBurst(*eq, 1);
1833
Tom Tsou7fec3032016-03-06 22:33:20 -08001834 /* Soft slice and normalize */
Tom Tsou04795622016-04-26 21:17:36 -07001835 bits = softSliceEdgeBurst(*rot);
Tom Tsoud3253432016-03-06 03:08:01 -08001836
1837 delete dec;
1838 delete eq;
1839 delete rot;
1840
1841 return bits;
1842}
1843
Alexander Chemerise0c12182017-03-18 13:27:48 -07001844SoftVector *demodAnyBurst(const signalVector &burst, int sps, complex amp,
Alexander Chemeris6e1dffd2017-03-17 16:13:51 -07001845 float toa, CorrType type)
1846{
1847 if (type == EDGE)
1848 return demodEdgeBurst(burst, sps, amp, toa);
1849 else
1850 return demodGmskBurst(burst, sps, amp, toa);
1851}
1852
Tom Tsou2079a3c2016-03-06 00:58:56 -08001853bool sigProcLibSetup()
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001854{
Thomas Tsou0e0e1f42013-11-09 22:08:51 -05001855 generateSincTable();
Tom Tsou2079a3c2016-03-06 00:58:56 -08001856 initGMSKRotationTables();
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001857
Tom Tsou2079a3c2016-03-06 00:58:56 -08001858 GSMPulse1 = generateGSMPulse(1);
1859 GSMPulse4 = generateGSMPulse(4);
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001860
Tom Tsou2079a3c2016-03-06 00:58:56 -08001861 generateRACHSequence(1);
Tom Tsoud3253432016-03-06 03:08:01 -08001862 for (int tsc = 0; tsc < 8; tsc++) {
Tom Tsou2079a3c2016-03-06 00:58:56 -08001863 generateMidamble(1, tsc);
Tom Tsoud3253432016-03-06 03:08:01 -08001864 gEdgeMidambles[tsc] = generateEdgeMidamble(tsc);
1865 }
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001866
Thomas Tsouf79c4d02013-11-09 15:51:56 -06001867 generateDelayFilters();
1868
Tom Tsoud3253432016-03-06 03:08:01 -08001869 dnsampler = new Resampler(1, 4);
1870 if (!dnsampler->init()) {
1871 LOG(ALERT) << "Rx resampler failed to initialize";
1872 goto fail;
1873 }
1874
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001875 return true;
Tom Tsoud3253432016-03-06 03:08:01 -08001876
1877fail:
1878 sigProcLibDestroy();
1879 return false;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001880}