dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1 | /* |
kurtis.heimerl | a198d45 | 2011-11-26 03:19:28 +0000 | [diff] [blame] | 2 | * Copyright 2008, 2011 Free Software Foundation, Inc. |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 3 | * |
| 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 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 25 | #include "sigProcLib.h" |
| 26 | #include "GSMCommon.h" |
| 27 | |
Alexander Chemeris | d734e2d | 2013-06-16 14:30:58 +0400 | [diff] [blame] | 28 | using namespace GSM; |
| 29 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 30 | extern "C" { |
| 31 | #include "convolve.h" |
| 32 | } |
| 33 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 34 | #define TABLESIZE 1024 |
| 35 | |
| 36 | /** Lookup tables for trigonometric approximation */ |
| 37 | float cosTable[TABLESIZE+1]; // add 1 element for wrap around |
| 38 | float sinTable[TABLESIZE+1]; |
| 39 | |
| 40 | /** Constants */ |
| 41 | static const float M_PI_F = (float)M_PI; |
| 42 | static const float M_2PI_F = (float)(2.0*M_PI); |
| 43 | static const float M_1_2PI_F = 1/M_2PI_F; |
| 44 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 45 | /* Precomputed rotation vectors */ |
| 46 | static signalVector *GMSKRotationN = NULL; |
| 47 | static signalVector *GMSKReverseRotationN = NULL; |
| 48 | static signalVector *GMSKRotation1 = NULL; |
| 49 | static signalVector *GMSKReverseRotation1 = NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 50 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 51 | /* |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 52 | * 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 Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 56 | */ |
| 57 | struct CorrelationSequence { |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 58 | CorrelationSequence() : sequence(NULL), buffer(NULL) |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 59 | { |
| 60 | } |
| 61 | |
| 62 | ~CorrelationSequence() |
| 63 | { |
| 64 | delete sequence; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 65 | free(buffer); |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 66 | } |
| 67 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 68 | signalVector *sequence; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 69 | void *buffer; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 70 | float toa; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 71 | complex gain; |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 72 | }; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 73 | |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 74 | /* |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 75 | * 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 Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 78 | */ |
| 79 | struct PulseSequence { |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 80 | PulseSequence() : c0(NULL), c1(NULL), empty(NULL), |
| 81 | c0_buffer(NULL), c1_buffer(NULL) |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 82 | { |
| 83 | } |
| 84 | |
| 85 | ~PulseSequence() |
| 86 | { |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 87 | delete c0; |
| 88 | delete c1; |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 89 | delete empty; |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 90 | free(c0_buffer); |
| 91 | free(c1_buffer); |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 92 | } |
| 93 | |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 94 | signalVector *c0; |
| 95 | signalVector *c1; |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 96 | signalVector *empty; |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 97 | void *c0_buffer; |
| 98 | void *c1_buffer; |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 99 | }; |
| 100 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 101 | CorrelationSequence *gMidambles[] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL}; |
| 102 | CorrelationSequence *gRACHSequence = NULL; |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 103 | PulseSequence *GSMPulse = NULL; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 104 | PulseSequence *GSMPulse1 = NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 105 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 106 | void sigProcLibDestroy() |
| 107 | { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 108 | for (int i = 0; i < 8; i++) { |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 109 | delete gMidambles[i]; |
| 110 | gMidambles[i] = NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 111 | } |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 112 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 113 | delete GMSKRotationN; |
| 114 | delete GMSKReverseRotationN; |
| 115 | delete GMSKRotation1; |
| 116 | delete GMSKReverseRotation1; |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 117 | delete gRACHSequence; |
| 118 | delete GSMPulse; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 119 | delete GSMPulse1; |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 120 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 121 | GMSKRotationN = NULL; |
| 122 | GMSKRotation1 = NULL; |
| 123 | GMSKReverseRotationN = NULL; |
| 124 | GMSKReverseRotation1 = NULL; |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 125 | gRACHSequence = NULL; |
| 126 | GSMPulse = NULL; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 127 | GSMPulse1 = NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 128 | } |
| 129 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 130 | // dB relative to 1.0. |
| 131 | // if > 1.0, then return 0 dB |
| 132 | float 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. |
| 161 | float 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 | |
| 190 | float 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 | |
| 201 | float vectorPower(const signalVector &x) |
| 202 | { |
| 203 | return vectorNorm2(x)/x.size(); |
| 204 | } |
| 205 | |
| 206 | /** compute cosine via lookup table */ |
| 207 | float 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 */ |
| 221 | float 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. */ |
| 236 | complex 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 */ |
| 251 | void 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 Tsou | d24cc2c | 2013-08-20 15:41:45 -0400 | [diff] [blame] | 258 | void initGMSKRotationTables(int sps) |
| 259 | { |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 260 | GMSKRotationN = new signalVector(157 * sps); |
| 261 | GMSKReverseRotationN = new signalVector(157 * sps); |
| 262 | signalVector::iterator rotPtr = GMSKRotationN->begin(); |
| 263 | signalVector::iterator revPtr = GMSKReverseRotationN->begin(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 264 | float phase = 0.0; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 265 | while (rotPtr != GMSKRotationN->end()) { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 266 | *rotPtr++ = expjLookup(phase); |
| 267 | *revPtr++ = expjLookup(-phase); |
Thomas Tsou | d24cc2c | 2013-08-20 15:41:45 -0400 | [diff] [blame] | 268 | phase += M_PI_F / 2.0F / (float) sps; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 269 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 270 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 271 | 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 Tsou | e57004d | 2013-08-20 18:55:33 -0400 | [diff] [blame] | 280 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 283 | static 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 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 292 | 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 Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 306 | static 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 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 315 | 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 Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 329 | signalVector *convolve(const signalVector *x, |
| 330 | const signalVector *h, |
| 331 | signalVector *y, |
| 332 | ConvType spanType, int start, |
| 333 | unsigned len, unsigned step, int offset) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 334 | { |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 335 | int rc, head = 0, tail = 0; |
| 336 | bool alloc = false, append = false; |
| 337 | const signalVector *_x = NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 338 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 339 | if (!x || !h) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 340 | return NULL; |
| 341 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 342 | switch (spanType) { |
| 343 | case START_ONLY: |
| 344 | start = 0; |
| 345 | head = h->size(); |
| 346 | len = x->size(); |
| 347 | append = true; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 348 | break; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 349 | 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; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 364 | } |
| 365 | break; |
| 366 | default: |
| 367 | return NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 368 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 369 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 370 | /* |
| 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 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 429 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 430 | static bool generateC1Pulse(int sps, PulseSequence *pulse) |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 431 | { |
| 432 | int len; |
| 433 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 434 | if (!pulse) |
| 435 | return false; |
| 436 | |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 437 | switch (sps) { |
| 438 | case 4: |
| 439 | len = 8; |
| 440 | break; |
| 441 | default: |
| 442 | return false; |
| 443 | } |
| 444 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 445 | 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 Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 449 | |
| 450 | /* Enable alignment for SSE usage */ |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 451 | pulse->c1->setAligned(true); |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 452 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 453 | signalVector::iterator xP = pulse->c1->begin(); |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 454 | |
| 455 | switch (sps) { |
| 456 | case 4: |
| 457 | /* BT = 0.30 */ |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 458 | *xP++ = 0.0; |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 459 | *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 Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 471 | static PulseSequence *generateGSMPulse(int sps, int symbolLength) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 472 | { |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 473 | int len; |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 474 | float arg, avg, center; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 475 | PulseSequence *pulse; |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 476 | |
| 477 | /* Store a single tap filter used for correlation sequence generation */ |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 478 | pulse = new PulseSequence(); |
| 479 | pulse->empty = new signalVector(1); |
| 480 | pulse->empty->isRealOnly(true); |
| 481 | *(pulse->empty->begin()) = 1.0f; |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 482 | |
Thomas Tsou | 9ccd9f2 | 2013-08-21 13:59:52 -0400 | [diff] [blame] | 483 | /* |
| 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 Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 498 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 499 | pulse->c0_buffer = convolve_h_alloc(len); |
| 500 | pulse->c0 = new signalVector((complex *) pulse->c0_buffer, 0, len); |
| 501 | pulse->c0->isRealOnly(true); |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 502 | |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 503 | /* Enable alingnment for SSE usage */ |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 504 | pulse->c0->setAligned(true); |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 505 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 506 | signalVector::iterator xP = pulse->c0->begin(); |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 507 | |
Thomas Tsou | 9ccd9f2 | 2013-08-21 13:59:52 -0400 | [diff] [blame] | 508 | if (sps == 4) { |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 509 | *xP++ = 0.0; |
Thomas Tsou | 9ccd9f2 | 2013-08-21 13:59:52 -0400 | [diff] [blame] | 510 | *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 Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 525 | generateC1Pulse(sps, pulse); |
Thomas Tsou | 9ccd9f2 | 2013-08-21 13:59:52 -0400 | [diff] [blame] | 526 | } else { |
| 527 | center = (float) (len - 1.0) / 2.0; |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 528 | |
Thomas Tsou | 9ccd9f2 | 2013-08-21 13:59:52 -0400 | [diff] [blame] | 529 | /* 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 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 535 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 536 | avg = sqrtf(vectorNorm2(*pulse->c0) / sps); |
| 537 | xP = pulse->c0->begin(); |
| 538 | for (int i = 0; i < len; i++) |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 539 | *xP++ /= avg; |
| 540 | } |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 541 | |
| 542 | return pulse; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | signalVector* 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 | |
| 586 | signalVector* 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 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 609 | /* soft output slicer */ |
| 610 | bool 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 Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 623 | |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 624 | static 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 Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 631 | pulse = GSMPulse1->empty; |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 632 | 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 Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 641 | GMSKRotate(rotated, sps); |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 642 | 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 | |
| 652 | static 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 Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 694 | GMSKRotate(c0_burst, sps); |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 695 | 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 | |
| 736 | static 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 Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 743 | if (sps == 1) |
| 744 | pulse = GSMPulse1->c0; |
| 745 | else |
| 746 | pulse = GSMPulse->c0; |
| 747 | |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 748 | 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 Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 760 | GMSKRotate(burst, sps); |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 761 | 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 Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 769 | /* Assume input bits are not differentially encoded */ |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 770 | signalVector *modulateBurst(const BitVector &wBurst, int guardPeriodLength, |
| 771 | int sps, bool emptyPulse) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 772 | { |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 773 | if (emptyPulse) |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 774 | return rotateBurst(wBurst, guardPeriodLength, sps); |
| 775 | else if (sps == 4) |
| 776 | return modulateBurstLaurent(wBurst, guardPeriodLength, sps); |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 777 | else |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 778 | return modulateBurstBasic(wBurst, guardPeriodLength, sps); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 781 | float sinc(float x) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 782 | { |
| 783 | if ((x >= 0.01F) || (x <= -0.01F)) return (sinLookup(x)/x); |
| 784 | return 1.0F; |
| 785 | } |
| 786 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 787 | bool delayVector(signalVector &wBurst, float delay) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 788 | { |
Thomas Tsou | 2c282f5 | 2013-10-08 21:34:35 -0400 | [diff] [blame] | 789 | int whole, h_len = 20; |
| 790 | float frac; |
| 791 | complex *data; |
| 792 | signalVector *h, *shift; |
| 793 | signalVector::iterator itr; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 794 | |
Thomas Tsou | 2c282f5 | 2013-10-08 21:34:35 -0400 | [diff] [blame] | 795 | 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 Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 815 | return false; |
Thomas Tsou | 2c282f5 | 2013-10-08 21:34:35 -0400 | [diff] [blame] | 816 | |
| 817 | wBurst.clone(*shift); |
| 818 | delete shift; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Thomas Tsou | 2c282f5 | 2013-10-08 21:34:35 -0400 | [diff] [blame] | 821 | /* Integer sample shift */ |
| 822 | if (whole < 0) { |
| 823 | whole = -whole; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 824 | signalVector::iterator wBurstItr = wBurst.begin(); |
Thomas Tsou | 2c282f5 | 2013-10-08 21:34:35 -0400 | [diff] [blame] | 825 | signalVector::iterator shiftedItr = wBurst.begin() + whole; |
| 826 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 827 | while (shiftedItr < wBurst.end()) |
| 828 | *wBurstItr++ = *shiftedItr++; |
| 829 | while (wBurstItr < wBurst.end()) |
| 830 | *wBurstItr++ = 0.0; |
Thomas Tsou | 2c282f5 | 2013-10-08 21:34:35 -0400 | [diff] [blame] | 831 | } else { |
| 832 | signalVector::iterator wBurstItr = wBurst.end() - 1; |
| 833 | signalVector::iterator shiftedItr = wBurst.end() - 1 - whole; |
| 834 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 835 | while (shiftedItr >= wBurst.begin()) |
| 836 | *wBurstItr-- = *shiftedItr--; |
| 837 | while (wBurstItr >= wBurst.begin()) |
| 838 | *wBurstItr-- = 0.0; |
| 839 | } |
Thomas Tsou | 2c282f5 | 2013-10-08 21:34:35 -0400 | [diff] [blame] | 840 | |
| 841 | return true; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 842 | } |
Thomas Tsou | 2c282f5 | 2013-10-08 21:34:35 -0400 | [diff] [blame] | 843 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 844 | signalVector *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 | |
| 865 | complex 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 Tsou | 8181b01 | 2013-08-20 21:17:19 -0400 | [diff] [blame] | 887 | static 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 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 908 | complex 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 | |
| 958 | void 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 */ |
| 978 | void 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!! |
| 991 | bool 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!! |
| 1006 | bool 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 | |
| 1021 | void 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 Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1040 | bool generateMidamble(int sps, int tsc) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1041 | { |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1042 | bool status = true; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 1043 | float toa; |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1044 | complex *data = NULL; |
| 1045 | signalVector *autocorr = NULL, *midamble = NULL; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1046 | signalVector *midMidamble = NULL, *_midMidamble = NULL; |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1047 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1048 | if ((tsc < 0) || (tsc > 7)) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1049 | return false; |
| 1050 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1051 | delete gMidambles[tsc]; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1052 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1053 | /* 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 Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1058 | /* Simulated receive sequence is pulse shaped */ |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1059 | midamble = modulateBurst(gTrainingSequence[tsc], 0, sps, false); |
| 1060 | if (!midamble) { |
| 1061 | status = false; |
| 1062 | goto release; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1063 | } |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1064 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1065 | // 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 Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1070 | scaleVector(*midMidamble, complex(-1.0, 0.0)); |
| 1071 | scaleVector(*midamble, complex(0.0, 1.0)); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1072 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1073 | conjugateVector(*midMidamble); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1074 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1075 | /* 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 Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1083 | if (!autocorr) { |
| 1084 | status = false; |
| 1085 | goto release; |
| 1086 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1087 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1088 | gMidambles[tsc] = new CorrelationSequence; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1089 | gMidambles[tsc]->buffer = data; |
| 1090 | gMidambles[tsc]->sequence = _midMidamble; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 1091 | 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; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1101 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1102 | release: |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1103 | delete autocorr; |
| 1104 | delete midamble; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1105 | delete midMidamble; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1106 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1107 | if (!status) { |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1108 | delete _midMidamble; |
| 1109 | free(data); |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1110 | gMidambles[tsc] = NULL; |
| 1111 | } |
| 1112 | |
| 1113 | return status; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 1116 | bool generateRACHSequence(int sps) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1117 | { |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1118 | bool status = true; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 1119 | float toa; |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1120 | complex *data = NULL; |
| 1121 | signalVector *autocorr = NULL; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1122 | signalVector *seq0 = NULL, *seq1 = NULL, *_seq1 = NULL; |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1123 | |
| 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; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1136 | conjugateVector(*seq1); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1137 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1138 | /* 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 Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1146 | status = false; |
| 1147 | goto release; |
| 1148 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1149 | |
| 1150 | gRACHSequence = new CorrelationSequence; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1151 | gRACHSequence->sequence = _seq1; |
| 1152 | gRACHSequence->buffer = data; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 1153 | 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 Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1163 | |
| 1164 | release: |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1165 | delete autocorr; |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1166 | delete seq0; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1167 | delete seq1; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1168 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1169 | if (!status) { |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1170 | delete _seq1; |
| 1171 | free(data); |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1172 | gRACHSequence = NULL; |
| 1173 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1174 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1175 | return status; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1176 | } |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1177 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1178 | static float computePeakRatio(signalVector *corr, |
| 1179 | int sps, float toa, complex amp) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1180 | { |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1181 | int num = 0; |
| 1182 | complex *peak; |
| 1183 | float rms, avg = 0.0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1184 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1185 | peak = corr->begin() + (int) rint(toa); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1186 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1187 | /* Check for bogus results */ |
| 1188 | if ((toa < 0.0) || (toa > corr->size())) |
| 1189 | return 0.0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1190 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1191 | for (int i = 2 * sps; i <= 5 * sps; i++) { |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1192 | if (peak - i >= corr->begin()) { |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1193 | avg += (peak - i)->norm2(); |
| 1194 | num++; |
| 1195 | } |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1196 | if (peak + i < corr->end()) { |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1197 | avg += (peak + i)->norm2(); |
| 1198 | num++; |
| 1199 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1202 | if (num < 2) |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1203 | return 0.0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1204 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1205 | rms = sqrtf(avg / (float) num) + 0.00001; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1206 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1207 | return (amp.abs()) / rms; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | bool 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; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1225 | return (energy/windowLength > detectThreshold*detectThreshold); |
| 1226 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1227 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1228 | /* |
| 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 | */ |
| 1236 | static int detectBurst(signalVector &burst, |
| 1237 | signalVector &corr, CorrelationSequence *sync, |
| 1238 | float thresh, int sps, complex *amp, float *toa, |
| 1239 | int start, int len) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1240 | { |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1241 | /* Correlate */ |
| 1242 | if (!convolve(&burst, sync->sequence, &corr, |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1243 | CUSTOM, start, len, sps, 0)) { |
| 1244 | return -1; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1247 | /* Peak detection - place restrictions at correlation edges */ |
| 1248 | *amp = fastPeakDetect(corr, toa); |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1249 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1250 | if ((*toa < 3 * sps) || (*toa > len - 3 * sps)) |
| 1251 | return 0; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1252 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1253 | /* 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 Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 1263 | /* Compenate for residual rotation with dual Laurent pulse */ |
| 1264 | if (sps == 4) |
| 1265 | *amp = *amp * complex(0.0, 1.0); |
| 1266 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 1267 | /* Compensate for residuate time lag */ |
| 1268 | *toa = *toa - sync->toa; |
| 1269 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1270 | 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 Tsou | dafb337 | 2013-09-18 16:21:26 -0400 | [diff] [blame] | 1278 | * head: Search 4 symbols before target |
| 1279 | * tail: Search 10 symbols after target |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1280 | */ |
| 1281 | int 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 Tsou | dafb337 | 2013-09-18 16:21:26 -0400 | [diff] [blame] | 1297 | head = 4; |
| 1298 | tail = 10; |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1299 | |
| 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; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1317 | /* Subtract forward search bits from delay */ |
| 1318 | if (toa) |
| 1319 | *toa = _toa - head * sps; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1320 | if (amp) |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1321 | *amp = _amp; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1322 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1323 | return 1; |
| 1324 | } |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1325 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1326 | /* |
| 1327 | * Normal burst detection |
| 1328 | * |
| 1329 | * Correlation window parameters: |
| 1330 | * target: Tail + data + mid-midamble + 1/2 remaining midamblebits |
Thomas Tsou | dafb337 | 2013-09-18 16:21:26 -0400 | [diff] [blame] | 1331 | * head: Search 4 symbols before target |
| 1332 | * tail: Search 4 symbols + maximum expected delay |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1333 | */ |
| 1334 | int 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 Tsou | dafb337 | 2013-09-18 16:21:26 -0400 | [diff] [blame] | 1348 | head = 4; |
| 1349 | tail = 4 + max_toa; |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1350 | |
| 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 Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1370 | if (toa) |
| 1371 | *toa = _toa; |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1372 | if (amp) |
| 1373 | *amp = _amp; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1374 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1375 | /* Equalization not currently supported */ |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1376 | if (chan_req) { |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1377 | *chan = new signalVector(6 * sps); |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1378 | |
| 1379 | if (chan_offset) |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1380 | *chan_offset = 0.0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1383 | return 1; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | signalVector *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 Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 1403 | SoftVector *demodulateBurst(signalVector &rxBurst, int sps, |
| 1404 | complex channel, float TOA) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1405 | { |
| 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 Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 1413 | GMSKReverseRotate(*shapedBurst, sps); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1414 | |
| 1415 | // run through slicer |
Thomas Tsou | d24cc2c | 2013-08-20 15:41:45 -0400 | [diff] [blame] | 1416 | if (sps > 1) { |
| 1417 | signalVector *decShapedBurst = decimateVector(*shapedBurst, sps); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1418 | shapedBurst = decShapedBurst; |
| 1419 | } |
| 1420 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1421 | 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 Tsou | d24cc2c | 2013-08-20 15:41:45 -0400 | [diff] [blame] | 1430 | if (sps > 1) |
| 1431 | delete shapedBurst; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1432 | |
| 1433 | return burstBits; |
| 1434 | |
| 1435 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1436 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1437 | // Assumes symbol-spaced sampling!!! |
| 1438 | // Based upon paper by Al-Dhahir and Cioffi |
| 1439 | bool 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 Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1514 | signalVector::iterator w = (*feedForwardFilter)->end(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1515 | 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 Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1525 | *--w = w_i/d; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | |
| 1529 | return true; |
| 1530 | |
| 1531 | } |
| 1532 | |
| 1533 | // Assumes symbol-rate sampling!!!! |
| 1534 | SoftVector *equalizeBurst(signalVector &rxBurst, |
| 1535 | float TOA, |
Thomas Tsou | d24cc2c | 2013-08-20 15:41:45 -0400 | [diff] [blame] | 1536 | int sps, |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1537 | signalVector &w, // feedforward filter |
| 1538 | signalVector &b) // feedback filter |
| 1539 | { |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1540 | signalVector *postForwardFull; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1541 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1542 | if (!delayVector(rxBurst, -TOA)) |
| 1543 | return NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1544 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1545 | postForwardFull = convolve(&rxBurst, &w, NULL, |
| 1546 | CUSTOM, 0, rxBurst.size() + w.size() - 1); |
| 1547 | if (!postForwardFull) |
| 1548 | return NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1549 | |
| 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 Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 1556 | signalVector::iterator rotPtr = GMSKRotationN->begin(); |
| 1557 | signalVector::iterator revRotPtr = GMSKReverseRotationN->begin(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1558 | |
| 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 Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 1596 | |
| 1597 | bool 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 | } |