blob: 2350abb7e216a6ba4595302e3eeea56c2b66d994 [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
dburgessb3a0ca42011-10-12 07:44:40 +000025#include "sigProcLib.h"
26#include "GSMCommon.h"
kurtis.heimerla198d452011-11-26 03:19:28 +000027#include "sendLPF_961.h"
28#include "rcvLPF_651.h"
dburgessb3a0ca42011-10-12 07:44:40 +000029
Alexander Chemerisd734e2d2013-06-16 14:30:58 +040030using namespace GSM;
31
Thomas Tsou3eaae802013-08-20 19:31:14 -040032extern "C" {
33#include "convolve.h"
34}
35
dburgessb3a0ca42011-10-12 07:44:40 +000036#define TABLESIZE 1024
37
38/** Lookup tables for trigonometric approximation */
39float cosTable[TABLESIZE+1]; // add 1 element for wrap around
40float sinTable[TABLESIZE+1];
41
42/** Constants */
43static const float M_PI_F = (float)M_PI;
44static const float M_2PI_F = (float)(2.0*M_PI);
45static const float M_1_2PI_F = 1/M_2PI_F;
46
47/** Static vectors that contain a precomputed +/- f_b/4 sinusoid */
48signalVector *GMSKRotation = NULL;
49signalVector *GMSKReverseRotation = NULL;
50
Thomas Tsoue5dcfc42013-08-20 16:27:12 -040051/*
Thomas Tsou3eaae802013-08-20 19:31:14 -040052 * RACH and midamble correlation waveforms. Store the buffer separately
53 * because we need to allocate it explicitly outside of the signal vector
54 * constructor. This is because C++ (prior to C++11) is unable to natively
55 * perform 16-byte memory alignment required by many SSE instructions.
Thomas Tsoue5dcfc42013-08-20 16:27:12 -040056 */
57struct CorrelationSequence {
Thomas Tsou3eaae802013-08-20 19:31:14 -040058 CorrelationSequence() : sequence(NULL), buffer(NULL)
Thomas Tsoue5dcfc42013-08-20 16:27:12 -040059 {
60 }
61
62 ~CorrelationSequence()
63 {
64 delete sequence;
Thomas Tsou3eaae802013-08-20 19:31:14 -040065 free(buffer);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -040066 }
67
dburgessb3a0ca42011-10-12 07:44:40 +000068 signalVector *sequence;
Thomas Tsou3eaae802013-08-20 19:31:14 -040069 void *buffer;
dburgessb3a0ca42011-10-12 07:44:40 +000070 float TOA;
71 complex gain;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -040072};
dburgessb3a0ca42011-10-12 07:44:40 +000073
Thomas Tsou83e06892013-08-20 16:10:01 -040074/*
Thomas Tsou3eaae802013-08-20 19:31:14 -040075 * Gaussian and empty modulation pulses. Like the correlation sequences,
76 * store the runtime (Gaussian) buffer separately because of needed alignment
77 * for SSE instructions.
Thomas Tsou83e06892013-08-20 16:10:01 -040078 */
79struct PulseSequence {
Thomas Tsoua57bc8a2013-09-05 08:16:47 +080080 PulseSequence() : c0(NULL), c1(NULL), empty(NULL),
81 c0_buffer(NULL), c1_buffer(NULL)
Thomas Tsou83e06892013-08-20 16:10:01 -040082 {
83 }
84
85 ~PulseSequence()
86 {
Thomas Tsoua57bc8a2013-09-05 08:16:47 +080087 delete c0;
88 delete c1;
Thomas Tsou83e06892013-08-20 16:10:01 -040089 delete empty;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +080090 free(c0_buffer);
91 free(c1_buffer);
Thomas Tsou83e06892013-08-20 16:10:01 -040092 }
93
Thomas Tsoua57bc8a2013-09-05 08:16:47 +080094 signalVector *c0;
95 signalVector *c1;
Thomas Tsou83e06892013-08-20 16:10:01 -040096 signalVector *empty;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +080097 void *c0_buffer;
98 void *c1_buffer;
Thomas Tsou83e06892013-08-20 16:10:01 -040099};
100
dburgessb3a0ca42011-10-12 07:44:40 +0000101CorrelationSequence *gMidambles[] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
102CorrelationSequence *gRACHSequence = NULL;
Thomas Tsou83e06892013-08-20 16:10:01 -0400103PulseSequence *GSMPulse = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000104
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400105void sigProcLibDestroy()
106{
dburgessb3a0ca42011-10-12 07:44:40 +0000107 for (int i = 0; i < 8; i++) {
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400108 delete gMidambles[i];
109 gMidambles[i] = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000110 }
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400111
112 delete GMSKRotation;
113 delete GMSKReverseRotation;
114 delete gRACHSequence;
115 delete GSMPulse;
116
117 GMSKRotation = NULL;
118 GMSKReverseRotation = NULL;
119 gRACHSequence = NULL;
120 GSMPulse = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000121}
122
dburgessb3a0ca42011-10-12 07:44:40 +0000123// dB relative to 1.0.
124// if > 1.0, then return 0 dB
125float dB(float x) {
126
127 float arg = 1.0F;
128 float dB = 0.0F;
129
130 if (x >= 1.0F) return 0.0F;
131 if (x <= 0.0F) return -200.0F;
132
133 float prevArg = arg;
134 float prevdB = dB;
135 float stepSize = 16.0F;
136 float dBstepSize = 12.0F;
137 while (stepSize > 1.0F) {
138 do {
139 prevArg = arg;
140 prevdB = dB;
141 arg /= stepSize;
142 dB -= dBstepSize;
143 } while (arg > x);
144 arg = prevArg;
145 dB = prevdB;
146 stepSize *= 0.5F;
147 dBstepSize -= 3.0F;
148 }
149 return ((arg-x)*(dB-3.0F) + (x-arg*0.5F)*dB)/(arg - arg*0.5F);
150
151}
152
153// 10^(-dB/10), inverse of dB func.
154float dBinv(float x) {
155
156 float arg = 1.0F;
157 float dB = 0.0F;
158
159 if (x >= 0.0F) return 1.0F;
160 if (x <= -200.0F) return 0.0F;
161
162 float prevArg = arg;
163 float prevdB = dB;
164 float stepSize = 16.0F;
165 float dBstepSize = 12.0F;
166 while (stepSize > 1.0F) {
167 do {
168 prevArg = arg;
169 prevdB = dB;
170 arg /= stepSize;
171 dB -= dBstepSize;
172 } while (dB > x);
173 arg = prevArg;
174 dB = prevdB;
175 stepSize *= 0.5F;
176 dBstepSize -= 3.0F;
177 }
178
179 return ((dB-x)*(arg*0.5F)+(x-(dB-3.0F))*(arg))/3.0F;
180
181}
182
183float vectorNorm2(const signalVector &x)
184{
185 signalVector::const_iterator xPtr = x.begin();
186 float Energy = 0.0;
187 for (;xPtr != x.end();xPtr++) {
188 Energy += xPtr->norm2();
189 }
190 return Energy;
191}
192
193
194float vectorPower(const signalVector &x)
195{
196 return vectorNorm2(x)/x.size();
197}
198
199/** compute cosine via lookup table */
200float cosLookup(const float x)
201{
202 float arg = x*M_1_2PI_F;
203 while (arg > 1.0F) arg -= 1.0F;
204 while (arg < 0.0F) arg += 1.0F;
205
206 const float argT = arg*((float)TABLESIZE);
207 const int argI = (int)argT;
208 const float delta = argT-argI;
209 const float iDelta = 1.0F-delta;
210 return iDelta*cosTable[argI] + delta*cosTable[argI+1];
211}
212
213/** compute sine via lookup table */
214float sinLookup(const float x)
215{
216 float arg = x*M_1_2PI_F;
217 while (arg > 1.0F) arg -= 1.0F;
218 while (arg < 0.0F) arg += 1.0F;
219
220 const float argT = arg*((float)TABLESIZE);
221 const int argI = (int)argT;
222 const float delta = argT-argI;
223 const float iDelta = 1.0F-delta;
224 return iDelta*sinTable[argI] + delta*sinTable[argI+1];
225}
226
227
228/** compute e^(-jx) via lookup table. */
229complex expjLookup(float x)
230{
231 float arg = x*M_1_2PI_F;
232 while (arg > 1.0F) arg -= 1.0F;
233 while (arg < 0.0F) arg += 1.0F;
234
235 const float argT = arg*((float)TABLESIZE);
236 const int argI = (int)argT;
237 const float delta = argT-argI;
238 const float iDelta = 1.0F-delta;
239 return complex(iDelta*cosTable[argI] + delta*cosTable[argI+1],
240 iDelta*sinTable[argI] + delta*sinTable[argI+1]);
241}
242
243/** Library setup functions */
244void initTrigTables() {
245 for (int i = 0; i < TABLESIZE+1; i++) {
246 cosTable[i] = cos(2.0*M_PI*i/TABLESIZE);
247 sinTable[i] = sin(2.0*M_PI*i/TABLESIZE);
248 }
249}
250
Thomas Tsoud24cc2c2013-08-20 15:41:45 -0400251void initGMSKRotationTables(int sps)
252{
253 GMSKRotation = new signalVector(157 * sps);
254 GMSKReverseRotation = new signalVector(157 * sps);
dburgessb3a0ca42011-10-12 07:44:40 +0000255 signalVector::iterator rotPtr = GMSKRotation->begin();
256 signalVector::iterator revPtr = GMSKReverseRotation->begin();
257 float phase = 0.0;
258 while (rotPtr != GMSKRotation->end()) {
259 *rotPtr++ = expjLookup(phase);
260 *revPtr++ = expjLookup(-phase);
Thomas Tsoud24cc2c2013-08-20 15:41:45 -0400261 phase += M_PI_F / 2.0F / (float) sps;
dburgessb3a0ca42011-10-12 07:44:40 +0000262 }
263}
264
Thomas Tsoue57004d2013-08-20 18:55:33 -0400265bool sigProcLibSetup(int sps)
Thomas Tsoud24cc2c2013-08-20 15:41:45 -0400266{
Thomas Tsou865bca42013-08-21 20:58:00 -0400267 if ((sps != 1) && (sps != 4))
Thomas Tsoue57004d2013-08-20 18:55:33 -0400268 return false;
269
dburgessb3a0ca42011-10-12 07:44:40 +0000270 initTrigTables();
Thomas Tsoud24cc2c2013-08-20 15:41:45 -0400271 initGMSKRotationTables(sps);
Thomas Tsou83e06892013-08-20 16:10:01 -0400272 generateGSMPulse(sps, 2);
Thomas Tsoue57004d2013-08-20 18:55:33 -0400273
274 if (!generateRACHSequence(sps)) {
275 sigProcLibDestroy();
276 return false;
277 }
278
279 return true;
dburgessb3a0ca42011-10-12 07:44:40 +0000280}
281
282void GMSKRotate(signalVector &x) {
283 signalVector::iterator xPtr = x.begin();
284 signalVector::iterator rotPtr = GMSKRotation->begin();
285 if (x.isRealOnly()) {
286 while (xPtr < x.end()) {
287 *xPtr = *rotPtr++ * (xPtr->real());
288 xPtr++;
289 }
290 }
291 else {
292 while (xPtr < x.end()) {
293 *xPtr = *rotPtr++ * (*xPtr);
294 xPtr++;
295 }
296 }
297}
298
299void GMSKReverseRotate(signalVector &x) {
300 signalVector::iterator xPtr= x.begin();
301 signalVector::iterator rotPtr = GMSKReverseRotation->begin();
302 if (x.isRealOnly()) {
303 while (xPtr < x.end()) {
304 *xPtr = *rotPtr++ * (xPtr->real());
305 xPtr++;
306 }
307 }
308 else {
309 while (xPtr < x.end()) {
310 *xPtr = *rotPtr++ * (*xPtr);
311 xPtr++;
312 }
313 }
314}
315
Thomas Tsou3eaae802013-08-20 19:31:14 -0400316signalVector *convolve(const signalVector *x,
317 const signalVector *h,
318 signalVector *y,
319 ConvType spanType, int start,
320 unsigned len, unsigned step, int offset)
dburgessb3a0ca42011-10-12 07:44:40 +0000321{
Thomas Tsou3eaae802013-08-20 19:31:14 -0400322 int rc, head = 0, tail = 0;
323 bool alloc = false, append = false;
324 const signalVector *_x = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000325
Thomas Tsou3eaae802013-08-20 19:31:14 -0400326 if (!x || !h)
dburgessb3a0ca42011-10-12 07:44:40 +0000327 return NULL;
328
Thomas Tsou3eaae802013-08-20 19:31:14 -0400329 switch (spanType) {
330 case START_ONLY:
331 start = 0;
332 head = h->size();
333 len = x->size();
334 append = true;
dburgessb3a0ca42011-10-12 07:44:40 +0000335 break;
Thomas Tsou3eaae802013-08-20 19:31:14 -0400336 case NO_DELAY:
337 start = h->size() / 2;
338 head = start;
339 tail = start;
340 len = x->size();
341 append = true;
342 break;
343 case CUSTOM:
344 if (start < h->size() - 1) {
345 head = h->size() - start;
346 append = true;
347 }
348 if (start + len > x->size()) {
349 tail = start + len - x->size();
350 append = true;
dburgessb3a0ca42011-10-12 07:44:40 +0000351 }
352 break;
353 default:
354 return NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000355 }
dburgessb3a0ca42011-10-12 07:44:40 +0000356
Thomas Tsou3eaae802013-08-20 19:31:14 -0400357 /*
358 * Error if the output vector is too small. Create the output vector
359 * if the pointer is NULL.
360 */
361 if (y && (len > y->size()))
362 return NULL;
363 if (!y) {
364 y = new signalVector(len);
365 alloc = true;
366 }
367
368 /* Prepend or post-pend the input vector if the parameters require it */
369 if (append)
370 _x = new signalVector(*x, head, tail);
371 else
372 _x = x;
373
374 /*
375 * Four convovle types:
376 * 1. Complex-Real (aligned)
377 * 2. Complex-Complex (aligned)
378 * 3. Complex-Real (!aligned)
379 * 4. Complex-Complex (!aligned)
380 */
381 if (h->isRealOnly() && h->isAligned()) {
382 rc = convolve_real((float *) _x->begin(), _x->size(),
383 (float *) h->begin(), h->size(),
384 (float *) y->begin(), y->size(),
385 start, len, step, offset);
386 } else if (!h->isRealOnly() && h->isAligned()) {
387 rc = convolve_complex((float *) _x->begin(), _x->size(),
388 (float *) h->begin(), h->size(),
389 (float *) y->begin(), y->size(),
390 start, len, step, offset);
391 } else if (h->isRealOnly() && !h->isAligned()) {
392 rc = base_convolve_real((float *) _x->begin(), _x->size(),
393 (float *) h->begin(), h->size(),
394 (float *) y->begin(), y->size(),
395 start, len, step, offset);
396 } else if (!h->isRealOnly() && !h->isAligned()) {
397 rc = base_convolve_complex((float *) _x->begin(), _x->size(),
398 (float *) h->begin(), h->size(),
399 (float *) y->begin(), y->size(),
400 start, len, step, offset);
401 } else {
402 rc = -1;
403 }
404
405 if (append)
406 delete _x;
407
408 if (rc < 0) {
409 if (alloc)
410 delete y;
411 return NULL;
412 }
413
414 return y;
415}
dburgessb3a0ca42011-10-12 07:44:40 +0000416
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800417bool generateC1Pulse(int sps)
418{
419 int len;
420
421 switch (sps) {
422 case 4:
423 len = 8;
424 break;
425 default:
426 return false;
427 }
428
429 GSMPulse->c1_buffer = convolve_h_alloc(len);
430 GSMPulse->c1 = new signalVector((complex *)
431 GSMPulse->c1_buffer, 0, len);
432 GSMPulse->c1->isRealOnly(true);
433
434 /* Enable alignment for SSE usage */
435 GSMPulse->c1->setAligned(true);
436
437 signalVector::iterator xP = GSMPulse->c1->begin();
438
439 switch (sps) {
440 case 4:
441 /* BT = 0.30 */
442 *xP++ = 0.0;
443 *xP++ = 8.16373112e-03;
444 *xP++ = 2.84385729e-02;
445 *xP++ = 5.64158904e-02;
446 *xP++ = 7.05463553e-02;
447 *xP++ = 5.64158904e-02;
448 *xP++ = 2.84385729e-02;
449 *xP++ = 8.16373112e-03;
450 }
451
452 return true;
453}
454
Thomas Tsou83e06892013-08-20 16:10:01 -0400455void generateGSMPulse(int sps, int symbolLength)
dburgessb3a0ca42011-10-12 07:44:40 +0000456{
Thomas Tsou83e06892013-08-20 16:10:01 -0400457 int len;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800458 float arg, avg, center;
dburgessb3a0ca42011-10-12 07:44:40 +0000459
Thomas Tsou83e06892013-08-20 16:10:01 -0400460 delete GSMPulse;
461
462 /* Store a single tap filter used for correlation sequence generation */
463 GSMPulse = new PulseSequence();
464 GSMPulse->empty = new signalVector(1);
465 GSMPulse->empty->isRealOnly(true);
466 *(GSMPulse->empty->begin()) = 1.0f;
467
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400468 /*
469 * For 4 samples-per-symbol use a precomputed single pulse Laurent
470 * approximation. This should yields below 2 degrees of phase error at
471 * the modulator output. Use the existing pulse approximation for all
472 * other oversampling factors.
473 */
474 switch (sps) {
475 case 4:
476 len = 16;
477 break;
478 default:
479 len = sps * symbolLength;
480 if (len < 4)
481 len = 4;
482 }
Thomas Tsou3eaae802013-08-20 19:31:14 -0400483
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800484 GSMPulse->c0_buffer = convolve_h_alloc(len);
485 GSMPulse->c0 = new signalVector((complex *)
486 GSMPulse->c0_buffer, 0, len);
487 GSMPulse->c0->isRealOnly(true);
Thomas Tsou3eaae802013-08-20 19:31:14 -0400488
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800489 /* Enable alingnment for SSE usage */
490 GSMPulse->c0->setAligned(true);
491
492 signalVector::iterator xP = GSMPulse->c0->begin();
Thomas Tsou83e06892013-08-20 16:10:01 -0400493
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400494 if (sps == 4) {
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800495 *xP++ = 0.0;
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400496 *xP++ = 4.46348606e-03;
497 *xP++ = 2.84385729e-02;
498 *xP++ = 1.03184855e-01;
499 *xP++ = 2.56065552e-01;
500 *xP++ = 4.76375085e-01;
501 *xP++ = 7.05961177e-01;
502 *xP++ = 8.71291644e-01;
503 *xP++ = 9.29453645e-01;
504 *xP++ = 8.71291644e-01;
505 *xP++ = 7.05961177e-01;
506 *xP++ = 4.76375085e-01;
507 *xP++ = 2.56065552e-01;
508 *xP++ = 1.03184855e-01;
509 *xP++ = 2.84385729e-02;
510 *xP++ = 4.46348606e-03;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800511 generateC1Pulse(sps);
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400512 } else {
513 center = (float) (len - 1.0) / 2.0;
Thomas Tsou83e06892013-08-20 16:10:01 -0400514
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400515 /* GSM pulse approximation */
516 for (int i = 0; i < len; i++) {
517 arg = ((float) i - center) / (float) sps;
518 *xP++ = 0.96 * exp(-1.1380 * arg * arg -
519 0.527 * arg * arg * arg * arg);
520 }
dburgessb3a0ca42011-10-12 07:44:40 +0000521
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800522 avg = sqrtf(vectorNorm2(*GSMPulse->c0) / sps);
523 xP = GSMPulse->c0->begin();
524 for (int i = 0; i < len; i++)
525 *xP++ /= avg;
526 }
dburgessb3a0ca42011-10-12 07:44:40 +0000527}
528
529signalVector* frequencyShift(signalVector *y,
530 signalVector *x,
531 float freq,
532 float startPhase,
533 float *finalPhase)
534{
535
536 if (!x) return NULL;
537
538 if (y==NULL) {
539 y = new signalVector(x->size());
540 y->isRealOnly(x->isRealOnly());
541 if (y==NULL) return NULL;
542 }
543
544 if (y->size() < x->size()) return NULL;
545
546 float phase = startPhase;
547 signalVector::iterator yP = y->begin();
548 signalVector::iterator xPEnd = x->end();
549 signalVector::iterator xP = x->begin();
550
551 if (x->isRealOnly()) {
552 while (xP < xPEnd) {
553 (*yP++) = expjLookup(phase)*( (xP++)->real() );
554 phase += freq;
555 }
556 }
557 else {
558 while (xP < xPEnd) {
559 (*yP++) = (*xP++)*expjLookup(phase);
560 phase += freq;
561 }
562 }
563
564
565 if (finalPhase) *finalPhase = phase;
566
567 return y;
568}
569
570signalVector* reverseConjugate(signalVector *b)
571{
572 signalVector *tmp = new signalVector(b->size());
573 tmp->isRealOnly(b->isRealOnly());
574 signalVector::iterator bP = b->begin();
575 signalVector::iterator bPEnd = b->end();
576 signalVector::iterator tmpP = tmp->end()-1;
577 if (!b->isRealOnly()) {
578 while (bP < bPEnd) {
579 *tmpP-- = bP->conj();
580 bP++;
581 }
582 }
583 else {
584 while (bP < bPEnd) {
585 *tmpP-- = bP->real();
586 bP++;
587 }
588 }
589
590 return tmp;
591}
592
dburgessb3a0ca42011-10-12 07:44:40 +0000593/* soft output slicer */
594bool vectorSlicer(signalVector *x)
595{
596
597 signalVector::iterator xP = x->begin();
598 signalVector::iterator xPEnd = x->end();
599 while (xP < xPEnd) {
600 *xP = (complex) (0.5*(xP->real()+1.0F));
601 if (xP->real() > 1.0) *xP = 1.0;
602 if (xP->real() < 0.0) *xP = 0.0;
603 xP++;
604 }
605 return true;
606}
Thomas Tsou3eaae802013-08-20 19:31:14 -0400607
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800608static signalVector *rotateBurst(const BitVector &wBurst,
609 int guardPeriodLength, int sps)
610{
611 int burst_len;
612 signalVector *pulse, rotated, *shaped;
613 signalVector::iterator itr;
614
615 pulse = GSMPulse->empty;
616 burst_len = sps * (wBurst.size() + guardPeriodLength);
617 rotated = signalVector(burst_len);
618 itr = rotated.begin();
619
620 for (unsigned i = 0; i < wBurst.size(); i++) {
621 *itr = 2.0 * (wBurst[i] & 0x01) - 1.0;
622 itr += sps;
623 }
624
625 GMSKRotate(rotated);
626 rotated.isRealOnly(false);
627
628 /* Dummy filter operation */
629 shaped = convolve(&rotated, pulse, NULL, START_ONLY);
630 if (!shaped)
631 return NULL;
632
633 return shaped;
634}
635
636static signalVector *modulateBurstLaurent(const BitVector &bits,
637 int guard_len, int sps)
638{
639 int burst_len;
640 float phase;
641 signalVector *c0_pulse, *c1_pulse, c0_burst, c1_burst, *c0_shaped, *c1_shaped;
642 signalVector::iterator c0_itr, c1_itr;
643
644 /*
645 * Apply before and after bits to reduce phase error at burst edges.
646 * Make sure there is enough room in the burst to accomodate all bits.
647 */
648 if (guard_len < 4)
649 guard_len = 4;
650
651 c0_pulse = GSMPulse->c0;
652 c1_pulse = GSMPulse->c1;
653
654 burst_len = sps * (bits.size() + guard_len);
655
656 c0_burst = signalVector(burst_len);
657 c0_burst.isRealOnly(true);
658 c0_itr = c0_burst.begin();
659
660 c1_burst = signalVector(burst_len);
661 c1_burst.isRealOnly(true);
662 c1_itr = c1_burst.begin();
663
664 /* Padded differential start bits */
665 *c0_itr = 2.0 * (0x00 & 0x01) - 1.0;
666 c0_itr += sps;
667
668 /* Main burst bits */
669 for (unsigned i = 0; i < bits.size(); i++) {
670 *c0_itr = 2.0 * (bits[i] & 0x01) - 1.0;
671 c0_itr += sps;
672 }
673
674 /* Padded differential end bits */
675 *c0_itr = 2.0 * (0x01 & 0x01) - 1.0;
676
677 /* Generate C0 phase coefficients */
678 GMSKRotate(c0_burst);
679 c0_burst.isRealOnly(false);
680
681 c0_itr = c0_burst.begin();
682 c0_itr += sps * 2;
683 c1_itr += sps * 2;
684
685 /* Start magic */
686 phase = 2.0 * ((0x01 & 0x01) ^ (0x01 & 0x01)) - 1.0;
687 *c1_itr = *c0_itr * Complex<float>(0, phase);
688 c0_itr += sps;
689 c1_itr += sps;
690
691 /* Generate C1 phase coefficients */
692 for (unsigned i = 2; i < bits.size(); i++) {
693 phase = 2.0 * ((bits[i - 1] & 0x01) ^ (bits[i - 2] & 0x01)) - 1.0;
694 *c1_itr = *c0_itr * Complex<float>(0, phase);
695
696 c0_itr += sps;
697 c1_itr += sps;
698 }
699
700 /* End magic */
701 int i = bits.size();
702 phase = 2.0 * ((bits[i-1] & 0x01) ^ (bits[i-2] & 0x01)) - 1.0;
703 *c1_itr = *c0_itr * Complex<float>(0, phase);
704
705 /* Primary (C0) and secondary (C1) pulse shaping */
706 c0_shaped = convolve(&c0_burst, c0_pulse, NULL, START_ONLY);
707 c1_shaped = convolve(&c1_burst, c1_pulse, NULL, START_ONLY);
708
709 /* Sum shaped outputs into C0 */
710 c0_itr = c0_shaped->begin();
711 c1_itr = c1_shaped->begin();
712 for (unsigned i = 0; i < c0_shaped->size(); i++ )
713 *c0_itr++ += *c1_itr++;
714
715 delete c1_shaped;
716
717 return c0_shaped;
718}
719
720static signalVector *modulateBurstBasic(const BitVector &bits,
721 int guard_len, int sps)
722{
723 int burst_len;
724 signalVector *pulse, burst, *shaped;
725 signalVector::iterator burst_itr;
726
727 pulse = GSMPulse->c0;
728 burst_len = sps * (bits.size() + guard_len);
729
730 burst = signalVector(burst_len);
731 burst.isRealOnly(true);
732 burst_itr = burst.begin();
733
734 /* Raw bits are not differentially encoded */
735 for (unsigned i = 0; i < bits.size(); i++) {
736 *burst_itr = 2.0 * (bits[i] & 0x01) - 1.0;
737 burst_itr += sps;
738 }
739
740 GMSKRotate(burst);
741 burst.isRealOnly(false);
742
743 /* Single Gaussian pulse approximation shaping */
744 shaped = convolve(&burst, pulse, NULL, START_ONLY);
745
746 return shaped;
747}
748
Thomas Tsou3eaae802013-08-20 19:31:14 -0400749/* Assume input bits are not differentially encoded */
Thomas Tsou83e06892013-08-20 16:10:01 -0400750signalVector *modulateBurst(const BitVector &wBurst, int guardPeriodLength,
751 int sps, bool emptyPulse)
dburgessb3a0ca42011-10-12 07:44:40 +0000752{
Thomas Tsou83e06892013-08-20 16:10:01 -0400753 if (emptyPulse)
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800754 return rotateBurst(wBurst, guardPeriodLength, sps);
755 else if (sps == 4)
756 return modulateBurstLaurent(wBurst, guardPeriodLength, sps);
Thomas Tsou83e06892013-08-20 16:10:01 -0400757 else
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800758 return modulateBurstBasic(wBurst, guardPeriodLength, sps);
dburgessb3a0ca42011-10-12 07:44:40 +0000759}
760
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800761float sinc(float x)
dburgessb3a0ca42011-10-12 07:44:40 +0000762{
763 if ((x >= 0.01F) || (x <= -0.01F)) return (sinLookup(x)/x);
764 return 1.0F;
765}
766
Thomas Tsou3eaae802013-08-20 19:31:14 -0400767bool delayVector(signalVector &wBurst, float delay)
dburgessb3a0ca42011-10-12 07:44:40 +0000768{
769
770 int intOffset = (int) floor(delay);
771 float fracOffset = delay - intOffset;
Thomas Tsou3eaae802013-08-20 19:31:14 -0400772
dburgessb3a0ca42011-10-12 07:44:40 +0000773 // do fractional shift first, only do it for reasonable offsets
774 if (fabs(fracOffset) > 1e-2) {
775 // create sinc function
776 signalVector sincVector(21);
777 sincVector.isRealOnly(true);
Thomas Tsou3eaae802013-08-20 19:31:14 -0400778 signalVector::iterator sincBurstItr = sincVector.end();
dburgessb3a0ca42011-10-12 07:44:40 +0000779 for (int i = 0; i < 21; i++)
Thomas Tsou3eaae802013-08-20 19:31:14 -0400780 *--sincBurstItr = (complex) sinc(M_PI_F*(i-10-fracOffset));
dburgessb3a0ca42011-10-12 07:44:40 +0000781
782 signalVector shiftedBurst(wBurst.size());
Thomas Tsou3eaae802013-08-20 19:31:14 -0400783 if (!convolve(&wBurst, &sincVector, &shiftedBurst, NO_DELAY))
784 return false;
dburgessb3a0ca42011-10-12 07:44:40 +0000785 wBurst.clone(shiftedBurst);
786 }
787
788 if (intOffset < 0) {
789 intOffset = -intOffset;
790 signalVector::iterator wBurstItr = wBurst.begin();
791 signalVector::iterator shiftedItr = wBurst.begin()+intOffset;
792 while (shiftedItr < wBurst.end())
793 *wBurstItr++ = *shiftedItr++;
794 while (wBurstItr < wBurst.end())
795 *wBurstItr++ = 0.0;
796 }
797 else {
798 signalVector::iterator wBurstItr = wBurst.end()-1;
799 signalVector::iterator shiftedItr = wBurst.end()-1-intOffset;
800 while (shiftedItr >= wBurst.begin())
801 *wBurstItr-- = *shiftedItr--;
802 while (wBurstItr >= wBurst.begin())
803 *wBurstItr-- = 0.0;
804 }
805}
806
807signalVector *gaussianNoise(int length,
808 float variance,
809 complex mean)
810{
811
812 signalVector *noise = new signalVector(length);
813 signalVector::iterator nPtr = noise->begin();
814 float stddev = sqrtf(variance);
815 while (nPtr < noise->end()) {
816 float u1 = (float) rand()/ (float) RAND_MAX;
817 while (u1==0.0)
818 u1 = (float) rand()/ (float) RAND_MAX;
819 float u2 = (float) rand()/ (float) RAND_MAX;
820 float arg = 2.0*M_PI*u2;
821 *nPtr = mean + stddev*complex(cos(arg),sin(arg))*sqrtf(-2.0*log(u1));
822 nPtr++;
823 }
824
825 return noise;
826}
827
828complex interpolatePoint(const signalVector &inSig,
829 float ix)
830{
831
832 int start = (int) (floor(ix) - 10);
833 if (start < 0) start = 0;
834 int end = (int) (floor(ix) + 11);
835 if ((unsigned) end > inSig.size()-1) end = inSig.size()-1;
836
837 complex pVal = 0.0;
838 if (!inSig.isRealOnly()) {
839 for (int i = start; i < end; i++)
840 pVal += inSig[i] * sinc(M_PI_F*(i-ix));
841 }
842 else {
843 for (int i = start; i < end; i++)
844 pVal += inSig[i].real() * sinc(M_PI_F*(i-ix));
845 }
846
847 return pVal;
848}
849
Thomas Tsou8181b012013-08-20 21:17:19 -0400850static complex fastPeakDetect(const signalVector &rxBurst, float *index)
851{
852 float val, max = 0.0f;
853 complex amp;
854 int _index = -1;
855
856 for (int i = 0; i < rxBurst.size(); i++) {
857 val = rxBurst[i].norm2();
858 if (val > max) {
859 max = val;
860 _index = i;
861 amp = rxBurst[i];
862 }
863 }
864
865 if (index)
866 *index = (float) _index;
867
868 return amp;
869}
870
dburgessb3a0ca42011-10-12 07:44:40 +0000871complex peakDetect(const signalVector &rxBurst,
872 float *peakIndex,
873 float *avgPwr)
874{
875
876
877 complex maxVal = 0.0;
878 float maxIndex = -1;
879 float sumPower = 0.0;
880
881 for (unsigned int i = 0; i < rxBurst.size(); i++) {
882 float samplePower = rxBurst[i].norm2();
883 if (samplePower > maxVal.real()) {
884 maxVal = samplePower;
885 maxIndex = i;
886 }
887 sumPower += samplePower;
888 }
889
890 // interpolate around the peak
891 // to save computation, we'll use early-late balancing
892 float earlyIndex = maxIndex-1;
893 float lateIndex = maxIndex+1;
894
895 float incr = 0.5;
896 while (incr > 1.0/1024.0) {
897 complex earlyP = interpolatePoint(rxBurst,earlyIndex);
898 complex lateP = interpolatePoint(rxBurst,lateIndex);
899 if (earlyP < lateP)
900 earlyIndex += incr;
901 else if (earlyP > lateP)
902 earlyIndex -= incr;
903 else break;
904 incr /= 2.0;
905 lateIndex = earlyIndex + 2.0;
906 }
907
908 maxIndex = earlyIndex + 1.0;
909 maxVal = interpolatePoint(rxBurst,maxIndex);
910
911 if (peakIndex!=NULL)
912 *peakIndex = maxIndex;
913
914 if (avgPwr!=NULL)
915 *avgPwr = (sumPower-maxVal.norm2()) / (rxBurst.size()-1);
916
917 return maxVal;
918
919}
920
921void scaleVector(signalVector &x,
922 complex scale)
923{
924 signalVector::iterator xP = x.begin();
925 signalVector::iterator xPEnd = x.end();
926 if (!x.isRealOnly()) {
927 while (xP < xPEnd) {
928 *xP = *xP * scale;
929 xP++;
930 }
931 }
932 else {
933 while (xP < xPEnd) {
934 *xP = xP->real() * scale;
935 xP++;
936 }
937 }
938}
939
940/** in-place conjugation */
941void conjugateVector(signalVector &x)
942{
943 if (x.isRealOnly()) return;
944 signalVector::iterator xP = x.begin();
945 signalVector::iterator xPEnd = x.end();
946 while (xP < xPEnd) {
947 *xP = xP->conj();
948 xP++;
949 }
950}
951
952
953// in-place addition!!
954bool addVector(signalVector &x,
955 signalVector &y)
956{
957 signalVector::iterator xP = x.begin();
958 signalVector::iterator yP = y.begin();
959 signalVector::iterator xPEnd = x.end();
960 signalVector::iterator yPEnd = y.end();
961 while ((xP < xPEnd) && (yP < yPEnd)) {
962 *xP = *xP + *yP;
963 xP++; yP++;
964 }
965 return true;
966}
967
968// in-place multiplication!!
969bool multVector(signalVector &x,
970 signalVector &y)
971{
972 signalVector::iterator xP = x.begin();
973 signalVector::iterator yP = y.begin();
974 signalVector::iterator xPEnd = x.end();
975 signalVector::iterator yPEnd = y.end();
976 while ((xP < xPEnd) && (yP < yPEnd)) {
977 *xP = (*xP) * (*yP);
978 xP++; yP++;
979 }
980 return true;
981}
982
983
984void offsetVector(signalVector &x,
985 complex offset)
986{
987 signalVector::iterator xP = x.begin();
988 signalVector::iterator xPEnd = x.end();
989 if (!x.isRealOnly()) {
990 while (xP < xPEnd) {
991 *xP += offset;
992 xP++;
993 }
994 }
995 else {
996 while (xP < xPEnd) {
997 *xP = xP->real() + offset;
998 xP++;
999 }
1000 }
1001}
1002
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001003bool generateMidamble(int sps, int tsc)
dburgessb3a0ca42011-10-12 07:44:40 +00001004{
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001005 bool status = true;
1006 complex *data = NULL;
1007 signalVector *autocorr = NULL, *midamble = NULL;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001008 signalVector *midMidamble = NULL, *_midMidamble = NULL;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001009
Thomas Tsou3eaae802013-08-20 19:31:14 -04001010 if ((tsc < 0) || (tsc > 7))
dburgessb3a0ca42011-10-12 07:44:40 +00001011 return false;
1012
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001013 delete gMidambles[tsc];
Thomas Tsou3eaae802013-08-20 19:31:14 -04001014
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001015 /* Use middle 16 bits of each TSC. Correlation sequence is not pulse shaped */
1016 midMidamble = modulateBurst(gTrainingSequence[tsc].segment(5,16), 0, sps, true);
1017 if (!midMidamble)
1018 return false;
1019
Thomas Tsou3eaae802013-08-20 19:31:14 -04001020 /* Simulated receive sequence is pulse shaped */
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001021 midamble = modulateBurst(gTrainingSequence[tsc], 0, sps, false);
1022 if (!midamble) {
1023 status = false;
1024 goto release;
dburgessb3a0ca42011-10-12 07:44:40 +00001025 }
Thomas Tsou3eaae802013-08-20 19:31:14 -04001026
dburgessb3a0ca42011-10-12 07:44:40 +00001027 // NOTE: Because ideal TSC 16-bit midamble is 66 symbols into burst,
1028 // the ideal TSC has an + 180 degree phase shift,
1029 // due to the pi/2 frequency shift, that
1030 // needs to be accounted for.
1031 // 26-midamble is 61 symbols into burst, has +90 degree phase shift.
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001032 scaleVector(*midMidamble, complex(-1.0, 0.0));
1033 scaleVector(*midamble, complex(0.0, 1.0));
dburgessb3a0ca42011-10-12 07:44:40 +00001034
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001035 conjugateVector(*midMidamble);
dburgessb3a0ca42011-10-12 07:44:40 +00001036
Thomas Tsou3eaae802013-08-20 19:31:14 -04001037 /* For SSE alignment, reallocate the midamble sequence on 16-byte boundary */
1038 data = (complex *) convolve_h_alloc(midMidamble->size());
1039 _midMidamble = new signalVector(data, 0, midMidamble->size());
1040 _midMidamble->setAligned(true);
1041 memcpy(_midMidamble->begin(), midMidamble->begin(),
1042 midMidamble->size() * sizeof(complex));
1043
1044 autocorr = convolve(midamble, _midMidamble, NULL, NO_DELAY);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001045 if (!autocorr) {
1046 status = false;
1047 goto release;
1048 }
dburgessb3a0ca42011-10-12 07:44:40 +00001049
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001050 gMidambles[tsc] = new CorrelationSequence;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001051 gMidambles[tsc]->buffer = data;
1052 gMidambles[tsc]->sequence = _midMidamble;
1053 gMidambles[tsc]->gain = peakDetect(*autocorr,&gMidambles[tsc]->TOA, NULL);
dburgessb3a0ca42011-10-12 07:44:40 +00001054
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001055release:
dburgessb3a0ca42011-10-12 07:44:40 +00001056 delete autocorr;
1057 delete midamble;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001058 delete midMidamble;
dburgessb3a0ca42011-10-12 07:44:40 +00001059
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001060 if (!status) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001061 delete _midMidamble;
1062 free(data);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001063 gMidambles[tsc] = NULL;
1064 }
1065
1066 return status;
dburgessb3a0ca42011-10-12 07:44:40 +00001067}
1068
Thomas Tsou83e06892013-08-20 16:10:01 -04001069bool generateRACHSequence(int sps)
dburgessb3a0ca42011-10-12 07:44:40 +00001070{
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001071 bool status = true;
1072 complex *data = NULL;
1073 signalVector *autocorr = NULL;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001074 signalVector *seq0 = NULL, *seq1 = NULL, *_seq1 = NULL;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001075
1076 delete gRACHSequence;
1077
1078 seq0 = modulateBurst(gRACHSynchSequence, 0, sps, false);
1079 if (!seq0)
1080 return false;
1081
1082 seq1 = modulateBurst(gRACHSynchSequence.segment(0, 40), 0, sps, true);
1083 if (!seq1) {
1084 status = false;
1085 goto release;
dburgessb3a0ca42011-10-12 07:44:40 +00001086 }
1087
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001088 conjugateVector(*seq1);
dburgessb3a0ca42011-10-12 07:44:40 +00001089
Thomas Tsou3eaae802013-08-20 19:31:14 -04001090 /* For SSE alignment, reallocate the midamble sequence on 16-byte boundary */
1091 data = (complex *) convolve_h_alloc(seq1->size());
1092 _seq1 = new signalVector(data, 0, seq1->size());
1093 _seq1->setAligned(true);
1094 memcpy(_seq1->begin(), seq1->begin(), seq1->size() * sizeof(complex));
1095
1096 autocorr = convolve(seq0, _seq1, autocorr, NO_DELAY);
1097 if (!autocorr) {
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001098 status = false;
1099 goto release;
1100 }
dburgessb3a0ca42011-10-12 07:44:40 +00001101
1102 gRACHSequence = new CorrelationSequence;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001103 gRACHSequence->sequence = _seq1;
1104 gRACHSequence->buffer = data;
1105 gRACHSequence->gain = peakDetect(*autocorr,&gRACHSequence->TOA, NULL);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001106
1107release:
dburgessb3a0ca42011-10-12 07:44:40 +00001108 delete autocorr;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001109 delete seq0;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001110 delete seq1;
dburgessb3a0ca42011-10-12 07:44:40 +00001111
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001112 if (!status) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001113 delete _seq1;
1114 free(data);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001115 gRACHSequence = NULL;
1116 }
dburgessb3a0ca42011-10-12 07:44:40 +00001117
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001118 return status;
dburgessb3a0ca42011-10-12 07:44:40 +00001119}
Thomas Tsou3eaae802013-08-20 19:31:14 -04001120
Thomas Tsou865bca42013-08-21 20:58:00 -04001121static float computePeakRatio(signalVector *corr,
1122 int sps, float toa, complex amp)
dburgessb3a0ca42011-10-12 07:44:40 +00001123{
Thomas Tsou865bca42013-08-21 20:58:00 -04001124 int num = 0;
1125 complex *peak;
1126 float rms, avg = 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001127
Thomas Tsou865bca42013-08-21 20:58:00 -04001128 peak = corr->begin() + (int) rint(toa);
dburgessb3a0ca42011-10-12 07:44:40 +00001129
Thomas Tsou865bca42013-08-21 20:58:00 -04001130 /* Check for bogus results */
1131 if ((toa < 0.0) || (toa > corr->size()))
1132 return 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001133
Thomas Tsou3eaae802013-08-20 19:31:14 -04001134 for (int i = 2 * sps; i <= 5 * sps; i++) {
Thomas Tsou865bca42013-08-21 20:58:00 -04001135 if (peak - i >= corr->begin()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001136 avg += (peak - i)->norm2();
1137 num++;
1138 }
Thomas Tsou865bca42013-08-21 20:58:00 -04001139 if (peak + i < corr->end()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001140 avg += (peak + i)->norm2();
1141 num++;
1142 }
dburgessb3a0ca42011-10-12 07:44:40 +00001143 }
1144
Thomas Tsou3eaae802013-08-20 19:31:14 -04001145 if (num < 2)
Thomas Tsou865bca42013-08-21 20:58:00 -04001146 return 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001147
Thomas Tsou3eaae802013-08-20 19:31:14 -04001148 rms = sqrtf(avg / (float) num) + 0.00001;
dburgessb3a0ca42011-10-12 07:44:40 +00001149
Thomas Tsou865bca42013-08-21 20:58:00 -04001150 return (amp.abs()) / rms;
dburgessb3a0ca42011-10-12 07:44:40 +00001151}
1152
1153bool energyDetect(signalVector &rxBurst,
1154 unsigned windowLength,
1155 float detectThreshold,
1156 float *avgPwr)
1157{
1158
1159 signalVector::const_iterator windowItr = rxBurst.begin(); //+rxBurst.size()/2 - 5*windowLength/2;
1160 float energy = 0.0;
1161 if (windowLength < 0) windowLength = 20;
1162 if (windowLength > rxBurst.size()) windowLength = rxBurst.size();
1163 for (unsigned i = 0; i < windowLength; i++) {
1164 energy += windowItr->norm2();
1165 windowItr+=4;
1166 }
1167 if (avgPwr) *avgPwr = energy/windowLength;
dburgessb3a0ca42011-10-12 07:44:40 +00001168 return (energy/windowLength > detectThreshold*detectThreshold);
1169}
dburgessb3a0ca42011-10-12 07:44:40 +00001170
Thomas Tsou865bca42013-08-21 20:58:00 -04001171/*
1172 * Detect a burst based on correlation and peak-to-average ratio
1173 *
1174 * For one sampler-per-symbol, perform fast peak detection (no interpolation)
1175 * for initial gating. We do this because energy detection should be disabled.
1176 * For higher oversampling values, we assume the energy detector is in place
1177 * and we run full interpolating peak detection.
1178 */
1179static int detectBurst(signalVector &burst,
1180 signalVector &corr, CorrelationSequence *sync,
1181 float thresh, int sps, complex *amp, float *toa,
1182 int start, int len)
dburgessb3a0ca42011-10-12 07:44:40 +00001183{
Thomas Tsou865bca42013-08-21 20:58:00 -04001184 /* Correlate */
1185 if (!convolve(&burst, sync->sequence, &corr,
Thomas Tsou3eaae802013-08-20 19:31:14 -04001186 CUSTOM, start, len, sps, 0)) {
1187 return -1;
dburgessb3a0ca42011-10-12 07:44:40 +00001188 }
1189
Thomas Tsou865bca42013-08-21 20:58:00 -04001190 /* Peak detection - place restrictions at correlation edges */
1191 *amp = fastPeakDetect(corr, toa);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001192
Thomas Tsou865bca42013-08-21 20:58:00 -04001193 if ((*toa < 3 * sps) || (*toa > len - 3 * sps))
1194 return 0;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001195
Thomas Tsou865bca42013-08-21 20:58:00 -04001196 /* Peak -to-average ratio */
1197 if (computePeakRatio(&corr, sps, *toa, *amp) < thresh)
1198 return 0;
1199
1200 /* Compute peak-to-average ratio. Reject if we don't have enough values */
1201 *amp = peakDetect(corr, toa, NULL);
1202
1203 /* Normalize our channel gain */
1204 *amp = *amp / sync->gain;
1205
Thomas Tsoua57bc8a2013-09-05 08:16:47 +08001206 /* Compenate for residual rotation with dual Laurent pulse */
1207 if (sps == 4)
1208 *amp = *amp * complex(0.0, 1.0);
1209
Thomas Tsou865bca42013-08-21 20:58:00 -04001210 return 1;
1211}
1212
1213/*
1214 * RACH burst detection
1215 *
1216 * Correlation window parameters:
1217 * target: Tail bits + RACH length (reduced from 41 to a multiple of 4)
1218 * head: Search 8 symbols before target
1219 * tail: Search 8 symbols after target
1220 */
1221int detectRACHBurst(signalVector &rxBurst,
1222 float thresh,
1223 int sps,
1224 complex *amp,
1225 float *toa)
1226{
1227 int rc, start, target, head, tail, len;
1228 float _toa;
1229 complex _amp;
1230 signalVector corr;
1231 CorrelationSequence *sync;
1232
1233 if ((sps != 1) && (sps != 4))
1234 return -1;
1235
1236 target = 8 + 40;
1237 head = 8;
1238 tail = head;
1239
1240 start = (target - head) * sps - 1;
1241 len = (head + tail) * sps;
1242 sync = gRACHSequence;
1243 corr = signalVector(len);
1244
1245 rc = detectBurst(rxBurst, corr, sync,
1246 thresh, sps, &_amp, &_toa, start, len);
1247 if (rc < 0) {
1248 return -1;
1249 } else if (!rc) {
1250 if (amp)
1251 *amp = 0.0f;
1252 if (toa)
1253 *toa = 0.0f;
1254 return 0;
dburgessb3a0ca42011-10-12 07:44:40 +00001255 }
1256
Thomas Tsou865bca42013-08-21 20:58:00 -04001257 /* Subtract forward search bits from delay */
1258 if (toa)
1259 *toa = _toa - head * sps;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001260 if (amp)
Thomas Tsou865bca42013-08-21 20:58:00 -04001261 *amp = _amp;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001262
Thomas Tsou865bca42013-08-21 20:58:00 -04001263 return 1;
1264}
Thomas Tsou3eaae802013-08-20 19:31:14 -04001265
Thomas Tsou865bca42013-08-21 20:58:00 -04001266/*
1267 * Normal burst detection
1268 *
1269 * Correlation window parameters:
1270 * target: Tail + data + mid-midamble + 1/2 remaining midamblebits
1271 * head: Search 8 symbols before target
1272 * tail: Search 8 symbols + maximum expected delay
1273 */
1274int analyzeTrafficBurst(signalVector &rxBurst, unsigned tsc, float thresh,
1275 int sps, complex *amp, float *toa, unsigned max_toa,
1276 bool chan_req, signalVector **chan, float *chan_offset)
1277{
1278 int rc, start, target, head, tail, len;
1279 complex _amp;
1280 float _toa;
1281 signalVector corr;
1282 CorrelationSequence *sync;
1283
1284 if ((tsc < 0) || (tsc > 7) || ((sps != 1) && (sps != 4)))
1285 return -1;
1286
1287 target = 3 + 58 + 16 + 5;
1288 head = 8;
1289 tail = head + max_toa;
1290
1291 start = (target - head) * sps - 1;
1292 len = (head + tail) * sps;
1293 sync = gMidambles[tsc];
1294 corr = signalVector(len);
1295
1296 rc = detectBurst(rxBurst, corr, sync,
1297 thresh, sps, &_amp, &_toa, start, len);
1298 if (rc < 0) {
1299 return -1;
1300 } else if (!rc) {
1301 if (amp)
1302 *amp = 0.0f;
1303 if (toa)
1304 *toa = 0.0f;
1305 return 0;
1306 }
1307
1308 /* Subtract forward search bits from delay */
1309 _toa -= head * sps;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001310 if (toa)
1311 *toa = _toa;
Thomas Tsou865bca42013-08-21 20:58:00 -04001312 if (amp)
1313 *amp = _amp;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001314
Thomas Tsou865bca42013-08-21 20:58:00 -04001315 /* Equalization not currently supported */
Thomas Tsou3eaae802013-08-20 19:31:14 -04001316 if (chan_req) {
Thomas Tsou865bca42013-08-21 20:58:00 -04001317 *chan = new signalVector(6 * sps);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001318
1319 if (chan_offset)
Thomas Tsou865bca42013-08-21 20:58:00 -04001320 *chan_offset = 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001321 }
1322
Thomas Tsou3eaae802013-08-20 19:31:14 -04001323 return 1;
dburgessb3a0ca42011-10-12 07:44:40 +00001324}
1325
1326signalVector *decimateVector(signalVector &wVector,
1327 int decimationFactor)
1328{
1329
1330 if (decimationFactor <= 1) return NULL;
1331
1332 signalVector *decVector = new signalVector(wVector.size()/decimationFactor);
1333 decVector->isRealOnly(wVector.isRealOnly());
1334
1335 signalVector::iterator vecItr = decVector->begin();
1336 for (unsigned int i = 0; i < wVector.size();i+=decimationFactor)
1337 *vecItr++ = wVector[i];
1338
1339 return decVector;
1340}
1341
1342
Thomas Tsou83e06892013-08-20 16:10:01 -04001343SoftVector *demodulateBurst(signalVector &rxBurst, int sps,
1344 complex channel, float TOA)
dburgessb3a0ca42011-10-12 07:44:40 +00001345{
1346 scaleVector(rxBurst,((complex) 1.0)/channel);
1347 delayVector(rxBurst,-TOA);
1348
1349 signalVector *shapedBurst = &rxBurst;
1350
1351 // shift up by a quarter of a frequency
1352 // ignore starting phase, since spec allows for discontinuous phase
1353 GMSKReverseRotate(*shapedBurst);
1354
1355 // run through slicer
Thomas Tsoud24cc2c2013-08-20 15:41:45 -04001356 if (sps > 1) {
1357 signalVector *decShapedBurst = decimateVector(*shapedBurst, sps);
dburgessb3a0ca42011-10-12 07:44:40 +00001358 shapedBurst = decShapedBurst;
1359 }
1360
dburgessb3a0ca42011-10-12 07:44:40 +00001361 vectorSlicer(shapedBurst);
1362
1363 SoftVector *burstBits = new SoftVector(shapedBurst->size());
1364
1365 SoftVector::iterator burstItr = burstBits->begin();
1366 signalVector::iterator shapedItr = shapedBurst->begin();
1367 for (; shapedItr < shapedBurst->end(); shapedItr++)
1368 *burstItr++ = shapedItr->real();
1369
Thomas Tsoud24cc2c2013-08-20 15:41:45 -04001370 if (sps > 1)
1371 delete shapedBurst;
dburgessb3a0ca42011-10-12 07:44:40 +00001372
1373 return burstBits;
1374
1375}
dburgessb3a0ca42011-10-12 07:44:40 +00001376
dburgessb3a0ca42011-10-12 07:44:40 +00001377// Assumes symbol-spaced sampling!!!
1378// Based upon paper by Al-Dhahir and Cioffi
1379bool designDFE(signalVector &channelResponse,
1380 float SNRestimate,
1381 int Nf,
1382 signalVector **feedForwardFilter,
1383 signalVector **feedbackFilter)
1384{
1385
1386 signalVector G0(Nf);
1387 signalVector G1(Nf);
1388 signalVector::iterator G0ptr = G0.begin();
1389 signalVector::iterator G1ptr = G1.begin();
1390 signalVector::iterator chanPtr = channelResponse.begin();
1391
1392 int nu = channelResponse.size()-1;
1393
1394 *G0ptr = 1.0/sqrtf(SNRestimate);
1395 for(int j = 0; j <= nu; j++) {
1396 *G1ptr = chanPtr->conj();
1397 G1ptr++; chanPtr++;
1398 }
1399
1400 signalVector *L[Nf];
1401 signalVector::iterator Lptr;
1402 float d;
1403 for(int i = 0; i < Nf; i++) {
1404 d = G0.begin()->norm2() + G1.begin()->norm2();
1405 L[i] = new signalVector(Nf+nu);
1406 Lptr = L[i]->begin()+i;
1407 G0ptr = G0.begin(); G1ptr = G1.begin();
1408 while ((G0ptr < G0.end()) && (Lptr < L[i]->end())) {
1409 *Lptr = (*G0ptr*(G0.begin()->conj()) + *G1ptr*(G1.begin()->conj()) )/d;
1410 Lptr++;
1411 G0ptr++;
1412 G1ptr++;
1413 }
1414 complex k = (*G1.begin())/(*G0.begin());
1415
1416 if (i != Nf-1) {
1417 signalVector G0new = G1;
1418 scaleVector(G0new,k.conj());
1419 addVector(G0new,G0);
1420
1421 signalVector G1new = G0;
1422 scaleVector(G1new,k*(-1.0));
1423 addVector(G1new,G1);
1424 delayVector(G1new,-1.0);
1425
1426 scaleVector(G0new,1.0/sqrtf(1.0+k.norm2()));
1427 scaleVector(G1new,1.0/sqrtf(1.0+k.norm2()));
1428 G0 = G0new;
1429 G1 = G1new;
1430 }
1431 }
1432
1433 *feedbackFilter = new signalVector(nu);
1434 L[Nf-1]->segmentCopyTo(**feedbackFilter,Nf,nu);
1435 scaleVector(**feedbackFilter,(complex) -1.0);
1436 conjugateVector(**feedbackFilter);
1437
1438 signalVector v(Nf);
1439 signalVector::iterator vStart = v.begin();
1440 signalVector::iterator vPtr;
1441 *(vStart+Nf-1) = (complex) 1.0;
1442 for(int k = Nf-2; k >= 0; k--) {
1443 Lptr = L[k]->begin()+k+1;
1444 vPtr = vStart + k+1;
1445 complex v_k = 0.0;
1446 for (int j = k+1; j < Nf; j++) {
1447 v_k -= (*vPtr)*(*Lptr);
1448 vPtr++; Lptr++;
1449 }
1450 *(vStart + k) = v_k;
1451 }
1452
1453 *feedForwardFilter = new signalVector(Nf);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001454 signalVector::iterator w = (*feedForwardFilter)->end();
dburgessb3a0ca42011-10-12 07:44:40 +00001455 for (int i = 0; i < Nf; i++) {
1456 delete L[i];
1457 complex w_i = 0.0;
1458 int endPt = ( nu < (Nf-1-i) ) ? nu : (Nf-1-i);
1459 vPtr = vStart+i;
1460 chanPtr = channelResponse.begin();
1461 for (int k = 0; k < endPt+1; k++) {
1462 w_i += (*vPtr)*(chanPtr->conj());
1463 vPtr++; chanPtr++;
1464 }
Thomas Tsou3eaae802013-08-20 19:31:14 -04001465 *--w = w_i/d;
dburgessb3a0ca42011-10-12 07:44:40 +00001466 }
1467
1468
1469 return true;
1470
1471}
1472
1473// Assumes symbol-rate sampling!!!!
1474SoftVector *equalizeBurst(signalVector &rxBurst,
1475 float TOA,
Thomas Tsoud24cc2c2013-08-20 15:41:45 -04001476 int sps,
dburgessb3a0ca42011-10-12 07:44:40 +00001477 signalVector &w, // feedforward filter
1478 signalVector &b) // feedback filter
1479{
Thomas Tsou3eaae802013-08-20 19:31:14 -04001480 signalVector *postForwardFull;
dburgessb3a0ca42011-10-12 07:44:40 +00001481
Thomas Tsou3eaae802013-08-20 19:31:14 -04001482 if (!delayVector(rxBurst, -TOA))
1483 return NULL;
dburgessb3a0ca42011-10-12 07:44:40 +00001484
Thomas Tsou3eaae802013-08-20 19:31:14 -04001485 postForwardFull = convolve(&rxBurst, &w, NULL,
1486 CUSTOM, 0, rxBurst.size() + w.size() - 1);
1487 if (!postForwardFull)
1488 return NULL;
dburgessb3a0ca42011-10-12 07:44:40 +00001489
1490 signalVector* postForward = new signalVector(rxBurst.size());
1491 postForwardFull->segmentCopyTo(*postForward,w.size()-1,rxBurst.size());
1492 delete postForwardFull;
1493
1494 signalVector::iterator dPtr = postForward->begin();
1495 signalVector::iterator dBackPtr;
1496 signalVector::iterator rotPtr = GMSKRotation->begin();
1497 signalVector::iterator revRotPtr = GMSKReverseRotation->begin();
1498
1499 signalVector *DFEoutput = new signalVector(postForward->size());
1500 signalVector::iterator DFEItr = DFEoutput->begin();
1501
1502 // NOTE: can insert the midamble and/or use midamble to estimate BER
1503 for (; dPtr < postForward->end(); dPtr++) {
1504 dBackPtr = dPtr-1;
1505 signalVector::iterator bPtr = b.begin();
1506 while ( (bPtr < b.end()) && (dBackPtr >= postForward->begin()) ) {
1507 *dPtr = *dPtr + (*bPtr)*(*dBackPtr);
1508 bPtr++;
1509 dBackPtr--;
1510 }
1511 *dPtr = *dPtr * (*revRotPtr);
1512 *DFEItr = *dPtr;
1513 // make decision on symbol
1514 *dPtr = (dPtr->real() > 0.0) ? 1.0 : -1.0;
1515 //*DFEItr = *dPtr;
1516 *dPtr = *dPtr * (*rotPtr);
1517 DFEItr++;
1518 rotPtr++;
1519 revRotPtr++;
1520 }
1521
1522 vectorSlicer(DFEoutput);
1523
1524 SoftVector *burstBits = new SoftVector(postForward->size());
1525 SoftVector::iterator burstItr = burstBits->begin();
1526 DFEItr = DFEoutput->begin();
1527 for (; DFEItr < DFEoutput->end(); DFEItr++)
1528 *burstItr++ = DFEItr->real();
1529
1530 delete postForward;
1531
1532 delete DFEoutput;
1533
1534 return burstBits;
1535}