blob: ab771ac1937da7cb9c8ea49598a090a310250070 [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{
Thomas Tsou2c282f52013-10-08 21:34:35 -0400769 int whole, h_len = 20;
770 float frac;
771 complex *data;
772 signalVector *h, *shift;
773 signalVector::iterator itr;
Thomas Tsou3eaae802013-08-20 19:31:14 -0400774
Thomas Tsou2c282f52013-10-08 21:34:35 -0400775 whole = floor(delay);
776 frac = delay - whole;
777
778 /* Sinc interpolated fractional shift (if allowable) */
779 if (fabs(frac) > 1e-2) {
780 data = (complex *) convolve_h_alloc(h_len);
781 h = new signalVector(data, 0, h_len);
782 h->setAligned(true);
783 h->isRealOnly(true);
784
785 itr = h->end();
786 for (int i = 0; i < h_len; i++)
787 *--itr = (complex) sinc(M_PI_F * (i - h_len / 2 - frac));
788
789 shift = convolve(&wBurst, h, NULL, NO_DELAY);
790
791 delete h;
792 free(data);
793
794 if (!shift)
Thomas Tsou3eaae802013-08-20 19:31:14 -0400795 return false;
Thomas Tsou2c282f52013-10-08 21:34:35 -0400796
797 wBurst.clone(*shift);
798 delete shift;
dburgessb3a0ca42011-10-12 07:44:40 +0000799 }
800
Thomas Tsou2c282f52013-10-08 21:34:35 -0400801 /* Integer sample shift */
802 if (whole < 0) {
803 whole = -whole;
dburgessb3a0ca42011-10-12 07:44:40 +0000804 signalVector::iterator wBurstItr = wBurst.begin();
Thomas Tsou2c282f52013-10-08 21:34:35 -0400805 signalVector::iterator shiftedItr = wBurst.begin() + whole;
806
dburgessb3a0ca42011-10-12 07:44:40 +0000807 while (shiftedItr < wBurst.end())
808 *wBurstItr++ = *shiftedItr++;
809 while (wBurstItr < wBurst.end())
810 *wBurstItr++ = 0.0;
Thomas Tsou2c282f52013-10-08 21:34:35 -0400811 } else {
812 signalVector::iterator wBurstItr = wBurst.end() - 1;
813 signalVector::iterator shiftedItr = wBurst.end() - 1 - whole;
814
dburgessb3a0ca42011-10-12 07:44:40 +0000815 while (shiftedItr >= wBurst.begin())
816 *wBurstItr-- = *shiftedItr--;
817 while (wBurstItr >= wBurst.begin())
818 *wBurstItr-- = 0.0;
819 }
Thomas Tsou2c282f52013-10-08 21:34:35 -0400820
821 return true;
dburgessb3a0ca42011-10-12 07:44:40 +0000822}
Thomas Tsou2c282f52013-10-08 21:34:35 -0400823
dburgessb3a0ca42011-10-12 07:44:40 +0000824signalVector *gaussianNoise(int length,
825 float variance,
826 complex mean)
827{
828
829 signalVector *noise = new signalVector(length);
830 signalVector::iterator nPtr = noise->begin();
831 float stddev = sqrtf(variance);
832 while (nPtr < noise->end()) {
833 float u1 = (float) rand()/ (float) RAND_MAX;
834 while (u1==0.0)
835 u1 = (float) rand()/ (float) RAND_MAX;
836 float u2 = (float) rand()/ (float) RAND_MAX;
837 float arg = 2.0*M_PI*u2;
838 *nPtr = mean + stddev*complex(cos(arg),sin(arg))*sqrtf(-2.0*log(u1));
839 nPtr++;
840 }
841
842 return noise;
843}
844
845complex interpolatePoint(const signalVector &inSig,
846 float ix)
847{
848
849 int start = (int) (floor(ix) - 10);
850 if (start < 0) start = 0;
851 int end = (int) (floor(ix) + 11);
852 if ((unsigned) end > inSig.size()-1) end = inSig.size()-1;
853
854 complex pVal = 0.0;
855 if (!inSig.isRealOnly()) {
856 for (int i = start; i < end; i++)
857 pVal += inSig[i] * sinc(M_PI_F*(i-ix));
858 }
859 else {
860 for (int i = start; i < end; i++)
861 pVal += inSig[i].real() * sinc(M_PI_F*(i-ix));
862 }
863
864 return pVal;
865}
866
Thomas Tsou8181b012013-08-20 21:17:19 -0400867static complex fastPeakDetect(const signalVector &rxBurst, float *index)
868{
869 float val, max = 0.0f;
870 complex amp;
871 int _index = -1;
872
873 for (int i = 0; i < rxBurst.size(); i++) {
874 val = rxBurst[i].norm2();
875 if (val > max) {
876 max = val;
877 _index = i;
878 amp = rxBurst[i];
879 }
880 }
881
882 if (index)
883 *index = (float) _index;
884
885 return amp;
886}
887
dburgessb3a0ca42011-10-12 07:44:40 +0000888complex peakDetect(const signalVector &rxBurst,
889 float *peakIndex,
890 float *avgPwr)
891{
892
893
894 complex maxVal = 0.0;
895 float maxIndex = -1;
896 float sumPower = 0.0;
897
898 for (unsigned int i = 0; i < rxBurst.size(); i++) {
899 float samplePower = rxBurst[i].norm2();
900 if (samplePower > maxVal.real()) {
901 maxVal = samplePower;
902 maxIndex = i;
903 }
904 sumPower += samplePower;
905 }
906
907 // interpolate around the peak
908 // to save computation, we'll use early-late balancing
909 float earlyIndex = maxIndex-1;
910 float lateIndex = maxIndex+1;
911
912 float incr = 0.5;
913 while (incr > 1.0/1024.0) {
914 complex earlyP = interpolatePoint(rxBurst,earlyIndex);
915 complex lateP = interpolatePoint(rxBurst,lateIndex);
916 if (earlyP < lateP)
917 earlyIndex += incr;
918 else if (earlyP > lateP)
919 earlyIndex -= incr;
920 else break;
921 incr /= 2.0;
922 lateIndex = earlyIndex + 2.0;
923 }
924
925 maxIndex = earlyIndex + 1.0;
926 maxVal = interpolatePoint(rxBurst,maxIndex);
927
928 if (peakIndex!=NULL)
929 *peakIndex = maxIndex;
930
931 if (avgPwr!=NULL)
932 *avgPwr = (sumPower-maxVal.norm2()) / (rxBurst.size()-1);
933
934 return maxVal;
935
936}
937
938void scaleVector(signalVector &x,
939 complex scale)
940{
941 signalVector::iterator xP = x.begin();
942 signalVector::iterator xPEnd = x.end();
943 if (!x.isRealOnly()) {
944 while (xP < xPEnd) {
945 *xP = *xP * scale;
946 xP++;
947 }
948 }
949 else {
950 while (xP < xPEnd) {
951 *xP = xP->real() * scale;
952 xP++;
953 }
954 }
955}
956
957/** in-place conjugation */
958void conjugateVector(signalVector &x)
959{
960 if (x.isRealOnly()) return;
961 signalVector::iterator xP = x.begin();
962 signalVector::iterator xPEnd = x.end();
963 while (xP < xPEnd) {
964 *xP = xP->conj();
965 xP++;
966 }
967}
968
969
970// in-place addition!!
971bool addVector(signalVector &x,
972 signalVector &y)
973{
974 signalVector::iterator xP = x.begin();
975 signalVector::iterator yP = y.begin();
976 signalVector::iterator xPEnd = x.end();
977 signalVector::iterator yPEnd = y.end();
978 while ((xP < xPEnd) && (yP < yPEnd)) {
979 *xP = *xP + *yP;
980 xP++; yP++;
981 }
982 return true;
983}
984
985// in-place multiplication!!
986bool multVector(signalVector &x,
987 signalVector &y)
988{
989 signalVector::iterator xP = x.begin();
990 signalVector::iterator yP = y.begin();
991 signalVector::iterator xPEnd = x.end();
992 signalVector::iterator yPEnd = y.end();
993 while ((xP < xPEnd) && (yP < yPEnd)) {
994 *xP = (*xP) * (*yP);
995 xP++; yP++;
996 }
997 return true;
998}
999
1000
1001void offsetVector(signalVector &x,
1002 complex offset)
1003{
1004 signalVector::iterator xP = x.begin();
1005 signalVector::iterator xPEnd = x.end();
1006 if (!x.isRealOnly()) {
1007 while (xP < xPEnd) {
1008 *xP += offset;
1009 xP++;
1010 }
1011 }
1012 else {
1013 while (xP < xPEnd) {
1014 *xP = xP->real() + offset;
1015 xP++;
1016 }
1017 }
1018}
1019
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001020bool generateMidamble(int sps, int tsc)
dburgessb3a0ca42011-10-12 07:44:40 +00001021{
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001022 bool status = true;
1023 complex *data = NULL;
1024 signalVector *autocorr = NULL, *midamble = NULL;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001025 signalVector *midMidamble = NULL, *_midMidamble = NULL;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001026
Thomas Tsou3eaae802013-08-20 19:31:14 -04001027 if ((tsc < 0) || (tsc > 7))
dburgessb3a0ca42011-10-12 07:44:40 +00001028 return false;
1029
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001030 delete gMidambles[tsc];
Thomas Tsou3eaae802013-08-20 19:31:14 -04001031
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001032 /* Use middle 16 bits of each TSC. Correlation sequence is not pulse shaped */
1033 midMidamble = modulateBurst(gTrainingSequence[tsc].segment(5,16), 0, sps, true);
1034 if (!midMidamble)
1035 return false;
1036
Thomas Tsou3eaae802013-08-20 19:31:14 -04001037 /* Simulated receive sequence is pulse shaped */
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001038 midamble = modulateBurst(gTrainingSequence[tsc], 0, sps, false);
1039 if (!midamble) {
1040 status = false;
1041 goto release;
dburgessb3a0ca42011-10-12 07:44:40 +00001042 }
Thomas Tsou3eaae802013-08-20 19:31:14 -04001043
dburgessb3a0ca42011-10-12 07:44:40 +00001044 // NOTE: Because ideal TSC 16-bit midamble is 66 symbols into burst,
1045 // the ideal TSC has an + 180 degree phase shift,
1046 // due to the pi/2 frequency shift, that
1047 // needs to be accounted for.
1048 // 26-midamble is 61 symbols into burst, has +90 degree phase shift.
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001049 scaleVector(*midMidamble, complex(-1.0, 0.0));
1050 scaleVector(*midamble, complex(0.0, 1.0));
dburgessb3a0ca42011-10-12 07:44:40 +00001051
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001052 conjugateVector(*midMidamble);
dburgessb3a0ca42011-10-12 07:44:40 +00001053
Thomas Tsou3eaae802013-08-20 19:31:14 -04001054 /* For SSE alignment, reallocate the midamble sequence on 16-byte boundary */
1055 data = (complex *) convolve_h_alloc(midMidamble->size());
1056 _midMidamble = new signalVector(data, 0, midMidamble->size());
1057 _midMidamble->setAligned(true);
1058 memcpy(_midMidamble->begin(), midMidamble->begin(),
1059 midMidamble->size() * sizeof(complex));
1060
1061 autocorr = convolve(midamble, _midMidamble, NULL, NO_DELAY);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001062 if (!autocorr) {
1063 status = false;
1064 goto release;
1065 }
dburgessb3a0ca42011-10-12 07:44:40 +00001066
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001067 gMidambles[tsc] = new CorrelationSequence;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001068 gMidambles[tsc]->buffer = data;
1069 gMidambles[tsc]->sequence = _midMidamble;
1070 gMidambles[tsc]->gain = peakDetect(*autocorr,&gMidambles[tsc]->TOA, NULL);
dburgessb3a0ca42011-10-12 07:44:40 +00001071
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001072release:
dburgessb3a0ca42011-10-12 07:44:40 +00001073 delete autocorr;
1074 delete midamble;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001075 delete midMidamble;
dburgessb3a0ca42011-10-12 07:44:40 +00001076
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001077 if (!status) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001078 delete _midMidamble;
1079 free(data);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001080 gMidambles[tsc] = NULL;
1081 }
1082
1083 return status;
dburgessb3a0ca42011-10-12 07:44:40 +00001084}
1085
Thomas Tsou83e06892013-08-20 16:10:01 -04001086bool generateRACHSequence(int sps)
dburgessb3a0ca42011-10-12 07:44:40 +00001087{
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001088 bool status = true;
1089 complex *data = NULL;
1090 signalVector *autocorr = NULL;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001091 signalVector *seq0 = NULL, *seq1 = NULL, *_seq1 = NULL;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001092
1093 delete gRACHSequence;
1094
1095 seq0 = modulateBurst(gRACHSynchSequence, 0, sps, false);
1096 if (!seq0)
1097 return false;
1098
1099 seq1 = modulateBurst(gRACHSynchSequence.segment(0, 40), 0, sps, true);
1100 if (!seq1) {
1101 status = false;
1102 goto release;
dburgessb3a0ca42011-10-12 07:44:40 +00001103 }
1104
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001105 conjugateVector(*seq1);
dburgessb3a0ca42011-10-12 07:44:40 +00001106
Thomas Tsou3eaae802013-08-20 19:31:14 -04001107 /* For SSE alignment, reallocate the midamble sequence on 16-byte boundary */
1108 data = (complex *) convolve_h_alloc(seq1->size());
1109 _seq1 = new signalVector(data, 0, seq1->size());
1110 _seq1->setAligned(true);
1111 memcpy(_seq1->begin(), seq1->begin(), seq1->size() * sizeof(complex));
1112
1113 autocorr = convolve(seq0, _seq1, autocorr, NO_DELAY);
1114 if (!autocorr) {
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001115 status = false;
1116 goto release;
1117 }
dburgessb3a0ca42011-10-12 07:44:40 +00001118
1119 gRACHSequence = new CorrelationSequence;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001120 gRACHSequence->sequence = _seq1;
1121 gRACHSequence->buffer = data;
1122 gRACHSequence->gain = peakDetect(*autocorr,&gRACHSequence->TOA, NULL);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001123
1124release:
dburgessb3a0ca42011-10-12 07:44:40 +00001125 delete autocorr;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001126 delete seq0;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001127 delete seq1;
dburgessb3a0ca42011-10-12 07:44:40 +00001128
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001129 if (!status) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001130 delete _seq1;
1131 free(data);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001132 gRACHSequence = NULL;
1133 }
dburgessb3a0ca42011-10-12 07:44:40 +00001134
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001135 return status;
dburgessb3a0ca42011-10-12 07:44:40 +00001136}
Thomas Tsou3eaae802013-08-20 19:31:14 -04001137
Thomas Tsou865bca42013-08-21 20:58:00 -04001138static float computePeakRatio(signalVector *corr,
1139 int sps, float toa, complex amp)
dburgessb3a0ca42011-10-12 07:44:40 +00001140{
Thomas Tsou865bca42013-08-21 20:58:00 -04001141 int num = 0;
1142 complex *peak;
1143 float rms, avg = 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001144
Thomas Tsou865bca42013-08-21 20:58:00 -04001145 peak = corr->begin() + (int) rint(toa);
dburgessb3a0ca42011-10-12 07:44:40 +00001146
Thomas Tsou865bca42013-08-21 20:58:00 -04001147 /* Check for bogus results */
1148 if ((toa < 0.0) || (toa > corr->size()))
1149 return 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001150
Thomas Tsou3eaae802013-08-20 19:31:14 -04001151 for (int i = 2 * sps; i <= 5 * sps; i++) {
Thomas Tsou865bca42013-08-21 20:58:00 -04001152 if (peak - i >= corr->begin()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001153 avg += (peak - i)->norm2();
1154 num++;
1155 }
Thomas Tsou865bca42013-08-21 20:58:00 -04001156 if (peak + i < corr->end()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001157 avg += (peak + i)->norm2();
1158 num++;
1159 }
dburgessb3a0ca42011-10-12 07:44:40 +00001160 }
1161
Thomas Tsou3eaae802013-08-20 19:31:14 -04001162 if (num < 2)
Thomas Tsou865bca42013-08-21 20:58:00 -04001163 return 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001164
Thomas Tsou3eaae802013-08-20 19:31:14 -04001165 rms = sqrtf(avg / (float) num) + 0.00001;
dburgessb3a0ca42011-10-12 07:44:40 +00001166
Thomas Tsou865bca42013-08-21 20:58:00 -04001167 return (amp.abs()) / rms;
dburgessb3a0ca42011-10-12 07:44:40 +00001168}
1169
1170bool energyDetect(signalVector &rxBurst,
1171 unsigned windowLength,
1172 float detectThreshold,
1173 float *avgPwr)
1174{
1175
1176 signalVector::const_iterator windowItr = rxBurst.begin(); //+rxBurst.size()/2 - 5*windowLength/2;
1177 float energy = 0.0;
1178 if (windowLength < 0) windowLength = 20;
1179 if (windowLength > rxBurst.size()) windowLength = rxBurst.size();
1180 for (unsigned i = 0; i < windowLength; i++) {
1181 energy += windowItr->norm2();
1182 windowItr+=4;
1183 }
1184 if (avgPwr) *avgPwr = energy/windowLength;
dburgessb3a0ca42011-10-12 07:44:40 +00001185 return (energy/windowLength > detectThreshold*detectThreshold);
1186}
dburgessb3a0ca42011-10-12 07:44:40 +00001187
Thomas Tsou865bca42013-08-21 20:58:00 -04001188/*
1189 * Detect a burst based on correlation and peak-to-average ratio
1190 *
1191 * For one sampler-per-symbol, perform fast peak detection (no interpolation)
1192 * for initial gating. We do this because energy detection should be disabled.
1193 * For higher oversampling values, we assume the energy detector is in place
1194 * and we run full interpolating peak detection.
1195 */
1196static int detectBurst(signalVector &burst,
1197 signalVector &corr, CorrelationSequence *sync,
1198 float thresh, int sps, complex *amp, float *toa,
1199 int start, int len)
dburgessb3a0ca42011-10-12 07:44:40 +00001200{
Thomas Tsou865bca42013-08-21 20:58:00 -04001201 /* Correlate */
1202 if (!convolve(&burst, sync->sequence, &corr,
Thomas Tsou3eaae802013-08-20 19:31:14 -04001203 CUSTOM, start, len, sps, 0)) {
1204 return -1;
dburgessb3a0ca42011-10-12 07:44:40 +00001205 }
1206
Thomas Tsou865bca42013-08-21 20:58:00 -04001207 /* Peak detection - place restrictions at correlation edges */
1208 *amp = fastPeakDetect(corr, toa);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001209
Thomas Tsou865bca42013-08-21 20:58:00 -04001210 if ((*toa < 3 * sps) || (*toa > len - 3 * sps))
1211 return 0;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001212
Thomas Tsou865bca42013-08-21 20:58:00 -04001213 /* Peak -to-average ratio */
1214 if (computePeakRatio(&corr, sps, *toa, *amp) < thresh)
1215 return 0;
1216
1217 /* Compute peak-to-average ratio. Reject if we don't have enough values */
1218 *amp = peakDetect(corr, toa, NULL);
1219
1220 /* Normalize our channel gain */
1221 *amp = *amp / sync->gain;
1222
Thomas Tsoua57bc8a2013-09-05 08:16:47 +08001223 /* Compenate for residual rotation with dual Laurent pulse */
1224 if (sps == 4)
1225 *amp = *amp * complex(0.0, 1.0);
1226
Thomas Tsou865bca42013-08-21 20:58:00 -04001227 return 1;
1228}
1229
1230/*
1231 * RACH burst detection
1232 *
1233 * Correlation window parameters:
1234 * target: Tail bits + RACH length (reduced from 41 to a multiple of 4)
Thomas Tsoudafb3372013-09-18 16:21:26 -04001235 * head: Search 4 symbols before target
1236 * tail: Search 10 symbols after target
Thomas Tsou865bca42013-08-21 20:58:00 -04001237 */
1238int detectRACHBurst(signalVector &rxBurst,
1239 float thresh,
1240 int sps,
1241 complex *amp,
1242 float *toa)
1243{
1244 int rc, start, target, head, tail, len;
1245 float _toa;
1246 complex _amp;
1247 signalVector corr;
1248 CorrelationSequence *sync;
1249
1250 if ((sps != 1) && (sps != 4))
1251 return -1;
1252
1253 target = 8 + 40;
Thomas Tsoudafb3372013-09-18 16:21:26 -04001254 head = 4;
1255 tail = 10;
Thomas Tsou865bca42013-08-21 20:58:00 -04001256
1257 start = (target - head) * sps - 1;
1258 len = (head + tail) * sps;
1259 sync = gRACHSequence;
1260 corr = signalVector(len);
1261
1262 rc = detectBurst(rxBurst, corr, sync,
1263 thresh, sps, &_amp, &_toa, start, len);
1264 if (rc < 0) {
1265 return -1;
1266 } else if (!rc) {
1267 if (amp)
1268 *amp = 0.0f;
1269 if (toa)
1270 *toa = 0.0f;
1271 return 0;
dburgessb3a0ca42011-10-12 07:44:40 +00001272 }
1273
Thomas Tsou865bca42013-08-21 20:58:00 -04001274 /* Subtract forward search bits from delay */
1275 if (toa)
1276 *toa = _toa - head * sps;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001277 if (amp)
Thomas Tsou865bca42013-08-21 20:58:00 -04001278 *amp = _amp;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001279
Thomas Tsou865bca42013-08-21 20:58:00 -04001280 return 1;
1281}
Thomas Tsou3eaae802013-08-20 19:31:14 -04001282
Thomas Tsou865bca42013-08-21 20:58:00 -04001283/*
1284 * Normal burst detection
1285 *
1286 * Correlation window parameters:
1287 * target: Tail + data + mid-midamble + 1/2 remaining midamblebits
Thomas Tsoudafb3372013-09-18 16:21:26 -04001288 * head: Search 4 symbols before target
1289 * tail: Search 4 symbols + maximum expected delay
Thomas Tsou865bca42013-08-21 20:58:00 -04001290 */
1291int analyzeTrafficBurst(signalVector &rxBurst, unsigned tsc, float thresh,
1292 int sps, complex *amp, float *toa, unsigned max_toa,
1293 bool chan_req, signalVector **chan, float *chan_offset)
1294{
1295 int rc, start, target, head, tail, len;
1296 complex _amp;
1297 float _toa;
1298 signalVector corr;
1299 CorrelationSequence *sync;
1300
1301 if ((tsc < 0) || (tsc > 7) || ((sps != 1) && (sps != 4)))
1302 return -1;
1303
1304 target = 3 + 58 + 16 + 5;
Thomas Tsoudafb3372013-09-18 16:21:26 -04001305 head = 4;
1306 tail = 4 + max_toa;
Thomas Tsou865bca42013-08-21 20:58:00 -04001307
1308 start = (target - head) * sps - 1;
1309 len = (head + tail) * sps;
1310 sync = gMidambles[tsc];
1311 corr = signalVector(len);
1312
1313 rc = detectBurst(rxBurst, corr, sync,
1314 thresh, sps, &_amp, &_toa, start, len);
1315 if (rc < 0) {
1316 return -1;
1317 } else if (!rc) {
1318 if (amp)
1319 *amp = 0.0f;
1320 if (toa)
1321 *toa = 0.0f;
1322 return 0;
1323 }
1324
1325 /* Subtract forward search bits from delay */
1326 _toa -= head * sps;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001327 if (toa)
1328 *toa = _toa;
Thomas Tsou865bca42013-08-21 20:58:00 -04001329 if (amp)
1330 *amp = _amp;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001331
Thomas Tsou865bca42013-08-21 20:58:00 -04001332 /* Equalization not currently supported */
Thomas Tsou3eaae802013-08-20 19:31:14 -04001333 if (chan_req) {
Thomas Tsou865bca42013-08-21 20:58:00 -04001334 *chan = new signalVector(6 * sps);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001335
1336 if (chan_offset)
Thomas Tsou865bca42013-08-21 20:58:00 -04001337 *chan_offset = 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001338 }
1339
Thomas Tsou3eaae802013-08-20 19:31:14 -04001340 return 1;
dburgessb3a0ca42011-10-12 07:44:40 +00001341}
1342
1343signalVector *decimateVector(signalVector &wVector,
1344 int decimationFactor)
1345{
1346
1347 if (decimationFactor <= 1) return NULL;
1348
1349 signalVector *decVector = new signalVector(wVector.size()/decimationFactor);
1350 decVector->isRealOnly(wVector.isRealOnly());
1351
1352 signalVector::iterator vecItr = decVector->begin();
1353 for (unsigned int i = 0; i < wVector.size();i+=decimationFactor)
1354 *vecItr++ = wVector[i];
1355
1356 return decVector;
1357}
1358
1359
Thomas Tsou83e06892013-08-20 16:10:01 -04001360SoftVector *demodulateBurst(signalVector &rxBurst, int sps,
1361 complex channel, float TOA)
dburgessb3a0ca42011-10-12 07:44:40 +00001362{
1363 scaleVector(rxBurst,((complex) 1.0)/channel);
1364 delayVector(rxBurst,-TOA);
1365
1366 signalVector *shapedBurst = &rxBurst;
1367
1368 // shift up by a quarter of a frequency
1369 // ignore starting phase, since spec allows for discontinuous phase
1370 GMSKReverseRotate(*shapedBurst);
1371
1372 // run through slicer
Thomas Tsoud24cc2c2013-08-20 15:41:45 -04001373 if (sps > 1) {
1374 signalVector *decShapedBurst = decimateVector(*shapedBurst, sps);
dburgessb3a0ca42011-10-12 07:44:40 +00001375 shapedBurst = decShapedBurst;
1376 }
1377
dburgessb3a0ca42011-10-12 07:44:40 +00001378 vectorSlicer(shapedBurst);
1379
1380 SoftVector *burstBits = new SoftVector(shapedBurst->size());
1381
1382 SoftVector::iterator burstItr = burstBits->begin();
1383 signalVector::iterator shapedItr = shapedBurst->begin();
1384 for (; shapedItr < shapedBurst->end(); shapedItr++)
1385 *burstItr++ = shapedItr->real();
1386
Thomas Tsoud24cc2c2013-08-20 15:41:45 -04001387 if (sps > 1)
1388 delete shapedBurst;
dburgessb3a0ca42011-10-12 07:44:40 +00001389
1390 return burstBits;
1391
1392}
dburgessb3a0ca42011-10-12 07:44:40 +00001393
dburgessb3a0ca42011-10-12 07:44:40 +00001394// Assumes symbol-spaced sampling!!!
1395// Based upon paper by Al-Dhahir and Cioffi
1396bool designDFE(signalVector &channelResponse,
1397 float SNRestimate,
1398 int Nf,
1399 signalVector **feedForwardFilter,
1400 signalVector **feedbackFilter)
1401{
1402
1403 signalVector G0(Nf);
1404 signalVector G1(Nf);
1405 signalVector::iterator G0ptr = G0.begin();
1406 signalVector::iterator G1ptr = G1.begin();
1407 signalVector::iterator chanPtr = channelResponse.begin();
1408
1409 int nu = channelResponse.size()-1;
1410
1411 *G0ptr = 1.0/sqrtf(SNRestimate);
1412 for(int j = 0; j <= nu; j++) {
1413 *G1ptr = chanPtr->conj();
1414 G1ptr++; chanPtr++;
1415 }
1416
1417 signalVector *L[Nf];
1418 signalVector::iterator Lptr;
1419 float d;
1420 for(int i = 0; i < Nf; i++) {
1421 d = G0.begin()->norm2() + G1.begin()->norm2();
1422 L[i] = new signalVector(Nf+nu);
1423 Lptr = L[i]->begin()+i;
1424 G0ptr = G0.begin(); G1ptr = G1.begin();
1425 while ((G0ptr < G0.end()) && (Lptr < L[i]->end())) {
1426 *Lptr = (*G0ptr*(G0.begin()->conj()) + *G1ptr*(G1.begin()->conj()) )/d;
1427 Lptr++;
1428 G0ptr++;
1429 G1ptr++;
1430 }
1431 complex k = (*G1.begin())/(*G0.begin());
1432
1433 if (i != Nf-1) {
1434 signalVector G0new = G1;
1435 scaleVector(G0new,k.conj());
1436 addVector(G0new,G0);
1437
1438 signalVector G1new = G0;
1439 scaleVector(G1new,k*(-1.0));
1440 addVector(G1new,G1);
1441 delayVector(G1new,-1.0);
1442
1443 scaleVector(G0new,1.0/sqrtf(1.0+k.norm2()));
1444 scaleVector(G1new,1.0/sqrtf(1.0+k.norm2()));
1445 G0 = G0new;
1446 G1 = G1new;
1447 }
1448 }
1449
1450 *feedbackFilter = new signalVector(nu);
1451 L[Nf-1]->segmentCopyTo(**feedbackFilter,Nf,nu);
1452 scaleVector(**feedbackFilter,(complex) -1.0);
1453 conjugateVector(**feedbackFilter);
1454
1455 signalVector v(Nf);
1456 signalVector::iterator vStart = v.begin();
1457 signalVector::iterator vPtr;
1458 *(vStart+Nf-1) = (complex) 1.0;
1459 for(int k = Nf-2; k >= 0; k--) {
1460 Lptr = L[k]->begin()+k+1;
1461 vPtr = vStart + k+1;
1462 complex v_k = 0.0;
1463 for (int j = k+1; j < Nf; j++) {
1464 v_k -= (*vPtr)*(*Lptr);
1465 vPtr++; Lptr++;
1466 }
1467 *(vStart + k) = v_k;
1468 }
1469
1470 *feedForwardFilter = new signalVector(Nf);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001471 signalVector::iterator w = (*feedForwardFilter)->end();
dburgessb3a0ca42011-10-12 07:44:40 +00001472 for (int i = 0; i < Nf; i++) {
1473 delete L[i];
1474 complex w_i = 0.0;
1475 int endPt = ( nu < (Nf-1-i) ) ? nu : (Nf-1-i);
1476 vPtr = vStart+i;
1477 chanPtr = channelResponse.begin();
1478 for (int k = 0; k < endPt+1; k++) {
1479 w_i += (*vPtr)*(chanPtr->conj());
1480 vPtr++; chanPtr++;
1481 }
Thomas Tsou3eaae802013-08-20 19:31:14 -04001482 *--w = w_i/d;
dburgessb3a0ca42011-10-12 07:44:40 +00001483 }
1484
1485
1486 return true;
1487
1488}
1489
1490// Assumes symbol-rate sampling!!!!
1491SoftVector *equalizeBurst(signalVector &rxBurst,
1492 float TOA,
Thomas Tsoud24cc2c2013-08-20 15:41:45 -04001493 int sps,
dburgessb3a0ca42011-10-12 07:44:40 +00001494 signalVector &w, // feedforward filter
1495 signalVector &b) // feedback filter
1496{
Thomas Tsou3eaae802013-08-20 19:31:14 -04001497 signalVector *postForwardFull;
dburgessb3a0ca42011-10-12 07:44:40 +00001498
Thomas Tsou3eaae802013-08-20 19:31:14 -04001499 if (!delayVector(rxBurst, -TOA))
1500 return NULL;
dburgessb3a0ca42011-10-12 07:44:40 +00001501
Thomas Tsou3eaae802013-08-20 19:31:14 -04001502 postForwardFull = convolve(&rxBurst, &w, NULL,
1503 CUSTOM, 0, rxBurst.size() + w.size() - 1);
1504 if (!postForwardFull)
1505 return NULL;
dburgessb3a0ca42011-10-12 07:44:40 +00001506
1507 signalVector* postForward = new signalVector(rxBurst.size());
1508 postForwardFull->segmentCopyTo(*postForward,w.size()-1,rxBurst.size());
1509 delete postForwardFull;
1510
1511 signalVector::iterator dPtr = postForward->begin();
1512 signalVector::iterator dBackPtr;
1513 signalVector::iterator rotPtr = GMSKRotation->begin();
1514 signalVector::iterator revRotPtr = GMSKReverseRotation->begin();
1515
1516 signalVector *DFEoutput = new signalVector(postForward->size());
1517 signalVector::iterator DFEItr = DFEoutput->begin();
1518
1519 // NOTE: can insert the midamble and/or use midamble to estimate BER
1520 for (; dPtr < postForward->end(); dPtr++) {
1521 dBackPtr = dPtr-1;
1522 signalVector::iterator bPtr = b.begin();
1523 while ( (bPtr < b.end()) && (dBackPtr >= postForward->begin()) ) {
1524 *dPtr = *dPtr + (*bPtr)*(*dBackPtr);
1525 bPtr++;
1526 dBackPtr--;
1527 }
1528 *dPtr = *dPtr * (*revRotPtr);
1529 *DFEItr = *dPtr;
1530 // make decision on symbol
1531 *dPtr = (dPtr->real() > 0.0) ? 1.0 : -1.0;
1532 //*DFEItr = *dPtr;
1533 *dPtr = *dPtr * (*rotPtr);
1534 DFEItr++;
1535 rotPtr++;
1536 revRotPtr++;
1537 }
1538
1539 vectorSlicer(DFEoutput);
1540
1541 SoftVector *burstBits = new SoftVector(postForward->size());
1542 SoftVector::iterator burstItr = burstBits->begin();
1543 DFEItr = DFEoutput->begin();
1544 for (; DFEItr < DFEoutput->end(); DFEItr++)
1545 *burstItr++ = DFEItr->real();
1546
1547 delete postForward;
1548
1549 delete DFEoutput;
1550
1551 return burstBits;
1552}