blob: 595efa3edbb46a041d227cd41ecfaf7d107c278e [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"
27
Alexander Chemerisd734e2d2013-06-16 14:30:58 +040028using namespace GSM;
29
Thomas Tsou3eaae802013-08-20 19:31:14 -040030extern "C" {
31#include "convolve.h"
32}
33
dburgessb3a0ca42011-10-12 07:44:40 +000034#define TABLESIZE 1024
35
36/** Lookup tables for trigonometric approximation */
37float cosTable[TABLESIZE+1]; // add 1 element for wrap around
38float sinTable[TABLESIZE+1];
39
40/** Constants */
41static const float M_PI_F = (float)M_PI;
42static const float M_2PI_F = (float)(2.0*M_PI);
43static const float M_1_2PI_F = 1/M_2PI_F;
44
Thomas Tsouc1f7c422013-10-11 13:49:55 -040045/* Precomputed rotation vectors */
46static signalVector *GMSKRotationN = NULL;
47static signalVector *GMSKReverseRotationN = NULL;
48static signalVector *GMSKRotation1 = NULL;
49static signalVector *GMSKReverseRotation1 = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +000050
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;
Thomas Tsouc1f7c422013-10-11 13:49:55 -040070 float toa;
dburgessb3a0ca42011-10-12 07:44:40 +000071 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;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400104PulseSequence *GSMPulse1 = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000105
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400106void sigProcLibDestroy()
107{
dburgessb3a0ca42011-10-12 07:44:40 +0000108 for (int i = 0; i < 8; i++) {
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400109 delete gMidambles[i];
110 gMidambles[i] = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000111 }
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400112
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400113 delete GMSKRotationN;
114 delete GMSKReverseRotationN;
115 delete GMSKRotation1;
116 delete GMSKReverseRotation1;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400117 delete gRACHSequence;
118 delete GSMPulse;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400119 delete GSMPulse1;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400120
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400121 GMSKRotationN = NULL;
122 GMSKRotation1 = NULL;
123 GMSKReverseRotationN = NULL;
124 GMSKReverseRotation1 = NULL;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -0400125 gRACHSequence = NULL;
126 GSMPulse = NULL;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400127 GSMPulse1 = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000128}
129
dburgessb3a0ca42011-10-12 07:44:40 +0000130// dB relative to 1.0.
131// if > 1.0, then return 0 dB
132float dB(float x) {
133
134 float arg = 1.0F;
135 float dB = 0.0F;
136
137 if (x >= 1.0F) return 0.0F;
138 if (x <= 0.0F) return -200.0F;
139
140 float prevArg = arg;
141 float prevdB = dB;
142 float stepSize = 16.0F;
143 float dBstepSize = 12.0F;
144 while (stepSize > 1.0F) {
145 do {
146 prevArg = arg;
147 prevdB = dB;
148 arg /= stepSize;
149 dB -= dBstepSize;
150 } while (arg > x);
151 arg = prevArg;
152 dB = prevdB;
153 stepSize *= 0.5F;
154 dBstepSize -= 3.0F;
155 }
156 return ((arg-x)*(dB-3.0F) + (x-arg*0.5F)*dB)/(arg - arg*0.5F);
157
158}
159
160// 10^(-dB/10), inverse of dB func.
161float dBinv(float x) {
162
163 float arg = 1.0F;
164 float dB = 0.0F;
165
166 if (x >= 0.0F) return 1.0F;
167 if (x <= -200.0F) return 0.0F;
168
169 float prevArg = arg;
170 float prevdB = dB;
171 float stepSize = 16.0F;
172 float dBstepSize = 12.0F;
173 while (stepSize > 1.0F) {
174 do {
175 prevArg = arg;
176 prevdB = dB;
177 arg /= stepSize;
178 dB -= dBstepSize;
179 } while (dB > x);
180 arg = prevArg;
181 dB = prevdB;
182 stepSize *= 0.5F;
183 dBstepSize -= 3.0F;
184 }
185
186 return ((dB-x)*(arg*0.5F)+(x-(dB-3.0F))*(arg))/3.0F;
187
188}
189
190float vectorNorm2(const signalVector &x)
191{
192 signalVector::const_iterator xPtr = x.begin();
193 float Energy = 0.0;
194 for (;xPtr != x.end();xPtr++) {
195 Energy += xPtr->norm2();
196 }
197 return Energy;
198}
199
200
201float vectorPower(const signalVector &x)
202{
203 return vectorNorm2(x)/x.size();
204}
205
206/** compute cosine via lookup table */
207float cosLookup(const float x)
208{
209 float arg = x*M_1_2PI_F;
210 while (arg > 1.0F) arg -= 1.0F;
211 while (arg < 0.0F) arg += 1.0F;
212
213 const float argT = arg*((float)TABLESIZE);
214 const int argI = (int)argT;
215 const float delta = argT-argI;
216 const float iDelta = 1.0F-delta;
217 return iDelta*cosTable[argI] + delta*cosTable[argI+1];
218}
219
220/** compute sine via lookup table */
221float sinLookup(const float x)
222{
223 float arg = x*M_1_2PI_F;
224 while (arg > 1.0F) arg -= 1.0F;
225 while (arg < 0.0F) arg += 1.0F;
226
227 const float argT = arg*((float)TABLESIZE);
228 const int argI = (int)argT;
229 const float delta = argT-argI;
230 const float iDelta = 1.0F-delta;
231 return iDelta*sinTable[argI] + delta*sinTable[argI+1];
232}
233
234
235/** compute e^(-jx) via lookup table. */
236complex expjLookup(float x)
237{
238 float arg = x*M_1_2PI_F;
239 while (arg > 1.0F) arg -= 1.0F;
240 while (arg < 0.0F) arg += 1.0F;
241
242 const float argT = arg*((float)TABLESIZE);
243 const int argI = (int)argT;
244 const float delta = argT-argI;
245 const float iDelta = 1.0F-delta;
246 return complex(iDelta*cosTable[argI] + delta*cosTable[argI+1],
247 iDelta*sinTable[argI] + delta*sinTable[argI+1]);
248}
249
250/** Library setup functions */
251void initTrigTables() {
252 for (int i = 0; i < TABLESIZE+1; i++) {
253 cosTable[i] = cos(2.0*M_PI*i/TABLESIZE);
254 sinTable[i] = sin(2.0*M_PI*i/TABLESIZE);
255 }
256}
257
Thomas Tsoud24cc2c2013-08-20 15:41:45 -0400258void initGMSKRotationTables(int sps)
259{
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400260 GMSKRotationN = new signalVector(157 * sps);
261 GMSKReverseRotationN = new signalVector(157 * sps);
262 signalVector::iterator rotPtr = GMSKRotationN->begin();
263 signalVector::iterator revPtr = GMSKReverseRotationN->begin();
dburgessb3a0ca42011-10-12 07:44:40 +0000264 float phase = 0.0;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400265 while (rotPtr != GMSKRotationN->end()) {
dburgessb3a0ca42011-10-12 07:44:40 +0000266 *rotPtr++ = expjLookup(phase);
267 *revPtr++ = expjLookup(-phase);
Thomas Tsoud24cc2c2013-08-20 15:41:45 -0400268 phase += M_PI_F / 2.0F / (float) sps;
dburgessb3a0ca42011-10-12 07:44:40 +0000269 }
dburgessb3a0ca42011-10-12 07:44:40 +0000270
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400271 GMSKRotation1 = new signalVector(157);
272 GMSKReverseRotation1 = new signalVector(157);
273 rotPtr = GMSKRotation1->begin();
274 revPtr = GMSKReverseRotation1->begin();
275 phase = 0.0;
276 while (rotPtr != GMSKRotation1->end()) {
277 *rotPtr++ = expjLookup(phase);
278 *revPtr++ = expjLookup(-phase);
279 phase += M_PI_F / 2.0F;
Thomas Tsoue57004d2013-08-20 18:55:33 -0400280 }
dburgessb3a0ca42011-10-12 07:44:40 +0000281}
282
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400283static void GMSKRotate(signalVector &x, int sps)
284{
285 signalVector::iterator rotPtr, xPtr = x.begin();
286
287 if (sps == 1)
288 rotPtr = GMSKRotation1->begin();
289 else
290 rotPtr = GMSKRotationN->begin();
291
dburgessb3a0ca42011-10-12 07:44:40 +0000292 if (x.isRealOnly()) {
293 while (xPtr < x.end()) {
294 *xPtr = *rotPtr++ * (xPtr->real());
295 xPtr++;
296 }
297 }
298 else {
299 while (xPtr < x.end()) {
300 *xPtr = *rotPtr++ * (*xPtr);
301 xPtr++;
302 }
303 }
304}
305
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400306static void GMSKReverseRotate(signalVector &x, int sps)
307{
308 signalVector::iterator rotPtr, xPtr= x.begin();
309
310 if (sps == 1)
311 rotPtr = GMSKReverseRotation1->begin();
312 else
313 rotPtr = GMSKReverseRotationN->begin();
314
dburgessb3a0ca42011-10-12 07:44:40 +0000315 if (x.isRealOnly()) {
316 while (xPtr < x.end()) {
317 *xPtr = *rotPtr++ * (xPtr->real());
318 xPtr++;
319 }
320 }
321 else {
322 while (xPtr < x.end()) {
323 *xPtr = *rotPtr++ * (*xPtr);
324 xPtr++;
325 }
326 }
327}
328
Thomas Tsou3eaae802013-08-20 19:31:14 -0400329signalVector *convolve(const signalVector *x,
330 const signalVector *h,
331 signalVector *y,
332 ConvType spanType, int start,
333 unsigned len, unsigned step, int offset)
dburgessb3a0ca42011-10-12 07:44:40 +0000334{
Thomas Tsou3eaae802013-08-20 19:31:14 -0400335 int rc, head = 0, tail = 0;
336 bool alloc = false, append = false;
337 const signalVector *_x = NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000338
Thomas Tsou3eaae802013-08-20 19:31:14 -0400339 if (!x || !h)
dburgessb3a0ca42011-10-12 07:44:40 +0000340 return NULL;
341
Thomas Tsou3eaae802013-08-20 19:31:14 -0400342 switch (spanType) {
343 case START_ONLY:
344 start = 0;
345 head = h->size();
346 len = x->size();
347 append = true;
dburgessb3a0ca42011-10-12 07:44:40 +0000348 break;
Thomas Tsou3eaae802013-08-20 19:31:14 -0400349 case NO_DELAY:
350 start = h->size() / 2;
351 head = start;
352 tail = start;
353 len = x->size();
354 append = true;
355 break;
356 case CUSTOM:
357 if (start < h->size() - 1) {
358 head = h->size() - start;
359 append = true;
360 }
361 if (start + len > x->size()) {
362 tail = start + len - x->size();
363 append = true;
dburgessb3a0ca42011-10-12 07:44:40 +0000364 }
365 break;
366 default:
367 return NULL;
dburgessb3a0ca42011-10-12 07:44:40 +0000368 }
dburgessb3a0ca42011-10-12 07:44:40 +0000369
Thomas Tsou3eaae802013-08-20 19:31:14 -0400370 /*
371 * Error if the output vector is too small. Create the output vector
372 * if the pointer is NULL.
373 */
374 if (y && (len > y->size()))
375 return NULL;
376 if (!y) {
377 y = new signalVector(len);
378 alloc = true;
379 }
380
381 /* Prepend or post-pend the input vector if the parameters require it */
382 if (append)
383 _x = new signalVector(*x, head, tail);
384 else
385 _x = x;
386
387 /*
388 * Four convovle types:
389 * 1. Complex-Real (aligned)
390 * 2. Complex-Complex (aligned)
391 * 3. Complex-Real (!aligned)
392 * 4. Complex-Complex (!aligned)
393 */
394 if (h->isRealOnly() && h->isAligned()) {
395 rc = convolve_real((float *) _x->begin(), _x->size(),
396 (float *) h->begin(), h->size(),
397 (float *) y->begin(), y->size(),
398 start, len, step, offset);
399 } else if (!h->isRealOnly() && h->isAligned()) {
400 rc = convolve_complex((float *) _x->begin(), _x->size(),
401 (float *) h->begin(), h->size(),
402 (float *) y->begin(), y->size(),
403 start, len, step, offset);
404 } else if (h->isRealOnly() && !h->isAligned()) {
405 rc = base_convolve_real((float *) _x->begin(), _x->size(),
406 (float *) h->begin(), h->size(),
407 (float *) y->begin(), y->size(),
408 start, len, step, offset);
409 } else if (!h->isRealOnly() && !h->isAligned()) {
410 rc = base_convolve_complex((float *) _x->begin(), _x->size(),
411 (float *) h->begin(), h->size(),
412 (float *) y->begin(), y->size(),
413 start, len, step, offset);
414 } else {
415 rc = -1;
416 }
417
418 if (append)
419 delete _x;
420
421 if (rc < 0) {
422 if (alloc)
423 delete y;
424 return NULL;
425 }
426
427 return y;
428}
dburgessb3a0ca42011-10-12 07:44:40 +0000429
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400430static bool generateC1Pulse(int sps, PulseSequence *pulse)
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800431{
432 int len;
433
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400434 if (!pulse)
435 return false;
436
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800437 switch (sps) {
438 case 4:
439 len = 8;
440 break;
441 default:
442 return false;
443 }
444
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400445 pulse->c1_buffer = convolve_h_alloc(len);
446 pulse->c1 = new signalVector((complex *)
447 pulse->c1_buffer, 0, len);
448 pulse->c1->isRealOnly(true);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800449
450 /* Enable alignment for SSE usage */
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400451 pulse->c1->setAligned(true);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800452
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400453 signalVector::iterator xP = pulse->c1->begin();
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800454
455 switch (sps) {
456 case 4:
457 /* BT = 0.30 */
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400458 *xP++ = 0.0;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800459 *xP++ = 8.16373112e-03;
460 *xP++ = 2.84385729e-02;
461 *xP++ = 5.64158904e-02;
462 *xP++ = 7.05463553e-02;
463 *xP++ = 5.64158904e-02;
464 *xP++ = 2.84385729e-02;
465 *xP++ = 8.16373112e-03;
466 }
467
468 return true;
469}
470
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400471static PulseSequence *generateGSMPulse(int sps, int symbolLength)
dburgessb3a0ca42011-10-12 07:44:40 +0000472{
Thomas Tsou83e06892013-08-20 16:10:01 -0400473 int len;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800474 float arg, avg, center;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400475 PulseSequence *pulse;
Thomas Tsou83e06892013-08-20 16:10:01 -0400476
477 /* Store a single tap filter used for correlation sequence generation */
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400478 pulse = new PulseSequence();
479 pulse->empty = new signalVector(1);
480 pulse->empty->isRealOnly(true);
481 *(pulse->empty->begin()) = 1.0f;
Thomas Tsou83e06892013-08-20 16:10:01 -0400482
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400483 /*
484 * For 4 samples-per-symbol use a precomputed single pulse Laurent
485 * approximation. This should yields below 2 degrees of phase error at
486 * the modulator output. Use the existing pulse approximation for all
487 * other oversampling factors.
488 */
489 switch (sps) {
490 case 4:
491 len = 16;
492 break;
493 default:
494 len = sps * symbolLength;
495 if (len < 4)
496 len = 4;
497 }
Thomas Tsou3eaae802013-08-20 19:31:14 -0400498
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400499 pulse->c0_buffer = convolve_h_alloc(len);
500 pulse->c0 = new signalVector((complex *) pulse->c0_buffer, 0, len);
501 pulse->c0->isRealOnly(true);
Thomas Tsou3eaae802013-08-20 19:31:14 -0400502
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800503 /* Enable alingnment for SSE usage */
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400504 pulse->c0->setAligned(true);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800505
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400506 signalVector::iterator xP = pulse->c0->begin();
Thomas Tsou83e06892013-08-20 16:10:01 -0400507
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400508 if (sps == 4) {
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800509 *xP++ = 0.0;
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400510 *xP++ = 4.46348606e-03;
511 *xP++ = 2.84385729e-02;
512 *xP++ = 1.03184855e-01;
513 *xP++ = 2.56065552e-01;
514 *xP++ = 4.76375085e-01;
515 *xP++ = 7.05961177e-01;
516 *xP++ = 8.71291644e-01;
517 *xP++ = 9.29453645e-01;
518 *xP++ = 8.71291644e-01;
519 *xP++ = 7.05961177e-01;
520 *xP++ = 4.76375085e-01;
521 *xP++ = 2.56065552e-01;
522 *xP++ = 1.03184855e-01;
523 *xP++ = 2.84385729e-02;
524 *xP++ = 4.46348606e-03;
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400525 generateC1Pulse(sps, pulse);
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400526 } else {
527 center = (float) (len - 1.0) / 2.0;
Thomas Tsou83e06892013-08-20 16:10:01 -0400528
Thomas Tsou9ccd9f22013-08-21 13:59:52 -0400529 /* GSM pulse approximation */
530 for (int i = 0; i < len; i++) {
531 arg = ((float) i - center) / (float) sps;
532 *xP++ = 0.96 * exp(-1.1380 * arg * arg -
533 0.527 * arg * arg * arg * arg);
534 }
dburgessb3a0ca42011-10-12 07:44:40 +0000535
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400536 avg = sqrtf(vectorNorm2(*pulse->c0) / sps);
537 xP = pulse->c0->begin();
538 for (int i = 0; i < len; i++)
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800539 *xP++ /= avg;
540 }
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400541
542 return pulse;
dburgessb3a0ca42011-10-12 07:44:40 +0000543}
544
545signalVector* frequencyShift(signalVector *y,
546 signalVector *x,
547 float freq,
548 float startPhase,
549 float *finalPhase)
550{
551
552 if (!x) return NULL;
553
554 if (y==NULL) {
555 y = new signalVector(x->size());
556 y->isRealOnly(x->isRealOnly());
557 if (y==NULL) return NULL;
558 }
559
560 if (y->size() < x->size()) return NULL;
561
562 float phase = startPhase;
563 signalVector::iterator yP = y->begin();
564 signalVector::iterator xPEnd = x->end();
565 signalVector::iterator xP = x->begin();
566
567 if (x->isRealOnly()) {
568 while (xP < xPEnd) {
569 (*yP++) = expjLookup(phase)*( (xP++)->real() );
570 phase += freq;
571 }
572 }
573 else {
574 while (xP < xPEnd) {
575 (*yP++) = (*xP++)*expjLookup(phase);
576 phase += freq;
577 }
578 }
579
580
581 if (finalPhase) *finalPhase = phase;
582
583 return y;
584}
585
586signalVector* reverseConjugate(signalVector *b)
587{
588 signalVector *tmp = new signalVector(b->size());
589 tmp->isRealOnly(b->isRealOnly());
590 signalVector::iterator bP = b->begin();
591 signalVector::iterator bPEnd = b->end();
592 signalVector::iterator tmpP = tmp->end()-1;
593 if (!b->isRealOnly()) {
594 while (bP < bPEnd) {
595 *tmpP-- = bP->conj();
596 bP++;
597 }
598 }
599 else {
600 while (bP < bPEnd) {
601 *tmpP-- = bP->real();
602 bP++;
603 }
604 }
605
606 return tmp;
607}
608
dburgessb3a0ca42011-10-12 07:44:40 +0000609/* soft output slicer */
610bool vectorSlicer(signalVector *x)
611{
612
613 signalVector::iterator xP = x->begin();
614 signalVector::iterator xPEnd = x->end();
615 while (xP < xPEnd) {
616 *xP = (complex) (0.5*(xP->real()+1.0F));
617 if (xP->real() > 1.0) *xP = 1.0;
618 if (xP->real() < 0.0) *xP = 0.0;
619 xP++;
620 }
621 return true;
622}
Thomas Tsou3eaae802013-08-20 19:31:14 -0400623
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800624static signalVector *rotateBurst(const BitVector &wBurst,
625 int guardPeriodLength, int sps)
626{
627 int burst_len;
628 signalVector *pulse, rotated, *shaped;
629 signalVector::iterator itr;
630
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400631 pulse = GSMPulse1->empty;
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800632 burst_len = sps * (wBurst.size() + guardPeriodLength);
633 rotated = signalVector(burst_len);
634 itr = rotated.begin();
635
636 for (unsigned i = 0; i < wBurst.size(); i++) {
637 *itr = 2.0 * (wBurst[i] & 0x01) - 1.0;
638 itr += sps;
639 }
640
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400641 GMSKRotate(rotated, sps);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800642 rotated.isRealOnly(false);
643
644 /* Dummy filter operation */
645 shaped = convolve(&rotated, pulse, NULL, START_ONLY);
646 if (!shaped)
647 return NULL;
648
649 return shaped;
650}
651
652static signalVector *modulateBurstLaurent(const BitVector &bits,
653 int guard_len, int sps)
654{
655 int burst_len;
656 float phase;
657 signalVector *c0_pulse, *c1_pulse, c0_burst, c1_burst, *c0_shaped, *c1_shaped;
658 signalVector::iterator c0_itr, c1_itr;
659
660 /*
661 * Apply before and after bits to reduce phase error at burst edges.
662 * Make sure there is enough room in the burst to accomodate all bits.
663 */
664 if (guard_len < 4)
665 guard_len = 4;
666
667 c0_pulse = GSMPulse->c0;
668 c1_pulse = GSMPulse->c1;
669
670 burst_len = sps * (bits.size() + guard_len);
671
672 c0_burst = signalVector(burst_len);
673 c0_burst.isRealOnly(true);
674 c0_itr = c0_burst.begin();
675
676 c1_burst = signalVector(burst_len);
677 c1_burst.isRealOnly(true);
678 c1_itr = c1_burst.begin();
679
680 /* Padded differential start bits */
681 *c0_itr = 2.0 * (0x00 & 0x01) - 1.0;
682 c0_itr += sps;
683
684 /* Main burst bits */
685 for (unsigned i = 0; i < bits.size(); i++) {
686 *c0_itr = 2.0 * (bits[i] & 0x01) - 1.0;
687 c0_itr += sps;
688 }
689
690 /* Padded differential end bits */
691 *c0_itr = 2.0 * (0x01 & 0x01) - 1.0;
692
693 /* Generate C0 phase coefficients */
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400694 GMSKRotate(c0_burst, sps);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800695 c0_burst.isRealOnly(false);
696
697 c0_itr = c0_burst.begin();
698 c0_itr += sps * 2;
699 c1_itr += sps * 2;
700
701 /* Start magic */
702 phase = 2.0 * ((0x01 & 0x01) ^ (0x01 & 0x01)) - 1.0;
703 *c1_itr = *c0_itr * Complex<float>(0, phase);
704 c0_itr += sps;
705 c1_itr += sps;
706
707 /* Generate C1 phase coefficients */
708 for (unsigned i = 2; i < bits.size(); i++) {
709 phase = 2.0 * ((bits[i - 1] & 0x01) ^ (bits[i - 2] & 0x01)) - 1.0;
710 *c1_itr = *c0_itr * Complex<float>(0, phase);
711
712 c0_itr += sps;
713 c1_itr += sps;
714 }
715
716 /* End magic */
717 int i = bits.size();
718 phase = 2.0 * ((bits[i-1] & 0x01) ^ (bits[i-2] & 0x01)) - 1.0;
719 *c1_itr = *c0_itr * Complex<float>(0, phase);
720
721 /* Primary (C0) and secondary (C1) pulse shaping */
722 c0_shaped = convolve(&c0_burst, c0_pulse, NULL, START_ONLY);
723 c1_shaped = convolve(&c1_burst, c1_pulse, NULL, START_ONLY);
724
725 /* Sum shaped outputs into C0 */
726 c0_itr = c0_shaped->begin();
727 c1_itr = c1_shaped->begin();
728 for (unsigned i = 0; i < c0_shaped->size(); i++ )
729 *c0_itr++ += *c1_itr++;
730
731 delete c1_shaped;
732
733 return c0_shaped;
734}
735
736static signalVector *modulateBurstBasic(const BitVector &bits,
737 int guard_len, int sps)
738{
739 int burst_len;
740 signalVector *pulse, burst, *shaped;
741 signalVector::iterator burst_itr;
742
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400743 if (sps == 1)
744 pulse = GSMPulse1->c0;
745 else
746 pulse = GSMPulse->c0;
747
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800748 burst_len = sps * (bits.size() + guard_len);
749
750 burst = signalVector(burst_len);
751 burst.isRealOnly(true);
752 burst_itr = burst.begin();
753
754 /* Raw bits are not differentially encoded */
755 for (unsigned i = 0; i < bits.size(); i++) {
756 *burst_itr = 2.0 * (bits[i] & 0x01) - 1.0;
757 burst_itr += sps;
758 }
759
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400760 GMSKRotate(burst, sps);
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800761 burst.isRealOnly(false);
762
763 /* Single Gaussian pulse approximation shaping */
764 shaped = convolve(&burst, pulse, NULL, START_ONLY);
765
766 return shaped;
767}
768
Thomas Tsou3eaae802013-08-20 19:31:14 -0400769/* Assume input bits are not differentially encoded */
Thomas Tsou83e06892013-08-20 16:10:01 -0400770signalVector *modulateBurst(const BitVector &wBurst, int guardPeriodLength,
771 int sps, bool emptyPulse)
dburgessb3a0ca42011-10-12 07:44:40 +0000772{
Thomas Tsou83e06892013-08-20 16:10:01 -0400773 if (emptyPulse)
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800774 return rotateBurst(wBurst, guardPeriodLength, sps);
775 else if (sps == 4)
776 return modulateBurstLaurent(wBurst, guardPeriodLength, sps);
Thomas Tsou83e06892013-08-20 16:10:01 -0400777 else
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800778 return modulateBurstBasic(wBurst, guardPeriodLength, sps);
dburgessb3a0ca42011-10-12 07:44:40 +0000779}
780
Thomas Tsoua57bc8a2013-09-05 08:16:47 +0800781float sinc(float x)
dburgessb3a0ca42011-10-12 07:44:40 +0000782{
783 if ((x >= 0.01F) || (x <= -0.01F)) return (sinLookup(x)/x);
784 return 1.0F;
785}
786
Thomas Tsou3eaae802013-08-20 19:31:14 -0400787bool delayVector(signalVector &wBurst, float delay)
dburgessb3a0ca42011-10-12 07:44:40 +0000788{
Thomas Tsou2c282f52013-10-08 21:34:35 -0400789 int whole, h_len = 20;
790 float frac;
791 complex *data;
792 signalVector *h, *shift;
793 signalVector::iterator itr;
Thomas Tsou3eaae802013-08-20 19:31:14 -0400794
Thomas Tsou2c282f52013-10-08 21:34:35 -0400795 whole = floor(delay);
796 frac = delay - whole;
797
798 /* Sinc interpolated fractional shift (if allowable) */
799 if (fabs(frac) > 1e-2) {
800 data = (complex *) convolve_h_alloc(h_len);
801 h = new signalVector(data, 0, h_len);
802 h->setAligned(true);
803 h->isRealOnly(true);
804
805 itr = h->end();
806 for (int i = 0; i < h_len; i++)
807 *--itr = (complex) sinc(M_PI_F * (i - h_len / 2 - frac));
808
809 shift = convolve(&wBurst, h, NULL, NO_DELAY);
810
811 delete h;
812 free(data);
813
814 if (!shift)
Thomas Tsou3eaae802013-08-20 19:31:14 -0400815 return false;
Thomas Tsou2c282f52013-10-08 21:34:35 -0400816
817 wBurst.clone(*shift);
818 delete shift;
dburgessb3a0ca42011-10-12 07:44:40 +0000819 }
820
Thomas Tsou2c282f52013-10-08 21:34:35 -0400821 /* Integer sample shift */
822 if (whole < 0) {
823 whole = -whole;
dburgessb3a0ca42011-10-12 07:44:40 +0000824 signalVector::iterator wBurstItr = wBurst.begin();
Thomas Tsou2c282f52013-10-08 21:34:35 -0400825 signalVector::iterator shiftedItr = wBurst.begin() + whole;
826
dburgessb3a0ca42011-10-12 07:44:40 +0000827 while (shiftedItr < wBurst.end())
828 *wBurstItr++ = *shiftedItr++;
829 while (wBurstItr < wBurst.end())
830 *wBurstItr++ = 0.0;
Thomas Tsou2c282f52013-10-08 21:34:35 -0400831 } else {
832 signalVector::iterator wBurstItr = wBurst.end() - 1;
833 signalVector::iterator shiftedItr = wBurst.end() - 1 - whole;
834
dburgessb3a0ca42011-10-12 07:44:40 +0000835 while (shiftedItr >= wBurst.begin())
836 *wBurstItr-- = *shiftedItr--;
837 while (wBurstItr >= wBurst.begin())
838 *wBurstItr-- = 0.0;
839 }
Thomas Tsou2c282f52013-10-08 21:34:35 -0400840
841 return true;
dburgessb3a0ca42011-10-12 07:44:40 +0000842}
Thomas Tsou2c282f52013-10-08 21:34:35 -0400843
dburgessb3a0ca42011-10-12 07:44:40 +0000844signalVector *gaussianNoise(int length,
845 float variance,
846 complex mean)
847{
848
849 signalVector *noise = new signalVector(length);
850 signalVector::iterator nPtr = noise->begin();
851 float stddev = sqrtf(variance);
852 while (nPtr < noise->end()) {
853 float u1 = (float) rand()/ (float) RAND_MAX;
854 while (u1==0.0)
855 u1 = (float) rand()/ (float) RAND_MAX;
856 float u2 = (float) rand()/ (float) RAND_MAX;
857 float arg = 2.0*M_PI*u2;
858 *nPtr = mean + stddev*complex(cos(arg),sin(arg))*sqrtf(-2.0*log(u1));
859 nPtr++;
860 }
861
862 return noise;
863}
864
865complex interpolatePoint(const signalVector &inSig,
866 float ix)
867{
868
869 int start = (int) (floor(ix) - 10);
870 if (start < 0) start = 0;
871 int end = (int) (floor(ix) + 11);
872 if ((unsigned) end > inSig.size()-1) end = inSig.size()-1;
873
874 complex pVal = 0.0;
875 if (!inSig.isRealOnly()) {
876 for (int i = start; i < end; i++)
877 pVal += inSig[i] * sinc(M_PI_F*(i-ix));
878 }
879 else {
880 for (int i = start; i < end; i++)
881 pVal += inSig[i].real() * sinc(M_PI_F*(i-ix));
882 }
883
884 return pVal;
885}
886
Thomas Tsou8181b012013-08-20 21:17:19 -0400887static complex fastPeakDetect(const signalVector &rxBurst, float *index)
888{
889 float val, max = 0.0f;
890 complex amp;
891 int _index = -1;
892
893 for (int i = 0; i < rxBurst.size(); i++) {
894 val = rxBurst[i].norm2();
895 if (val > max) {
896 max = val;
897 _index = i;
898 amp = rxBurst[i];
899 }
900 }
901
902 if (index)
903 *index = (float) _index;
904
905 return amp;
906}
907
dburgessb3a0ca42011-10-12 07:44:40 +0000908complex peakDetect(const signalVector &rxBurst,
909 float *peakIndex,
910 float *avgPwr)
911{
912
913
914 complex maxVal = 0.0;
915 float maxIndex = -1;
916 float sumPower = 0.0;
917
918 for (unsigned int i = 0; i < rxBurst.size(); i++) {
919 float samplePower = rxBurst[i].norm2();
920 if (samplePower > maxVal.real()) {
921 maxVal = samplePower;
922 maxIndex = i;
923 }
924 sumPower += samplePower;
925 }
926
927 // interpolate around the peak
928 // to save computation, we'll use early-late balancing
929 float earlyIndex = maxIndex-1;
930 float lateIndex = maxIndex+1;
931
932 float incr = 0.5;
933 while (incr > 1.0/1024.0) {
934 complex earlyP = interpolatePoint(rxBurst,earlyIndex);
935 complex lateP = interpolatePoint(rxBurst,lateIndex);
936 if (earlyP < lateP)
937 earlyIndex += incr;
938 else if (earlyP > lateP)
939 earlyIndex -= incr;
940 else break;
941 incr /= 2.0;
942 lateIndex = earlyIndex + 2.0;
943 }
944
945 maxIndex = earlyIndex + 1.0;
946 maxVal = interpolatePoint(rxBurst,maxIndex);
947
948 if (peakIndex!=NULL)
949 *peakIndex = maxIndex;
950
951 if (avgPwr!=NULL)
952 *avgPwr = (sumPower-maxVal.norm2()) / (rxBurst.size()-1);
953
954 return maxVal;
955
956}
957
958void scaleVector(signalVector &x,
959 complex scale)
960{
961 signalVector::iterator xP = x.begin();
962 signalVector::iterator xPEnd = x.end();
963 if (!x.isRealOnly()) {
964 while (xP < xPEnd) {
965 *xP = *xP * scale;
966 xP++;
967 }
968 }
969 else {
970 while (xP < xPEnd) {
971 *xP = xP->real() * scale;
972 xP++;
973 }
974 }
975}
976
977/** in-place conjugation */
978void conjugateVector(signalVector &x)
979{
980 if (x.isRealOnly()) return;
981 signalVector::iterator xP = x.begin();
982 signalVector::iterator xPEnd = x.end();
983 while (xP < xPEnd) {
984 *xP = xP->conj();
985 xP++;
986 }
987}
988
989
990// in-place addition!!
991bool addVector(signalVector &x,
992 signalVector &y)
993{
994 signalVector::iterator xP = x.begin();
995 signalVector::iterator yP = y.begin();
996 signalVector::iterator xPEnd = x.end();
997 signalVector::iterator yPEnd = y.end();
998 while ((xP < xPEnd) && (yP < yPEnd)) {
999 *xP = *xP + *yP;
1000 xP++; yP++;
1001 }
1002 return true;
1003}
1004
1005// in-place multiplication!!
1006bool multVector(signalVector &x,
1007 signalVector &y)
1008{
1009 signalVector::iterator xP = x.begin();
1010 signalVector::iterator yP = y.begin();
1011 signalVector::iterator xPEnd = x.end();
1012 signalVector::iterator yPEnd = y.end();
1013 while ((xP < xPEnd) && (yP < yPEnd)) {
1014 *xP = (*xP) * (*yP);
1015 xP++; yP++;
1016 }
1017 return true;
1018}
1019
1020
1021void offsetVector(signalVector &x,
1022 complex offset)
1023{
1024 signalVector::iterator xP = x.begin();
1025 signalVector::iterator xPEnd = x.end();
1026 if (!x.isRealOnly()) {
1027 while (xP < xPEnd) {
1028 *xP += offset;
1029 xP++;
1030 }
1031 }
1032 else {
1033 while (xP < xPEnd) {
1034 *xP = xP->real() + offset;
1035 xP++;
1036 }
1037 }
1038}
1039
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001040bool generateMidamble(int sps, int tsc)
dburgessb3a0ca42011-10-12 07:44:40 +00001041{
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001042 bool status = true;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001043 float toa;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001044 complex *data = NULL;
1045 signalVector *autocorr = NULL, *midamble = NULL;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001046 signalVector *midMidamble = NULL, *_midMidamble = NULL;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001047
Thomas Tsou3eaae802013-08-20 19:31:14 -04001048 if ((tsc < 0) || (tsc > 7))
dburgessb3a0ca42011-10-12 07:44:40 +00001049 return false;
1050
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001051 delete gMidambles[tsc];
Thomas Tsou3eaae802013-08-20 19:31:14 -04001052
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001053 /* Use middle 16 bits of each TSC. Correlation sequence is not pulse shaped */
1054 midMidamble = modulateBurst(gTrainingSequence[tsc].segment(5,16), 0, sps, true);
1055 if (!midMidamble)
1056 return false;
1057
Thomas Tsou3eaae802013-08-20 19:31:14 -04001058 /* Simulated receive sequence is pulse shaped */
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001059 midamble = modulateBurst(gTrainingSequence[tsc], 0, sps, false);
1060 if (!midamble) {
1061 status = false;
1062 goto release;
dburgessb3a0ca42011-10-12 07:44:40 +00001063 }
Thomas Tsou3eaae802013-08-20 19:31:14 -04001064
dburgessb3a0ca42011-10-12 07:44:40 +00001065 // NOTE: Because ideal TSC 16-bit midamble is 66 symbols into burst,
1066 // the ideal TSC has an + 180 degree phase shift,
1067 // due to the pi/2 frequency shift, that
1068 // needs to be accounted for.
1069 // 26-midamble is 61 symbols into burst, has +90 degree phase shift.
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001070 scaleVector(*midMidamble, complex(-1.0, 0.0));
1071 scaleVector(*midamble, complex(0.0, 1.0));
dburgessb3a0ca42011-10-12 07:44:40 +00001072
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001073 conjugateVector(*midMidamble);
dburgessb3a0ca42011-10-12 07:44:40 +00001074
Thomas Tsou3eaae802013-08-20 19:31:14 -04001075 /* For SSE alignment, reallocate the midamble sequence on 16-byte boundary */
1076 data = (complex *) convolve_h_alloc(midMidamble->size());
1077 _midMidamble = new signalVector(data, 0, midMidamble->size());
1078 _midMidamble->setAligned(true);
1079 memcpy(_midMidamble->begin(), midMidamble->begin(),
1080 midMidamble->size() * sizeof(complex));
1081
1082 autocorr = convolve(midamble, _midMidamble, NULL, NO_DELAY);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001083 if (!autocorr) {
1084 status = false;
1085 goto release;
1086 }
dburgessb3a0ca42011-10-12 07:44:40 +00001087
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001088 gMidambles[tsc] = new CorrelationSequence;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001089 gMidambles[tsc]->buffer = data;
1090 gMidambles[tsc]->sequence = _midMidamble;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001091 gMidambles[tsc]->gain = peakDetect(*autocorr, &toa, NULL);
1092
1093 /* For 1 sps only
1094 * (Half of correlation length - 1) + midpoint of pulse shape + remainder
1095 * 13.5 = (16 / 2 - 1) + 1.5 + (26 - 10) / 2
1096 */
1097 if (sps == 1)
1098 gMidambles[tsc]->toa = toa - 13.5;
1099 else
1100 gMidambles[tsc]->toa = 0;
dburgessb3a0ca42011-10-12 07:44:40 +00001101
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001102release:
dburgessb3a0ca42011-10-12 07:44:40 +00001103 delete autocorr;
1104 delete midamble;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001105 delete midMidamble;
dburgessb3a0ca42011-10-12 07:44:40 +00001106
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001107 if (!status) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001108 delete _midMidamble;
1109 free(data);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001110 gMidambles[tsc] = NULL;
1111 }
1112
1113 return status;
dburgessb3a0ca42011-10-12 07:44:40 +00001114}
1115
Thomas Tsou83e06892013-08-20 16:10:01 -04001116bool generateRACHSequence(int sps)
dburgessb3a0ca42011-10-12 07:44:40 +00001117{
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001118 bool status = true;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001119 float toa;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001120 complex *data = NULL;
1121 signalVector *autocorr = NULL;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001122 signalVector *seq0 = NULL, *seq1 = NULL, *_seq1 = NULL;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001123
1124 delete gRACHSequence;
1125
1126 seq0 = modulateBurst(gRACHSynchSequence, 0, sps, false);
1127 if (!seq0)
1128 return false;
1129
1130 seq1 = modulateBurst(gRACHSynchSequence.segment(0, 40), 0, sps, true);
1131 if (!seq1) {
1132 status = false;
1133 goto release;
dburgessb3a0ca42011-10-12 07:44:40 +00001134 }
1135
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001136 conjugateVector(*seq1);
dburgessb3a0ca42011-10-12 07:44:40 +00001137
Thomas Tsou3eaae802013-08-20 19:31:14 -04001138 /* For SSE alignment, reallocate the midamble sequence on 16-byte boundary */
1139 data = (complex *) convolve_h_alloc(seq1->size());
1140 _seq1 = new signalVector(data, 0, seq1->size());
1141 _seq1->setAligned(true);
1142 memcpy(_seq1->begin(), seq1->begin(), seq1->size() * sizeof(complex));
1143
1144 autocorr = convolve(seq0, _seq1, autocorr, NO_DELAY);
1145 if (!autocorr) {
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001146 status = false;
1147 goto release;
1148 }
dburgessb3a0ca42011-10-12 07:44:40 +00001149
1150 gRACHSequence = new CorrelationSequence;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001151 gRACHSequence->sequence = _seq1;
1152 gRACHSequence->buffer = data;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001153 gRACHSequence->gain = peakDetect(*autocorr, &toa, NULL);
1154
1155 /* For 1 sps only
1156 * (Half of correlation length - 1) + midpoint of pulse shaping filer
1157 * 20.5 = (40 / 2 - 1) + 1.5
1158 */
1159 if (sps == 1)
1160 gRACHSequence->toa = toa - 20.5;
1161 else
1162 gRACHSequence->toa = 0.0;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001163
1164release:
dburgessb3a0ca42011-10-12 07:44:40 +00001165 delete autocorr;
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001166 delete seq0;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001167 delete seq1;
dburgessb3a0ca42011-10-12 07:44:40 +00001168
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001169 if (!status) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001170 delete _seq1;
1171 free(data);
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001172 gRACHSequence = NULL;
1173 }
dburgessb3a0ca42011-10-12 07:44:40 +00001174
Thomas Tsoue5dcfc42013-08-20 16:27:12 -04001175 return status;
dburgessb3a0ca42011-10-12 07:44:40 +00001176}
Thomas Tsou3eaae802013-08-20 19:31:14 -04001177
Thomas Tsou865bca42013-08-21 20:58:00 -04001178static float computePeakRatio(signalVector *corr,
1179 int sps, float toa, complex amp)
dburgessb3a0ca42011-10-12 07:44:40 +00001180{
Thomas Tsou865bca42013-08-21 20:58:00 -04001181 int num = 0;
1182 complex *peak;
1183 float rms, avg = 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001184
Thomas Tsou865bca42013-08-21 20:58:00 -04001185 peak = corr->begin() + (int) rint(toa);
dburgessb3a0ca42011-10-12 07:44:40 +00001186
Thomas Tsou865bca42013-08-21 20:58:00 -04001187 /* Check for bogus results */
1188 if ((toa < 0.0) || (toa > corr->size()))
1189 return 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001190
Thomas Tsou3eaae802013-08-20 19:31:14 -04001191 for (int i = 2 * sps; i <= 5 * sps; i++) {
Thomas Tsou865bca42013-08-21 20:58:00 -04001192 if (peak - i >= corr->begin()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001193 avg += (peak - i)->norm2();
1194 num++;
1195 }
Thomas Tsou865bca42013-08-21 20:58:00 -04001196 if (peak + i < corr->end()) {
Thomas Tsou3eaae802013-08-20 19:31:14 -04001197 avg += (peak + i)->norm2();
1198 num++;
1199 }
dburgessb3a0ca42011-10-12 07:44:40 +00001200 }
1201
Thomas Tsou3eaae802013-08-20 19:31:14 -04001202 if (num < 2)
Thomas Tsou865bca42013-08-21 20:58:00 -04001203 return 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001204
Thomas Tsou3eaae802013-08-20 19:31:14 -04001205 rms = sqrtf(avg / (float) num) + 0.00001;
dburgessb3a0ca42011-10-12 07:44:40 +00001206
Thomas Tsou865bca42013-08-21 20:58:00 -04001207 return (amp.abs()) / rms;
dburgessb3a0ca42011-10-12 07:44:40 +00001208}
1209
1210bool energyDetect(signalVector &rxBurst,
1211 unsigned windowLength,
1212 float detectThreshold,
1213 float *avgPwr)
1214{
1215
1216 signalVector::const_iterator windowItr = rxBurst.begin(); //+rxBurst.size()/2 - 5*windowLength/2;
1217 float energy = 0.0;
1218 if (windowLength < 0) windowLength = 20;
1219 if (windowLength > rxBurst.size()) windowLength = rxBurst.size();
1220 for (unsigned i = 0; i < windowLength; i++) {
1221 energy += windowItr->norm2();
1222 windowItr+=4;
1223 }
1224 if (avgPwr) *avgPwr = energy/windowLength;
dburgessb3a0ca42011-10-12 07:44:40 +00001225 return (energy/windowLength > detectThreshold*detectThreshold);
1226}
dburgessb3a0ca42011-10-12 07:44:40 +00001227
Thomas Tsou865bca42013-08-21 20:58:00 -04001228/*
1229 * Detect a burst based on correlation and peak-to-average ratio
1230 *
1231 * For one sampler-per-symbol, perform fast peak detection (no interpolation)
1232 * for initial gating. We do this because energy detection should be disabled.
1233 * For higher oversampling values, we assume the energy detector is in place
1234 * and we run full interpolating peak detection.
1235 */
1236static int detectBurst(signalVector &burst,
1237 signalVector &corr, CorrelationSequence *sync,
1238 float thresh, int sps, complex *amp, float *toa,
1239 int start, int len)
dburgessb3a0ca42011-10-12 07:44:40 +00001240{
Thomas Tsou865bca42013-08-21 20:58:00 -04001241 /* Correlate */
1242 if (!convolve(&burst, sync->sequence, &corr,
Thomas Tsou3eaae802013-08-20 19:31:14 -04001243 CUSTOM, start, len, sps, 0)) {
1244 return -1;
dburgessb3a0ca42011-10-12 07:44:40 +00001245 }
1246
Thomas Tsou865bca42013-08-21 20:58:00 -04001247 /* Peak detection - place restrictions at correlation edges */
1248 *amp = fastPeakDetect(corr, toa);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001249
Thomas Tsou865bca42013-08-21 20:58:00 -04001250 if ((*toa < 3 * sps) || (*toa > len - 3 * sps))
1251 return 0;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001252
Thomas Tsou865bca42013-08-21 20:58:00 -04001253 /* Peak -to-average ratio */
1254 if (computePeakRatio(&corr, sps, *toa, *amp) < thresh)
1255 return 0;
1256
1257 /* Compute peak-to-average ratio. Reject if we don't have enough values */
1258 *amp = peakDetect(corr, toa, NULL);
1259
1260 /* Normalize our channel gain */
1261 *amp = *amp / sync->gain;
1262
Thomas Tsoua57bc8a2013-09-05 08:16:47 +08001263 /* Compenate for residual rotation with dual Laurent pulse */
1264 if (sps == 4)
1265 *amp = *amp * complex(0.0, 1.0);
1266
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001267 /* Compensate for residuate time lag */
1268 *toa = *toa - sync->toa;
1269
Thomas Tsou865bca42013-08-21 20:58:00 -04001270 return 1;
1271}
1272
1273/*
1274 * RACH burst detection
1275 *
1276 * Correlation window parameters:
1277 * target: Tail bits + RACH length (reduced from 41 to a multiple of 4)
Thomas Tsoudafb3372013-09-18 16:21:26 -04001278 * head: Search 4 symbols before target
1279 * tail: Search 10 symbols after target
Thomas Tsou865bca42013-08-21 20:58:00 -04001280 */
1281int detectRACHBurst(signalVector &rxBurst,
1282 float thresh,
1283 int sps,
1284 complex *amp,
1285 float *toa)
1286{
1287 int rc, start, target, head, tail, len;
1288 float _toa;
1289 complex _amp;
1290 signalVector corr;
1291 CorrelationSequence *sync;
1292
1293 if ((sps != 1) && (sps != 4))
1294 return -1;
1295
1296 target = 8 + 40;
Thomas Tsoudafb3372013-09-18 16:21:26 -04001297 head = 4;
1298 tail = 10;
Thomas Tsou865bca42013-08-21 20:58:00 -04001299
1300 start = (target - head) * sps - 1;
1301 len = (head + tail) * sps;
1302 sync = gRACHSequence;
1303 corr = signalVector(len);
1304
1305 rc = detectBurst(rxBurst, corr, sync,
1306 thresh, sps, &_amp, &_toa, start, len);
1307 if (rc < 0) {
1308 return -1;
1309 } else if (!rc) {
1310 if (amp)
1311 *amp = 0.0f;
1312 if (toa)
1313 *toa = 0.0f;
1314 return 0;
dburgessb3a0ca42011-10-12 07:44:40 +00001315 }
1316
Thomas Tsou865bca42013-08-21 20:58:00 -04001317 /* Subtract forward search bits from delay */
1318 if (toa)
1319 *toa = _toa - head * sps;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001320 if (amp)
Thomas Tsou865bca42013-08-21 20:58:00 -04001321 *amp = _amp;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001322
Thomas Tsou865bca42013-08-21 20:58:00 -04001323 return 1;
1324}
Thomas Tsou3eaae802013-08-20 19:31:14 -04001325
Thomas Tsou865bca42013-08-21 20:58:00 -04001326/*
1327 * Normal burst detection
1328 *
1329 * Correlation window parameters:
1330 * target: Tail + data + mid-midamble + 1/2 remaining midamblebits
Thomas Tsoudafb3372013-09-18 16:21:26 -04001331 * head: Search 4 symbols before target
1332 * tail: Search 4 symbols + maximum expected delay
Thomas Tsou865bca42013-08-21 20:58:00 -04001333 */
1334int analyzeTrafficBurst(signalVector &rxBurst, unsigned tsc, float thresh,
1335 int sps, complex *amp, float *toa, unsigned max_toa,
1336 bool chan_req, signalVector **chan, float *chan_offset)
1337{
1338 int rc, start, target, head, tail, len;
1339 complex _amp;
1340 float _toa;
1341 signalVector corr;
1342 CorrelationSequence *sync;
1343
1344 if ((tsc < 0) || (tsc > 7) || ((sps != 1) && (sps != 4)))
1345 return -1;
1346
1347 target = 3 + 58 + 16 + 5;
Thomas Tsoudafb3372013-09-18 16:21:26 -04001348 head = 4;
1349 tail = 4 + max_toa;
Thomas Tsou865bca42013-08-21 20:58:00 -04001350
1351 start = (target - head) * sps - 1;
1352 len = (head + tail) * sps;
1353 sync = gMidambles[tsc];
1354 corr = signalVector(len);
1355
1356 rc = detectBurst(rxBurst, corr, sync,
1357 thresh, sps, &_amp, &_toa, start, len);
1358 if (rc < 0) {
1359 return -1;
1360 } else if (!rc) {
1361 if (amp)
1362 *amp = 0.0f;
1363 if (toa)
1364 *toa = 0.0f;
1365 return 0;
1366 }
1367
1368 /* Subtract forward search bits from delay */
1369 _toa -= head * sps;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001370 if (toa)
1371 *toa = _toa;
Thomas Tsou865bca42013-08-21 20:58:00 -04001372 if (amp)
1373 *amp = _amp;
Thomas Tsou3eaae802013-08-20 19:31:14 -04001374
Thomas Tsou865bca42013-08-21 20:58:00 -04001375 /* Equalization not currently supported */
Thomas Tsou3eaae802013-08-20 19:31:14 -04001376 if (chan_req) {
Thomas Tsou865bca42013-08-21 20:58:00 -04001377 *chan = new signalVector(6 * sps);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001378
1379 if (chan_offset)
Thomas Tsou865bca42013-08-21 20:58:00 -04001380 *chan_offset = 0.0;
dburgessb3a0ca42011-10-12 07:44:40 +00001381 }
1382
Thomas Tsou3eaae802013-08-20 19:31:14 -04001383 return 1;
dburgessb3a0ca42011-10-12 07:44:40 +00001384}
1385
1386signalVector *decimateVector(signalVector &wVector,
1387 int decimationFactor)
1388{
1389
1390 if (decimationFactor <= 1) return NULL;
1391
1392 signalVector *decVector = new signalVector(wVector.size()/decimationFactor);
1393 decVector->isRealOnly(wVector.isRealOnly());
1394
1395 signalVector::iterator vecItr = decVector->begin();
1396 for (unsigned int i = 0; i < wVector.size();i+=decimationFactor)
1397 *vecItr++ = wVector[i];
1398
1399 return decVector;
1400}
1401
1402
Thomas Tsou83e06892013-08-20 16:10:01 -04001403SoftVector *demodulateBurst(signalVector &rxBurst, int sps,
1404 complex channel, float TOA)
dburgessb3a0ca42011-10-12 07:44:40 +00001405{
1406 scaleVector(rxBurst,((complex) 1.0)/channel);
1407 delayVector(rxBurst,-TOA);
1408
1409 signalVector *shapedBurst = &rxBurst;
1410
1411 // shift up by a quarter of a frequency
1412 // ignore starting phase, since spec allows for discontinuous phase
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001413 GMSKReverseRotate(*shapedBurst, sps);
dburgessb3a0ca42011-10-12 07:44:40 +00001414
1415 // run through slicer
Thomas Tsoud24cc2c2013-08-20 15:41:45 -04001416 if (sps > 1) {
1417 signalVector *decShapedBurst = decimateVector(*shapedBurst, sps);
dburgessb3a0ca42011-10-12 07:44:40 +00001418 shapedBurst = decShapedBurst;
1419 }
1420
dburgessb3a0ca42011-10-12 07:44:40 +00001421 vectorSlicer(shapedBurst);
1422
1423 SoftVector *burstBits = new SoftVector(shapedBurst->size());
1424
1425 SoftVector::iterator burstItr = burstBits->begin();
1426 signalVector::iterator shapedItr = shapedBurst->begin();
1427 for (; shapedItr < shapedBurst->end(); shapedItr++)
1428 *burstItr++ = shapedItr->real();
1429
Thomas Tsoud24cc2c2013-08-20 15:41:45 -04001430 if (sps > 1)
1431 delete shapedBurst;
dburgessb3a0ca42011-10-12 07:44:40 +00001432
1433 return burstBits;
1434
1435}
dburgessb3a0ca42011-10-12 07:44:40 +00001436
dburgessb3a0ca42011-10-12 07:44:40 +00001437// Assumes symbol-spaced sampling!!!
1438// Based upon paper by Al-Dhahir and Cioffi
1439bool designDFE(signalVector &channelResponse,
1440 float SNRestimate,
1441 int Nf,
1442 signalVector **feedForwardFilter,
1443 signalVector **feedbackFilter)
1444{
1445
1446 signalVector G0(Nf);
1447 signalVector G1(Nf);
1448 signalVector::iterator G0ptr = G0.begin();
1449 signalVector::iterator G1ptr = G1.begin();
1450 signalVector::iterator chanPtr = channelResponse.begin();
1451
1452 int nu = channelResponse.size()-1;
1453
1454 *G0ptr = 1.0/sqrtf(SNRestimate);
1455 for(int j = 0; j <= nu; j++) {
1456 *G1ptr = chanPtr->conj();
1457 G1ptr++; chanPtr++;
1458 }
1459
1460 signalVector *L[Nf];
1461 signalVector::iterator Lptr;
1462 float d;
1463 for(int i = 0; i < Nf; i++) {
1464 d = G0.begin()->norm2() + G1.begin()->norm2();
1465 L[i] = new signalVector(Nf+nu);
1466 Lptr = L[i]->begin()+i;
1467 G0ptr = G0.begin(); G1ptr = G1.begin();
1468 while ((G0ptr < G0.end()) && (Lptr < L[i]->end())) {
1469 *Lptr = (*G0ptr*(G0.begin()->conj()) + *G1ptr*(G1.begin()->conj()) )/d;
1470 Lptr++;
1471 G0ptr++;
1472 G1ptr++;
1473 }
1474 complex k = (*G1.begin())/(*G0.begin());
1475
1476 if (i != Nf-1) {
1477 signalVector G0new = G1;
1478 scaleVector(G0new,k.conj());
1479 addVector(G0new,G0);
1480
1481 signalVector G1new = G0;
1482 scaleVector(G1new,k*(-1.0));
1483 addVector(G1new,G1);
1484 delayVector(G1new,-1.0);
1485
1486 scaleVector(G0new,1.0/sqrtf(1.0+k.norm2()));
1487 scaleVector(G1new,1.0/sqrtf(1.0+k.norm2()));
1488 G0 = G0new;
1489 G1 = G1new;
1490 }
1491 }
1492
1493 *feedbackFilter = new signalVector(nu);
1494 L[Nf-1]->segmentCopyTo(**feedbackFilter,Nf,nu);
1495 scaleVector(**feedbackFilter,(complex) -1.0);
1496 conjugateVector(**feedbackFilter);
1497
1498 signalVector v(Nf);
1499 signalVector::iterator vStart = v.begin();
1500 signalVector::iterator vPtr;
1501 *(vStart+Nf-1) = (complex) 1.0;
1502 for(int k = Nf-2; k >= 0; k--) {
1503 Lptr = L[k]->begin()+k+1;
1504 vPtr = vStart + k+1;
1505 complex v_k = 0.0;
1506 for (int j = k+1; j < Nf; j++) {
1507 v_k -= (*vPtr)*(*Lptr);
1508 vPtr++; Lptr++;
1509 }
1510 *(vStart + k) = v_k;
1511 }
1512
1513 *feedForwardFilter = new signalVector(Nf);
Thomas Tsou3eaae802013-08-20 19:31:14 -04001514 signalVector::iterator w = (*feedForwardFilter)->end();
dburgessb3a0ca42011-10-12 07:44:40 +00001515 for (int i = 0; i < Nf; i++) {
1516 delete L[i];
1517 complex w_i = 0.0;
1518 int endPt = ( nu < (Nf-1-i) ) ? nu : (Nf-1-i);
1519 vPtr = vStart+i;
1520 chanPtr = channelResponse.begin();
1521 for (int k = 0; k < endPt+1; k++) {
1522 w_i += (*vPtr)*(chanPtr->conj());
1523 vPtr++; chanPtr++;
1524 }
Thomas Tsou3eaae802013-08-20 19:31:14 -04001525 *--w = w_i/d;
dburgessb3a0ca42011-10-12 07:44:40 +00001526 }
1527
1528
1529 return true;
1530
1531}
1532
1533// Assumes symbol-rate sampling!!!!
1534SoftVector *equalizeBurst(signalVector &rxBurst,
1535 float TOA,
Thomas Tsoud24cc2c2013-08-20 15:41:45 -04001536 int sps,
dburgessb3a0ca42011-10-12 07:44:40 +00001537 signalVector &w, // feedforward filter
1538 signalVector &b) // feedback filter
1539{
Thomas Tsou3eaae802013-08-20 19:31:14 -04001540 signalVector *postForwardFull;
dburgessb3a0ca42011-10-12 07:44:40 +00001541
Thomas Tsou3eaae802013-08-20 19:31:14 -04001542 if (!delayVector(rxBurst, -TOA))
1543 return NULL;
dburgessb3a0ca42011-10-12 07:44:40 +00001544
Thomas Tsou3eaae802013-08-20 19:31:14 -04001545 postForwardFull = convolve(&rxBurst, &w, NULL,
1546 CUSTOM, 0, rxBurst.size() + w.size() - 1);
1547 if (!postForwardFull)
1548 return NULL;
dburgessb3a0ca42011-10-12 07:44:40 +00001549
1550 signalVector* postForward = new signalVector(rxBurst.size());
1551 postForwardFull->segmentCopyTo(*postForward,w.size()-1,rxBurst.size());
1552 delete postForwardFull;
1553
1554 signalVector::iterator dPtr = postForward->begin();
1555 signalVector::iterator dBackPtr;
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001556 signalVector::iterator rotPtr = GMSKRotationN->begin();
1557 signalVector::iterator revRotPtr = GMSKReverseRotationN->begin();
dburgessb3a0ca42011-10-12 07:44:40 +00001558
1559 signalVector *DFEoutput = new signalVector(postForward->size());
1560 signalVector::iterator DFEItr = DFEoutput->begin();
1561
1562 // NOTE: can insert the midamble and/or use midamble to estimate BER
1563 for (; dPtr < postForward->end(); dPtr++) {
1564 dBackPtr = dPtr-1;
1565 signalVector::iterator bPtr = b.begin();
1566 while ( (bPtr < b.end()) && (dBackPtr >= postForward->begin()) ) {
1567 *dPtr = *dPtr + (*bPtr)*(*dBackPtr);
1568 bPtr++;
1569 dBackPtr--;
1570 }
1571 *dPtr = *dPtr * (*revRotPtr);
1572 *DFEItr = *dPtr;
1573 // make decision on symbol
1574 *dPtr = (dPtr->real() > 0.0) ? 1.0 : -1.0;
1575 //*DFEItr = *dPtr;
1576 *dPtr = *dPtr * (*rotPtr);
1577 DFEItr++;
1578 rotPtr++;
1579 revRotPtr++;
1580 }
1581
1582 vectorSlicer(DFEoutput);
1583
1584 SoftVector *burstBits = new SoftVector(postForward->size());
1585 SoftVector::iterator burstItr = burstBits->begin();
1586 DFEItr = DFEoutput->begin();
1587 for (; DFEItr < DFEoutput->end(); DFEItr++)
1588 *burstItr++ = DFEItr->real();
1589
1590 delete postForward;
1591
1592 delete DFEoutput;
1593
1594 return burstBits;
1595}
Thomas Tsouc1f7c422013-10-11 13:49:55 -04001596
1597bool sigProcLibSetup(int sps)
1598{
1599 if ((sps != 1) && (sps != 4))
1600 return false;
1601
1602 initTrigTables();
1603 initGMSKRotationTables(sps);
1604
1605 GSMPulse1 = generateGSMPulse(1, 2);
1606 if (sps > 1)
1607 GSMPulse = generateGSMPulse(sps, 2);
1608
1609 if (!generateRACHSequence(1)) {
1610 sigProcLibDestroy();
1611 return false;
1612 }
1613
1614 return true;
1615}