blob: b5160ca4f7046437a71e360cf9b918d6ad562a99 [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 Chemerisded68da2015-06-04 15:39:41 -040031#include "Logger.h"
dburgessb3a0ca42011-10-12 07:44:40 +000032
Thomas Tsou3eaae802013-08-20 19:31:14 -040033extern "C" {
34#include "convolve.h"
Thomas Tsou7e4e5362013-10-30 21:18:55 -040035#include "scale.h"
Thomas Tsou0a3dc4c2013-11-09 02:29:55 -050036#include "mult.h"
Thomas Tsou3eaae802013-08-20 19:31:14 -040037}
38
Thomas Tsou7e4e5362013-10-30 21:18:55 -040039using namespace GSM;
40
Thomas Tsouf79c4d02013-11-09 15:51:56 -060041#define TABLESIZE 1024
42#define DELAYFILTS 64
dburgessb3a0ca42011-10-12 07:44:40 +000043
Tom Tsou577cd022015-05-18 13:57:54 -070044/* Clipping detection threshold */
45#define CLIP_THRESH 30000.0f
46
dburgessb3a0ca42011-10-12 07:44:40 +000047/** Lookup tables for trigonometric approximation */
48float cosTable[TABLESIZE+1]; // add 1 element for wrap around
49float sinTable[TABLESIZE+1];
Thomas Tsou0e0e1f42013-11-09 22:08:51 -050050float sincTable[TABLESIZE+1];
dburgessb3a0ca42011-10-12 07:44:40 +000051
52/** Constants */
53static const float M_PI_F = (float)M_PI;
54static const float M_2PI_F = (float)(2.0*M_PI);
55static const float M_1_2PI_F = 1/M_2PI_F;
56
Thomas Tsouc1f7c422013-10-11 13:49:55 -040057/* Precomputed rotation vectors */
58static signalVector *GMSKRotationN = NULL;
59static signalVector *GMSKReverseRotationN = NULL;
60static signalVector *GMSKRotation1 = NULL;
61static signalVector *GMSKReverseRotation1 = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +000062
Thomas Tsouf79c4d02013-11-09 15:51:56 -060063/* Precomputed fractional delay filters */
64static signalVector *delayFilters[DELAYFILTS];
65
Thomas Tsoue5dcfc42013-08-20 16:27:12 -040066/*
Thomas Tsou3eaae802013-08-20 19:31:14 -040067 * RACH and midamble correlation waveforms. Store the buffer separately
68 * because we need to allocate it explicitly outside of the signal vector
69 * constructor. This is because C++ (prior to C++11) is unable to natively
70 * perform 16-byte memory alignment required by many SSE instructions.
Thomas Tsoue5dcfc42013-08-20 16:27:12 -040071 */
72struct CorrelationSequence {
Thomas Tsou3eaae802013-08-20 19:31:14 -040073 CorrelationSequence() : sequence(NULL), buffer(NULL)
Thomas Tsoue5dcfc42013-08-20 16:27:12 -040074 {
75 }
76
77 ~CorrelationSequence()
78 {
79 delete sequence;
Thomas Tsou3eaae802013-08-20 19:31:14 -040080 free(buffer);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -040081 }
82
dburgessb3a0ca42011-10-12 07:44:40 +000083 signalVector *sequence;
Thomas Tsou3eaae802013-08-20 19:31:14 -040084 void *buffer;
Thomas Tsouc1f7c422013-10-11 13:49:55 -040085 float toa;
dburgessb3a0ca42011-10-12 07:44:40 +000086 complex gain;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -040087};
dburgessb3a0ca42011-10-12 07:44:40 +000088
Thomas Tsou83e06892013-08-20 16:10:01 -040089/*
Thomas Tsou3eaae802013-08-20 19:31:14 -040090 * Gaussian and empty modulation pulses. Like the correlation sequences,
91 * store the runtime (Gaussian) buffer separately because of needed alignment
92 * for SSE instructions.
Thomas Tsou83e06892013-08-20 16:10:01 -040093 */
94struct PulseSequence {
Thomas Tsoua57bc8a2013-09-05 08:16:47 +080095 PulseSequence() : c0(NULL), c1(NULL), empty(NULL),
96 c0_buffer(NULL), c1_buffer(NULL)
Thomas Tsou83e06892013-08-20 16:10:01 -040097 {
98 }
99
100 ~PulseSequence()
101 {
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800102 delete c0;
103 delete c1;
Thomas Tsou83e06892013-08-20 16:10:01 -0400104 delete empty;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800105 free(c0_buffer);
106 free(c1_buffer);
Thomas Tsou83e06892013-08-20 16:10:01 -0400107 }
108
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800109 signalVector *c0;
110 signalVector *c1;
Thomas Tsou83e06892013-08-20 16:10:01 -0400111 signalVector *empty;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800112 void *c0_buffer;
113 void *c1_buffer;
Thomas Tsou83e06892013-08-20 16:10:01 -0400114};
115
dburgessb3a0ca42011-10-12 07:44:40 +0000116CorrelationSequence *gMidambles[] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
117CorrelationSequence *gRACHSequence = NULL;
Thomas Tsou83e06892013-08-20 16:10:01 -0400118PulseSequence *GSMPulse = NULL;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400119PulseSequence *GSMPulse1 = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000120
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400121void sigProcLibDestroy()
122{
dburgessb3a0ca42011-10-12 07:44:40 +0000123 for (int i = 0; i < 8; i++) {
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400124 delete gMidambles[i];
125 gMidambles[i] = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000126 }
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400127
Thomas Tsouf79c4d02013-11-09 15:51:56 -0600128 for (int i = 0; i < DELAYFILTS; i++) {
129 delete delayFilters[i];
130 delayFilters[i] = NULL;
131 }
132
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400133 delete GMSKRotationN;
134 delete GMSKReverseRotationN;
135 delete GMSKRotation1;
136 delete GMSKReverseRotation1;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400137 delete gRACHSequence;
138 delete GSMPulse;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400139 delete GSMPulse1;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400140
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400141 GMSKRotationN = NULL;
142 GMSKRotation1 = NULL;
143 GMSKReverseRotationN = NULL;
144 GMSKReverseRotation1 = NULL;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400145 gRACHSequence = NULL;
146 GSMPulse = NULL;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400147 GSMPulse1 = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000148}
149
dburgessb3a0ca42011-10-12 07:44:40 +0000150// dB relative to 1.0.
151// if > 1.0, then return 0 dB
152float dB(float x) {
153
154 float arg = 1.0F;
155 float dB = 0.0F;
156
157 if (x >= 1.0F) return 0.0F;
158 if (x <= 0.0F) return -200.0F;
159
160 float prevArg = arg;
161 float prevdB = dB;
162 float stepSize = 16.0F;
163 float dBstepSize = 12.0F;
164 while (stepSize > 1.0F) {
165 do {
166 prevArg = arg;
167 prevdB = dB;
168 arg /= stepSize;
169 dB -= dBstepSize;
170 } while (arg > x);
171 arg = prevArg;
172 dB = prevdB;
173 stepSize *= 0.5F;
174 dBstepSize -= 3.0F;
175 }
176 return ((arg-x)*(dB-3.0F) + (x-arg*0.5F)*dB)/(arg - arg*0.5F);
177
178}
179
180// 10^(-dB/10), inverse of dB func.
181float dBinv(float x) {
182
183 float arg = 1.0F;
184 float dB = 0.0F;
185
186 if (x >= 0.0F) return 1.0F;
187 if (x <= -200.0F) return 0.0F;
188
189 float prevArg = arg;
190 float prevdB = dB;
191 float stepSize = 16.0F;
192 float dBstepSize = 12.0F;
193 while (stepSize > 1.0F) {
194 do {
195 prevArg = arg;
196 prevdB = dB;
197 arg /= stepSize;
198 dB -= dBstepSize;
199 } while (dB > x);
200 arg = prevArg;
201 dB = prevdB;
202 stepSize *= 0.5F;
203 dBstepSize -= 3.0F;
204 }
205
206 return ((dB-x)*(arg*0.5F)+(x-(dB-3.0F))*(arg))/3.0F;
207
208}
209
210float vectorNorm2(const signalVector &x)
211{
212 signalVector::const_iterator xPtr = x.begin();
213 float Energy = 0.0;
214 for (;xPtr != x.end();xPtr++) {
215 Energy += xPtr->norm2();
216 }
217 return Energy;
218}
219
220
221float vectorPower(const signalVector &x)
222{
223 return vectorNorm2(x)/x.size();
224}
225
226/** compute cosine via lookup table */
227float cosLookup(const float x)
228{
229 float arg = x*M_1_2PI_F;
230 while (arg > 1.0F) arg -= 1.0F;
231 while (arg < 0.0F) arg += 1.0F;
232
233 const float argT = arg*((float)TABLESIZE);
234 const int argI = (int)argT;
235 const float delta = argT-argI;
236 const float iDelta = 1.0F-delta;
237 return iDelta*cosTable[argI] + delta*cosTable[argI+1];
238}
239
240/** compute sine via lookup table */
241float sinLookup(const float x)
242{
243 float arg = x*M_1_2PI_F;
244 while (arg > 1.0F) arg -= 1.0F;
245 while (arg < 0.0F) arg += 1.0F;
246
247 const float argT = arg*((float)TABLESIZE);
248 const int argI = (int)argT;
249 const float delta = argT-argI;
250 const float iDelta = 1.0F-delta;
251 return iDelta*sinTable[argI] + delta*sinTable[argI+1];
252}
253
254
255/** compute e^(-jx) via lookup table. */
256complex expjLookup(float x)
257{
258 float arg = x*M_1_2PI_F;
259 while (arg > 1.0F) arg -= 1.0F;
260 while (arg < 0.0F) arg += 1.0F;
261
262 const float argT = arg*((float)TABLESIZE);
263 const int argI = (int)argT;
264 const float delta = argT-argI;
265 const float iDelta = 1.0F-delta;
266 return complex(iDelta*cosTable[argI] + delta*cosTable[argI+1],
267 iDelta*sinTable[argI] + delta*sinTable[argI+1]);
268}
269
270/** Library setup functions */
271void initTrigTables() {
272 for (int i = 0; i < TABLESIZE+1; i++) {
273 cosTable[i] = cos(2.0*M_PI*i/TABLESIZE);
274 sinTable[i] = sin(2.0*M_PI*i/TABLESIZE);
275 }
276}
277
Thomas Tsoud24cc2c2013-08-20 15:41:45 -0400278void initGMSKRotationTables(int sps)
279{
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400280 GMSKRotationN = new signalVector(157 * sps);
281 GMSKReverseRotationN = new signalVector(157 * sps);
282 signalVector::iterator rotPtr = GMSKRotationN->begin();
283 signalVector::iterator revPtr = GMSKReverseRotationN->begin();
dburgessb3a0ca42011-10-12 07:44:40 +0000284 float phase = 0.0;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400285 while (rotPtr != GMSKRotationN->end()) {
dburgessb3a0ca42011-10-12 07:44:40 +0000286 *rotPtr++ = expjLookup(phase);
287 *revPtr++ = expjLookup(-phase);
Thomas Tsoud24cc2c2013-08-20 15:41:45 -0400288 phase += M_PI_F / 2.0F / (float) sps;
dburgessb3a0ca42011-10-12 07:44:40 +0000289 }
dburgessb3a0ca42011-10-12 07:44:40 +0000290
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400291 GMSKRotation1 = new signalVector(157);
292 GMSKReverseRotation1 = new signalVector(157);
293 rotPtr = GMSKRotation1->begin();
294 revPtr = GMSKReverseRotation1->begin();
295 phase = 0.0;
296 while (rotPtr != GMSKRotation1->end()) {
297 *rotPtr++ = expjLookup(phase);
298 *revPtr++ = expjLookup(-phase);
299 phase += M_PI_F / 2.0F;
Thomas Tsoue57004d2013-08-20 18:55:33 -0400300 }
dburgessb3a0ca42011-10-12 07:44:40 +0000301}
302
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400303static void GMSKRotate(signalVector &x, int sps)
304{
Thomas Tsou0a3dc4c2013-11-09 02:29:55 -0500305#if HAVE_NEON
306 size_t len;
307 signalVector *a, *b, *out;
308
309 a = &x;
310 out = &x;
311 len = out->size();
312
313 if (len == 157)
314 len--;
315
316 if (sps == 1)
317 b = GMSKRotation1;
318 else
319 b = GMSKRotationN;
320
321 mul_complex((float *) out->begin(),
322 (float *) a->begin(),
323 (float *) b->begin(), len);
324#else
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400325 signalVector::iterator rotPtr, xPtr = x.begin();
326
327 if (sps == 1)
328 rotPtr = GMSKRotation1->begin();
329 else
330 rotPtr = GMSKRotationN->begin();
331
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500332 if (x.isReal()) {
dburgessb3a0ca42011-10-12 07:44:40 +0000333 while (xPtr < x.end()) {
334 *xPtr = *rotPtr++ * (xPtr->real());
335 xPtr++;
336 }
337 }
338 else {
339 while (xPtr < x.end()) {
340 *xPtr = *rotPtr++ * (*xPtr);
341 xPtr++;
342 }
343 }
Thomas Tsou0a3dc4c2013-11-09 02:29:55 -0500344#endif
dburgessb3a0ca42011-10-12 07:44:40 +0000345}
346
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400347static void GMSKReverseRotate(signalVector &x, int sps)
348{
349 signalVector::iterator rotPtr, xPtr= x.begin();
350
351 if (sps == 1)
352 rotPtr = GMSKReverseRotation1->begin();
353 else
354 rotPtr = GMSKReverseRotationN->begin();
355
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500356 if (x.isReal()) {
dburgessb3a0ca42011-10-12 07:44:40 +0000357 while (xPtr < x.end()) {
358 *xPtr = *rotPtr++ * (xPtr->real());
359 xPtr++;
360 }
361 }
362 else {
363 while (xPtr < x.end()) {
364 *xPtr = *rotPtr++ * (*xPtr);
365 xPtr++;
366 }
367 }
368}
369
Thomas Tsou3eaae802013-08-20 19:31:14 -0400370signalVector *convolve(const signalVector *x,
371 const signalVector *h,
372 signalVector *y,
Thomas Tsou3f32ab52013-11-15 16:32:54 -0500373 ConvType spanType, size_t start,
374 size_t len, size_t step, int offset)
dburgessb3a0ca42011-10-12 07:44:40 +0000375{
Thomas Tsou3f32ab52013-11-15 16:32:54 -0500376 int rc;
377 size_t head = 0, tail = 0;
Thomas Tsou3eaae802013-08-20 19:31:14 -0400378 bool alloc = false, append = false;
379 const signalVector *_x = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000380
Thomas Tsou3eaae802013-08-20 19:31:14 -0400381 if (!x || !h)
dburgessb3a0ca42011-10-12 07:44:40 +0000382 return NULL;
383
Thomas Tsou3eaae802013-08-20 19:31:14 -0400384 switch (spanType) {
385 case START_ONLY:
386 start = 0;
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500387 head = h->size() - 1;
Thomas Tsou3eaae802013-08-20 19:31:14 -0400388 len = x->size();
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500389
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500390 if (x->getStart() < head)
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500391 append = true;
dburgessb3a0ca42011-10-12 07:44:40 +0000392 break;
Thomas Tsou3eaae802013-08-20 19:31:14 -0400393 case NO_DELAY:
394 start = h->size() / 2;
395 head = start;
396 tail = start;
397 len = x->size();
398 append = true;
399 break;
400 case CUSTOM:
401 if (start < h->size() - 1) {
402 head = h->size() - start;
403 append = true;
404 }
405 if (start + len > x->size()) {
406 tail = start + len - x->size();
407 append = true;
dburgessb3a0ca42011-10-12 07:44:40 +0000408 }
409 break;
410 default:
411 return NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000412 }
dburgessb3a0ca42011-10-12 07:44:40 +0000413
Thomas Tsou3eaae802013-08-20 19:31:14 -0400414 /*
415 * Error if the output vector is too small. Create the output vector
416 * if the pointer is NULL.
417 */
418 if (y && (len > y->size()))
419 return NULL;
420 if (!y) {
421 y = new signalVector(len);
422 alloc = true;
423 }
424
425 /* Prepend or post-pend the input vector if the parameters require it */
426 if (append)
427 _x = new signalVector(*x, head, tail);
428 else
429 _x = x;
430
431 /*
432 * Four convovle types:
433 * 1. Complex-Real (aligned)
434 * 2. Complex-Complex (aligned)
435 * 3. Complex-Real (!aligned)
436 * 4. Complex-Complex (!aligned)
437 */
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500438 if (h->isReal() && h->isAligned()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -0400439 rc = convolve_real((float *) _x->begin(), _x->size(),
440 (float *) h->begin(), h->size(),
441 (float *) y->begin(), y->size(),
442 start, len, step, offset);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500443 } else if (!h->isReal() && h->isAligned()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -0400444 rc = convolve_complex((float *) _x->begin(), _x->size(),
445 (float *) h->begin(), h->size(),
446 (float *) y->begin(), y->size(),
447 start, len, step, offset);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500448 } else if (h->isReal() && !h->isAligned()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -0400449 rc = base_convolve_real((float *) _x->begin(), _x->size(),
450 (float *) h->begin(), h->size(),
451 (float *) y->begin(), y->size(),
452 start, len, step, offset);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500453 } else if (!h->isReal() && !h->isAligned()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -0400454 rc = base_convolve_complex((float *) _x->begin(), _x->size(),
455 (float *) h->begin(), h->size(),
456 (float *) y->begin(), y->size(),
457 start, len, step, offset);
458 } else {
459 rc = -1;
460 }
461
462 if (append)
463 delete _x;
464
465 if (rc < 0) {
466 if (alloc)
467 delete y;
468 return NULL;
469 }
470
471 return y;
472}
dburgessb3a0ca42011-10-12 07:44:40 +0000473
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400474static bool generateC1Pulse(int sps, PulseSequence *pulse)
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800475{
476 int len;
477
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400478 if (!pulse)
479 return false;
480
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800481 switch (sps) {
482 case 4:
483 len = 8;
484 break;
485 default:
486 return false;
487 }
488
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400489 pulse->c1_buffer = convolve_h_alloc(len);
490 pulse->c1 = new signalVector((complex *)
491 pulse->c1_buffer, 0, len);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500492 pulse->c1->isReal(true);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800493
494 /* Enable alignment for SSE usage */
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400495 pulse->c1->setAligned(true);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800496
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400497 signalVector::iterator xP = pulse->c1->begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800498
499 switch (sps) {
500 case 4:
501 /* BT = 0.30 */
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400502 *xP++ = 0.0;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800503 *xP++ = 8.16373112e-03;
504 *xP++ = 2.84385729e-02;
505 *xP++ = 5.64158904e-02;
506 *xP++ = 7.05463553e-02;
507 *xP++ = 5.64158904e-02;
508 *xP++ = 2.84385729e-02;
509 *xP++ = 8.16373112e-03;
510 }
511
512 return true;
513}
514
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400515static PulseSequence *generateGSMPulse(int sps, int symbolLength)
dburgessb3a0ca42011-10-12 07:44:40 +0000516{
Thomas Tsou83e06892013-08-20 16:10:01 -0400517 int len;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800518 float arg, avg, center;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400519 PulseSequence *pulse;
Thomas Tsou83e06892013-08-20 16:10:01 -0400520
521 /* Store a single tap filter used for correlation sequence generation */
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400522 pulse = new PulseSequence();
523 pulse->empty = new signalVector(1);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500524 pulse->empty->isReal(true);
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400525 *(pulse->empty->begin()) = 1.0f;
Thomas Tsou83e06892013-08-20 16:10:01 -0400526
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400527 /*
528 * For 4 samples-per-symbol use a precomputed single pulse Laurent
529 * approximation. This should yields below 2 degrees of phase error at
530 * the modulator output. Use the existing pulse approximation for all
531 * other oversampling factors.
532 */
533 switch (sps) {
534 case 4:
535 len = 16;
536 break;
537 default:
538 len = sps * symbolLength;
539 if (len < 4)
540 len = 4;
541 }
Thomas Tsou3eaae802013-08-20 19:31:14 -0400542
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400543 pulse->c0_buffer = convolve_h_alloc(len);
544 pulse->c0 = new signalVector((complex *) pulse->c0_buffer, 0, len);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500545 pulse->c0->isReal(true);
Thomas Tsou3eaae802013-08-20 19:31:14 -0400546
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800547 /* Enable alingnment for SSE usage */
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400548 pulse->c0->setAligned(true);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800549
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400550 signalVector::iterator xP = pulse->c0->begin();
Thomas Tsou83e06892013-08-20 16:10:01 -0400551
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400552 if (sps == 4) {
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800553 *xP++ = 0.0;
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400554 *xP++ = 4.46348606e-03;
555 *xP++ = 2.84385729e-02;
556 *xP++ = 1.03184855e-01;
557 *xP++ = 2.56065552e-01;
558 *xP++ = 4.76375085e-01;
559 *xP++ = 7.05961177e-01;
560 *xP++ = 8.71291644e-01;
561 *xP++ = 9.29453645e-01;
562 *xP++ = 8.71291644e-01;
563 *xP++ = 7.05961177e-01;
564 *xP++ = 4.76375085e-01;
565 *xP++ = 2.56065552e-01;
566 *xP++ = 1.03184855e-01;
567 *xP++ = 2.84385729e-02;
568 *xP++ = 4.46348606e-03;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400569 generateC1Pulse(sps, pulse);
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400570 } else {
571 center = (float) (len - 1.0) / 2.0;
Thomas Tsou83e06892013-08-20 16:10:01 -0400572
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400573 /* GSM pulse approximation */
574 for (int i = 0; i < len; i++) {
575 arg = ((float) i - center) / (float) sps;
576 *xP++ = 0.96 * exp(-1.1380 * arg * arg -
577 0.527 * arg * arg * arg * arg);
578 }
dburgessb3a0ca42011-10-12 07:44:40 +0000579
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400580 avg = sqrtf(vectorNorm2(*pulse->c0) / sps);
581 xP = pulse->c0->begin();
582 for (int i = 0; i < len; i++)
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800583 *xP++ /= avg;
584 }
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400585
586 return pulse;
dburgessb3a0ca42011-10-12 07:44:40 +0000587}
588
589signalVector* frequencyShift(signalVector *y,
590 signalVector *x,
591 float freq,
592 float startPhase,
593 float *finalPhase)
594{
595
596 if (!x) return NULL;
597
598 if (y==NULL) {
599 y = new signalVector(x->size());
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500600 y->isReal(x->isReal());
dburgessb3a0ca42011-10-12 07:44:40 +0000601 if (y==NULL) return NULL;
602 }
603
604 if (y->size() < x->size()) return NULL;
605
606 float phase = startPhase;
607 signalVector::iterator yP = y->begin();
608 signalVector::iterator xPEnd = x->end();
609 signalVector::iterator xP = x->begin();
610
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500611 if (x->isReal()) {
dburgessb3a0ca42011-10-12 07:44:40 +0000612 while (xP < xPEnd) {
613 (*yP++) = expjLookup(phase)*( (xP++)->real() );
614 phase += freq;
615 }
616 }
617 else {
618 while (xP < xPEnd) {
619 (*yP++) = (*xP++)*expjLookup(phase);
620 phase += freq;
Thomas Tsou34bbef72013-11-13 22:58:15 -0500621 if (phase > 2 * M_PI)
622 phase -= 2 * M_PI;
623 else if (phase < -2 * M_PI)
624 phase += 2 * M_PI;
dburgessb3a0ca42011-10-12 07:44:40 +0000625 }
626 }
627
628
629 if (finalPhase) *finalPhase = phase;
630
631 return y;
632}
633
634signalVector* reverseConjugate(signalVector *b)
635{
636 signalVector *tmp = new signalVector(b->size());
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500637 tmp->isReal(b->isReal());
dburgessb3a0ca42011-10-12 07:44:40 +0000638 signalVector::iterator bP = b->begin();
639 signalVector::iterator bPEnd = b->end();
640 signalVector::iterator tmpP = tmp->end()-1;
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500641 if (!b->isReal()) {
dburgessb3a0ca42011-10-12 07:44:40 +0000642 while (bP < bPEnd) {
643 *tmpP-- = bP->conj();
644 bP++;
645 }
646 }
647 else {
648 while (bP < bPEnd) {
649 *tmpP-- = bP->real();
650 bP++;
651 }
652 }
653
654 return tmp;
655}
656
dburgessb3a0ca42011-10-12 07:44:40 +0000657/* soft output slicer */
658bool vectorSlicer(signalVector *x)
659{
660
661 signalVector::iterator xP = x->begin();
662 signalVector::iterator xPEnd = x->end();
663 while (xP < xPEnd) {
664 *xP = (complex) (0.5*(xP->real()+1.0F));
665 if (xP->real() > 1.0) *xP = 1.0;
666 if (xP->real() < 0.0) *xP = 0.0;
667 xP++;
668 }
669 return true;
670}
Thomas Tsou3eaae802013-08-20 19:31:14 -0400671
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800672static signalVector *rotateBurst(const BitVector &wBurst,
673 int guardPeriodLength, int sps)
674{
675 int burst_len;
676 signalVector *pulse, rotated, *shaped;
677 signalVector::iterator itr;
678
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400679 pulse = GSMPulse1->empty;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800680 burst_len = sps * (wBurst.size() + guardPeriodLength);
681 rotated = signalVector(burst_len);
682 itr = rotated.begin();
683
684 for (unsigned i = 0; i < wBurst.size(); i++) {
685 *itr = 2.0 * (wBurst[i] & 0x01) - 1.0;
686 itr += sps;
687 }
688
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400689 GMSKRotate(rotated, sps);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500690 rotated.isReal(false);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800691
692 /* Dummy filter operation */
693 shaped = convolve(&rotated, pulse, NULL, START_ONLY);
694 if (!shaped)
695 return NULL;
696
697 return shaped;
698}
699
700static signalVector *modulateBurstLaurent(const BitVector &bits,
701 int guard_len, int sps)
702{
703 int burst_len;
704 float phase;
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500705 signalVector *c0_pulse, *c1_pulse, *c0_burst;
706 signalVector *c1_burst, *c0_shaped, *c1_shaped;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800707 signalVector::iterator c0_itr, c1_itr;
708
709 /*
710 * Apply before and after bits to reduce phase error at burst edges.
711 * Make sure there is enough room in the burst to accomodate all bits.
712 */
713 if (guard_len < 4)
714 guard_len = 4;
715
716 c0_pulse = GSMPulse->c0;
717 c1_pulse = GSMPulse->c1;
718
719 burst_len = sps * (bits.size() + guard_len);
720
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500721 c0_burst = new signalVector(burst_len, c0_pulse->size());
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500722 c0_burst->isReal(true);
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500723 c0_itr = c0_burst->begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800724
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500725 c1_burst = new signalVector(burst_len, c1_pulse->size());
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500726 c1_burst->isReal(true);
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500727 c1_itr = c1_burst->begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800728
729 /* Padded differential start bits */
Alexander Chemeris351fd762015-05-24 20:16:51 -0400730 *c0_itr = 2.0 * (0x01 & 0x01) - 1.0;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800731 c0_itr += sps;
732
733 /* Main burst bits */
734 for (unsigned i = 0; i < bits.size(); i++) {
735 *c0_itr = 2.0 * (bits[i] & 0x01) - 1.0;
736 c0_itr += sps;
737 }
738
739 /* Padded differential end bits */
740 *c0_itr = 2.0 * (0x01 & 0x01) - 1.0;
741
742 /* Generate C0 phase coefficients */
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500743 GMSKRotate(*c0_burst, sps);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500744 c0_burst->isReal(false);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800745
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500746 c0_itr = c0_burst->begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800747 c0_itr += sps * 2;
748 c1_itr += sps * 2;
749
750 /* Start magic */
751 phase = 2.0 * ((0x01 & 0x01) ^ (0x01 & 0x01)) - 1.0;
752 *c1_itr = *c0_itr * Complex<float>(0, phase);
753 c0_itr += sps;
754 c1_itr += sps;
755
756 /* Generate C1 phase coefficients */
757 for (unsigned i = 2; i < bits.size(); i++) {
758 phase = 2.0 * ((bits[i - 1] & 0x01) ^ (bits[i - 2] & 0x01)) - 1.0;
759 *c1_itr = *c0_itr * Complex<float>(0, phase);
760
761 c0_itr += sps;
762 c1_itr += sps;
763 }
764
765 /* End magic */
766 int i = bits.size();
767 phase = 2.0 * ((bits[i-1] & 0x01) ^ (bits[i-2] & 0x01)) - 1.0;
768 *c1_itr = *c0_itr * Complex<float>(0, phase);
769
770 /* Primary (C0) and secondary (C1) pulse shaping */
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500771 c0_shaped = convolve(c0_burst, c0_pulse, NULL, START_ONLY);
772 c1_shaped = convolve(c1_burst, c1_pulse, NULL, START_ONLY);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800773
774 /* Sum shaped outputs into C0 */
775 c0_itr = c0_shaped->begin();
776 c1_itr = c1_shaped->begin();
777 for (unsigned i = 0; i < c0_shaped->size(); i++ )
778 *c0_itr++ += *c1_itr++;
779
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500780 delete c0_burst;
781 delete c1_burst;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800782 delete c1_shaped;
783
784 return c0_shaped;
785}
786
787static signalVector *modulateBurstBasic(const BitVector &bits,
788 int guard_len, int sps)
789{
790 int burst_len;
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500791 signalVector *pulse, *burst, *shaped;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800792 signalVector::iterator burst_itr;
793
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400794 if (sps == 1)
795 pulse = GSMPulse1->c0;
796 else
797 pulse = GSMPulse->c0;
798
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800799 burst_len = sps * (bits.size() + guard_len);
800
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500801 burst = new signalVector(burst_len, pulse->size());
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500802 burst->isReal(true);
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500803 burst_itr = burst->begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800804
805 /* Raw bits are not differentially encoded */
806 for (unsigned i = 0; i < bits.size(); i++) {
807 *burst_itr = 2.0 * (bits[i] & 0x01) - 1.0;
808 burst_itr += sps;
809 }
810
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500811 GMSKRotate(*burst, sps);
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500812 burst->isReal(false);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800813
814 /* Single Gaussian pulse approximation shaping */
Thomas Tsou6f4906e2013-11-09 02:40:18 -0500815 shaped = convolve(burst, pulse, NULL, START_ONLY);
816
817 delete burst;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800818
819 return shaped;
820}
821
Thomas Tsou3eaae802013-08-20 19:31:14 -0400822/* Assume input bits are not differentially encoded */
Thomas Tsou83e06892013-08-20 16:10:01 -0400823signalVector *modulateBurst(const BitVector &wBurst, int guardPeriodLength,
824 int sps, bool emptyPulse)
dburgessb3a0ca42011-10-12 07:44:40 +0000825{
Thomas Tsou83e06892013-08-20 16:10:01 -0400826 if (emptyPulse)
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800827 return rotateBurst(wBurst, guardPeriodLength, sps);
828 else if (sps == 4)
829 return modulateBurstLaurent(wBurst, guardPeriodLength, sps);
Thomas Tsou83e06892013-08-20 16:10:01 -0400830 else
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800831 return modulateBurstBasic(wBurst, guardPeriodLength, sps);
dburgessb3a0ca42011-10-12 07:44:40 +0000832}
833
Thomas Tsou0e0e1f42013-11-09 22:08:51 -0500834void generateSincTable()
835{
836 float x;
837
838 for (int i = 0; i < TABLESIZE; i++) {
839 x = (float) i / TABLESIZE * 8 * M_PI;
840 if (fabs(x) < 0.01) {
841 sincTable[i] = 1.0f;
842 continue;
843 }
844
845 sincTable[i] = sinf(x) / x;
846 }
847}
848
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800849float sinc(float x)
dburgessb3a0ca42011-10-12 07:44:40 +0000850{
Thomas Tsou0e0e1f42013-11-09 22:08:51 -0500851 if (fabs(x) >= 8 * M_PI)
852 return 0.0;
853
854 int index = (int) floorf(fabs(x) / (8 * M_PI) * TABLESIZE);
855
856 return sincTable[index];
dburgessb3a0ca42011-10-12 07:44:40 +0000857}
858
Thomas Tsouf79c4d02013-11-09 15:51:56 -0600859/*
860 * Create fractional delay filterbank with Blackman-harris windowed
861 * sinc function generator. The number of filters generated is specified
862 * by the DELAYFILTS value.
863 */
864void generateDelayFilters()
865{
866 int h_len = 20;
867 complex *data;
868 signalVector *h;
869 signalVector::iterator itr;
870
871 float k, sum;
872 float a0 = 0.35875;
873 float a1 = 0.48829;
874 float a2 = 0.14128;
875 float a3 = 0.01168;
876
877 for (int i = 0; i < DELAYFILTS; i++) {
878 data = (complex *) convolve_h_alloc(h_len);
879 h = new signalVector(data, 0, h_len);
880 h->setAligned(true);
881 h->isReal(true);
882
883 sum = 0.0;
884 itr = h->end();
885 for (int n = 0; n < h_len; n++) {
886 k = (float) n;
887 *--itr = (complex) sinc(M_PI_F *
888 (k - (float) h_len / 2.0 - (float) i / DELAYFILTS));
889 *itr *= a0 -
890 a1 * cos(2 * M_PI * n / (h_len - 1)) +
891 a2 * cos(4 * M_PI * n / (h_len - 1)) -
892 a3 * cos(6 * M_PI * n / (h_len - 1));
893
894 sum += itr->real();
895 }
896
897 itr = h->begin();
898 for (int n = 0; n < h_len; n++)
899 *itr++ /= sum;
900
901 delayFilters[i] = h;
902 }
903}
904
Thomas Tsou94edaae2013-11-09 22:19:19 -0500905signalVector *delayVector(signalVector *in, signalVector *out, float delay)
dburgessb3a0ca42011-10-12 07:44:40 +0000906{
Thomas Tsouf79c4d02013-11-09 15:51:56 -0600907 int whole, index;
Thomas Tsou2c282f52013-10-08 21:34:35 -0400908 float frac;
Thomas Tsou94edaae2013-11-09 22:19:19 -0500909 signalVector *h, *shift, *fshift = NULL;
Thomas Tsou3eaae802013-08-20 19:31:14 -0400910
Thomas Tsou2c282f52013-10-08 21:34:35 -0400911 whole = floor(delay);
912 frac = delay - whole;
913
914 /* Sinc interpolated fractional shift (if allowable) */
915 if (fabs(frac) > 1e-2) {
Thomas Tsouf79c4d02013-11-09 15:51:56 -0600916 index = floorf(frac * (float) DELAYFILTS);
917 h = delayFilters[index];
Thomas Tsou2c282f52013-10-08 21:34:35 -0400918
Thomas Tsou94edaae2013-11-09 22:19:19 -0500919 fshift = convolve(in, h, NULL, NO_DELAY);
920 if (!fshift)
921 return NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000922 }
923
Thomas Tsou94edaae2013-11-09 22:19:19 -0500924 if (!fshift)
925 shift = new signalVector(*in);
926 else
927 shift = fshift;
928
Thomas Tsou2c282f52013-10-08 21:34:35 -0400929 /* Integer sample shift */
930 if (whole < 0) {
931 whole = -whole;
Thomas Tsou94edaae2013-11-09 22:19:19 -0500932 signalVector::iterator wBurstItr = shift->begin();
933 signalVector::iterator shiftedItr = shift->begin() + whole;
Thomas Tsou2c282f52013-10-08 21:34:35 -0400934
Thomas Tsou94edaae2013-11-09 22:19:19 -0500935 while (shiftedItr < shift->end())
dburgessb3a0ca42011-10-12 07:44:40 +0000936 *wBurstItr++ = *shiftedItr++;
Thomas Tsou2c282f52013-10-08 21:34:35 -0400937
Thomas Tsou94edaae2013-11-09 22:19:19 -0500938 while (wBurstItr < shift->end())
939 *wBurstItr++ = 0.0;
940 } else if (whole >= 0) {
941 signalVector::iterator wBurstItr = shift->end() - 1;
942 signalVector::iterator shiftedItr = shift->end() - 1 - whole;
943
944 while (shiftedItr >= shift->begin())
dburgessb3a0ca42011-10-12 07:44:40 +0000945 *wBurstItr-- = *shiftedItr--;
Thomas Tsou94edaae2013-11-09 22:19:19 -0500946
947 while (wBurstItr >= shift->begin())
dburgessb3a0ca42011-10-12 07:44:40 +0000948 *wBurstItr-- = 0.0;
949 }
Thomas Tsou2c282f52013-10-08 21:34:35 -0400950
Thomas Tsou94edaae2013-11-09 22:19:19 -0500951 if (!out)
952 return shift;
953
954 out->clone(*shift);
955 delete shift;
956 return out;
dburgessb3a0ca42011-10-12 07:44:40 +0000957}
Thomas Tsou2c282f52013-10-08 21:34:35 -0400958
dburgessb3a0ca42011-10-12 07:44:40 +0000959signalVector *gaussianNoise(int length,
960 float variance,
961 complex mean)
962{
963
964 signalVector *noise = new signalVector(length);
965 signalVector::iterator nPtr = noise->begin();
966 float stddev = sqrtf(variance);
967 while (nPtr < noise->end()) {
968 float u1 = (float) rand()/ (float) RAND_MAX;
969 while (u1==0.0)
970 u1 = (float) rand()/ (float) RAND_MAX;
971 float u2 = (float) rand()/ (float) RAND_MAX;
972 float arg = 2.0*M_PI*u2;
973 *nPtr = mean + stddev*complex(cos(arg),sin(arg))*sqrtf(-2.0*log(u1));
974 nPtr++;
975 }
976
977 return noise;
978}
979
980complex interpolatePoint(const signalVector &inSig,
981 float ix)
982{
983
984 int start = (int) (floor(ix) - 10);
985 if (start < 0) start = 0;
986 int end = (int) (floor(ix) + 11);
987 if ((unsigned) end > inSig.size()-1) end = inSig.size()-1;
988
989 complex pVal = 0.0;
Thomas Tsou20eb6d62013-11-09 14:30:41 -0500990 if (!inSig.isReal()) {
dburgessb3a0ca42011-10-12 07:44:40 +0000991 for (int i = start; i < end; i++)
992 pVal += inSig[i] * sinc(M_PI_F*(i-ix));
993 }
994 else {
995 for (int i = start; i < end; i++)
996 pVal += inSig[i].real() * sinc(M_PI_F*(i-ix));
997 }
998
999 return pVal;
1000}
1001
Thomas Tsou8181b012013-08-20 21:17:19 -04001002static complex fastPeakDetect(const signalVector &rxBurst, float *index)
1003{
1004 float val, max = 0.0f;
1005 complex amp;
1006 int _index = -1;
1007
Thomas Tsou3f32ab52013-11-15 16:32:54 -05001008 for (size_t i = 0; i < rxBurst.size(); i++) {
Thomas Tsou8181b012013-08-20 21:17:19 -04001009 val = rxBurst[i].norm2();
1010 if (val > max) {
1011 max = val;
1012 _index = i;
1013 amp = rxBurst[i];
1014 }
1015 }
1016
1017 if (index)
1018 *index = (float) _index;
1019
1020 return amp;
1021}
1022
dburgessb3a0ca42011-10-12 07:44:40 +00001023complex peakDetect(const signalVector &rxBurst,
1024 float *peakIndex,
1025 float *avgPwr)
1026{
1027
1028
1029 complex maxVal = 0.0;
1030 float maxIndex = -1;
1031 float sumPower = 0.0;
1032
1033 for (unsigned int i = 0; i < rxBurst.size(); i++) {
1034 float samplePower = rxBurst[i].norm2();
1035 if (samplePower > maxVal.real()) {
1036 maxVal = samplePower;
1037 maxIndex = i;
1038 }
1039 sumPower += samplePower;
1040 }
1041
1042 // interpolate around the peak
1043 // to save computation, we'll use early-late balancing
1044 float earlyIndex = maxIndex-1;
1045 float lateIndex = maxIndex+1;
1046
1047 float incr = 0.5;
1048 while (incr > 1.0/1024.0) {
1049 complex earlyP = interpolatePoint(rxBurst,earlyIndex);
1050 complex lateP = interpolatePoint(rxBurst,lateIndex);
1051 if (earlyP < lateP)
1052 earlyIndex += incr;
1053 else if (earlyP > lateP)
1054 earlyIndex -= incr;
1055 else break;
1056 incr /= 2.0;
1057 lateIndex = earlyIndex + 2.0;
1058 }
1059
1060 maxIndex = earlyIndex + 1.0;
1061 maxVal = interpolatePoint(rxBurst,maxIndex);
1062
1063 if (peakIndex!=NULL)
1064 *peakIndex = maxIndex;
1065
1066 if (avgPwr!=NULL)
1067 *avgPwr = (sumPower-maxVal.norm2()) / (rxBurst.size()-1);
1068
1069 return maxVal;
1070
1071}
1072
1073void scaleVector(signalVector &x,
1074 complex scale)
1075{
Thomas Tsou7e4e5362013-10-30 21:18:55 -04001076#ifdef HAVE_NEON
1077 int len = x.size();
1078
1079 scale_complex((float *) x.begin(),
1080 (float *) x.begin(),
1081 (float *) &scale, len);
1082#else
dburgessb3a0ca42011-10-12 07:44:40 +00001083 signalVector::iterator xP = x.begin();
1084 signalVector::iterator xPEnd = x.end();
Thomas Tsou20eb6d62013-11-09 14:30:41 -05001085 if (!x.isReal()) {
dburgessb3a0ca42011-10-12 07:44:40 +00001086 while (xP < xPEnd) {
1087 *xP = *xP * scale;
1088 xP++;
1089 }
1090 }
1091 else {
1092 while (xP < xPEnd) {
1093 *xP = xP->real() * scale;
1094 xP++;
1095 }
1096 }
Thomas Tsou7e4e5362013-10-30 21:18:55 -04001097#endif
dburgessb3a0ca42011-10-12 07:44:40 +00001098}
1099
1100/** in-place conjugation */
1101void conjugateVector(signalVector &x)
1102{
Thomas Tsou20eb6d62013-11-09 14:30:41 -05001103 if (x.isReal()) return;
dburgessb3a0ca42011-10-12 07:44:40 +00001104 signalVector::iterator xP = x.begin();
1105 signalVector::iterator xPEnd = x.end();
1106 while (xP < xPEnd) {
1107 *xP = xP->conj();
1108 xP++;
1109 }
1110}
1111
1112
1113// in-place addition!!
1114bool addVector(signalVector &x,
1115 signalVector &y)
1116{
1117 signalVector::iterator xP = x.begin();
1118 signalVector::iterator yP = y.begin();
1119 signalVector::iterator xPEnd = x.end();
1120 signalVector::iterator yPEnd = y.end();
1121 while ((xP < xPEnd) && (yP < yPEnd)) {
1122 *xP = *xP + *yP;
1123 xP++; yP++;
1124 }
1125 return true;
1126}
1127
1128// in-place multiplication!!
1129bool multVector(signalVector &x,
1130 signalVector &y)
1131{
1132 signalVector::iterator xP = x.begin();
1133 signalVector::iterator yP = y.begin();
1134 signalVector::iterator xPEnd = x.end();
1135 signalVector::iterator yPEnd = y.end();
1136 while ((xP < xPEnd) && (yP < yPEnd)) {
1137 *xP = (*xP) * (*yP);
1138 xP++; yP++;
1139 }
1140 return true;
1141}
1142
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001143bool generateMidamble(int sps, int tsc)
dburgessb3a0ca42011-10-12 07:44:40 +00001144{
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001145 bool status = true;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001146 float toa;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001147 complex *data = NULL;
1148 signalVector *autocorr = NULL, *midamble = NULL;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001149 signalVector *midMidamble = NULL, *_midMidamble = NULL;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001150
Thomas Tsou3eaae802013-08-20 19:31:14 -04001151 if ((tsc < 0) || (tsc > 7))
dburgessb3a0ca42011-10-12 07:44:40 +00001152 return false;
1153
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001154 delete gMidambles[tsc];
Thomas Tsou3eaae802013-08-20 19:31:14 -04001155
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001156 /* Use middle 16 bits of each TSC. Correlation sequence is not pulse shaped */
1157 midMidamble = modulateBurst(gTrainingSequence[tsc].segment(5,16), 0, sps, true);
1158 if (!midMidamble)
1159 return false;
1160
Thomas Tsou3eaae802013-08-20 19:31:14 -04001161 /* Simulated receive sequence is pulse shaped */
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001162 midamble = modulateBurst(gTrainingSequence[tsc], 0, sps, false);
1163 if (!midamble) {
1164 status = false;
1165 goto release;
dburgessb3a0ca42011-10-12 07:44:40 +00001166 }
Thomas Tsou3eaae802013-08-20 19:31:14 -04001167
dburgessb3a0ca42011-10-12 07:44:40 +00001168 // NOTE: Because ideal TSC 16-bit midamble is 66 symbols into burst,
1169 // the ideal TSC has an + 180 degree phase shift,
1170 // due to the pi/2 frequency shift, that
1171 // needs to be accounted for.
1172 // 26-midamble is 61 symbols into burst, has +90 degree phase shift.
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001173 scaleVector(*midMidamble, complex(-1.0, 0.0));
1174 scaleVector(*midamble, complex(0.0, 1.0));
dburgessb3a0ca42011-10-12 07:44:40 +00001175
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001176 conjugateVector(*midMidamble);
dburgessb3a0ca42011-10-12 07:44:40 +00001177
Thomas Tsou3eaae802013-08-20 19:31:14 -04001178 /* For SSE alignment, reallocate the midamble sequence on 16-byte boundary */
1179 data = (complex *) convolve_h_alloc(midMidamble->size());
1180 _midMidamble = new signalVector(data, 0, midMidamble->size());
1181 _midMidamble->setAligned(true);
1182 memcpy(_midMidamble->begin(), midMidamble->begin(),
1183 midMidamble->size() * sizeof(complex));
1184
1185 autocorr = convolve(midamble, _midMidamble, NULL, NO_DELAY);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001186 if (!autocorr) {
1187 status = false;
1188 goto release;
1189 }
dburgessb3a0ca42011-10-12 07:44:40 +00001190
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001191 gMidambles[tsc] = new CorrelationSequence;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001192 gMidambles[tsc]->buffer = data;
1193 gMidambles[tsc]->sequence = _midMidamble;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001194 gMidambles[tsc]->gain = peakDetect(*autocorr, &toa, NULL);
1195
1196 /* For 1 sps only
1197 * (Half of correlation length - 1) + midpoint of pulse shape + remainder
1198 * 13.5 = (16 / 2 - 1) + 1.5 + (26 - 10) / 2
1199 */
1200 if (sps == 1)
1201 gMidambles[tsc]->toa = toa - 13.5;
1202 else
1203 gMidambles[tsc]->toa = 0;
dburgessb3a0ca42011-10-12 07:44:40 +00001204
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001205release:
dburgessb3a0ca42011-10-12 07:44:40 +00001206 delete autocorr;
1207 delete midamble;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001208 delete midMidamble;
dburgessb3a0ca42011-10-12 07:44:40 +00001209
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001210 if (!status) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001211 delete _midMidamble;
1212 free(data);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001213 gMidambles[tsc] = NULL;
1214 }
1215
1216 return status;
dburgessb3a0ca42011-10-12 07:44:40 +00001217}
1218
Thomas Tsou83e06892013-08-20 16:10:01 -04001219bool generateRACHSequence(int sps)
dburgessb3a0ca42011-10-12 07:44:40 +00001220{
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001221 bool status = true;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001222 float toa;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001223 complex *data = NULL;
1224 signalVector *autocorr = NULL;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001225 signalVector *seq0 = NULL, *seq1 = NULL, *_seq1 = NULL;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001226
1227 delete gRACHSequence;
1228
1229 seq0 = modulateBurst(gRACHSynchSequence, 0, sps, false);
1230 if (!seq0)
1231 return false;
1232
1233 seq1 = modulateBurst(gRACHSynchSequence.segment(0, 40), 0, sps, true);
1234 if (!seq1) {
1235 status = false;
1236 goto release;
dburgessb3a0ca42011-10-12 07:44:40 +00001237 }
1238
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001239 conjugateVector(*seq1);
dburgessb3a0ca42011-10-12 07:44:40 +00001240
Thomas Tsou3eaae802013-08-20 19:31:14 -04001241 /* For SSE alignment, reallocate the midamble sequence on 16-byte boundary */
1242 data = (complex *) convolve_h_alloc(seq1->size());
1243 _seq1 = new signalVector(data, 0, seq1->size());
1244 _seq1->setAligned(true);
1245 memcpy(_seq1->begin(), seq1->begin(), seq1->size() * sizeof(complex));
1246
1247 autocorr = convolve(seq0, _seq1, autocorr, NO_DELAY);
1248 if (!autocorr) {
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001249 status = false;
1250 goto release;
1251 }
dburgessb3a0ca42011-10-12 07:44:40 +00001252
1253 gRACHSequence = new CorrelationSequence;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001254 gRACHSequence->sequence = _seq1;
1255 gRACHSequence->buffer = data;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001256 gRACHSequence->gain = peakDetect(*autocorr, &toa, NULL);
1257
1258 /* For 1 sps only
1259 * (Half of correlation length - 1) + midpoint of pulse shaping filer
1260 * 20.5 = (40 / 2 - 1) + 1.5
1261 */
1262 if (sps == 1)
1263 gRACHSequence->toa = toa - 20.5;
1264 else
1265 gRACHSequence->toa = 0.0;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001266
1267release:
dburgessb3a0ca42011-10-12 07:44:40 +00001268 delete autocorr;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001269 delete seq0;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001270 delete seq1;
dburgessb3a0ca42011-10-12 07:44:40 +00001271
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001272 if (!status) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001273 delete _seq1;
1274 free(data);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001275 gRACHSequence = NULL;
1276 }
dburgessb3a0ca42011-10-12 07:44:40 +00001277
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001278 return status;
dburgessb3a0ca42011-10-12 07:44:40 +00001279}
Thomas Tsou3eaae802013-08-20 19:31:14 -04001280
Thomas Tsou865bca42013-08-21 20:58:00 -04001281static float computePeakRatio(signalVector *corr,
1282 int sps, float toa, complex amp)
dburgessb3a0ca42011-10-12 07:44:40 +00001283{
Thomas Tsou865bca42013-08-21 20:58:00 -04001284 int num = 0;
1285 complex *peak;
1286 float rms, avg = 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001287
Thomas Tsou865bca42013-08-21 20:58:00 -04001288 /* Check for bogus results */
1289 if ((toa < 0.0) || (toa > corr->size()))
1290 return 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001291
Alexander Chemeris65128122015-06-04 19:05:28 -04001292 peak = corr->begin() + (int) rint(toa);
1293
Thomas Tsou3eaae802013-08-20 19:31:14 -04001294 for (int i = 2 * sps; i <= 5 * sps; i++) {
Thomas Tsou865bca42013-08-21 20:58:00 -04001295 if (peak - i >= corr->begin()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001296 avg += (peak - i)->norm2();
1297 num++;
1298 }
Thomas Tsou865bca42013-08-21 20:58:00 -04001299 if (peak + i < corr->end()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001300 avg += (peak + i)->norm2();
1301 num++;
1302 }
dburgessb3a0ca42011-10-12 07:44:40 +00001303 }
1304
Thomas Tsou3eaae802013-08-20 19:31:14 -04001305 if (num < 2)
Thomas Tsou865bca42013-08-21 20:58:00 -04001306 return 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001307
Thomas Tsou3eaae802013-08-20 19:31:14 -04001308 rms = sqrtf(avg / (float) num) + 0.00001;
dburgessb3a0ca42011-10-12 07:44:40 +00001309
Thomas Tsou865bca42013-08-21 20:58:00 -04001310 return (amp.abs()) / rms;
dburgessb3a0ca42011-10-12 07:44:40 +00001311}
1312
1313bool energyDetect(signalVector &rxBurst,
1314 unsigned windowLength,
1315 float detectThreshold,
1316 float *avgPwr)
1317{
1318
1319 signalVector::const_iterator windowItr = rxBurst.begin(); //+rxBurst.size()/2 - 5*windowLength/2;
1320 float energy = 0.0;
1321 if (windowLength < 0) windowLength = 20;
1322 if (windowLength > rxBurst.size()) windowLength = rxBurst.size();
1323 for (unsigned i = 0; i < windowLength; i++) {
1324 energy += windowItr->norm2();
1325 windowItr+=4;
1326 }
1327 if (avgPwr) *avgPwr = energy/windowLength;
dburgessb3a0ca42011-10-12 07:44:40 +00001328 return (energy/windowLength > detectThreshold*detectThreshold);
1329}
dburgessb3a0ca42011-10-12 07:44:40 +00001330
Thomas Tsou865bca42013-08-21 20:58:00 -04001331/*
1332 * Detect a burst based on correlation and peak-to-average ratio
1333 *
1334 * For one sampler-per-symbol, perform fast peak detection (no interpolation)
1335 * for initial gating. We do this because energy detection should be disabled.
1336 * For higher oversampling values, we assume the energy detector is in place
1337 * and we run full interpolating peak detection.
1338 */
1339static int detectBurst(signalVector &burst,
1340 signalVector &corr, CorrelationSequence *sync,
1341 float thresh, int sps, complex *amp, float *toa,
1342 int start, int len)
dburgessb3a0ca42011-10-12 07:44:40 +00001343{
Thomas Tsou865bca42013-08-21 20:58:00 -04001344 /* Correlate */
1345 if (!convolve(&burst, sync->sequence, &corr,
Thomas Tsou3eaae802013-08-20 19:31:14 -04001346 CUSTOM, start, len, sps, 0)) {
1347 return -1;
dburgessb3a0ca42011-10-12 07:44:40 +00001348 }
1349
Thomas Tsou865bca42013-08-21 20:58:00 -04001350 /* Peak detection - place restrictions at correlation edges */
1351 *amp = fastPeakDetect(corr, toa);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001352
Thomas Tsou865bca42013-08-21 20:58:00 -04001353 if ((*toa < 3 * sps) || (*toa > len - 3 * sps))
1354 return 0;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001355
Thomas Tsou865bca42013-08-21 20:58:00 -04001356 /* Peak -to-average ratio */
1357 if (computePeakRatio(&corr, sps, *toa, *amp) < thresh)
1358 return 0;
1359
1360 /* Compute peak-to-average ratio. Reject if we don't have enough values */
1361 *amp = peakDetect(corr, toa, NULL);
1362
1363 /* Normalize our channel gain */
1364 *amp = *amp / sync->gain;
1365
Thomas Tsoua57bc8a2013-09-05 08:16:47 +08001366 /* Compenate for residual rotation with dual Laurent pulse */
1367 if (sps == 4)
1368 *amp = *amp * complex(0.0, 1.0);
1369
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001370 /* Compensate for residuate time lag */
1371 *toa = *toa - sync->toa;
1372
Thomas Tsou865bca42013-08-21 20:58:00 -04001373 return 1;
1374}
1375
Alexander Chemerisded68da2015-06-04 15:39:41 -04001376static float maxAmplitude(signalVector &burst)
Tom Tsou577cd022015-05-18 13:57:54 -07001377{
Alexander Chemerisded68da2015-06-04 15:39:41 -04001378 float max = 0.0;
1379 for (size_t i = 0; i < burst.size(); i++) {
1380 if (fabs(burst[i].real()) > max)
1381 max = fabs(burst[i].real());
1382 if (fabs(burst[i].imag()) > max)
1383 max = fabs(burst[i].imag());
1384 }
Tom Tsou577cd022015-05-18 13:57:54 -07001385
Alexander Chemerisded68da2015-06-04 15:39:41 -04001386 return max;
Tom Tsou577cd022015-05-18 13:57:54 -07001387}
1388
Alexander Chemeris03095162015-06-09 20:52:11 -04001389/*
1390 * RACH/Normal burst detection with clipping detection
Thomas Tsou865bca42013-08-21 20:58:00 -04001391 *
1392 * Correlation window parameters:
Alexander Chemeris03095162015-06-09 20:52:11 -04001393 * target: Tail bits + burst length
1394 * head: Search symbols before target
1395 * tail: Search symbols after target
Thomas Tsou865bca42013-08-21 20:58:00 -04001396 */
Alexander Chemeris03095162015-06-09 20:52:11 -04001397int detectGeneralBurst(signalVector &rxBurst,
1398 float thresh,
1399 int sps,
1400 complex &amp,
1401 float &toa,
1402 int target, int head, int tail,
1403 CorrelationSequence *sync)
Thomas Tsou865bca42013-08-21 20:58:00 -04001404{
Alexander Chemeris03095162015-06-09 20:52:11 -04001405 int rc, start, len;
Alexander Chemerisded68da2015-06-04 15:39:41 -04001406 bool clipping = false;
Thomas Tsoub075dd22013-11-09 22:25:46 -05001407 signalVector *corr;
Thomas Tsou865bca42013-08-21 20:58:00 -04001408
1409 if ((sps != 1) && (sps != 4))
Tom Tsou577cd022015-05-18 13:57:54 -07001410 return -SIGERR_UNSUPPORTED;
1411
Alexander Chemerisded68da2015-06-04 15:39:41 -04001412 // Detect potential clipping
1413 // We still may be able to demod the burst, so we'll give it a try
1414 // and only report clipping if we can't demod.
1415 float maxAmpl = maxAmplitude(rxBurst);
1416 if (maxAmpl > CLIP_THRESH) {
1417 LOG(DEBUG) << "max burst amplitude: " << maxAmpl << " is above the clipping threshold: " << CLIP_THRESH << std::endl;
1418 clipping = true;
1419 }
Thomas Tsou865bca42013-08-21 20:58:00 -04001420
Thomas Tsou865bca42013-08-21 20:58:00 -04001421 start = (target - head) * sps - 1;
1422 len = (head + tail) * sps;
Thomas Tsoub075dd22013-11-09 22:25:46 -05001423 corr = new signalVector(len);
Thomas Tsou865bca42013-08-21 20:58:00 -04001424
Thomas Tsoub075dd22013-11-09 22:25:46 -05001425 rc = detectBurst(rxBurst, *corr, sync,
Alexander Chemeris03095162015-06-09 20:52:11 -04001426 thresh, sps, &amp, &toa, start, len);
Thomas Tsoub075dd22013-11-09 22:25:46 -05001427 delete corr;
1428
Thomas Tsou865bca42013-08-21 20:58:00 -04001429 if (rc < 0) {
Tom Tsou577cd022015-05-18 13:57:54 -07001430 return -SIGERR_INTERNAL;
Thomas Tsou865bca42013-08-21 20:58:00 -04001431 } else if (!rc) {
Alexander Chemeris03095162015-06-09 20:52:11 -04001432 amp = 0.0f;
1433 toa = 0.0f;
Alexander Chemerisded68da2015-06-04 15:39:41 -04001434 return clipping?-SIGERR_CLIP:SIGERR_NONE;
dburgessb3a0ca42011-10-12 07:44:40 +00001435 }
1436
Thomas Tsou865bca42013-08-21 20:58:00 -04001437 /* Subtract forward search bits from delay */
Alexander Chemeris03095162015-06-09 20:52:11 -04001438 toa -= head * sps;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001439
Thomas Tsou865bca42013-08-21 20:58:00 -04001440 return 1;
1441}
Thomas Tsou3eaae802013-08-20 19:31:14 -04001442
Alexander Chemeris03095162015-06-09 20:52:11 -04001443
1444/*
1445 * RACH burst detection
1446 *
1447 * Correlation window parameters:
1448 * target: Tail bits + RACH length (reduced from 41 to a multiple of 4)
1449 * head: Search 4 symbols before target
1450 * tail: Search 10 symbols after target
1451 */
1452int detectRACHBurst(signalVector &rxBurst,
1453 float thresh,
1454 int sps,
1455 complex &amp,
1456 float &toa)
1457{
1458 int rc, target, head, tail;
1459 CorrelationSequence *sync;
1460
1461 target = 8 + 40;
1462 head = 4;
1463 tail = 10;
1464 sync = gRACHSequence;
1465
1466 rc = detectGeneralBurst(rxBurst, thresh, sps, amp, toa,
1467 target, head, tail, sync);
1468
1469 return rc;
1470}
1471
Thomas Tsou865bca42013-08-21 20:58:00 -04001472/*
1473 * Normal burst detection
1474 *
1475 * Correlation window parameters:
1476 * target: Tail + data + mid-midamble + 1/2 remaining midamblebits
Thomas Tsoudafb3372013-09-18 16:21:26 -04001477 * head: Search 4 symbols before target
1478 * tail: Search 4 symbols + maximum expected delay
Thomas Tsou865bca42013-08-21 20:58:00 -04001479 */
1480int analyzeTrafficBurst(signalVector &rxBurst, unsigned tsc, float thresh,
Alexander Chemeris03095162015-06-09 20:52:11 -04001481 int sps, complex &amp, float &toa, unsigned max_toa,
Thomas Tsou865bca42013-08-21 20:58:00 -04001482 bool chan_req, signalVector **chan, float *chan_offset)
1483{
Alexander Chemeris03095162015-06-09 20:52:11 -04001484 int rc, target, head, tail;
Thomas Tsou865bca42013-08-21 20:58:00 -04001485 CorrelationSequence *sync;
1486
Alexander Chemeris03095162015-06-09 20:52:11 -04001487 if ((tsc < 0) || (tsc > 7))
Tom Tsou577cd022015-05-18 13:57:54 -07001488 return -SIGERR_UNSUPPORTED;
1489
Thomas Tsou865bca42013-08-21 20:58:00 -04001490 target = 3 + 58 + 16 + 5;
Thomas Tsoudafb3372013-09-18 16:21:26 -04001491 head = 4;
1492 tail = 4 + max_toa;
Thomas Tsou865bca42013-08-21 20:58:00 -04001493 sync = gMidambles[tsc];
Thomas Tsou865bca42013-08-21 20:58:00 -04001494
Alexander Chemeris03095162015-06-09 20:52:11 -04001495 rc = detectGeneralBurst(rxBurst, thresh, sps, amp, toa,
1496 target, head, tail, sync);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001497
Thomas Tsou865bca42013-08-21 20:58:00 -04001498 /* Equalization not currently supported */
Alexander Chemeris03095162015-06-09 20:52:11 -04001499 if (rc > 0 && chan_req) {
Thomas Tsou865bca42013-08-21 20:58:00 -04001500 *chan = new signalVector(6 * sps);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001501
1502 if (chan_offset)
Thomas Tsou865bca42013-08-21 20:58:00 -04001503 *chan_offset = 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001504 }
1505
Alexander Chemeris03095162015-06-09 20:52:11 -04001506 return rc;
dburgessb3a0ca42011-10-12 07:44:40 +00001507}
1508
Thomas Tsou94edaae2013-11-09 22:19:19 -05001509signalVector *decimateVector(signalVector &wVector, size_t factor)
dburgessb3a0ca42011-10-12 07:44:40 +00001510{
Thomas Tsou94edaae2013-11-09 22:19:19 -05001511 signalVector *dec;
dburgessb3a0ca42011-10-12 07:44:40 +00001512
Thomas Tsou94edaae2013-11-09 22:19:19 -05001513 if (factor <= 1)
1514 return NULL;
dburgessb3a0ca42011-10-12 07:44:40 +00001515
Thomas Tsou94edaae2013-11-09 22:19:19 -05001516 dec = new signalVector(wVector.size() / factor);
1517 dec->isReal(wVector.isReal());
dburgessb3a0ca42011-10-12 07:44:40 +00001518
Thomas Tsou94edaae2013-11-09 22:19:19 -05001519 signalVector::iterator itr = dec->begin();
1520 for (size_t i = 0; i < wVector.size(); i += factor)
1521 *itr++ = wVector[i];
1522
1523 return dec;
dburgessb3a0ca42011-10-12 07:44:40 +00001524}
1525
Thomas Tsou83e06892013-08-20 16:10:01 -04001526SoftVector *demodulateBurst(signalVector &rxBurst, int sps,
Thomas Tsou94edaae2013-11-09 22:19:19 -05001527 complex channel, float TOA)
dburgessb3a0ca42011-10-12 07:44:40 +00001528{
Thomas Tsou94edaae2013-11-09 22:19:19 -05001529 signalVector *delay, *dec = NULL;
1530 SoftVector *bits;
dburgessb3a0ca42011-10-12 07:44:40 +00001531
Thomas Tsou94edaae2013-11-09 22:19:19 -05001532 scaleVector(rxBurst, ((complex) 1.0) / channel);
1533 delay = delayVector(&rxBurst, NULL, -TOA);
dburgessb3a0ca42011-10-12 07:44:40 +00001534
Thomas Tsou94edaae2013-11-09 22:19:19 -05001535 /* Shift up by a quarter of a frequency */
1536 GMSKReverseRotate(*delay, sps);
dburgessb3a0ca42011-10-12 07:44:40 +00001537
Thomas Tsou94edaae2013-11-09 22:19:19 -05001538 /* Decimate and slice */
Thomas Tsoud24cc2c2013-08-20 15:41:45 -04001539 if (sps > 1) {
Thomas Tsou94edaae2013-11-09 22:19:19 -05001540 dec = decimateVector(*delay, sps);
1541 delete delay;
1542 delay = NULL;
1543 } else {
1544 dec = delay;
dburgessb3a0ca42011-10-12 07:44:40 +00001545 }
1546
Thomas Tsou94edaae2013-11-09 22:19:19 -05001547 vectorSlicer(dec);
dburgessb3a0ca42011-10-12 07:44:40 +00001548
Thomas Tsou94edaae2013-11-09 22:19:19 -05001549 bits = new SoftVector(dec->size());
dburgessb3a0ca42011-10-12 07:44:40 +00001550
Thomas Tsou94edaae2013-11-09 22:19:19 -05001551 SoftVector::iterator bit_itr = bits->begin();
1552 signalVector::iterator burst_itr = dec->begin();
dburgessb3a0ca42011-10-12 07:44:40 +00001553
Thomas Tsou94edaae2013-11-09 22:19:19 -05001554 for (; burst_itr < dec->end(); burst_itr++)
1555 *bit_itr++ = burst_itr->real();
dburgessb3a0ca42011-10-12 07:44:40 +00001556
Thomas Tsou94edaae2013-11-09 22:19:19 -05001557 delete dec;
dburgessb3a0ca42011-10-12 07:44:40 +00001558
Thomas Tsou94edaae2013-11-09 22:19:19 -05001559 return bits;
dburgessb3a0ca42011-10-12 07:44:40 +00001560}
Thomas Tsou94edaae2013-11-09 22:19:19 -05001561
dburgessb3a0ca42011-10-12 07:44:40 +00001562// Assumes symbol-spaced sampling!!!
1563// Based upon paper by Al-Dhahir and Cioffi
1564bool designDFE(signalVector &channelResponse,
1565 float SNRestimate,
1566 int Nf,
1567 signalVector **feedForwardFilter,
1568 signalVector **feedbackFilter)
1569{
1570
1571 signalVector G0(Nf);
1572 signalVector G1(Nf);
1573 signalVector::iterator G0ptr = G0.begin();
1574 signalVector::iterator G1ptr = G1.begin();
1575 signalVector::iterator chanPtr = channelResponse.begin();
1576
1577 int nu = channelResponse.size()-1;
1578
1579 *G0ptr = 1.0/sqrtf(SNRestimate);
1580 for(int j = 0; j <= nu; j++) {
1581 *G1ptr = chanPtr->conj();
1582 G1ptr++; chanPtr++;
1583 }
1584
1585 signalVector *L[Nf];
1586 signalVector::iterator Lptr;
Thomas Tsou3f32ab52013-11-15 16:32:54 -05001587 float d = 1.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001588 for(int i = 0; i < Nf; i++) {
1589 d = G0.begin()->norm2() + G1.begin()->norm2();
1590 L[i] = new signalVector(Nf+nu);
1591 Lptr = L[i]->begin()+i;
1592 G0ptr = G0.begin(); G1ptr = G1.begin();
1593 while ((G0ptr < G0.end()) && (Lptr < L[i]->end())) {
1594 *Lptr = (*G0ptr*(G0.begin()->conj()) + *G1ptr*(G1.begin()->conj()) )/d;
1595 Lptr++;
1596 G0ptr++;
1597 G1ptr++;
1598 }
1599 complex k = (*G1.begin())/(*G0.begin());
1600
1601 if (i != Nf-1) {
1602 signalVector G0new = G1;
1603 scaleVector(G0new,k.conj());
1604 addVector(G0new,G0);
1605
1606 signalVector G1new = G0;
1607 scaleVector(G1new,k*(-1.0));
1608 addVector(G1new,G1);
Thomas Tsou94edaae2013-11-09 22:19:19 -05001609 delayVector(&G1new, &G1new, -1.0);
dburgessb3a0ca42011-10-12 07:44:40 +00001610
1611 scaleVector(G0new,1.0/sqrtf(1.0+k.norm2()));
1612 scaleVector(G1new,1.0/sqrtf(1.0+k.norm2()));
1613 G0 = G0new;
1614 G1 = G1new;
1615 }
1616 }
1617
1618 *feedbackFilter = new signalVector(nu);
1619 L[Nf-1]->segmentCopyTo(**feedbackFilter,Nf,nu);
1620 scaleVector(**feedbackFilter,(complex) -1.0);
1621 conjugateVector(**feedbackFilter);
1622
1623 signalVector v(Nf);
1624 signalVector::iterator vStart = v.begin();
1625 signalVector::iterator vPtr;
1626 *(vStart+Nf-1) = (complex) 1.0;
1627 for(int k = Nf-2; k >= 0; k--) {
1628 Lptr = L[k]->begin()+k+1;
1629 vPtr = vStart + k+1;
1630 complex v_k = 0.0;
1631 for (int j = k+1; j < Nf; j++) {
1632 v_k -= (*vPtr)*(*Lptr);
1633 vPtr++; Lptr++;
1634 }
1635 *(vStart + k) = v_k;
1636 }
1637
1638 *feedForwardFilter = new signalVector(Nf);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001639 signalVector::iterator w = (*feedForwardFilter)->end();
dburgessb3a0ca42011-10-12 07:44:40 +00001640 for (int i = 0; i < Nf; i++) {
1641 delete L[i];
1642 complex w_i = 0.0;
1643 int endPt = ( nu < (Nf-1-i) ) ? nu : (Nf-1-i);
1644 vPtr = vStart+i;
1645 chanPtr = channelResponse.begin();
1646 for (int k = 0; k < endPt+1; k++) {
1647 w_i += (*vPtr)*(chanPtr->conj());
1648 vPtr++; chanPtr++;
1649 }
Thomas Tsou3eaae802013-08-20 19:31:14 -04001650 *--w = w_i/d;
dburgessb3a0ca42011-10-12 07:44:40 +00001651 }
1652
1653
1654 return true;
1655
1656}
1657
1658// Assumes symbol-rate sampling!!!!
1659SoftVector *equalizeBurst(signalVector &rxBurst,
1660 float TOA,
Thomas Tsoud24cc2c2013-08-20 15:41:45 -04001661 int sps,
dburgessb3a0ca42011-10-12 07:44:40 +00001662 signalVector &w, // feedforward filter
1663 signalVector &b) // feedback filter
1664{
Thomas Tsou3eaae802013-08-20 19:31:14 -04001665 signalVector *postForwardFull;
dburgessb3a0ca42011-10-12 07:44:40 +00001666
Thomas Tsou94edaae2013-11-09 22:19:19 -05001667 if (!delayVector(&rxBurst, &rxBurst, -TOA))
Thomas Tsou3eaae802013-08-20 19:31:14 -04001668 return NULL;
dburgessb3a0ca42011-10-12 07:44:40 +00001669
Thomas Tsou3eaae802013-08-20 19:31:14 -04001670 postForwardFull = convolve(&rxBurst, &w, NULL,
1671 CUSTOM, 0, rxBurst.size() + w.size() - 1);
1672 if (!postForwardFull)
1673 return NULL;
dburgessb3a0ca42011-10-12 07:44:40 +00001674
1675 signalVector* postForward = new signalVector(rxBurst.size());
1676 postForwardFull->segmentCopyTo(*postForward,w.size()-1,rxBurst.size());
1677 delete postForwardFull;
1678
1679 signalVector::iterator dPtr = postForward->begin();
1680 signalVector::iterator dBackPtr;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001681 signalVector::iterator rotPtr = GMSKRotationN->begin();
1682 signalVector::iterator revRotPtr = GMSKReverseRotationN->begin();
dburgessb3a0ca42011-10-12 07:44:40 +00001683
1684 signalVector *DFEoutput = new signalVector(postForward->size());
1685 signalVector::iterator DFEItr = DFEoutput->begin();
1686
1687 // NOTE: can insert the midamble and/or use midamble to estimate BER
1688 for (; dPtr < postForward->end(); dPtr++) {
1689 dBackPtr = dPtr-1;
1690 signalVector::iterator bPtr = b.begin();
1691 while ( (bPtr < b.end()) && (dBackPtr >= postForward->begin()) ) {
1692 *dPtr = *dPtr + (*bPtr)*(*dBackPtr);
1693 bPtr++;
1694 dBackPtr--;
1695 }
1696 *dPtr = *dPtr * (*revRotPtr);
1697 *DFEItr = *dPtr;
1698 // make decision on symbol
1699 *dPtr = (dPtr->real() > 0.0) ? 1.0 : -1.0;
1700 //*DFEItr = *dPtr;
1701 *dPtr = *dPtr * (*rotPtr);
1702 DFEItr++;
1703 rotPtr++;
1704 revRotPtr++;
1705 }
1706
1707 vectorSlicer(DFEoutput);
1708
1709 SoftVector *burstBits = new SoftVector(postForward->size());
1710 SoftVector::iterator burstItr = burstBits->begin();
1711 DFEItr = DFEoutput->begin();
1712 for (; DFEItr < DFEoutput->end(); DFEItr++)
1713 *burstItr++ = DFEItr->real();
1714
1715 delete postForward;
1716
1717 delete DFEoutput;
1718
1719 return burstBits;
1720}
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001721
1722bool sigProcLibSetup(int sps)
1723{
1724 if ((sps != 1) && (sps != 4))
1725 return false;
1726
1727 initTrigTables();
Thomas Tsou0e0e1f42013-11-09 22:08:51 -05001728 generateSincTable();
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001729 initGMSKRotationTables(sps);
1730
1731 GSMPulse1 = generateGSMPulse(1, 2);
1732 if (sps > 1)
1733 GSMPulse = generateGSMPulse(sps, 2);
1734
1735 if (!generateRACHSequence(1)) {
1736 sigProcLibDestroy();
1737 return false;
1738 }
1739
Thomas Tsouf79c4d02013-11-09 15:51:56 -06001740 generateDelayFilters();
1741
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001742 return true;
1743}