blob: ae528947f60e92da175801c0827cb91fb566b4fd [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
25
26
27#define NDEBUG
28
29#include "sigProcLib.h"
30#include "GSMCommon.h"
kurtis.heimerla198d452011-11-26 03:19:28 +000031#include "sendLPF_961.h"
32#include "rcvLPF_651.h"
dburgessb3a0ca42011-10-12 07:44:40 +000033
34#include <Logger.h>
35
36#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
51/** Static ideal RACH and midamble correlation waveforms */
52typedef struct {
53 signalVector *sequence;
54 signalVector *sequenceReversedConjugated;
55 float TOA;
56 complex gain;
57} CorrelationSequence;
58
59CorrelationSequence *gMidambles[] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
60CorrelationSequence *gRACHSequence = NULL;
61
62void sigProcLibDestroy(void) {
63 if (GMSKRotation) {
64 delete GMSKRotation;
65 GMSKRotation = NULL;
66 }
67 if (GMSKReverseRotation) {
68 delete GMSKReverseRotation;
69 GMSKReverseRotation = NULL;
70 }
71 for (int i = 0; i < 8; i++) {
72 if (gMidambles[i]!=NULL) {
73 if (gMidambles[i]->sequence) delete gMidambles[i]->sequence;
74 if (gMidambles[i]->sequenceReversedConjugated) delete gMidambles[i]->sequenceReversedConjugated;
75 delete gMidambles[i];
76 gMidambles[i] = NULL;
77 }
78 }
79 if (gRACHSequence) {
80 if (gRACHSequence->sequence) delete gRACHSequence->sequence;
81 if (gRACHSequence->sequenceReversedConjugated) delete gRACHSequence->sequenceReversedConjugated;
82 delete gRACHSequence;
83 gRACHSequence = NULL;
84 }
85}
86
87
88
89// dB relative to 1.0.
90// if > 1.0, then return 0 dB
91float dB(float x) {
92
93 float arg = 1.0F;
94 float dB = 0.0F;
95
96 if (x >= 1.0F) return 0.0F;
97 if (x <= 0.0F) return -200.0F;
98
99 float prevArg = arg;
100 float prevdB = dB;
101 float stepSize = 16.0F;
102 float dBstepSize = 12.0F;
103 while (stepSize > 1.0F) {
104 do {
105 prevArg = arg;
106 prevdB = dB;
107 arg /= stepSize;
108 dB -= dBstepSize;
109 } while (arg > x);
110 arg = prevArg;
111 dB = prevdB;
112 stepSize *= 0.5F;
113 dBstepSize -= 3.0F;
114 }
115 return ((arg-x)*(dB-3.0F) + (x-arg*0.5F)*dB)/(arg - arg*0.5F);
116
117}
118
119// 10^(-dB/10), inverse of dB func.
120float dBinv(float x) {
121
122 float arg = 1.0F;
123 float dB = 0.0F;
124
125 if (x >= 0.0F) return 1.0F;
126 if (x <= -200.0F) return 0.0F;
127
128 float prevArg = arg;
129 float prevdB = dB;
130 float stepSize = 16.0F;
131 float dBstepSize = 12.0F;
132 while (stepSize > 1.0F) {
133 do {
134 prevArg = arg;
135 prevdB = dB;
136 arg /= stepSize;
137 dB -= dBstepSize;
138 } while (dB > x);
139 arg = prevArg;
140 dB = prevdB;
141 stepSize *= 0.5F;
142 dBstepSize -= 3.0F;
143 }
144
145 return ((dB-x)*(arg*0.5F)+(x-(dB-3.0F))*(arg))/3.0F;
146
147}
148
149float vectorNorm2(const signalVector &x)
150{
151 signalVector::const_iterator xPtr = x.begin();
152 float Energy = 0.0;
153 for (;xPtr != x.end();xPtr++) {
154 Energy += xPtr->norm2();
155 }
156 return Energy;
157}
158
159
160float vectorPower(const signalVector &x)
161{
162 return vectorNorm2(x)/x.size();
163}
164
165/** compute cosine via lookup table */
166float cosLookup(const float x)
167{
168 float arg = x*M_1_2PI_F;
169 while (arg > 1.0F) arg -= 1.0F;
170 while (arg < 0.0F) arg += 1.0F;
171
172 const float argT = arg*((float)TABLESIZE);
173 const int argI = (int)argT;
174 const float delta = argT-argI;
175 const float iDelta = 1.0F-delta;
176 return iDelta*cosTable[argI] + delta*cosTable[argI+1];
177}
178
179/** compute sine via lookup table */
180float sinLookup(const float x)
181{
182 float arg = x*M_1_2PI_F;
183 while (arg > 1.0F) arg -= 1.0F;
184 while (arg < 0.0F) arg += 1.0F;
185
186 const float argT = arg*((float)TABLESIZE);
187 const int argI = (int)argT;
188 const float delta = argT-argI;
189 const float iDelta = 1.0F-delta;
190 return iDelta*sinTable[argI] + delta*sinTable[argI+1];
191}
192
193
194/** compute e^(-jx) via lookup table. */
195complex expjLookup(float x)
196{
197 float arg = x*M_1_2PI_F;
198 while (arg > 1.0F) arg -= 1.0F;
199 while (arg < 0.0F) arg += 1.0F;
200
201 const float argT = arg*((float)TABLESIZE);
202 const int argI = (int)argT;
203 const float delta = argT-argI;
204 const float iDelta = 1.0F-delta;
205 return complex(iDelta*cosTable[argI] + delta*cosTable[argI+1],
206 iDelta*sinTable[argI] + delta*sinTable[argI+1]);
207}
208
209/** Library setup functions */
210void initTrigTables() {
211 for (int i = 0; i < TABLESIZE+1; i++) {
212 cosTable[i] = cos(2.0*M_PI*i/TABLESIZE);
213 sinTable[i] = sin(2.0*M_PI*i/TABLESIZE);
214 }
215}
216
217void initGMSKRotationTables(int samplesPerSymbol) {
218 GMSKRotation = new signalVector(157*samplesPerSymbol);
219 GMSKReverseRotation = new signalVector(157*samplesPerSymbol);
220 signalVector::iterator rotPtr = GMSKRotation->begin();
221 signalVector::iterator revPtr = GMSKReverseRotation->begin();
222 float phase = 0.0;
223 while (rotPtr != GMSKRotation->end()) {
224 *rotPtr++ = expjLookup(phase);
225 *revPtr++ = expjLookup(-phase);
226 phase += M_PI_F/2.0F/(float) samplesPerSymbol;
227 }
228}
229
230void sigProcLibSetup(int samplesPerSymbol) {
231 initTrigTables();
232 initGMSKRotationTables(samplesPerSymbol);
233}
234
235void GMSKRotate(signalVector &x) {
236 signalVector::iterator xPtr = x.begin();
237 signalVector::iterator rotPtr = GMSKRotation->begin();
238 if (x.isRealOnly()) {
239 while (xPtr < x.end()) {
240 *xPtr = *rotPtr++ * (xPtr->real());
241 xPtr++;
242 }
243 }
244 else {
245 while (xPtr < x.end()) {
246 *xPtr = *rotPtr++ * (*xPtr);
247 xPtr++;
248 }
249 }
250}
251
252void GMSKReverseRotate(signalVector &x) {
253 signalVector::iterator xPtr= x.begin();
254 signalVector::iterator rotPtr = GMSKReverseRotation->begin();
255 if (x.isRealOnly()) {
256 while (xPtr < x.end()) {
257 *xPtr = *rotPtr++ * (xPtr->real());
258 xPtr++;
259 }
260 }
261 else {
262 while (xPtr < x.end()) {
263 *xPtr = *rotPtr++ * (*xPtr);
264 xPtr++;
265 }
266 }
267}
268
269
270signalVector* convolve(const signalVector *a,
271 const signalVector *b,
272 signalVector *c,
273 ConvType spanType,
274 unsigned startIx,
275 unsigned len)
276{
277 if ((a==NULL) || (b==NULL)) return NULL;
278 int La = a->size();
279 int Lb = b->size();
280
281 int startIndex;
282 unsigned int outSize;
283 switch (spanType) {
284 case FULL_SPAN:
285 startIndex = 0;
286 outSize = La+Lb-1;
287 break;
288 case OVERLAP_ONLY:
289 startIndex = La;
290 outSize = abs(La-Lb)+1;
291 break;
292 case START_ONLY:
293 startIndex = 0;
294 outSize = La;
295 break;
296 case WITH_TAIL:
297 startIndex = Lb;
298 outSize = La;
299 break;
300 case NO_DELAY:
301 if (Lb % 2)
302 startIndex = Lb/2;
303 else
304 startIndex = Lb/2-1;
305 outSize = La;
306 break;
307 case CUSTOM:
308 startIndex = startIx;
309 outSize = len;
310 break;
311 default:
312 return NULL;
313 }
314
315
316 if (c==NULL)
317 c = new signalVector(outSize);
318 else if (c->size()!=outSize)
319 return NULL;
320
321 signalVector::const_iterator aStart = a->begin();
322 signalVector::const_iterator bStart = b->begin();
323 signalVector::const_iterator aEnd = a->end();
324 signalVector::const_iterator bEnd = b->end();
325 signalVector::iterator cPtr = c->begin();
326 int t = startIndex;
327 int stopIndex = startIndex + outSize;
328 switch (b->getSymmetry()) {
329 case NONE:
330 {
331 while (t < stopIndex) {
332 signalVector::const_iterator aP = aStart+t;
333 signalVector::const_iterator bP = bStart;
334 if (a->isRealOnly() && b->isRealOnly()) {
335 float sum = 0.0;
336 while (bP < bEnd) {
337 if (aP < aStart) break;
338 if (aP < aEnd) sum += (aP->real())*(bP->real());
339 aP--;
340 bP++;
341 }
342 *cPtr++ = sum;
343 }
344 else if (a->isRealOnly()) {
345 complex sum = 0.0;
346 while (bP < bEnd) {
347 if (aP < aStart) break;
348 if (aP < aEnd) sum += (*bP)*(aP->real());
349 aP--;
350 bP++;
351 }
352 *cPtr++ = sum;
353 }
354 else if (b->isRealOnly()) {
355 complex sum = 0.0;
356 while (bP < bEnd) {
357 if (aP < aStart) break;
358 if (aP < aEnd) sum += (*aP)*(bP->real());
359 aP--;
360 bP++;
361 }
362 *cPtr++ = sum;
363 }
364 else {
365 complex sum = 0.0;
366 while (bP < bEnd) {
367 if (aP < aStart) break;
368 if (aP < aEnd) sum += (*aP)*(*bP);
369 aP--;
370 bP++;
371 }
372 *cPtr++ = sum;
373 }
374 t++;
375 }
376 }
377 break;
378 case ABSSYM:
379 {
380 complex sum = 0.0;
381 bool isOdd = (bool) (Lb % 2);
382 if (isOdd)
383 bEnd = bStart + (Lb+1)/2;
384 else
385 bEnd = bStart + Lb/2;
386 while (t < stopIndex) {
387 signalVector::const_iterator aP = aStart+t;
388 signalVector::const_iterator aPsym = aP-Lb+1;
389 signalVector::const_iterator bP = bStart;
390 sum = 0.0;
391 if (!b->isRealOnly()) {
392 while (bP < bEnd) {
393 if (aP < aStart) break;
394 if (aP == aPsym)
395 sum+= (*aP)*(*bP);
396 else if ((aP < aEnd) && (aPsym >= aStart))
397 sum+= ((*aP)+(*aPsym))*(*bP);
398 else if (aP < aEnd)
399 sum += (*aP)*(*bP);
400 else if (aPsym >= aStart)
401 sum += (*aPsym)*(*bP);
402 aP--;
403 aPsym++;
404 bP++;
405 }
406 }
407 else {
408 while (bP < bEnd) {
409 if (aP < aStart) break;
410 if (aP == aPsym)
411 sum+= (*aP)*(bP->real());
412 else if ((aP < aEnd) && (aPsym >= aStart))
413 sum+= ((*aP)+(*aPsym))*(bP->real());
414 else if (aP < aEnd)
415 sum += (*aP)*(bP->real());
416 else if (aPsym >= aStart)
417 sum += (*aPsym)*(bP->real());
418 aP--;
419 aPsym++;
420 bP++;
421 }
422 }
423 *cPtr++ = sum;
424 t++;
425 }
426 }
427 break;
428 default:
429 return NULL;
430 break;
431 }
432
433
434 return c;
435}
436
437
438signalVector* generateGSMPulse(int symbolLength,
439 int samplesPerSymbol)
440{
441
442 int numSamples = samplesPerSymbol*symbolLength + 1;
443 signalVector *x = new signalVector(numSamples);
444 signalVector::iterator xP = x->begin();
445 int centerPoint = (numSamples-1)/2;
446 for (int i = 0; i < numSamples; i++) {
447 float arg = (float) (i-centerPoint)/(float) samplesPerSymbol;
448 *xP++ = 0.96*exp(-1.1380*arg*arg-0.527*arg*arg*arg*arg); // GSM pulse approx.
449 }
450
451 float avgAbsval = sqrtf(vectorNorm2(*x)/samplesPerSymbol);
452 xP = x->begin();
453 for (int i = 0; i < numSamples; i++)
454 *xP++ /= avgAbsval;
455 x->isRealOnly(true);
456 x->setSymmetry(ABSSYM);
457 return x;
458}
459
460signalVector* frequencyShift(signalVector *y,
461 signalVector *x,
462 float freq,
463 float startPhase,
464 float *finalPhase)
465{
466
467 if (!x) return NULL;
468
469 if (y==NULL) {
470 y = new signalVector(x->size());
471 y->isRealOnly(x->isRealOnly());
472 if (y==NULL) return NULL;
473 }
474
475 if (y->size() < x->size()) return NULL;
476
477 float phase = startPhase;
478 signalVector::iterator yP = y->begin();
479 signalVector::iterator xPEnd = x->end();
480 signalVector::iterator xP = x->begin();
481
482 if (x->isRealOnly()) {
483 while (xP < xPEnd) {
484 (*yP++) = expjLookup(phase)*( (xP++)->real() );
485 phase += freq;
486 }
487 }
488 else {
489 while (xP < xPEnd) {
490 (*yP++) = (*xP++)*expjLookup(phase);
491 phase += freq;
492 }
493 }
494
495
496 if (finalPhase) *finalPhase = phase;
497
498 return y;
499}
500
501signalVector* reverseConjugate(signalVector *b)
502{
503 signalVector *tmp = new signalVector(b->size());
504 tmp->isRealOnly(b->isRealOnly());
505 signalVector::iterator bP = b->begin();
506 signalVector::iterator bPEnd = b->end();
507 signalVector::iterator tmpP = tmp->end()-1;
508 if (!b->isRealOnly()) {
509 while (bP < bPEnd) {
510 *tmpP-- = bP->conj();
511 bP++;
512 }
513 }
514 else {
515 while (bP < bPEnd) {
516 *tmpP-- = bP->real();
517 bP++;
518 }
519 }
520
521 return tmp;
522}
523
524signalVector* correlate(signalVector *a,
525 signalVector *b,
526 signalVector *c,
527 ConvType spanType,
528 bool bReversedConjugated,
529 unsigned startIx,
530 unsigned len)
531{
532 signalVector *tmp = NULL;
533
534 if (!bReversedConjugated) {
535 tmp = reverseConjugate(b);
536 }
537 else {
538 tmp = b;
539 }
540
541 c = convolve(a,tmp,c,spanType,startIx,len);
542
543 if (!bReversedConjugated) delete tmp;
544
545 return c;
546}
547
548
549/* soft output slicer */
550bool vectorSlicer(signalVector *x)
551{
552
553 signalVector::iterator xP = x->begin();
554 signalVector::iterator xPEnd = x->end();
555 while (xP < xPEnd) {
556 *xP = (complex) (0.5*(xP->real()+1.0F));
557 if (xP->real() > 1.0) *xP = 1.0;
558 if (xP->real() < 0.0) *xP = 0.0;
559 xP++;
560 }
561 return true;
562}
563
564signalVector *modulateBurst(const BitVector &wBurst,
565 const signalVector &gsmPulse,
566 int guardPeriodLength,
567 int samplesPerSymbol)
568{
569
570 //static complex staticBurst[157];
571
572 int burstSize = samplesPerSymbol*(wBurst.size()+guardPeriodLength);
573 //signalVector modBurst((complex *) staticBurst,0,burstSize);
574 signalVector modBurst(burstSize);// = new signalVector(burstSize);
575 modBurst.isRealOnly(true);
576 //memset(staticBurst,0,sizeof(complex)*burstSize);
577 modBurst.fill(0.0);
578 signalVector::iterator modBurstItr = modBurst.begin();
579
580#if 0
581 // if wBurst is already differentially decoded
582 *modBurstItr = 2.0*(wBurst[0] & 0x01)-1.0;
583 signalVector::iterator prevVal = modBurstItr;
584 for (unsigned int i = 1; i < wBurst.size(); i++) {
585 modBurstItr += samplesPerSymbol;
586 if (wBurst[i] & 0x01)
587 *modBurstItr = *prevVal * complex(0.0,1.0);
588 else
589 *modBurstItr = *prevVal * complex(0.0,-1.0);
590 prevVal = modBurstItr;
591 }
592#else
593 // if wBurst are the raw bits
594 for (unsigned int i = 0; i < wBurst.size(); i++) {
595 *modBurstItr = 2.0*(wBurst[i] & 0x01)-1.0;
596 modBurstItr += samplesPerSymbol;
597 }
598
599 // shift up pi/2
600 // ignore starting phase, since spec allows for discontinuous phase
601 GMSKRotate(modBurst);
602#endif
603 modBurst.isRealOnly(false);
604
605 // filter w/ pulse shape
606 signalVector *shapedBurst = convolve(&modBurst,&gsmPulse,NULL,NO_DELAY);
607
608 //delete modBurst;
609
610 return shapedBurst;
611
612}
613
614float sinc(float x)
615{
616 if ((x >= 0.01F) || (x <= -0.01F)) return (sinLookup(x)/x);
617 return 1.0F;
618}
619
620void delayVector(signalVector &wBurst,
621 float delay)
622{
623
624 int intOffset = (int) floor(delay);
625 float fracOffset = delay - intOffset;
626
627 // do fractional shift first, only do it for reasonable offsets
628 if (fabs(fracOffset) > 1e-2) {
629 // create sinc function
630 signalVector sincVector(21);
631 sincVector.isRealOnly(true);
632 signalVector::iterator sincBurstItr = sincVector.begin();
633 for (int i = 0; i < 21; i++)
634 *sincBurstItr++ = (complex) sinc(M_PI_F*(i-10-fracOffset));
635
636 signalVector shiftedBurst(wBurst.size());
637 convolve(&wBurst,&sincVector,&shiftedBurst,NO_DELAY);
638 wBurst.clone(shiftedBurst);
639 }
640
641 if (intOffset < 0) {
642 intOffset = -intOffset;
643 signalVector::iterator wBurstItr = wBurst.begin();
644 signalVector::iterator shiftedItr = wBurst.begin()+intOffset;
645 while (shiftedItr < wBurst.end())
646 *wBurstItr++ = *shiftedItr++;
647 while (wBurstItr < wBurst.end())
648 *wBurstItr++ = 0.0;
649 }
650 else {
651 signalVector::iterator wBurstItr = wBurst.end()-1;
652 signalVector::iterator shiftedItr = wBurst.end()-1-intOffset;
653 while (shiftedItr >= wBurst.begin())
654 *wBurstItr-- = *shiftedItr--;
655 while (wBurstItr >= wBurst.begin())
656 *wBurstItr-- = 0.0;
657 }
658}
659
660signalVector *gaussianNoise(int length,
661 float variance,
662 complex mean)
663{
664
665 signalVector *noise = new signalVector(length);
666 signalVector::iterator nPtr = noise->begin();
667 float stddev = sqrtf(variance);
668 while (nPtr < noise->end()) {
669 float u1 = (float) rand()/ (float) RAND_MAX;
670 while (u1==0.0)
671 u1 = (float) rand()/ (float) RAND_MAX;
672 float u2 = (float) rand()/ (float) RAND_MAX;
673 float arg = 2.0*M_PI*u2;
674 *nPtr = mean + stddev*complex(cos(arg),sin(arg))*sqrtf(-2.0*log(u1));
675 nPtr++;
676 }
677
678 return noise;
679}
680
681complex interpolatePoint(const signalVector &inSig,
682 float ix)
683{
684
685 int start = (int) (floor(ix) - 10);
686 if (start < 0) start = 0;
687 int end = (int) (floor(ix) + 11);
688 if ((unsigned) end > inSig.size()-1) end = inSig.size()-1;
689
690 complex pVal = 0.0;
691 if (!inSig.isRealOnly()) {
692 for (int i = start; i < end; i++)
693 pVal += inSig[i] * sinc(M_PI_F*(i-ix));
694 }
695 else {
696 for (int i = start; i < end; i++)
697 pVal += inSig[i].real() * sinc(M_PI_F*(i-ix));
698 }
699
700 return pVal;
701}
702
703
704
705complex peakDetect(const signalVector &rxBurst,
706 float *peakIndex,
707 float *avgPwr)
708{
709
710
711 complex maxVal = 0.0;
712 float maxIndex = -1;
713 float sumPower = 0.0;
714
715 for (unsigned int i = 0; i < rxBurst.size(); i++) {
716 float samplePower = rxBurst[i].norm2();
717 if (samplePower > maxVal.real()) {
718 maxVal = samplePower;
719 maxIndex = i;
720 }
721 sumPower += samplePower;
722 }
723
724 // interpolate around the peak
725 // to save computation, we'll use early-late balancing
726 float earlyIndex = maxIndex-1;
727 float lateIndex = maxIndex+1;
728
729 float incr = 0.5;
730 while (incr > 1.0/1024.0) {
731 complex earlyP = interpolatePoint(rxBurst,earlyIndex);
732 complex lateP = interpolatePoint(rxBurst,lateIndex);
733 if (earlyP < lateP)
734 earlyIndex += incr;
735 else if (earlyP > lateP)
736 earlyIndex -= incr;
737 else break;
738 incr /= 2.0;
739 lateIndex = earlyIndex + 2.0;
740 }
741
742 maxIndex = earlyIndex + 1.0;
743 maxVal = interpolatePoint(rxBurst,maxIndex);
744
745 if (peakIndex!=NULL)
746 *peakIndex = maxIndex;
747
748 if (avgPwr!=NULL)
749 *avgPwr = (sumPower-maxVal.norm2()) / (rxBurst.size()-1);
750
751 return maxVal;
752
753}
754
755void scaleVector(signalVector &x,
756 complex scale)
757{
758 signalVector::iterator xP = x.begin();
759 signalVector::iterator xPEnd = x.end();
760 if (!x.isRealOnly()) {
761 while (xP < xPEnd) {
762 *xP = *xP * scale;
763 xP++;
764 }
765 }
766 else {
767 while (xP < xPEnd) {
768 *xP = xP->real() * scale;
769 xP++;
770 }
771 }
772}
773
774/** in-place conjugation */
775void conjugateVector(signalVector &x)
776{
777 if (x.isRealOnly()) return;
778 signalVector::iterator xP = x.begin();
779 signalVector::iterator xPEnd = x.end();
780 while (xP < xPEnd) {
781 *xP = xP->conj();
782 xP++;
783 }
784}
785
786
787// in-place addition!!
788bool addVector(signalVector &x,
789 signalVector &y)
790{
791 signalVector::iterator xP = x.begin();
792 signalVector::iterator yP = y.begin();
793 signalVector::iterator xPEnd = x.end();
794 signalVector::iterator yPEnd = y.end();
795 while ((xP < xPEnd) && (yP < yPEnd)) {
796 *xP = *xP + *yP;
797 xP++; yP++;
798 }
799 return true;
800}
801
802// in-place multiplication!!
803bool multVector(signalVector &x,
804 signalVector &y)
805{
806 signalVector::iterator xP = x.begin();
807 signalVector::iterator yP = y.begin();
808 signalVector::iterator xPEnd = x.end();
809 signalVector::iterator yPEnd = y.end();
810 while ((xP < xPEnd) && (yP < yPEnd)) {
811 *xP = (*xP) * (*yP);
812 xP++; yP++;
813 }
814 return true;
815}
816
817
818void offsetVector(signalVector &x,
819 complex offset)
820{
821 signalVector::iterator xP = x.begin();
822 signalVector::iterator xPEnd = x.end();
823 if (!x.isRealOnly()) {
824 while (xP < xPEnd) {
825 *xP += offset;
826 xP++;
827 }
828 }
829 else {
830 while (xP < xPEnd) {
831 *xP = xP->real() + offset;
832 xP++;
833 }
834 }
835}
836
837bool generateMidamble(signalVector &gsmPulse,
838 int samplesPerSymbol,
839 int TSC)
840{
841
842 if ((TSC < 0) || (TSC > 7))
843 return false;
844
845 if (gMidambles[TSC]) {
846 if (gMidambles[TSC]->sequence!=NULL) delete gMidambles[TSC]->sequence;
847 if (gMidambles[TSC]->sequenceReversedConjugated!=NULL) delete gMidambles[TSC]->sequenceReversedConjugated;
848 }
849
850 signalVector emptyPulse(1);
851 *(emptyPulse.begin()) = 1.0;
852
853 // only use middle 16 bits of each TSC
854 signalVector *middleMidamble = modulateBurst(gTrainingSequence[TSC].segment(5,16),
855 emptyPulse,
856 0,
857 samplesPerSymbol);
858 signalVector *midamble = modulateBurst(gTrainingSequence[TSC],
859 gsmPulse,
860 0,
861 samplesPerSymbol);
862
863 if (midamble == NULL) return false;
864 if (middleMidamble == NULL) return false;
865
866 // NOTE: Because ideal TSC 16-bit midamble is 66 symbols into burst,
867 // the ideal TSC has an + 180 degree phase shift,
868 // due to the pi/2 frequency shift, that
869 // needs to be accounted for.
870 // 26-midamble is 61 symbols into burst, has +90 degree phase shift.
871 scaleVector(*middleMidamble,complex(-1.0,0.0));
872 scaleVector(*midamble,complex(0.0,1.0));
873
874 signalVector *autocorr = correlate(midamble,middleMidamble,NULL,NO_DELAY);
875
876 if (autocorr == NULL) return false;
877
878 gMidambles[TSC] = new CorrelationSequence;
879 gMidambles[TSC]->sequence = middleMidamble;
880 gMidambles[TSC]->sequenceReversedConjugated = reverseConjugate(middleMidamble);
881 gMidambles[TSC]->gain = peakDetect(*autocorr,&gMidambles[TSC]->TOA,NULL);
882
883 LOG(DEBUG) << "midamble autocorr: " << *autocorr;
884
885 LOG(DEBUG) << "TOA: " << gMidambles[TSC]->TOA;
886
887 //gMidambles[TSC]->TOA -= 5*samplesPerSymbol;
888
889 delete autocorr;
890 delete midamble;
891
892 return true;
893}
894
895bool generateRACHSequence(signalVector &gsmPulse,
896 int samplesPerSymbol)
897{
898
899 if (gRACHSequence) {
900 if (gRACHSequence->sequence!=NULL) delete gRACHSequence->sequence;
901 if (gRACHSequence->sequenceReversedConjugated!=NULL) delete gRACHSequence->sequenceReversedConjugated;
902 }
903
904 signalVector *RACHSeq = modulateBurst(gRACHSynchSequence,
905 gsmPulse,
906 0,
907 samplesPerSymbol);
908
909 assert(RACHSeq);
910
911 signalVector *autocorr = correlate(RACHSeq,RACHSeq,NULL,NO_DELAY);
912
913 assert(autocorr);
914
915 gRACHSequence = new CorrelationSequence;
916 gRACHSequence->sequence = RACHSeq;
917 gRACHSequence->sequenceReversedConjugated = reverseConjugate(RACHSeq);
918 gRACHSequence->gain = peakDetect(*autocorr,&gRACHSequence->TOA,NULL);
919
920 delete autocorr;
921
922 return true;
923
924}
925
926
927bool detectRACHBurst(signalVector &rxBurst,
928 float detectThreshold,
929 int samplesPerSymbol,
930 complex *amplitude,
931 float* TOA)
932{
933
934 //static complex staticData[500];
935
936 //signalVector correlatedRACH(staticData,0,rxBurst.size());
937 signalVector correlatedRACH(rxBurst.size());
938 correlate(&rxBurst,gRACHSequence->sequenceReversedConjugated,&correlatedRACH,NO_DELAY,true);
939
940 float meanPower;
941 complex peakAmpl = peakDetect(correlatedRACH,TOA,&meanPower);
942
943 float valleyPower = 0.0;
944
945 // check for bogus results
946 if ((*TOA < 0.0) || (*TOA > correlatedRACH.size())) {
947 *amplitude = 0.0;
948 return false;
949 }
950 complex *peakPtr = correlatedRACH.begin() + (int) rint(*TOA);
951
952 LOG(DEBUG) << "RACH corr: " << correlatedRACH;
953
954 float numSamples = 0.0;
955 for (int i = 57*samplesPerSymbol; i <= 107*samplesPerSymbol;i++) {
956 if (peakPtr+i >= correlatedRACH.end())
957 break;
958 valleyPower += (peakPtr+i)->norm2();
959 numSamples++;
960 }
961
962 if (numSamples < 2) {
963 *amplitude = 0.0;
964 return false;
965 }
966
967 float RMS = sqrtf(valleyPower/(float) numSamples)+0.00001;
968 float peakToMean = peakAmpl.abs()/RMS;
969
970 LOG(DEBUG) << "RACH peakAmpl=" << peakAmpl << " RMS=" << RMS << " peakToMean=" << peakToMean;
971 *amplitude = peakAmpl/(gRACHSequence->gain);
972
973 *TOA = (*TOA) - gRACHSequence->TOA - 8*samplesPerSymbol;
974
975 LOG(DEBUG) << "RACH thresh: " << peakToMean;
976
977 return (peakToMean > detectThreshold);
978}
979
980bool energyDetect(signalVector &rxBurst,
981 unsigned windowLength,
982 float detectThreshold,
983 float *avgPwr)
984{
985
986 signalVector::const_iterator windowItr = rxBurst.begin(); //+rxBurst.size()/2 - 5*windowLength/2;
987 float energy = 0.0;
988 if (windowLength < 0) windowLength = 20;
989 if (windowLength > rxBurst.size()) windowLength = rxBurst.size();
990 for (unsigned i = 0; i < windowLength; i++) {
991 energy += windowItr->norm2();
992 windowItr+=4;
993 }
994 if (avgPwr) *avgPwr = energy/windowLength;
995 LOG(DEBUG) << "detected energy: " << energy/windowLength;
996 return (energy/windowLength > detectThreshold*detectThreshold);
997}
998
999
1000bool analyzeTrafficBurst(signalVector &rxBurst,
1001 unsigned TSC,
1002 float detectThreshold,
1003 int samplesPerSymbol,
1004 complex *amplitude,
1005 float *TOA,
1006 unsigned maxTOA,
1007 bool requestChannel,
1008 signalVector **channelResponse,
1009 float *channelResponseOffset)
1010{
1011
1012 assert(TSC<8);
1013 assert(amplitude);
1014 assert(TOA);
1015 assert(gMidambles[TSC]);
1016
1017 if (maxTOA < 3*samplesPerSymbol) maxTOA = 3*samplesPerSymbol;
1018 unsigned spanTOA = maxTOA;
1019 if (spanTOA < 5*samplesPerSymbol) spanTOA = 5*samplesPerSymbol;
1020
1021 unsigned startIx = (66-spanTOA)*samplesPerSymbol;
1022 unsigned endIx = (66+16+spanTOA)*samplesPerSymbol;
1023 unsigned windowLen = endIx - startIx;
1024 unsigned corrLen = 2*maxTOA+1;
1025
1026 unsigned expectedTOAPeak = (unsigned) round(gMidambles[TSC]->TOA + (gMidambles[TSC]->sequenceReversedConjugated->size()-1)/2);
1027
1028 signalVector burstSegment(rxBurst.begin(),startIx,windowLen);
1029
1030 //static complex staticData[200];
1031 //signalVector correlatedBurst(staticData,0,corrLen);
1032 signalVector correlatedBurst(corrLen);
1033 correlate(&burstSegment, gMidambles[TSC]->sequenceReversedConjugated,
1034 &correlatedBurst, CUSTOM,true,
1035 expectedTOAPeak-maxTOA,corrLen);
1036
1037 float meanPower;
1038 *amplitude = peakDetect(correlatedBurst,TOA,&meanPower);
1039 float valleyPower = 0.0; //amplitude->norm2();
1040 complex *peakPtr = correlatedBurst.begin() + (int) rint(*TOA);
1041
1042 // check for bogus results
1043 if ((*TOA < 0.0) || (*TOA > correlatedBurst.size())) {
1044 *amplitude = 0.0;
1045 return false;
1046 }
1047
1048 int numRms = 0;
1049 for (int i = 2*samplesPerSymbol; i <= 5*samplesPerSymbol;i++) {
1050 if (peakPtr - i >= correlatedBurst.begin()) {
1051 valleyPower += (peakPtr-i)->norm2();
1052 numRms++;
1053 }
1054 if (peakPtr + i < correlatedBurst.end()) {
1055 valleyPower += (peakPtr+i)->norm2();
1056 numRms++;
1057 }
1058 }
1059
1060 if (numRms < 2) {
1061 // check for bogus results
1062 *amplitude = 0.0;
1063 return false;
1064 }
1065
1066 float RMS = sqrtf(valleyPower/(float)numRms)+0.00001;
1067 float peakToMean = (amplitude->abs())/RMS;
1068
1069 // NOTE: Because ideal TSC is 66 symbols into burst,
1070 // the ideal TSC has an +/- 180 degree phase shift,
1071 // due to the pi/4 frequency shift, that
1072 // needs to be accounted for.
1073
1074 *amplitude = (*amplitude)/gMidambles[TSC]->gain;
1075 *TOA = (*TOA) - (maxTOA);
1076
1077 LOG(DEBUG) << "TCH peakAmpl=" << amplitude->abs() << " RMS=" << RMS << " peakToMean=" << peakToMean << " TOA=" << *TOA;
1078
1079 LOG(DEBUG) << "autocorr: " << correlatedBurst;
1080
1081 if (requestChannel && (peakToMean > detectThreshold)) {
1082 float TOAoffset = maxTOA; //gMidambles[TSC]->TOA+(66*samplesPerSymbol-startIx);
1083 delayVector(correlatedBurst,-(*TOA));
1084 // midamble only allows estimation of a 6-tap channel
1085 signalVector channelVector(6*samplesPerSymbol);
1086 float maxEnergy = -1.0;
1087 int maxI = -1;
1088 for (int i = 0; i < 7; i++) {
1089 if (TOAoffset+(i-5)*samplesPerSymbol + channelVector.size() > correlatedBurst.size()) continue;
1090 if (TOAoffset+(i-5)*samplesPerSymbol < 0) continue;
1091 correlatedBurst.segmentCopyTo(channelVector,(int) floor(TOAoffset+(i-5)*samplesPerSymbol),channelVector.size());
1092 float energy = vectorNorm2(channelVector);
1093 if (energy > 0.95*maxEnergy) {
1094 maxI = i;
1095 maxEnergy = energy;
1096 }
1097 }
1098
1099 *channelResponse = new signalVector(channelVector.size());
1100 correlatedBurst.segmentCopyTo(**channelResponse,(int) floor(TOAoffset+(maxI-5)*samplesPerSymbol),(*channelResponse)->size());
1101 scaleVector(**channelResponse,complex(1.0,0.0)/gMidambles[TSC]->gain);
1102 LOG(DEBUG) << "channelResponse: " << **channelResponse;
1103
1104 if (channelResponseOffset)
1105 *channelResponseOffset = 5*samplesPerSymbol-maxI;
1106
1107 }
1108
1109 return (peakToMean > detectThreshold);
1110
1111}
1112
1113signalVector *decimateVector(signalVector &wVector,
1114 int decimationFactor)
1115{
1116
1117 if (decimationFactor <= 1) return NULL;
1118
1119 signalVector *decVector = new signalVector(wVector.size()/decimationFactor);
1120 decVector->isRealOnly(wVector.isRealOnly());
1121
1122 signalVector::iterator vecItr = decVector->begin();
1123 for (unsigned int i = 0; i < wVector.size();i+=decimationFactor)
1124 *vecItr++ = wVector[i];
1125
1126 return decVector;
1127}
1128
1129
1130SoftVector *demodulateBurst(signalVector &rxBurst,
1131 const signalVector &gsmPulse,
1132 int samplesPerSymbol,
1133 complex channel,
1134 float TOA)
1135
1136{
1137 scaleVector(rxBurst,((complex) 1.0)/channel);
1138 delayVector(rxBurst,-TOA);
1139
1140 signalVector *shapedBurst = &rxBurst;
1141
1142 // shift up by a quarter of a frequency
1143 // ignore starting phase, since spec allows for discontinuous phase
1144 GMSKReverseRotate(*shapedBurst);
1145
1146 // run through slicer
1147 if (samplesPerSymbol > 1) {
1148 signalVector *decShapedBurst = decimateVector(*shapedBurst,samplesPerSymbol);
1149 shapedBurst = decShapedBurst;
1150 }
1151
1152 LOG(DEBUG) << "shapedBurst: " << *shapedBurst;
1153
1154 vectorSlicer(shapedBurst);
1155
1156 SoftVector *burstBits = new SoftVector(shapedBurst->size());
1157
1158 SoftVector::iterator burstItr = burstBits->begin();
1159 signalVector::iterator shapedItr = shapedBurst->begin();
1160 for (; shapedItr < shapedBurst->end(); shapedItr++)
1161 *burstItr++ = shapedItr->real();
1162
1163 if (samplesPerSymbol > 1) delete shapedBurst;
1164
1165 return burstBits;
1166
1167}
1168
1169
1170// 1.0 is sampling frequency
1171// must satisfy cutoffFreq > 1/filterLen
1172signalVector *createLPF(float cutoffFreq,
1173 int filterLen,
1174 float gainDC)
1175{
kurtis.heimerla198d452011-11-26 03:19:28 +00001176#if 0
dburgessb3a0ca42011-10-12 07:44:40 +00001177 signalVector *LPF = new signalVector(filterLen-1);
1178 LPF->isRealOnly(true);
1179 LPF->setSymmetry(ABSSYM);
1180 signalVector::iterator itr = LPF->begin();
1181 double sum = 0.0;
1182 for (int i = 1; i < filterLen; i++) {
1183 float ys = sinc(M_2PI_F*cutoffFreq*((float)i-(float)(filterLen)/2.0F));
1184 float yg = 4.0F * cutoffFreq;
1185 // Blackman -- less brickwall (sloping transition) but larger stopband attenuation
1186 float yw = 0.42 - 0.5*cos(((float)i)*M_2PI_F/(float)(filterLen)) + 0.08*cos(((float)i)*2*M_2PI_F/(float)(filterLen));
1187 // Hamming -- more brickwall with smaller stopband attenuation
1188 //float yw = 0.53836F - 0.46164F * cos(((float)i)*M_2PI_F/(float)(filterLen+1));
1189 *itr++ = (complex) ys*yg*yw;
1190 sum += ys*yg*yw;
1191 }
kurtis.heimerla198d452011-11-26 03:19:28 +00001192#else
1193 double sum = 0.0;
1194 signalVector *LPF;
1195 signalVector::iterator itr;
1196 if (filterLen == 651) { // receive LPF
1197 LPF = new signalVector(651);
1198 LPF->isRealOnly(true);
1199 itr = LPF->begin();
1200 for (int i = 0; i < filterLen; i++) {
1201 *itr++ = complex(rcvLPF_651[i],0.0);
1202 sum += rcvLPF_651[i];
1203 }
1204 }
1205 else {
1206 LPF = new signalVector(961);
1207 LPF->isRealOnly(true);
1208 itr = LPF->begin();
1209 for (int i = 0; i < filterLen; i++) {
1210 *itr++ = complex(sendLPF_961[i],0.0);
1211 sum += sendLPF_961[i];
1212 }
1213 }
1214#endif
1215
dburgessb3a0ca42011-10-12 07:44:40 +00001216 float normFactor = gainDC/sum; //sqrtf(gainDC/vectorNorm2(*LPF));
1217 // normalize power
1218 itr = LPF->begin();
kurtis.heimerla198d452011-11-26 03:19:28 +00001219 for (int i = 0; i < filterLen; i++) {
dburgessb3a0ca42011-10-12 07:44:40 +00001220 *itr = *itr*normFactor;
1221 itr++;
1222 }
1223 return LPF;
1224
1225}
1226
1227
1228
1229#define POLYPHASESPAN 10
1230
1231// assumes filter group delay is 0.5*(length of filter)
1232signalVector *polyphaseResampleVector(signalVector &wVector,
1233 int P, int Q,
1234 signalVector *LPF)
1235
1236{
1237
1238 bool deleteLPF = false;
1239
1240 if (LPF==NULL) {
1241 float cutoffFreq = (P < Q) ? (1.0/(float) Q) : (1.0/(float) P);
1242 LPF = createLPF(cutoffFreq/3.0,100*POLYPHASESPAN+1,Q);
1243 deleteLPF = true;
1244 }
1245
1246 signalVector *resampledVector = new signalVector((int) ceil(wVector.size()*(float) P / (float) Q));
1247 resampledVector->fill(0);
1248 resampledVector->isRealOnly(wVector.isRealOnly());
1249 signalVector::iterator newItr = resampledVector->begin();
1250
1251 //FIXME: need to update for real-only vectors
1252 int outputIx = (LPF->size()+1)/2/Q; //((P > Q) ? P : Q);
1253 while (newItr < resampledVector->end()) {
1254 int outputBranch = (outputIx*Q) % P;
1255 int inputOffset = (outputIx*Q - outputBranch)/P;
1256 signalVector::const_iterator inputItr = wVector.begin() + inputOffset;
1257 signalVector::const_iterator filtItr = LPF->begin() + outputBranch;
1258 while (inputItr >= wVector.end()) {
1259 inputItr--;
1260 filtItr+=P;
1261 }
1262 complex sum = 0.0;
1263 if ((LPF->getSymmetry()!=ABSSYM) || (P>1)) {
1264 if (!LPF->isRealOnly()) {
1265 while ( (inputItr >= wVector.begin()) && (filtItr < LPF->end()) ) {
1266 sum += (*inputItr)*(*filtItr);
1267 inputItr--;
1268 filtItr += P;
1269 }
1270 }
1271 else {
1272 while ( (inputItr >= wVector.begin()) && (filtItr < LPF->end()) ) {
1273 sum += (*inputItr)*(filtItr->real());
1274 inputItr--;
1275 filtItr += P;
1276 }
1277 }
1278 }
1279 else {
1280 signalVector::const_iterator revInputItr = inputItr- LPF->size() + 1;
1281 signalVector::const_iterator filtMidpoint = LPF->begin()+(LPF->size()-1)/2;
1282 if (!LPF->isRealOnly()) {
1283 while (filtItr <= filtMidpoint) {
1284 if (inputItr < revInputItr) break;
1285 if (inputItr == revInputItr)
1286 sum += (*inputItr)*(*filtItr);
1287 else if ( (inputItr < wVector.end()) && (revInputItr >= wVector.begin()) )
1288 sum += (*inputItr + *revInputItr)*(*filtItr);
1289 else if ( inputItr < wVector.end() )
1290 sum += (*inputItr)*(*filtItr);
1291 else if ( revInputItr >= wVector.begin() )
1292 sum += (*revInputItr)*(*filtItr);
1293 inputItr--;
1294 revInputItr++;
1295 filtItr++;
1296 }
1297 }
1298 else {
1299 while (filtItr <= filtMidpoint) {
1300 if (inputItr < revInputItr) break;
1301 if (inputItr == revInputItr)
1302 sum += (*inputItr)*(filtItr->real());
1303 else if ( (inputItr < wVector.end()) && (revInputItr >= wVector.begin()) )
1304 sum += (*inputItr + *revInputItr)*(filtItr->real());
1305 else if ( inputItr < wVector.end() )
1306 sum += (*inputItr)*(filtItr->real());
1307 else if ( revInputItr >= wVector.begin() )
1308 sum += (*revInputItr)*(filtItr->real());
1309 inputItr--;
1310 revInputItr++;
1311 filtItr++;
1312 }
1313 }
1314 }
1315 *newItr = sum;
1316 newItr++;
1317 outputIx++;
1318 }
1319
1320 if (deleteLPF) delete LPF;
1321
1322 return resampledVector;
1323}
1324
1325
1326signalVector *resampleVector(signalVector &wVector,
1327 float expFactor,
1328 complex endPoint)
1329
1330{
1331
1332 if (expFactor < 1.0) return NULL;
1333
1334 signalVector *retVec = new signalVector((int) ceil(wVector.size()*expFactor));
1335
1336 float t = 0.0;
1337
1338 signalVector::iterator retItr = retVec->begin();
1339 while (retItr < retVec->end()) {
1340 unsigned tLow = (unsigned int) floor(t);
1341 unsigned tHigh = tLow + 1;
1342 if (tLow > wVector.size()-1) break;
1343 if (tHigh > wVector.size()) break;
1344 complex lowPoint = wVector[tLow];
1345 complex highPoint = (tHigh == wVector.size()) ? endPoint : wVector[tHigh];
1346 complex a = (tHigh-t);
1347 complex b = (t-tLow);
1348 *retItr = (a*lowPoint + b*highPoint);
1349 t += 1.0/expFactor;
1350 }
1351
1352 return retVec;
1353
1354}
1355
1356
1357// Assumes symbol-spaced sampling!!!
1358// Based upon paper by Al-Dhahir and Cioffi
1359bool designDFE(signalVector &channelResponse,
1360 float SNRestimate,
1361 int Nf,
1362 signalVector **feedForwardFilter,
1363 signalVector **feedbackFilter)
1364{
1365
1366 signalVector G0(Nf);
1367 signalVector G1(Nf);
1368 signalVector::iterator G0ptr = G0.begin();
1369 signalVector::iterator G1ptr = G1.begin();
1370 signalVector::iterator chanPtr = channelResponse.begin();
1371
1372 int nu = channelResponse.size()-1;
1373
1374 *G0ptr = 1.0/sqrtf(SNRestimate);
1375 for(int j = 0; j <= nu; j++) {
1376 *G1ptr = chanPtr->conj();
1377 G1ptr++; chanPtr++;
1378 }
1379
1380 signalVector *L[Nf];
1381 signalVector::iterator Lptr;
1382 float d;
1383 for(int i = 0; i < Nf; i++) {
1384 d = G0.begin()->norm2() + G1.begin()->norm2();
1385 L[i] = new signalVector(Nf+nu);
1386 Lptr = L[i]->begin()+i;
1387 G0ptr = G0.begin(); G1ptr = G1.begin();
1388 while ((G0ptr < G0.end()) && (Lptr < L[i]->end())) {
1389 *Lptr = (*G0ptr*(G0.begin()->conj()) + *G1ptr*(G1.begin()->conj()) )/d;
1390 Lptr++;
1391 G0ptr++;
1392 G1ptr++;
1393 }
1394 complex k = (*G1.begin())/(*G0.begin());
1395
1396 if (i != Nf-1) {
1397 signalVector G0new = G1;
1398 scaleVector(G0new,k.conj());
1399 addVector(G0new,G0);
1400
1401 signalVector G1new = G0;
1402 scaleVector(G1new,k*(-1.0));
1403 addVector(G1new,G1);
1404 delayVector(G1new,-1.0);
1405
1406 scaleVector(G0new,1.0/sqrtf(1.0+k.norm2()));
1407 scaleVector(G1new,1.0/sqrtf(1.0+k.norm2()));
1408 G0 = G0new;
1409 G1 = G1new;
1410 }
1411 }
1412
1413 *feedbackFilter = new signalVector(nu);
1414 L[Nf-1]->segmentCopyTo(**feedbackFilter,Nf,nu);
1415 scaleVector(**feedbackFilter,(complex) -1.0);
1416 conjugateVector(**feedbackFilter);
1417
1418 signalVector v(Nf);
1419 signalVector::iterator vStart = v.begin();
1420 signalVector::iterator vPtr;
1421 *(vStart+Nf-1) = (complex) 1.0;
1422 for(int k = Nf-2; k >= 0; k--) {
1423 Lptr = L[k]->begin()+k+1;
1424 vPtr = vStart + k+1;
1425 complex v_k = 0.0;
1426 for (int j = k+1; j < Nf; j++) {
1427 v_k -= (*vPtr)*(*Lptr);
1428 vPtr++; Lptr++;
1429 }
1430 *(vStart + k) = v_k;
1431 }
1432
1433 *feedForwardFilter = new signalVector(Nf);
1434 signalVector::iterator w = (*feedForwardFilter)->begin();
1435 for (int i = 0; i < Nf; i++) {
1436 delete L[i];
1437 complex w_i = 0.0;
1438 int endPt = ( nu < (Nf-1-i) ) ? nu : (Nf-1-i);
1439 vPtr = vStart+i;
1440 chanPtr = channelResponse.begin();
1441 for (int k = 0; k < endPt+1; k++) {
1442 w_i += (*vPtr)*(chanPtr->conj());
1443 vPtr++; chanPtr++;
1444 }
1445 *w = w_i/d;
1446 w++;
1447 }
1448
1449
1450 return true;
1451
1452}
1453
1454// Assumes symbol-rate sampling!!!!
1455SoftVector *equalizeBurst(signalVector &rxBurst,
1456 float TOA,
1457 int samplesPerSymbol,
1458 signalVector &w, // feedforward filter
1459 signalVector &b) // feedback filter
1460{
1461
1462 delayVector(rxBurst,-TOA);
1463
1464 signalVector* postForwardFull = convolve(&rxBurst,&w,NULL,FULL_SPAN);
1465
1466 signalVector* postForward = new signalVector(rxBurst.size());
1467 postForwardFull->segmentCopyTo(*postForward,w.size()-1,rxBurst.size());
1468 delete postForwardFull;
1469
1470 signalVector::iterator dPtr = postForward->begin();
1471 signalVector::iterator dBackPtr;
1472 signalVector::iterator rotPtr = GMSKRotation->begin();
1473 signalVector::iterator revRotPtr = GMSKReverseRotation->begin();
1474
1475 signalVector *DFEoutput = new signalVector(postForward->size());
1476 signalVector::iterator DFEItr = DFEoutput->begin();
1477
1478 // NOTE: can insert the midamble and/or use midamble to estimate BER
1479 for (; dPtr < postForward->end(); dPtr++) {
1480 dBackPtr = dPtr-1;
1481 signalVector::iterator bPtr = b.begin();
1482 while ( (bPtr < b.end()) && (dBackPtr >= postForward->begin()) ) {
1483 *dPtr = *dPtr + (*bPtr)*(*dBackPtr);
1484 bPtr++;
1485 dBackPtr--;
1486 }
1487 *dPtr = *dPtr * (*revRotPtr);
1488 *DFEItr = *dPtr;
1489 // make decision on symbol
1490 *dPtr = (dPtr->real() > 0.0) ? 1.0 : -1.0;
1491 //*DFEItr = *dPtr;
1492 *dPtr = *dPtr * (*rotPtr);
1493 DFEItr++;
1494 rotPtr++;
1495 revRotPtr++;
1496 }
1497
1498 vectorSlicer(DFEoutput);
1499
1500 SoftVector *burstBits = new SoftVector(postForward->size());
1501 SoftVector::iterator burstItr = burstBits->begin();
1502 DFEItr = DFEoutput->begin();
1503 for (; DFEItr < DFEoutput->end(); DFEItr++)
1504 *burstItr++ = DFEItr->real();
1505
1506 delete postForward;
1507
1508 delete DFEoutput;
1509
1510 return burstBits;
1511}