blob: c776501a966d6e514cf658fd4c0b94305f236289 [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{
982 float x;
983
984 for (int i = 0; i < TABLESIZE; i++) {
985 x = (float) i / TABLESIZE * 8 * M_PI;
986 if (fabs(x) < 0.01) {
987 sincTable[i] = 1.0f;
988 continue;
989 }
990
991 sincTable[i] = sinf(x) / x;
992 }
993}
994
Tom Tsou70134a02017-06-12 14:23:53 -0700995static float sinc(float x)
dburgessb3a0ca42011-10-12 07:44:40 +0000996{
Thomas Tsou0e0e1f42013-11-09 22:08:51 -0500997 if (fabs(x) >= 8 * M_PI)
998 return 0.0;
999
1000 int index = (int) floorf(fabs(x) / (8 * M_PI) * TABLESIZE);
1001
1002 return sincTable[index];
dburgessb3a0ca42011-10-12 07:44:40 +00001003}
1004
Thomas Tsouf79c4d02013-11-09 15:51:56 -06001005/*
1006 * Create fractional delay filterbank with Blackman-harris windowed
1007 * sinc function generator. The number of filters generated is specified
1008 * by the DELAYFILTS value.
1009 */
Tom Tsou70134a02017-06-12 14:23:53 -07001010static void generateDelayFilters()
Thomas Tsouf79c4d02013-11-09 15:51:56 -06001011{
1012 int h_len = 20;
1013 complex *data;
1014 signalVector *h;
1015 signalVector::iterator itr;
1016
1017 float k, sum;
1018 float a0 = 0.35875;
1019 float a1 = 0.48829;
1020 float a2 = 0.14128;
1021 float a3 = 0.01168;
1022
1023 for (int i = 0; i < DELAYFILTS; i++) {
1024 data = (complex *) convolve_h_alloc(h_len);
1025 h = new signalVector(data, 0, h_len);
1026 h->setAligned(true);
1027 h->isReal(true);
1028
1029 sum = 0.0;
1030 itr = h->end();
1031 for (int n = 0; n < h_len; n++) {
1032 k = (float) n;
1033 *--itr = (complex) sinc(M_PI_F *
1034 (k - (float) h_len / 2.0 - (float) i / DELAYFILTS));
1035 *itr *= a0 -
1036 a1 * cos(2 * M_PI * n / (h_len - 1)) +
1037 a2 * cos(4 * M_PI * n / (h_len - 1)) -
1038 a3 * cos(6 * M_PI * n / (h_len - 1));
1039
1040 sum += itr->real();
1041 }
1042
1043 itr = h->begin();
1044 for (int n = 0; n < h_len; n++)
1045 *itr++ /= sum;
1046
1047 delayFilters[i] = h;
1048 }
1049}
1050
Alexander Chemerise0c12182017-03-18 13:27:48 -07001051signalVector *delayVector(const signalVector *in, signalVector *out, float delay)
dburgessb3a0ca42011-10-12 07:44:40 +00001052{
Thomas Tsouf79c4d02013-11-09 15:51:56 -06001053 int whole, index;
Thomas Tsou2c282f52013-10-08 21:34:35 -04001054 float frac;
Thomas Tsou94edaae2013-11-09 22:19:19 -05001055 signalVector *h, *shift, *fshift = NULL;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001056
Thomas Tsou2c282f52013-10-08 21:34:35 -04001057 whole = floor(delay);
1058 frac = delay - whole;
1059
1060 /* Sinc interpolated fractional shift (if allowable) */
1061 if (fabs(frac) > 1e-2) {
Thomas Tsouf79c4d02013-11-09 15:51:56 -06001062 index = floorf(frac * (float) DELAYFILTS);
1063 h = delayFilters[index];
Thomas Tsou2c282f52013-10-08 21:34:35 -04001064
Thomas Tsou94edaae2013-11-09 22:19:19 -05001065 fshift = convolve(in, h, NULL, NO_DELAY);
1066 if (!fshift)
1067 return NULL;
dburgessb3a0ca42011-10-12 07:44:40 +00001068 }
1069
Thomas Tsou94edaae2013-11-09 22:19:19 -05001070 if (!fshift)
1071 shift = new signalVector(*in);
1072 else
1073 shift = fshift;
1074
Thomas Tsou2c282f52013-10-08 21:34:35 -04001075 /* Integer sample shift */
1076 if (whole < 0) {
1077 whole = -whole;
Thomas Tsou94edaae2013-11-09 22:19:19 -05001078 signalVector::iterator wBurstItr = shift->begin();
1079 signalVector::iterator shiftedItr = shift->begin() + whole;
Thomas Tsou2c282f52013-10-08 21:34:35 -04001080
Thomas Tsou94edaae2013-11-09 22:19:19 -05001081 while (shiftedItr < shift->end())
dburgessb3a0ca42011-10-12 07:44:40 +00001082 *wBurstItr++ = *shiftedItr++;
Thomas Tsou2c282f52013-10-08 21:34:35 -04001083
Thomas Tsou94edaae2013-11-09 22:19:19 -05001084 while (wBurstItr < shift->end())
1085 *wBurstItr++ = 0.0;
1086 } else if (whole >= 0) {
1087 signalVector::iterator wBurstItr = shift->end() - 1;
1088 signalVector::iterator shiftedItr = shift->end() - 1 - whole;
1089
1090 while (shiftedItr >= shift->begin())
dburgessb3a0ca42011-10-12 07:44:40 +00001091 *wBurstItr-- = *shiftedItr--;
Thomas Tsou94edaae2013-11-09 22:19:19 -05001092
1093 while (wBurstItr >= shift->begin())
dburgessb3a0ca42011-10-12 07:44:40 +00001094 *wBurstItr-- = 0.0;
1095 }
Thomas Tsou2c282f52013-10-08 21:34:35 -04001096
Thomas Tsou94edaae2013-11-09 22:19:19 -05001097 if (!out)
1098 return shift;
1099
1100 out->clone(*shift);
1101 delete shift;
1102 return out;
dburgessb3a0ca42011-10-12 07:44:40 +00001103}
Thomas Tsou2c282f52013-10-08 21:34:35 -04001104
Tom Tsou70134a02017-06-12 14:23:53 -07001105static complex interpolatePoint(const signalVector &inSig, float ix)
dburgessb3a0ca42011-10-12 07:44:40 +00001106{
dburgessb3a0ca42011-10-12 07:44:40 +00001107 int start = (int) (floor(ix) - 10);
1108 if (start < 0) start = 0;
1109 int end = (int) (floor(ix) + 11);
1110 if ((unsigned) end > inSig.size()-1) end = inSig.size()-1;
1111
1112 complex pVal = 0.0;
Thomas Tsou20eb6d62013-11-09 14:30:41 -05001113 if (!inSig.isReal()) {
dburgessb3a0ca42011-10-12 07:44:40 +00001114 for (int i = start; i < end; i++)
1115 pVal += inSig[i] * sinc(M_PI_F*(i-ix));
1116 }
1117 else {
1118 for (int i = start; i < end; i++)
1119 pVal += inSig[i].real() * sinc(M_PI_F*(i-ix));
1120 }
1121
1122 return pVal;
1123}
1124
Thomas Tsou8181b012013-08-20 21:17:19 -04001125static complex fastPeakDetect(const signalVector &rxBurst, float *index)
1126{
1127 float val, max = 0.0f;
1128 complex amp;
1129 int _index = -1;
1130
Thomas Tsou3f32ab52013-11-15 16:32:54 -05001131 for (size_t i = 0; i < rxBurst.size(); i++) {
Thomas Tsou8181b012013-08-20 21:17:19 -04001132 val = rxBurst[i].norm2();
1133 if (val > max) {
1134 max = val;
1135 _index = i;
1136 amp = rxBurst[i];
1137 }
1138 }
1139
1140 if (index)
1141 *index = (float) _index;
1142
1143 return amp;
1144}
1145
Tom Tsou70134a02017-06-12 14:23:53 -07001146static complex peakDetect(const signalVector &rxBurst,
1147 float *peakIndex, float *avgPwr)
dburgessb3a0ca42011-10-12 07:44:40 +00001148{
dburgessb3a0ca42011-10-12 07:44:40 +00001149 complex maxVal = 0.0;
1150 float maxIndex = -1;
1151 float sumPower = 0.0;
1152
1153 for (unsigned int i = 0; i < rxBurst.size(); i++) {
1154 float samplePower = rxBurst[i].norm2();
1155 if (samplePower > maxVal.real()) {
1156 maxVal = samplePower;
1157 maxIndex = i;
1158 }
1159 sumPower += samplePower;
1160 }
1161
1162 // interpolate around the peak
1163 // to save computation, we'll use early-late balancing
1164 float earlyIndex = maxIndex-1;
1165 float lateIndex = maxIndex+1;
1166
1167 float incr = 0.5;
1168 while (incr > 1.0/1024.0) {
1169 complex earlyP = interpolatePoint(rxBurst,earlyIndex);
1170 complex lateP = interpolatePoint(rxBurst,lateIndex);
1171 if (earlyP < lateP)
1172 earlyIndex += incr;
1173 else if (earlyP > lateP)
1174 earlyIndex -= incr;
1175 else break;
1176 incr /= 2.0;
1177 lateIndex = earlyIndex + 2.0;
1178 }
1179
1180 maxIndex = earlyIndex + 1.0;
1181 maxVal = interpolatePoint(rxBurst,maxIndex);
1182
1183 if (peakIndex!=NULL)
1184 *peakIndex = maxIndex;
1185
1186 if (avgPwr!=NULL)
1187 *avgPwr = (sumPower-maxVal.norm2()) / (rxBurst.size()-1);
1188
1189 return maxVal;
1190
1191}
1192
1193void scaleVector(signalVector &x,
1194 complex scale)
1195{
Thomas Tsou7e4e5362013-10-30 21:18:55 -04001196#ifdef HAVE_NEON
1197 int len = x.size();
1198
1199 scale_complex((float *) x.begin(),
1200 (float *) x.begin(),
1201 (float *) &scale, len);
1202#else
dburgessb3a0ca42011-10-12 07:44:40 +00001203 signalVector::iterator xP = x.begin();
1204 signalVector::iterator xPEnd = x.end();
Thomas Tsou20eb6d62013-11-09 14:30:41 -05001205 if (!x.isReal()) {
dburgessb3a0ca42011-10-12 07:44:40 +00001206 while (xP < xPEnd) {
1207 *xP = *xP * scale;
1208 xP++;
1209 }
1210 }
1211 else {
1212 while (xP < xPEnd) {
1213 *xP = xP->real() * scale;
1214 xP++;
1215 }
1216 }
Thomas Tsou7e4e5362013-10-30 21:18:55 -04001217#endif
dburgessb3a0ca42011-10-12 07:44:40 +00001218}
1219
1220/** in-place conjugation */
Tom Tsou70134a02017-06-12 14:23:53 -07001221static void conjugateVector(signalVector &x)
dburgessb3a0ca42011-10-12 07:44:40 +00001222{
Thomas Tsou20eb6d62013-11-09 14:30:41 -05001223 if (x.isReal()) return;
dburgessb3a0ca42011-10-12 07:44:40 +00001224 signalVector::iterator xP = x.begin();
1225 signalVector::iterator xPEnd = x.end();
1226 while (xP < xPEnd) {
1227 *xP = xP->conj();
1228 xP++;
1229 }
1230}
1231
Tom Tsou2079a3c2016-03-06 00:58:56 -08001232static bool generateMidamble(int sps, int tsc)
dburgessb3a0ca42011-10-12 07:44:40 +00001233{
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001234 bool status = true;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001235 float toa;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001236 complex *data = NULL;
1237 signalVector *autocorr = NULL, *midamble = NULL;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001238 signalVector *midMidamble = NULL, *_midMidamble = NULL;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001239
Thomas Tsou3eaae802013-08-20 19:31:14 -04001240 if ((tsc < 0) || (tsc > 7))
dburgessb3a0ca42011-10-12 07:44:40 +00001241 return false;
1242
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001243 delete gMidambles[tsc];
Thomas Tsou3eaae802013-08-20 19:31:14 -04001244
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001245 /* Use middle 16 bits of each TSC. Correlation sequence is not pulse shaped */
1246 midMidamble = modulateBurst(gTrainingSequence[tsc].segment(5,16), 0, sps, true);
1247 if (!midMidamble)
1248 return false;
1249
Thomas Tsou3eaae802013-08-20 19:31:14 -04001250 /* Simulated receive sequence is pulse shaped */
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001251 midamble = modulateBurst(gTrainingSequence[tsc], 0, sps, false);
1252 if (!midamble) {
1253 status = false;
1254 goto release;
dburgessb3a0ca42011-10-12 07:44:40 +00001255 }
Thomas Tsou3eaae802013-08-20 19:31:14 -04001256
dburgessb3a0ca42011-10-12 07:44:40 +00001257 // NOTE: Because ideal TSC 16-bit midamble is 66 symbols into burst,
1258 // the ideal TSC has an + 180 degree phase shift,
1259 // due to the pi/2 frequency shift, that
1260 // needs to be accounted for.
1261 // 26-midamble is 61 symbols into burst, has +90 degree phase shift.
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001262 scaleVector(*midMidamble, complex(-1.0, 0.0));
1263 scaleVector(*midamble, complex(0.0, 1.0));
dburgessb3a0ca42011-10-12 07:44:40 +00001264
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001265 conjugateVector(*midMidamble);
dburgessb3a0ca42011-10-12 07:44:40 +00001266
Thomas Tsou3eaae802013-08-20 19:31:14 -04001267 /* For SSE alignment, reallocate the midamble sequence on 16-byte boundary */
1268 data = (complex *) convolve_h_alloc(midMidamble->size());
1269 _midMidamble = new signalVector(data, 0, midMidamble->size());
1270 _midMidamble->setAligned(true);
1271 memcpy(_midMidamble->begin(), midMidamble->begin(),
1272 midMidamble->size() * sizeof(complex));
1273
1274 autocorr = convolve(midamble, _midMidamble, NULL, NO_DELAY);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001275 if (!autocorr) {
1276 status = false;
1277 goto release;
1278 }
dburgessb3a0ca42011-10-12 07:44:40 +00001279
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001280 gMidambles[tsc] = new CorrelationSequence;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001281 gMidambles[tsc]->buffer = data;
1282 gMidambles[tsc]->sequence = _midMidamble;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001283 gMidambles[tsc]->gain = peakDetect(*autocorr, &toa, NULL);
1284
1285 /* For 1 sps only
1286 * (Half of correlation length - 1) + midpoint of pulse shape + remainder
1287 * 13.5 = (16 / 2 - 1) + 1.5 + (26 - 10) / 2
1288 */
1289 if (sps == 1)
1290 gMidambles[tsc]->toa = toa - 13.5;
1291 else
1292 gMidambles[tsc]->toa = 0;
dburgessb3a0ca42011-10-12 07:44:40 +00001293
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001294release:
dburgessb3a0ca42011-10-12 07:44:40 +00001295 delete autocorr;
1296 delete midamble;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001297 delete midMidamble;
dburgessb3a0ca42011-10-12 07:44:40 +00001298
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001299 if (!status) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001300 delete _midMidamble;
1301 free(data);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001302 gMidambles[tsc] = NULL;
1303 }
1304
1305 return status;
dburgessb3a0ca42011-10-12 07:44:40 +00001306}
1307
Tom Tsou70134a02017-06-12 14:23:53 -07001308static CorrelationSequence *generateEdgeMidamble(int tsc)
Tom Tsoud3253432016-03-06 03:08:01 -08001309{
1310 complex *data = NULL;
1311 signalVector *midamble = NULL, *_midamble = NULL;
1312 CorrelationSequence *seq;
1313
1314 if ((tsc < 0) || (tsc > 7))
1315 return NULL;
1316
1317 /* Use middle 48 bits of each TSC. Correlation sequence is not pulse shaped */
1318 const BitVector *bits = &gEdgeTrainingSequence[tsc];
1319 midamble = modulateEdgeBurst(bits->segment(15, 48), 1, true);
1320 if (!midamble)
1321 return NULL;
1322
1323 conjugateVector(*midamble);
1324
1325 data = (complex *) convolve_h_alloc(midamble->size());
1326 _midamble = new signalVector(data, 0, midamble->size());
1327 _midamble->setAligned(true);
1328 memcpy(_midamble->begin(), midamble->begin(),
1329 midamble->size() * sizeof(complex));
1330
1331 /* Channel gain is an empirically measured value */
1332 seq = new CorrelationSequence;
1333 seq->buffer = data;
1334 seq->sequence = _midamble;
1335 seq->gain = Complex<float>(-19.6432, 19.5006) / 1.18;
1336 seq->toa = 0;
1337
1338 delete midamble;
1339
1340 return seq;
1341}
1342
Tom Tsou2079a3c2016-03-06 00:58:56 -08001343static bool generateRACHSequence(int sps)
dburgessb3a0ca42011-10-12 07:44:40 +00001344{
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001345 bool status = true;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001346 float toa;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001347 complex *data = NULL;
1348 signalVector *autocorr = NULL;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001349 signalVector *seq0 = NULL, *seq1 = NULL, *_seq1 = NULL;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001350
1351 delete gRACHSequence;
1352
1353 seq0 = modulateBurst(gRACHSynchSequence, 0, sps, false);
1354 if (!seq0)
1355 return false;
1356
1357 seq1 = modulateBurst(gRACHSynchSequence.segment(0, 40), 0, sps, true);
1358 if (!seq1) {
1359 status = false;
1360 goto release;
dburgessb3a0ca42011-10-12 07:44:40 +00001361 }
1362
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001363 conjugateVector(*seq1);
dburgessb3a0ca42011-10-12 07:44:40 +00001364
Thomas Tsou3eaae802013-08-20 19:31:14 -04001365 /* For SSE alignment, reallocate the midamble sequence on 16-byte boundary */
1366 data = (complex *) convolve_h_alloc(seq1->size());
1367 _seq1 = new signalVector(data, 0, seq1->size());
1368 _seq1->setAligned(true);
1369 memcpy(_seq1->begin(), seq1->begin(), seq1->size() * sizeof(complex));
1370
1371 autocorr = convolve(seq0, _seq1, autocorr, NO_DELAY);
1372 if (!autocorr) {
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001373 status = false;
1374 goto release;
1375 }
dburgessb3a0ca42011-10-12 07:44:40 +00001376
1377 gRACHSequence = new CorrelationSequence;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001378 gRACHSequence->sequence = _seq1;
1379 gRACHSequence->buffer = data;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001380 gRACHSequence->gain = peakDetect(*autocorr, &toa, NULL);
1381
1382 /* For 1 sps only
1383 * (Half of correlation length - 1) + midpoint of pulse shaping filer
1384 * 20.5 = (40 / 2 - 1) + 1.5
1385 */
1386 if (sps == 1)
1387 gRACHSequence->toa = toa - 20.5;
1388 else
1389 gRACHSequence->toa = 0.0;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001390
1391release:
dburgessb3a0ca42011-10-12 07:44:40 +00001392 delete autocorr;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001393 delete seq0;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001394 delete seq1;
dburgessb3a0ca42011-10-12 07:44:40 +00001395
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001396 if (!status) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001397 delete _seq1;
1398 free(data);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001399 gRACHSequence = NULL;
1400 }
dburgessb3a0ca42011-10-12 07:44:40 +00001401
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001402 return status;
dburgessb3a0ca42011-10-12 07:44:40 +00001403}
Thomas Tsou3eaae802013-08-20 19:31:14 -04001404
Tom Tsoua84e1622016-06-29 14:50:25 -07001405/*
1406 * Peak-to-average computation +/- range from peak in symbols
1407 */
1408#define COMPUTE_PEAK_MIN 2
1409#define COMPUTE_PEAK_MAX 5
1410
1411/*
1412 * Minimum number of values needed to compute peak-to-average
1413 */
1414#define COMPUTE_PEAK_CNT 5
1415
Thomas Tsou865bca42013-08-21 20:58:00 -04001416static float computePeakRatio(signalVector *corr,
1417 int sps, float toa, complex amp)
dburgessb3a0ca42011-10-12 07:44:40 +00001418{
Thomas Tsou865bca42013-08-21 20:58:00 -04001419 int num = 0;
1420 complex *peak;
1421 float rms, avg = 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001422
Thomas Tsou865bca42013-08-21 20:58:00 -04001423 /* Check for bogus results */
1424 if ((toa < 0.0) || (toa > corr->size()))
1425 return 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001426
Alexander Chemeris1e9b4d52015-06-04 19:05:28 -04001427 peak = corr->begin() + (int) rint(toa);
1428
Tom Tsoua84e1622016-06-29 14:50:25 -07001429 for (int i = COMPUTE_PEAK_MIN * sps; i <= COMPUTE_PEAK_MAX * sps; i++) {
Thomas Tsou865bca42013-08-21 20:58:00 -04001430 if (peak - i >= corr->begin()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001431 avg += (peak - i)->norm2();
1432 num++;
1433 }
Thomas Tsou865bca42013-08-21 20:58:00 -04001434 if (peak + i < corr->end()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001435 avg += (peak + i)->norm2();
1436 num++;
1437 }
dburgessb3a0ca42011-10-12 07:44:40 +00001438 }
1439
Tom Tsoua84e1622016-06-29 14:50:25 -07001440 if (num < COMPUTE_PEAK_CNT)
Thomas Tsou865bca42013-08-21 20:58:00 -04001441 return 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001442
Thomas Tsou3eaae802013-08-20 19:31:14 -04001443 rms = sqrtf(avg / (float) num) + 0.00001;
dburgessb3a0ca42011-10-12 07:44:40 +00001444
Thomas Tsou865bca42013-08-21 20:58:00 -04001445 return (amp.abs()) / rms;
dburgessb3a0ca42011-10-12 07:44:40 +00001446}
1447
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001448float energyDetect(const signalVector &rxBurst, unsigned windowLength)
dburgessb3a0ca42011-10-12 07:44:40 +00001449{
1450
1451 signalVector::const_iterator windowItr = rxBurst.begin(); //+rxBurst.size()/2 - 5*windowLength/2;
1452 float energy = 0.0;
Tom Tsou2af14402017-03-23 14:54:00 -07001453 if (windowLength == 0) return 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001454 if (windowLength > rxBurst.size()) windowLength = rxBurst.size();
1455 for (unsigned i = 0; i < windowLength; i++) {
1456 energy += windowItr->norm2();
1457 windowItr+=4;
1458 }
Alexander Chemeris1dd05cf2017-03-15 23:23:36 +03001459 return energy/windowLength;
dburgessb3a0ca42011-10-12 07:44:40 +00001460}
dburgessb3a0ca42011-10-12 07:44:40 +00001461
Tom Tsou70134a02017-06-12 14:23:53 -07001462static signalVector *downsampleBurst(const signalVector &burst)
1463{
Tom Tsou7278a872017-06-14 14:50:39 -07001464 signalVector in(DOWNSAMPLE_IN_LEN, dnsampler->len());
1465 signalVector *out = new signalVector(DOWNSAMPLE_OUT_LEN);
1466 memcpy(in.begin(), burst.begin(), DOWNSAMPLE_IN_LEN * 2 * sizeof(float));
Tom Tsou70134a02017-06-12 14:23:53 -07001467
Tom Tsou7278a872017-06-14 14:50:39 -07001468 if (dnsampler->rotate((float *) in.begin(), DOWNSAMPLE_IN_LEN,
Tom Tsou70134a02017-06-12 14:23:53 -07001469 (float *) out->begin(), DOWNSAMPLE_OUT_LEN) < 0) {
1470 delete out;
1471 out = NULL;
1472 }
1473
Tom Tsou70134a02017-06-12 14:23:53 -07001474 return out;
1475};
1476
Thomas Tsou865bca42013-08-21 20:58:00 -04001477/*
1478 * Detect a burst based on correlation and peak-to-average ratio
1479 *
1480 * For one sampler-per-symbol, perform fast peak detection (no interpolation)
1481 * for initial gating. We do this because energy detection should be disabled.
1482 * For higher oversampling values, we assume the energy detector is in place
1483 * and we run full interpolating peak detection.
1484 */
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001485static int detectBurst(const signalVector &burst,
Thomas Tsou865bca42013-08-21 20:58:00 -04001486 signalVector &corr, CorrelationSequence *sync,
1487 float thresh, int sps, complex *amp, float *toa,
1488 int start, int len)
dburgessb3a0ca42011-10-12 07:44:40 +00001489{
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001490 const signalVector *corr_in;
1491 signalVector *dec = NULL;
Tom Tsoud3253432016-03-06 03:08:01 -08001492
1493 if (sps == 4) {
1494 dec = downsampleBurst(burst);
1495 corr_in = dec;
1496 sps = 1;
1497 } else {
1498 corr_in = &burst;
1499 }
1500
Thomas Tsou865bca42013-08-21 20:58:00 -04001501 /* Correlate */
Tom Tsoud3253432016-03-06 03:08:01 -08001502 if (!convolve(corr_in, sync->sequence, &corr,
1503 CUSTOM, start, len, 1, 0)) {
1504 delete dec;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001505 return -1;
dburgessb3a0ca42011-10-12 07:44:40 +00001506 }
1507
Tom Tsoud3253432016-03-06 03:08:01 -08001508 delete dec;
1509
1510 /* Running at the downsampled rate at this point */
1511 sps = 1;
1512
Thomas Tsou865bca42013-08-21 20:58:00 -04001513 /* Peak detection - place restrictions at correlation edges */
1514 *amp = fastPeakDetect(corr, toa);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001515
Thomas Tsou865bca42013-08-21 20:58:00 -04001516 if ((*toa < 3 * sps) || (*toa > len - 3 * sps))
1517 return 0;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001518
Thomas Tsou865bca42013-08-21 20:58:00 -04001519 /* Peak -to-average ratio */
1520 if (computePeakRatio(&corr, sps, *toa, *amp) < thresh)
1521 return 0;
1522
1523 /* Compute peak-to-average ratio. Reject if we don't have enough values */
1524 *amp = peakDetect(corr, toa, NULL);
1525
1526 /* Normalize our channel gain */
1527 *amp = *amp / sync->gain;
1528
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001529 /* Compensate for residuate time lag */
1530 *toa = *toa - sync->toa;
1531
Thomas Tsou865bca42013-08-21 20:58:00 -04001532 return 1;
1533}
1534
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001535static float maxAmplitude(const signalVector &burst)
Tom Tsou577cd022015-05-18 13:57:54 -07001536{
Alexander Chemeris954b1182015-06-04 15:39:41 -04001537 float max = 0.0;
1538 for (size_t i = 0; i < burst.size(); i++) {
1539 if (fabs(burst[i].real()) > max)
1540 max = fabs(burst[i].real());
1541 if (fabs(burst[i].imag()) > max)
1542 max = fabs(burst[i].imag());
1543 }
Tom Tsou577cd022015-05-18 13:57:54 -07001544
Alexander Chemeris954b1182015-06-04 15:39:41 -04001545 return max;
Tom Tsou577cd022015-05-18 13:57:54 -07001546}
1547
Alexander Chemeris130a8002015-06-09 20:52:11 -04001548/*
1549 * RACH/Normal burst detection with clipping detection
Thomas Tsou865bca42013-08-21 20:58:00 -04001550 *
1551 * Correlation window parameters:
Alexander Chemeris130a8002015-06-09 20:52:11 -04001552 * target: Tail bits + burst length
1553 * head: Search symbols before target
1554 * tail: Search symbols after target
Thomas Tsou865bca42013-08-21 20:58:00 -04001555 */
Alexander Chemeris1470fcd2017-03-17 22:35:02 -07001556static int detectGeneralBurst(const signalVector &rxBurst,
1557 float thresh,
1558 int sps,
1559 complex &amp,
1560 float &toa,
1561 int target, int head, int tail,
1562 CorrelationSequence *sync)
Thomas Tsou865bca42013-08-21 20:58:00 -04001563{
Alexander Chemeris130a8002015-06-09 20:52:11 -04001564 int rc, start, len;
Alexander Chemeris954b1182015-06-04 15:39:41 -04001565 bool clipping = false;
Thomas Tsou865bca42013-08-21 20:58:00 -04001566
1567 if ((sps != 1) && (sps != 4))
Tom Tsou577cd022015-05-18 13:57:54 -07001568 return -SIGERR_UNSUPPORTED;
1569
Alexander Chemeris954b1182015-06-04 15:39:41 -04001570 // Detect potential clipping
1571 // We still may be able to demod the burst, so we'll give it a try
1572 // and only report clipping if we can't demod.
1573 float maxAmpl = maxAmplitude(rxBurst);
1574 if (maxAmpl > CLIP_THRESH) {
1575 LOG(DEBUG) << "max burst amplitude: " << maxAmpl << " is above the clipping threshold: " << CLIP_THRESH << std::endl;
1576 clipping = true;
1577 }
Thomas Tsou865bca42013-08-21 20:58:00 -04001578
Tom Tsoud3253432016-03-06 03:08:01 -08001579 start = target - head - 1;
1580 len = head + tail;
Tom Tsou7278a872017-06-14 14:50:39 -07001581 signalVector corr(len);
Thomas Tsou865bca42013-08-21 20:58:00 -04001582
Tom Tsou7278a872017-06-14 14:50:39 -07001583 rc = detectBurst(rxBurst, corr, sync,
Alexander Chemeris130a8002015-06-09 20:52:11 -04001584 thresh, sps, &amp, &toa, start, len);
Thomas Tsou865bca42013-08-21 20:58:00 -04001585 if (rc < 0) {
Tom Tsou577cd022015-05-18 13:57:54 -07001586 return -SIGERR_INTERNAL;
Thomas Tsou865bca42013-08-21 20:58:00 -04001587 } else if (!rc) {
Alexander Chemeris130a8002015-06-09 20:52:11 -04001588 amp = 0.0f;
1589 toa = 0.0f;
Alexander Chemeris954b1182015-06-04 15:39:41 -04001590 return clipping?-SIGERR_CLIP:SIGERR_NONE;
dburgessb3a0ca42011-10-12 07:44:40 +00001591 }
1592
Thomas Tsou865bca42013-08-21 20:58:00 -04001593 /* Subtract forward search bits from delay */
Tom Tsoud3253432016-03-06 03:08:01 -08001594 toa -= head;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001595
Thomas Tsou865bca42013-08-21 20:58:00 -04001596 return 1;
1597}
Thomas Tsou3eaae802013-08-20 19:31:14 -04001598
Alexander Chemeris130a8002015-06-09 20:52:11 -04001599
1600/*
1601 * RACH burst detection
1602 *
1603 * Correlation window parameters:
1604 * target: Tail bits + RACH length (reduced from 41 to a multiple of 4)
Tom Tsoue90c24c2016-06-21 16:14:39 -07001605 * head: Search 8 symbols before target
1606 * tail: Search 8 symbols + maximum expected delay
Alexander Chemeris130a8002015-06-09 20:52:11 -04001607 */
Tom Tsou70134a02017-06-12 14:23:53 -07001608static int detectRACHBurst(const signalVector &burst, float threshold, int sps,
1609 complex &amplitude, float &toa, unsigned max_toa)
Alexander Chemeris130a8002015-06-09 20:52:11 -04001610{
1611 int rc, target, head, tail;
1612 CorrelationSequence *sync;
1613
1614 target = 8 + 40;
Tom Tsoue90c24c2016-06-21 16:14:39 -07001615 head = 8;
Alexander Chemeris14d13b62017-03-17 15:12:17 -07001616 tail = 8 + max_toa;
Alexander Chemeris130a8002015-06-09 20:52:11 -04001617 sync = gRACHSequence;
1618
Alexander Chemeris14d13b62017-03-17 15:12:17 -07001619 rc = detectGeneralBurst(burst, threshold, sps, amplitude, toa,
Alexander Chemeris130a8002015-06-09 20:52:11 -04001620 target, head, tail, sync);
1621
1622 return rc;
1623}
1624
Thomas Tsou865bca42013-08-21 20:58:00 -04001625/*
1626 * 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;
1689 case RACH:
1690 rc = detectRACHBurst(burst, threshold, sps, amp, toa,
1691 max_toa);
1692 break;
1693 default:
1694 LOG(ERR) << "Invalid correlation type";
1695 }
1696
1697 if (rc > 0)
1698 return type;
1699
1700 return rc;
1701}
1702
Tom Tsoud3253432016-03-06 03:08:01 -08001703/*
1704 * Soft 8-PSK decoding using Manhattan distance metric
1705 */
1706static SoftVector *softSliceEdgeBurst(signalVector &burst)
1707{
1708 size_t nsyms = 148;
1709
1710 if (burst.size() < nsyms)
1711 return NULL;
1712
1713 signalVector::iterator itr;
1714 SoftVector *bits = new SoftVector(nsyms * 3);
1715
1716 /*
1717 * Bits 0 and 1 - First and second bits of the symbol respectively
1718 */
1719 rotateBurst2(burst, -M_PI / 8.0);
1720 itr = burst.begin();
1721 for (size_t i = 0; i < nsyms; i++) {
1722 (*bits)[3 * i + 0] = -itr->imag();
1723 (*bits)[3 * i + 1] = itr->real();
1724 itr++;
1725 }
1726
1727 /*
1728 * Bit 2 - Collapse symbols into quadrant 0 (positive X and Y).
1729 * Decision area is then simplified to X=Y axis. Rotate again to
1730 * place decision boundary on X-axis.
1731 */
1732 itr = burst.begin();
1733 for (size_t i = 0; i < burst.size(); i++) {
1734 burst[i] = Complex<float>(fabs(itr->real()), fabs(itr->imag()));
1735 itr++;
1736 }
1737
1738 rotateBurst2(burst, -M_PI / 4.0);
1739 itr = burst.begin();
1740 for (size_t i = 0; i < nsyms; i++) {
1741 (*bits)[3 * i + 2] = -itr->imag();
1742 itr++;
1743 }
1744
1745 signalVector soft(bits->size());
1746 for (size_t i = 0; i < bits->size(); i++)
1747 soft[i] = (*bits)[i];
1748
1749 return bits;
1750}
1751
1752/*
Alexander Chemeris132fb242017-03-17 17:22:33 -07001753 * Convert signalVector to SoftVector by taking real part of the signal.
1754 */
1755static SoftVector *signalToSoftVector(signalVector *dec)
1756{
1757 SoftVector *bits = new SoftVector(dec->size());
1758
1759 SoftVector::iterator bit_itr = bits->begin();
1760 signalVector::iterator burst_itr = dec->begin();
1761
1762 for (; burst_itr < dec->end(); burst_itr++)
1763 *bit_itr++ = burst_itr->real();
1764
1765 return bits;
1766}
1767
1768/*
Tom Tsou7fec3032016-03-06 22:33:20 -08001769 * Shared portion of GMSK and EDGE demodulators consisting of timing
1770 * recovery and single tap channel correction. For 4 SPS (if activated),
1771 * the output is downsampled prior to the 1 SPS modulation specific
1772 * stages.
1773 */
Alexander Chemerise0c12182017-03-18 13:27:48 -07001774static signalVector *demodCommon(const signalVector &burst, int sps,
Tom Tsou7fec3032016-03-06 22:33:20 -08001775 complex chan, float toa)
1776{
1777 signalVector *delay, *dec;
1778
1779 if ((sps != 1) && (sps != 4))
1780 return NULL;
1781
Tom Tsou7fec3032016-03-06 22:33:20 -08001782 delay = delayVector(&burst, NULL, -toa * (float) sps);
Alexander Chemerise0c12182017-03-18 13:27:48 -07001783 scaleVector(*delay, (complex) 1.0 / chan);
Tom Tsou7fec3032016-03-06 22:33:20 -08001784
1785 if (sps == 1)
1786 return delay;
1787
1788 dec = downsampleBurst(*delay);
1789
1790 delete delay;
1791 return dec;
1792}
1793
1794/*
Tom Tsoud3253432016-03-06 03:08:01 -08001795 * Demodulate GSMK burst. Prior to symbol rotation, operate at
1796 * 4 SPS (if activated) to minimize distortion through the fractional
1797 * delay filters. Symbol rotation and after always operates at 1 SPS.
1798 */
Tom Tsou70134a02017-06-12 14:23:53 -07001799static SoftVector *demodGmskBurst(const signalVector &rxBurst,
1800 int sps, complex channel, float TOA)
dburgessb3a0ca42011-10-12 07:44:40 +00001801{
Thomas Tsou94edaae2013-11-09 22:19:19 -05001802 SoftVector *bits;
Tom Tsou7fec3032016-03-06 22:33:20 -08001803 signalVector *dec;
dburgessb3a0ca42011-10-12 07:44:40 +00001804
Tom Tsou7fec3032016-03-06 22:33:20 -08001805 dec = demodCommon(rxBurst, sps, channel, TOA);
1806 if (!dec)
1807 return NULL;
dburgessb3a0ca42011-10-12 07:44:40 +00001808
Tom Tsoud3253432016-03-06 03:08:01 -08001809 /* Shift up by a quarter of a frequency */
1810 GMSKReverseRotate(*dec, 1);
Alexander Chemeris132fb242017-03-17 17:22:33 -07001811 /* Take real part of the signal */
1812 bits = signalToSoftVector(dec);
Thomas Tsou94edaae2013-11-09 22:19:19 -05001813 delete dec;
dburgessb3a0ca42011-10-12 07:44:40 +00001814
Thomas Tsou94edaae2013-11-09 22:19:19 -05001815 return bits;
dburgessb3a0ca42011-10-12 07:44:40 +00001816}
Thomas Tsou94edaae2013-11-09 22:19:19 -05001817
Tom Tsoud3253432016-03-06 03:08:01 -08001818/*
1819 * Demodulate an 8-PSK burst. Prior to symbol rotation, operate at
1820 * 4 SPS (if activated) to minimize distortion through the fractional
1821 * delay filters. Symbol rotation and after always operates at 1 SPS.
1822 *
1823 * Allow 1 SPS demodulation here, but note that other parts of the
1824 * transceiver restrict EDGE operatoin to 4 SPS - 8-PSK distortion
1825 * through the fractional delay filters at 1 SPS renders signal
1826 * nearly unrecoverable.
1827 */
Tom Tsou70134a02017-06-12 14:23:53 -07001828static SoftVector *demodEdgeBurst(const signalVector &burst,
1829 int sps, complex chan, float toa)
Tom Tsoud3253432016-03-06 03:08:01 -08001830{
1831 SoftVector *bits;
Tom Tsou7fec3032016-03-06 22:33:20 -08001832 signalVector *dec, *rot, *eq;
Tom Tsoud3253432016-03-06 03:08:01 -08001833
Tom Tsou7fec3032016-03-06 22:33:20 -08001834 dec = demodCommon(burst, sps, chan, toa);
1835 if (!dec)
Tom Tsoud3253432016-03-06 03:08:01 -08001836 return NULL;
1837
Tom Tsou7fec3032016-03-06 22:33:20 -08001838 /* Equalize and derotate */
Tom Tsoud3253432016-03-06 03:08:01 -08001839 eq = convolve(dec, GSMPulse4->c0_inv, NULL, NO_DELAY);
1840 rot = derotateEdgeBurst(*eq, 1);
1841
Tom Tsou7fec3032016-03-06 22:33:20 -08001842 /* Soft slice and normalize */
Tom Tsou04795622016-04-26 21:17:36 -07001843 bits = softSliceEdgeBurst(*rot);
Tom Tsoud3253432016-03-06 03:08:01 -08001844
1845 delete dec;
1846 delete eq;
1847 delete rot;
1848
1849 return bits;
1850}
1851
Alexander Chemerise0c12182017-03-18 13:27:48 -07001852SoftVector *demodAnyBurst(const signalVector &burst, int sps, complex amp,
Alexander Chemeris6e1dffd2017-03-17 16:13:51 -07001853 float toa, CorrType type)
1854{
1855 if (type == EDGE)
1856 return demodEdgeBurst(burst, sps, amp, toa);
1857 else
1858 return demodGmskBurst(burst, sps, amp, toa);
1859}
1860
Tom Tsou2079a3c2016-03-06 00:58:56 -08001861bool sigProcLibSetup()
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001862{
Thomas Tsou0e0e1f42013-11-09 22:08:51 -05001863 generateSincTable();
Tom Tsou2079a3c2016-03-06 00:58:56 -08001864 initGMSKRotationTables();
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001865
Tom Tsou2079a3c2016-03-06 00:58:56 -08001866 GSMPulse1 = generateGSMPulse(1);
1867 GSMPulse4 = generateGSMPulse(4);
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001868
Tom Tsou2079a3c2016-03-06 00:58:56 -08001869 generateRACHSequence(1);
Tom Tsoud3253432016-03-06 03:08:01 -08001870 for (int tsc = 0; tsc < 8; tsc++) {
Tom Tsou2079a3c2016-03-06 00:58:56 -08001871 generateMidamble(1, tsc);
Tom Tsoud3253432016-03-06 03:08:01 -08001872 gEdgeMidambles[tsc] = generateEdgeMidamble(tsc);
1873 }
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001874
Thomas Tsouf79c4d02013-11-09 15:51:56 -06001875 generateDelayFilters();
1876
Tom Tsoud3253432016-03-06 03:08:01 -08001877 dnsampler = new Resampler(1, 4);
1878 if (!dnsampler->init()) {
1879 LOG(ALERT) << "Rx resampler failed to initialize";
1880 goto fail;
1881 }
1882
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001883 return true;
Tom Tsoud3253432016-03-06 03:08:01 -08001884
1885fail:
1886 sigProcLibDestroy();
1887 return false;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001888}