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" |
kurtis.heimerl | a198d45 | 2011-11-26 03:19:28 +0000 | [diff] [blame] | 27 | #include "sendLPF_961.h" |
| 28 | #include "rcvLPF_651.h" |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 29 | |
Alexander Chemeris | d734e2d | 2013-06-16 14:30:58 +0400 | [diff] [blame] | 30 | using namespace GSM; |
| 31 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 32 | extern "C" { |
| 33 | #include "convolve.h" |
| 34 | } |
| 35 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 36 | #define TABLESIZE 1024 |
| 37 | |
| 38 | /** Lookup tables for trigonometric approximation */ |
| 39 | float cosTable[TABLESIZE+1]; // add 1 element for wrap around |
| 40 | float sinTable[TABLESIZE+1]; |
| 41 | |
| 42 | /** Constants */ |
| 43 | static const float M_PI_F = (float)M_PI; |
| 44 | static const float M_2PI_F = (float)(2.0*M_PI); |
| 45 | static const float M_1_2PI_F = 1/M_2PI_F; |
| 46 | |
| 47 | /** Static vectors that contain a precomputed +/- f_b/4 sinusoid */ |
| 48 | signalVector *GMSKRotation = NULL; |
| 49 | signalVector *GMSKReverseRotation = NULL; |
| 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; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 70 | float TOA; |
| 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; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 104 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 105 | void sigProcLibDestroy() |
| 106 | { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 107 | for (int i = 0; i < 8; i++) { |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 108 | delete gMidambles[i]; |
| 109 | gMidambles[i] = NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 110 | } |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 111 | |
| 112 | delete GMSKRotation; |
| 113 | delete GMSKReverseRotation; |
| 114 | delete gRACHSequence; |
| 115 | delete GSMPulse; |
| 116 | |
| 117 | GMSKRotation = NULL; |
| 118 | GMSKReverseRotation = NULL; |
| 119 | gRACHSequence = NULL; |
| 120 | GSMPulse = NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 121 | } |
| 122 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 123 | // dB relative to 1.0. |
| 124 | // if > 1.0, then return 0 dB |
| 125 | float dB(float x) { |
| 126 | |
| 127 | float arg = 1.0F; |
| 128 | float dB = 0.0F; |
| 129 | |
| 130 | if (x >= 1.0F) return 0.0F; |
| 131 | if (x <= 0.0F) return -200.0F; |
| 132 | |
| 133 | float prevArg = arg; |
| 134 | float prevdB = dB; |
| 135 | float stepSize = 16.0F; |
| 136 | float dBstepSize = 12.0F; |
| 137 | while (stepSize > 1.0F) { |
| 138 | do { |
| 139 | prevArg = arg; |
| 140 | prevdB = dB; |
| 141 | arg /= stepSize; |
| 142 | dB -= dBstepSize; |
| 143 | } while (arg > x); |
| 144 | arg = prevArg; |
| 145 | dB = prevdB; |
| 146 | stepSize *= 0.5F; |
| 147 | dBstepSize -= 3.0F; |
| 148 | } |
| 149 | return ((arg-x)*(dB-3.0F) + (x-arg*0.5F)*dB)/(arg - arg*0.5F); |
| 150 | |
| 151 | } |
| 152 | |
| 153 | // 10^(-dB/10), inverse of dB func. |
| 154 | float dBinv(float x) { |
| 155 | |
| 156 | float arg = 1.0F; |
| 157 | float dB = 0.0F; |
| 158 | |
| 159 | if (x >= 0.0F) return 1.0F; |
| 160 | if (x <= -200.0F) return 0.0F; |
| 161 | |
| 162 | float prevArg = arg; |
| 163 | float prevdB = dB; |
| 164 | float stepSize = 16.0F; |
| 165 | float dBstepSize = 12.0F; |
| 166 | while (stepSize > 1.0F) { |
| 167 | do { |
| 168 | prevArg = arg; |
| 169 | prevdB = dB; |
| 170 | arg /= stepSize; |
| 171 | dB -= dBstepSize; |
| 172 | } while (dB > x); |
| 173 | arg = prevArg; |
| 174 | dB = prevdB; |
| 175 | stepSize *= 0.5F; |
| 176 | dBstepSize -= 3.0F; |
| 177 | } |
| 178 | |
| 179 | return ((dB-x)*(arg*0.5F)+(x-(dB-3.0F))*(arg))/3.0F; |
| 180 | |
| 181 | } |
| 182 | |
| 183 | float vectorNorm2(const signalVector &x) |
| 184 | { |
| 185 | signalVector::const_iterator xPtr = x.begin(); |
| 186 | float Energy = 0.0; |
| 187 | for (;xPtr != x.end();xPtr++) { |
| 188 | Energy += xPtr->norm2(); |
| 189 | } |
| 190 | return Energy; |
| 191 | } |
| 192 | |
| 193 | |
| 194 | float vectorPower(const signalVector &x) |
| 195 | { |
| 196 | return vectorNorm2(x)/x.size(); |
| 197 | } |
| 198 | |
| 199 | /** compute cosine via lookup table */ |
| 200 | float cosLookup(const float x) |
| 201 | { |
| 202 | float arg = x*M_1_2PI_F; |
| 203 | while (arg > 1.0F) arg -= 1.0F; |
| 204 | while (arg < 0.0F) arg += 1.0F; |
| 205 | |
| 206 | const float argT = arg*((float)TABLESIZE); |
| 207 | const int argI = (int)argT; |
| 208 | const float delta = argT-argI; |
| 209 | const float iDelta = 1.0F-delta; |
| 210 | return iDelta*cosTable[argI] + delta*cosTable[argI+1]; |
| 211 | } |
| 212 | |
| 213 | /** compute sine via lookup table */ |
| 214 | float sinLookup(const float x) |
| 215 | { |
| 216 | float arg = x*M_1_2PI_F; |
| 217 | while (arg > 1.0F) arg -= 1.0F; |
| 218 | while (arg < 0.0F) arg += 1.0F; |
| 219 | |
| 220 | const float argT = arg*((float)TABLESIZE); |
| 221 | const int argI = (int)argT; |
| 222 | const float delta = argT-argI; |
| 223 | const float iDelta = 1.0F-delta; |
| 224 | return iDelta*sinTable[argI] + delta*sinTable[argI+1]; |
| 225 | } |
| 226 | |
| 227 | |
| 228 | /** compute e^(-jx) via lookup table. */ |
| 229 | complex expjLookup(float x) |
| 230 | { |
| 231 | float arg = x*M_1_2PI_F; |
| 232 | while (arg > 1.0F) arg -= 1.0F; |
| 233 | while (arg < 0.0F) arg += 1.0F; |
| 234 | |
| 235 | const float argT = arg*((float)TABLESIZE); |
| 236 | const int argI = (int)argT; |
| 237 | const float delta = argT-argI; |
| 238 | const float iDelta = 1.0F-delta; |
| 239 | return complex(iDelta*cosTable[argI] + delta*cosTable[argI+1], |
| 240 | iDelta*sinTable[argI] + delta*sinTable[argI+1]); |
| 241 | } |
| 242 | |
| 243 | /** Library setup functions */ |
| 244 | void initTrigTables() { |
| 245 | for (int i = 0; i < TABLESIZE+1; i++) { |
| 246 | cosTable[i] = cos(2.0*M_PI*i/TABLESIZE); |
| 247 | sinTable[i] = sin(2.0*M_PI*i/TABLESIZE); |
| 248 | } |
| 249 | } |
| 250 | |
Thomas Tsou | d24cc2c | 2013-08-20 15:41:45 -0400 | [diff] [blame] | 251 | void initGMSKRotationTables(int sps) |
| 252 | { |
| 253 | GMSKRotation = new signalVector(157 * sps); |
| 254 | GMSKReverseRotation = new signalVector(157 * sps); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 255 | signalVector::iterator rotPtr = GMSKRotation->begin(); |
| 256 | signalVector::iterator revPtr = GMSKReverseRotation->begin(); |
| 257 | float phase = 0.0; |
| 258 | while (rotPtr != GMSKRotation->end()) { |
| 259 | *rotPtr++ = expjLookup(phase); |
| 260 | *revPtr++ = expjLookup(-phase); |
Thomas Tsou | d24cc2c | 2013-08-20 15:41:45 -0400 | [diff] [blame] | 261 | phase += M_PI_F / 2.0F / (float) sps; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
Thomas Tsou | e57004d | 2013-08-20 18:55:33 -0400 | [diff] [blame] | 265 | bool sigProcLibSetup(int sps) |
Thomas Tsou | d24cc2c | 2013-08-20 15:41:45 -0400 | [diff] [blame] | 266 | { |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 267 | if ((sps != 1) && (sps != 4)) |
Thomas Tsou | e57004d | 2013-08-20 18:55:33 -0400 | [diff] [blame] | 268 | return false; |
| 269 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 270 | initTrigTables(); |
Thomas Tsou | d24cc2c | 2013-08-20 15:41:45 -0400 | [diff] [blame] | 271 | initGMSKRotationTables(sps); |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 272 | generateGSMPulse(sps, 2); |
Thomas Tsou | e57004d | 2013-08-20 18:55:33 -0400 | [diff] [blame] | 273 | |
| 274 | if (!generateRACHSequence(sps)) { |
| 275 | sigProcLibDestroy(); |
| 276 | return false; |
| 277 | } |
| 278 | |
| 279 | return true; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | void GMSKRotate(signalVector &x) { |
| 283 | signalVector::iterator xPtr = x.begin(); |
| 284 | signalVector::iterator rotPtr = GMSKRotation->begin(); |
| 285 | if (x.isRealOnly()) { |
| 286 | while (xPtr < x.end()) { |
| 287 | *xPtr = *rotPtr++ * (xPtr->real()); |
| 288 | xPtr++; |
| 289 | } |
| 290 | } |
| 291 | else { |
| 292 | while (xPtr < x.end()) { |
| 293 | *xPtr = *rotPtr++ * (*xPtr); |
| 294 | xPtr++; |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | void GMSKReverseRotate(signalVector &x) { |
| 300 | signalVector::iterator xPtr= x.begin(); |
| 301 | signalVector::iterator rotPtr = GMSKReverseRotation->begin(); |
| 302 | if (x.isRealOnly()) { |
| 303 | while (xPtr < x.end()) { |
| 304 | *xPtr = *rotPtr++ * (xPtr->real()); |
| 305 | xPtr++; |
| 306 | } |
| 307 | } |
| 308 | else { |
| 309 | while (xPtr < x.end()) { |
| 310 | *xPtr = *rotPtr++ * (*xPtr); |
| 311 | xPtr++; |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 316 | signalVector *convolve(const signalVector *x, |
| 317 | const signalVector *h, |
| 318 | signalVector *y, |
| 319 | ConvType spanType, int start, |
| 320 | unsigned len, unsigned step, int offset) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 321 | { |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 322 | int rc, head = 0, tail = 0; |
| 323 | bool alloc = false, append = false; |
| 324 | const signalVector *_x = NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 325 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 326 | if (!x || !h) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 327 | return NULL; |
| 328 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 329 | switch (spanType) { |
| 330 | case START_ONLY: |
| 331 | start = 0; |
| 332 | head = h->size(); |
| 333 | len = x->size(); |
| 334 | append = true; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 335 | break; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 336 | case NO_DELAY: |
| 337 | start = h->size() / 2; |
| 338 | head = start; |
| 339 | tail = start; |
| 340 | len = x->size(); |
| 341 | append = true; |
| 342 | break; |
| 343 | case CUSTOM: |
| 344 | if (start < h->size() - 1) { |
| 345 | head = h->size() - start; |
| 346 | append = true; |
| 347 | } |
| 348 | if (start + len > x->size()) { |
| 349 | tail = start + len - x->size(); |
| 350 | append = true; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 351 | } |
| 352 | break; |
| 353 | default: |
| 354 | return NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 355 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 356 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 357 | /* |
| 358 | * Error if the output vector is too small. Create the output vector |
| 359 | * if the pointer is NULL. |
| 360 | */ |
| 361 | if (y && (len > y->size())) |
| 362 | return NULL; |
| 363 | if (!y) { |
| 364 | y = new signalVector(len); |
| 365 | alloc = true; |
| 366 | } |
| 367 | |
| 368 | /* Prepend or post-pend the input vector if the parameters require it */ |
| 369 | if (append) |
| 370 | _x = new signalVector(*x, head, tail); |
| 371 | else |
| 372 | _x = x; |
| 373 | |
| 374 | /* |
| 375 | * Four convovle types: |
| 376 | * 1. Complex-Real (aligned) |
| 377 | * 2. Complex-Complex (aligned) |
| 378 | * 3. Complex-Real (!aligned) |
| 379 | * 4. Complex-Complex (!aligned) |
| 380 | */ |
| 381 | if (h->isRealOnly() && h->isAligned()) { |
| 382 | rc = convolve_real((float *) _x->begin(), _x->size(), |
| 383 | (float *) h->begin(), h->size(), |
| 384 | (float *) y->begin(), y->size(), |
| 385 | start, len, step, offset); |
| 386 | } else if (!h->isRealOnly() && h->isAligned()) { |
| 387 | rc = convolve_complex((float *) _x->begin(), _x->size(), |
| 388 | (float *) h->begin(), h->size(), |
| 389 | (float *) y->begin(), y->size(), |
| 390 | start, len, step, offset); |
| 391 | } else if (h->isRealOnly() && !h->isAligned()) { |
| 392 | rc = base_convolve_real((float *) _x->begin(), _x->size(), |
| 393 | (float *) h->begin(), h->size(), |
| 394 | (float *) y->begin(), y->size(), |
| 395 | start, len, step, offset); |
| 396 | } else if (!h->isRealOnly() && !h->isAligned()) { |
| 397 | rc = base_convolve_complex((float *) _x->begin(), _x->size(), |
| 398 | (float *) h->begin(), h->size(), |
| 399 | (float *) y->begin(), y->size(), |
| 400 | start, len, step, offset); |
| 401 | } else { |
| 402 | rc = -1; |
| 403 | } |
| 404 | |
| 405 | if (append) |
| 406 | delete _x; |
| 407 | |
| 408 | if (rc < 0) { |
| 409 | if (alloc) |
| 410 | delete y; |
| 411 | return NULL; |
| 412 | } |
| 413 | |
| 414 | return y; |
| 415 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 416 | |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 417 | bool generateC1Pulse(int sps) |
| 418 | { |
| 419 | int len; |
| 420 | |
| 421 | switch (sps) { |
| 422 | case 4: |
| 423 | len = 8; |
| 424 | break; |
| 425 | default: |
| 426 | return false; |
| 427 | } |
| 428 | |
| 429 | GSMPulse->c1_buffer = convolve_h_alloc(len); |
| 430 | GSMPulse->c1 = new signalVector((complex *) |
| 431 | GSMPulse->c1_buffer, 0, len); |
| 432 | GSMPulse->c1->isRealOnly(true); |
| 433 | |
| 434 | /* Enable alignment for SSE usage */ |
| 435 | GSMPulse->c1->setAligned(true); |
| 436 | |
| 437 | signalVector::iterator xP = GSMPulse->c1->begin(); |
| 438 | |
| 439 | switch (sps) { |
| 440 | case 4: |
| 441 | /* BT = 0.30 */ |
| 442 | *xP++ = 0.0; |
| 443 | *xP++ = 8.16373112e-03; |
| 444 | *xP++ = 2.84385729e-02; |
| 445 | *xP++ = 5.64158904e-02; |
| 446 | *xP++ = 7.05463553e-02; |
| 447 | *xP++ = 5.64158904e-02; |
| 448 | *xP++ = 2.84385729e-02; |
| 449 | *xP++ = 8.16373112e-03; |
| 450 | } |
| 451 | |
| 452 | return true; |
| 453 | } |
| 454 | |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 455 | void generateGSMPulse(int sps, int symbolLength) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 456 | { |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 457 | int len; |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 458 | float arg, avg, center; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 459 | |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 460 | delete GSMPulse; |
| 461 | |
| 462 | /* Store a single tap filter used for correlation sequence generation */ |
| 463 | GSMPulse = new PulseSequence(); |
| 464 | GSMPulse->empty = new signalVector(1); |
| 465 | GSMPulse->empty->isRealOnly(true); |
| 466 | *(GSMPulse->empty->begin()) = 1.0f; |
| 467 | |
Thomas Tsou | 9ccd9f2 | 2013-08-21 13:59:52 -0400 | [diff] [blame] | 468 | /* |
| 469 | * For 4 samples-per-symbol use a precomputed single pulse Laurent |
| 470 | * approximation. This should yields below 2 degrees of phase error at |
| 471 | * the modulator output. Use the existing pulse approximation for all |
| 472 | * other oversampling factors. |
| 473 | */ |
| 474 | switch (sps) { |
| 475 | case 4: |
| 476 | len = 16; |
| 477 | break; |
| 478 | default: |
| 479 | len = sps * symbolLength; |
| 480 | if (len < 4) |
| 481 | len = 4; |
| 482 | } |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 483 | |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 484 | GSMPulse->c0_buffer = convolve_h_alloc(len); |
| 485 | GSMPulse->c0 = new signalVector((complex *) |
| 486 | GSMPulse->c0_buffer, 0, len); |
| 487 | GSMPulse->c0->isRealOnly(true); |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 488 | |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 489 | /* Enable alingnment for SSE usage */ |
| 490 | GSMPulse->c0->setAligned(true); |
| 491 | |
| 492 | signalVector::iterator xP = GSMPulse->c0->begin(); |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 493 | |
Thomas Tsou | 9ccd9f2 | 2013-08-21 13:59:52 -0400 | [diff] [blame] | 494 | if (sps == 4) { |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 495 | *xP++ = 0.0; |
Thomas Tsou | 9ccd9f2 | 2013-08-21 13:59:52 -0400 | [diff] [blame] | 496 | *xP++ = 4.46348606e-03; |
| 497 | *xP++ = 2.84385729e-02; |
| 498 | *xP++ = 1.03184855e-01; |
| 499 | *xP++ = 2.56065552e-01; |
| 500 | *xP++ = 4.76375085e-01; |
| 501 | *xP++ = 7.05961177e-01; |
| 502 | *xP++ = 8.71291644e-01; |
| 503 | *xP++ = 9.29453645e-01; |
| 504 | *xP++ = 8.71291644e-01; |
| 505 | *xP++ = 7.05961177e-01; |
| 506 | *xP++ = 4.76375085e-01; |
| 507 | *xP++ = 2.56065552e-01; |
| 508 | *xP++ = 1.03184855e-01; |
| 509 | *xP++ = 2.84385729e-02; |
| 510 | *xP++ = 4.46348606e-03; |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 511 | generateC1Pulse(sps); |
Thomas Tsou | 9ccd9f2 | 2013-08-21 13:59:52 -0400 | [diff] [blame] | 512 | } else { |
| 513 | center = (float) (len - 1.0) / 2.0; |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 514 | |
Thomas Tsou | 9ccd9f2 | 2013-08-21 13:59:52 -0400 | [diff] [blame] | 515 | /* GSM pulse approximation */ |
| 516 | for (int i = 0; i < len; i++) { |
| 517 | arg = ((float) i - center) / (float) sps; |
| 518 | *xP++ = 0.96 * exp(-1.1380 * arg * arg - |
| 519 | 0.527 * arg * arg * arg * arg); |
| 520 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 521 | |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 522 | avg = sqrtf(vectorNorm2(*GSMPulse->c0) / sps); |
| 523 | xP = GSMPulse->c0->begin(); |
| 524 | for (int i = 0; i < len; i++) |
| 525 | *xP++ /= avg; |
| 526 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | signalVector* frequencyShift(signalVector *y, |
| 530 | signalVector *x, |
| 531 | float freq, |
| 532 | float startPhase, |
| 533 | float *finalPhase) |
| 534 | { |
| 535 | |
| 536 | if (!x) return NULL; |
| 537 | |
| 538 | if (y==NULL) { |
| 539 | y = new signalVector(x->size()); |
| 540 | y->isRealOnly(x->isRealOnly()); |
| 541 | if (y==NULL) return NULL; |
| 542 | } |
| 543 | |
| 544 | if (y->size() < x->size()) return NULL; |
| 545 | |
| 546 | float phase = startPhase; |
| 547 | signalVector::iterator yP = y->begin(); |
| 548 | signalVector::iterator xPEnd = x->end(); |
| 549 | signalVector::iterator xP = x->begin(); |
| 550 | |
| 551 | if (x->isRealOnly()) { |
| 552 | while (xP < xPEnd) { |
| 553 | (*yP++) = expjLookup(phase)*( (xP++)->real() ); |
| 554 | phase += freq; |
| 555 | } |
| 556 | } |
| 557 | else { |
| 558 | while (xP < xPEnd) { |
| 559 | (*yP++) = (*xP++)*expjLookup(phase); |
| 560 | phase += freq; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | |
| 565 | if (finalPhase) *finalPhase = phase; |
| 566 | |
| 567 | return y; |
| 568 | } |
| 569 | |
| 570 | signalVector* reverseConjugate(signalVector *b) |
| 571 | { |
| 572 | signalVector *tmp = new signalVector(b->size()); |
| 573 | tmp->isRealOnly(b->isRealOnly()); |
| 574 | signalVector::iterator bP = b->begin(); |
| 575 | signalVector::iterator bPEnd = b->end(); |
| 576 | signalVector::iterator tmpP = tmp->end()-1; |
| 577 | if (!b->isRealOnly()) { |
| 578 | while (bP < bPEnd) { |
| 579 | *tmpP-- = bP->conj(); |
| 580 | bP++; |
| 581 | } |
| 582 | } |
| 583 | else { |
| 584 | while (bP < bPEnd) { |
| 585 | *tmpP-- = bP->real(); |
| 586 | bP++; |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | return tmp; |
| 591 | } |
| 592 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 593 | /* soft output slicer */ |
| 594 | bool vectorSlicer(signalVector *x) |
| 595 | { |
| 596 | |
| 597 | signalVector::iterator xP = x->begin(); |
| 598 | signalVector::iterator xPEnd = x->end(); |
| 599 | while (xP < xPEnd) { |
| 600 | *xP = (complex) (0.5*(xP->real()+1.0F)); |
| 601 | if (xP->real() > 1.0) *xP = 1.0; |
| 602 | if (xP->real() < 0.0) *xP = 0.0; |
| 603 | xP++; |
| 604 | } |
| 605 | return true; |
| 606 | } |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 607 | |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 608 | static signalVector *rotateBurst(const BitVector &wBurst, |
| 609 | int guardPeriodLength, int sps) |
| 610 | { |
| 611 | int burst_len; |
| 612 | signalVector *pulse, rotated, *shaped; |
| 613 | signalVector::iterator itr; |
| 614 | |
| 615 | pulse = GSMPulse->empty; |
| 616 | burst_len = sps * (wBurst.size() + guardPeriodLength); |
| 617 | rotated = signalVector(burst_len); |
| 618 | itr = rotated.begin(); |
| 619 | |
| 620 | for (unsigned i = 0; i < wBurst.size(); i++) { |
| 621 | *itr = 2.0 * (wBurst[i] & 0x01) - 1.0; |
| 622 | itr += sps; |
| 623 | } |
| 624 | |
| 625 | GMSKRotate(rotated); |
| 626 | rotated.isRealOnly(false); |
| 627 | |
| 628 | /* Dummy filter operation */ |
| 629 | shaped = convolve(&rotated, pulse, NULL, START_ONLY); |
| 630 | if (!shaped) |
| 631 | return NULL; |
| 632 | |
| 633 | return shaped; |
| 634 | } |
| 635 | |
| 636 | static signalVector *modulateBurstLaurent(const BitVector &bits, |
| 637 | int guard_len, int sps) |
| 638 | { |
| 639 | int burst_len; |
| 640 | float phase; |
| 641 | signalVector *c0_pulse, *c1_pulse, c0_burst, c1_burst, *c0_shaped, *c1_shaped; |
| 642 | signalVector::iterator c0_itr, c1_itr; |
| 643 | |
| 644 | /* |
| 645 | * Apply before and after bits to reduce phase error at burst edges. |
| 646 | * Make sure there is enough room in the burst to accomodate all bits. |
| 647 | */ |
| 648 | if (guard_len < 4) |
| 649 | guard_len = 4; |
| 650 | |
| 651 | c0_pulse = GSMPulse->c0; |
| 652 | c1_pulse = GSMPulse->c1; |
| 653 | |
| 654 | burst_len = sps * (bits.size() + guard_len); |
| 655 | |
| 656 | c0_burst = signalVector(burst_len); |
| 657 | c0_burst.isRealOnly(true); |
| 658 | c0_itr = c0_burst.begin(); |
| 659 | |
| 660 | c1_burst = signalVector(burst_len); |
| 661 | c1_burst.isRealOnly(true); |
| 662 | c1_itr = c1_burst.begin(); |
| 663 | |
| 664 | /* Padded differential start bits */ |
| 665 | *c0_itr = 2.0 * (0x00 & 0x01) - 1.0; |
| 666 | c0_itr += sps; |
| 667 | |
| 668 | /* Main burst bits */ |
| 669 | for (unsigned i = 0; i < bits.size(); i++) { |
| 670 | *c0_itr = 2.0 * (bits[i] & 0x01) - 1.0; |
| 671 | c0_itr += sps; |
| 672 | } |
| 673 | |
| 674 | /* Padded differential end bits */ |
| 675 | *c0_itr = 2.0 * (0x01 & 0x01) - 1.0; |
| 676 | |
| 677 | /* Generate C0 phase coefficients */ |
| 678 | GMSKRotate(c0_burst); |
| 679 | c0_burst.isRealOnly(false); |
| 680 | |
| 681 | c0_itr = c0_burst.begin(); |
| 682 | c0_itr += sps * 2; |
| 683 | c1_itr += sps * 2; |
| 684 | |
| 685 | /* Start magic */ |
| 686 | phase = 2.0 * ((0x01 & 0x01) ^ (0x01 & 0x01)) - 1.0; |
| 687 | *c1_itr = *c0_itr * Complex<float>(0, phase); |
| 688 | c0_itr += sps; |
| 689 | c1_itr += sps; |
| 690 | |
| 691 | /* Generate C1 phase coefficients */ |
| 692 | for (unsigned i = 2; i < bits.size(); i++) { |
| 693 | phase = 2.0 * ((bits[i - 1] & 0x01) ^ (bits[i - 2] & 0x01)) - 1.0; |
| 694 | *c1_itr = *c0_itr * Complex<float>(0, phase); |
| 695 | |
| 696 | c0_itr += sps; |
| 697 | c1_itr += sps; |
| 698 | } |
| 699 | |
| 700 | /* End magic */ |
| 701 | int i = bits.size(); |
| 702 | phase = 2.0 * ((bits[i-1] & 0x01) ^ (bits[i-2] & 0x01)) - 1.0; |
| 703 | *c1_itr = *c0_itr * Complex<float>(0, phase); |
| 704 | |
| 705 | /* Primary (C0) and secondary (C1) pulse shaping */ |
| 706 | c0_shaped = convolve(&c0_burst, c0_pulse, NULL, START_ONLY); |
| 707 | c1_shaped = convolve(&c1_burst, c1_pulse, NULL, START_ONLY); |
| 708 | |
| 709 | /* Sum shaped outputs into C0 */ |
| 710 | c0_itr = c0_shaped->begin(); |
| 711 | c1_itr = c1_shaped->begin(); |
| 712 | for (unsigned i = 0; i < c0_shaped->size(); i++ ) |
| 713 | *c0_itr++ += *c1_itr++; |
| 714 | |
| 715 | delete c1_shaped; |
| 716 | |
| 717 | return c0_shaped; |
| 718 | } |
| 719 | |
| 720 | static signalVector *modulateBurstBasic(const BitVector &bits, |
| 721 | int guard_len, int sps) |
| 722 | { |
| 723 | int burst_len; |
| 724 | signalVector *pulse, burst, *shaped; |
| 725 | signalVector::iterator burst_itr; |
| 726 | |
| 727 | pulse = GSMPulse->c0; |
| 728 | burst_len = sps * (bits.size() + guard_len); |
| 729 | |
| 730 | burst = signalVector(burst_len); |
| 731 | burst.isRealOnly(true); |
| 732 | burst_itr = burst.begin(); |
| 733 | |
| 734 | /* Raw bits are not differentially encoded */ |
| 735 | for (unsigned i = 0; i < bits.size(); i++) { |
| 736 | *burst_itr = 2.0 * (bits[i] & 0x01) - 1.0; |
| 737 | burst_itr += sps; |
| 738 | } |
| 739 | |
| 740 | GMSKRotate(burst); |
| 741 | burst.isRealOnly(false); |
| 742 | |
| 743 | /* Single Gaussian pulse approximation shaping */ |
| 744 | shaped = convolve(&burst, pulse, NULL, START_ONLY); |
| 745 | |
| 746 | return shaped; |
| 747 | } |
| 748 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 749 | /* Assume input bits are not differentially encoded */ |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 750 | signalVector *modulateBurst(const BitVector &wBurst, int guardPeriodLength, |
| 751 | int sps, bool emptyPulse) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 752 | { |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 753 | if (emptyPulse) |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 754 | return rotateBurst(wBurst, guardPeriodLength, sps); |
| 755 | else if (sps == 4) |
| 756 | return modulateBurstLaurent(wBurst, guardPeriodLength, sps); |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 757 | else |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 758 | return modulateBurstBasic(wBurst, guardPeriodLength, sps); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 761 | float sinc(float x) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 762 | { |
| 763 | if ((x >= 0.01F) || (x <= -0.01F)) return (sinLookup(x)/x); |
| 764 | return 1.0F; |
| 765 | } |
| 766 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 767 | bool delayVector(signalVector &wBurst, float delay) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 768 | { |
Thomas Tsou | 2c282f5 | 2013-10-08 21:34:35 -0400 | [diff] [blame^] | 769 | int whole, h_len = 20; |
| 770 | float frac; |
| 771 | complex *data; |
| 772 | signalVector *h, *shift; |
| 773 | signalVector::iterator itr; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 774 | |
Thomas Tsou | 2c282f5 | 2013-10-08 21:34:35 -0400 | [diff] [blame^] | 775 | whole = floor(delay); |
| 776 | frac = delay - whole; |
| 777 | |
| 778 | /* Sinc interpolated fractional shift (if allowable) */ |
| 779 | if (fabs(frac) > 1e-2) { |
| 780 | data = (complex *) convolve_h_alloc(h_len); |
| 781 | h = new signalVector(data, 0, h_len); |
| 782 | h->setAligned(true); |
| 783 | h->isRealOnly(true); |
| 784 | |
| 785 | itr = h->end(); |
| 786 | for (int i = 0; i < h_len; i++) |
| 787 | *--itr = (complex) sinc(M_PI_F * (i - h_len / 2 - frac)); |
| 788 | |
| 789 | shift = convolve(&wBurst, h, NULL, NO_DELAY); |
| 790 | |
| 791 | delete h; |
| 792 | free(data); |
| 793 | |
| 794 | if (!shift) |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 795 | return false; |
Thomas Tsou | 2c282f5 | 2013-10-08 21:34:35 -0400 | [diff] [blame^] | 796 | |
| 797 | wBurst.clone(*shift); |
| 798 | delete shift; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Thomas Tsou | 2c282f5 | 2013-10-08 21:34:35 -0400 | [diff] [blame^] | 801 | /* Integer sample shift */ |
| 802 | if (whole < 0) { |
| 803 | whole = -whole; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 804 | signalVector::iterator wBurstItr = wBurst.begin(); |
Thomas Tsou | 2c282f5 | 2013-10-08 21:34:35 -0400 | [diff] [blame^] | 805 | signalVector::iterator shiftedItr = wBurst.begin() + whole; |
| 806 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 807 | while (shiftedItr < wBurst.end()) |
| 808 | *wBurstItr++ = *shiftedItr++; |
| 809 | while (wBurstItr < wBurst.end()) |
| 810 | *wBurstItr++ = 0.0; |
Thomas Tsou | 2c282f5 | 2013-10-08 21:34:35 -0400 | [diff] [blame^] | 811 | } else { |
| 812 | signalVector::iterator wBurstItr = wBurst.end() - 1; |
| 813 | signalVector::iterator shiftedItr = wBurst.end() - 1 - whole; |
| 814 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 815 | while (shiftedItr >= wBurst.begin()) |
| 816 | *wBurstItr-- = *shiftedItr--; |
| 817 | while (wBurstItr >= wBurst.begin()) |
| 818 | *wBurstItr-- = 0.0; |
| 819 | } |
Thomas Tsou | 2c282f5 | 2013-10-08 21:34:35 -0400 | [diff] [blame^] | 820 | |
| 821 | return true; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 822 | } |
Thomas Tsou | 2c282f5 | 2013-10-08 21:34:35 -0400 | [diff] [blame^] | 823 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 824 | signalVector *gaussianNoise(int length, |
| 825 | float variance, |
| 826 | complex mean) |
| 827 | { |
| 828 | |
| 829 | signalVector *noise = new signalVector(length); |
| 830 | signalVector::iterator nPtr = noise->begin(); |
| 831 | float stddev = sqrtf(variance); |
| 832 | while (nPtr < noise->end()) { |
| 833 | float u1 = (float) rand()/ (float) RAND_MAX; |
| 834 | while (u1==0.0) |
| 835 | u1 = (float) rand()/ (float) RAND_MAX; |
| 836 | float u2 = (float) rand()/ (float) RAND_MAX; |
| 837 | float arg = 2.0*M_PI*u2; |
| 838 | *nPtr = mean + stddev*complex(cos(arg),sin(arg))*sqrtf(-2.0*log(u1)); |
| 839 | nPtr++; |
| 840 | } |
| 841 | |
| 842 | return noise; |
| 843 | } |
| 844 | |
| 845 | complex interpolatePoint(const signalVector &inSig, |
| 846 | float ix) |
| 847 | { |
| 848 | |
| 849 | int start = (int) (floor(ix) - 10); |
| 850 | if (start < 0) start = 0; |
| 851 | int end = (int) (floor(ix) + 11); |
| 852 | if ((unsigned) end > inSig.size()-1) end = inSig.size()-1; |
| 853 | |
| 854 | complex pVal = 0.0; |
| 855 | if (!inSig.isRealOnly()) { |
| 856 | for (int i = start; i < end; i++) |
| 857 | pVal += inSig[i] * sinc(M_PI_F*(i-ix)); |
| 858 | } |
| 859 | else { |
| 860 | for (int i = start; i < end; i++) |
| 861 | pVal += inSig[i].real() * sinc(M_PI_F*(i-ix)); |
| 862 | } |
| 863 | |
| 864 | return pVal; |
| 865 | } |
| 866 | |
Thomas Tsou | 8181b01 | 2013-08-20 21:17:19 -0400 | [diff] [blame] | 867 | static complex fastPeakDetect(const signalVector &rxBurst, float *index) |
| 868 | { |
| 869 | float val, max = 0.0f; |
| 870 | complex amp; |
| 871 | int _index = -1; |
| 872 | |
| 873 | for (int i = 0; i < rxBurst.size(); i++) { |
| 874 | val = rxBurst[i].norm2(); |
| 875 | if (val > max) { |
| 876 | max = val; |
| 877 | _index = i; |
| 878 | amp = rxBurst[i]; |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | if (index) |
| 883 | *index = (float) _index; |
| 884 | |
| 885 | return amp; |
| 886 | } |
| 887 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 888 | complex peakDetect(const signalVector &rxBurst, |
| 889 | float *peakIndex, |
| 890 | float *avgPwr) |
| 891 | { |
| 892 | |
| 893 | |
| 894 | complex maxVal = 0.0; |
| 895 | float maxIndex = -1; |
| 896 | float sumPower = 0.0; |
| 897 | |
| 898 | for (unsigned int i = 0; i < rxBurst.size(); i++) { |
| 899 | float samplePower = rxBurst[i].norm2(); |
| 900 | if (samplePower > maxVal.real()) { |
| 901 | maxVal = samplePower; |
| 902 | maxIndex = i; |
| 903 | } |
| 904 | sumPower += samplePower; |
| 905 | } |
| 906 | |
| 907 | // interpolate around the peak |
| 908 | // to save computation, we'll use early-late balancing |
| 909 | float earlyIndex = maxIndex-1; |
| 910 | float lateIndex = maxIndex+1; |
| 911 | |
| 912 | float incr = 0.5; |
| 913 | while (incr > 1.0/1024.0) { |
| 914 | complex earlyP = interpolatePoint(rxBurst,earlyIndex); |
| 915 | complex lateP = interpolatePoint(rxBurst,lateIndex); |
| 916 | if (earlyP < lateP) |
| 917 | earlyIndex += incr; |
| 918 | else if (earlyP > lateP) |
| 919 | earlyIndex -= incr; |
| 920 | else break; |
| 921 | incr /= 2.0; |
| 922 | lateIndex = earlyIndex + 2.0; |
| 923 | } |
| 924 | |
| 925 | maxIndex = earlyIndex + 1.0; |
| 926 | maxVal = interpolatePoint(rxBurst,maxIndex); |
| 927 | |
| 928 | if (peakIndex!=NULL) |
| 929 | *peakIndex = maxIndex; |
| 930 | |
| 931 | if (avgPwr!=NULL) |
| 932 | *avgPwr = (sumPower-maxVal.norm2()) / (rxBurst.size()-1); |
| 933 | |
| 934 | return maxVal; |
| 935 | |
| 936 | } |
| 937 | |
| 938 | void scaleVector(signalVector &x, |
| 939 | complex scale) |
| 940 | { |
| 941 | signalVector::iterator xP = x.begin(); |
| 942 | signalVector::iterator xPEnd = x.end(); |
| 943 | if (!x.isRealOnly()) { |
| 944 | while (xP < xPEnd) { |
| 945 | *xP = *xP * scale; |
| 946 | xP++; |
| 947 | } |
| 948 | } |
| 949 | else { |
| 950 | while (xP < xPEnd) { |
| 951 | *xP = xP->real() * scale; |
| 952 | xP++; |
| 953 | } |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | /** in-place conjugation */ |
| 958 | void conjugateVector(signalVector &x) |
| 959 | { |
| 960 | if (x.isRealOnly()) return; |
| 961 | signalVector::iterator xP = x.begin(); |
| 962 | signalVector::iterator xPEnd = x.end(); |
| 963 | while (xP < xPEnd) { |
| 964 | *xP = xP->conj(); |
| 965 | xP++; |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | |
| 970 | // in-place addition!! |
| 971 | bool addVector(signalVector &x, |
| 972 | signalVector &y) |
| 973 | { |
| 974 | signalVector::iterator xP = x.begin(); |
| 975 | signalVector::iterator yP = y.begin(); |
| 976 | signalVector::iterator xPEnd = x.end(); |
| 977 | signalVector::iterator yPEnd = y.end(); |
| 978 | while ((xP < xPEnd) && (yP < yPEnd)) { |
| 979 | *xP = *xP + *yP; |
| 980 | xP++; yP++; |
| 981 | } |
| 982 | return true; |
| 983 | } |
| 984 | |
| 985 | // in-place multiplication!! |
| 986 | bool multVector(signalVector &x, |
| 987 | signalVector &y) |
| 988 | { |
| 989 | signalVector::iterator xP = x.begin(); |
| 990 | signalVector::iterator yP = y.begin(); |
| 991 | signalVector::iterator xPEnd = x.end(); |
| 992 | signalVector::iterator yPEnd = y.end(); |
| 993 | while ((xP < xPEnd) && (yP < yPEnd)) { |
| 994 | *xP = (*xP) * (*yP); |
| 995 | xP++; yP++; |
| 996 | } |
| 997 | return true; |
| 998 | } |
| 999 | |
| 1000 | |
| 1001 | void offsetVector(signalVector &x, |
| 1002 | complex offset) |
| 1003 | { |
| 1004 | signalVector::iterator xP = x.begin(); |
| 1005 | signalVector::iterator xPEnd = x.end(); |
| 1006 | if (!x.isRealOnly()) { |
| 1007 | while (xP < xPEnd) { |
| 1008 | *xP += offset; |
| 1009 | xP++; |
| 1010 | } |
| 1011 | } |
| 1012 | else { |
| 1013 | while (xP < xPEnd) { |
| 1014 | *xP = xP->real() + offset; |
| 1015 | xP++; |
| 1016 | } |
| 1017 | } |
| 1018 | } |
| 1019 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1020 | bool generateMidamble(int sps, int tsc) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1021 | { |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1022 | bool status = true; |
| 1023 | complex *data = NULL; |
| 1024 | signalVector *autocorr = NULL, *midamble = NULL; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1025 | signalVector *midMidamble = NULL, *_midMidamble = NULL; |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1026 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1027 | if ((tsc < 0) || (tsc > 7)) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1028 | return false; |
| 1029 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1030 | delete gMidambles[tsc]; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1031 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1032 | /* Use middle 16 bits of each TSC. Correlation sequence is not pulse shaped */ |
| 1033 | midMidamble = modulateBurst(gTrainingSequence[tsc].segment(5,16), 0, sps, true); |
| 1034 | if (!midMidamble) |
| 1035 | return false; |
| 1036 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1037 | /* Simulated receive sequence is pulse shaped */ |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1038 | midamble = modulateBurst(gTrainingSequence[tsc], 0, sps, false); |
| 1039 | if (!midamble) { |
| 1040 | status = false; |
| 1041 | goto release; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1042 | } |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1043 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1044 | // NOTE: Because ideal TSC 16-bit midamble is 66 symbols into burst, |
| 1045 | // the ideal TSC has an + 180 degree phase shift, |
| 1046 | // due to the pi/2 frequency shift, that |
| 1047 | // needs to be accounted for. |
| 1048 | // 26-midamble is 61 symbols into burst, has +90 degree phase shift. |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1049 | scaleVector(*midMidamble, complex(-1.0, 0.0)); |
| 1050 | scaleVector(*midamble, complex(0.0, 1.0)); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1051 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1052 | conjugateVector(*midMidamble); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1053 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1054 | /* For SSE alignment, reallocate the midamble sequence on 16-byte boundary */ |
| 1055 | data = (complex *) convolve_h_alloc(midMidamble->size()); |
| 1056 | _midMidamble = new signalVector(data, 0, midMidamble->size()); |
| 1057 | _midMidamble->setAligned(true); |
| 1058 | memcpy(_midMidamble->begin(), midMidamble->begin(), |
| 1059 | midMidamble->size() * sizeof(complex)); |
| 1060 | |
| 1061 | autocorr = convolve(midamble, _midMidamble, NULL, NO_DELAY); |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1062 | if (!autocorr) { |
| 1063 | status = false; |
| 1064 | goto release; |
| 1065 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1066 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1067 | gMidambles[tsc] = new CorrelationSequence; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1068 | gMidambles[tsc]->buffer = data; |
| 1069 | gMidambles[tsc]->sequence = _midMidamble; |
| 1070 | gMidambles[tsc]->gain = peakDetect(*autocorr,&gMidambles[tsc]->TOA, NULL); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1071 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1072 | release: |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1073 | delete autocorr; |
| 1074 | delete midamble; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1075 | delete midMidamble; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1076 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1077 | if (!status) { |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1078 | delete _midMidamble; |
| 1079 | free(data); |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1080 | gMidambles[tsc] = NULL; |
| 1081 | } |
| 1082 | |
| 1083 | return status; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 1086 | bool generateRACHSequence(int sps) |
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 | bool status = true; |
| 1089 | complex *data = NULL; |
| 1090 | signalVector *autocorr = NULL; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1091 | signalVector *seq0 = NULL, *seq1 = NULL, *_seq1 = NULL; |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1092 | |
| 1093 | delete gRACHSequence; |
| 1094 | |
| 1095 | seq0 = modulateBurst(gRACHSynchSequence, 0, sps, false); |
| 1096 | if (!seq0) |
| 1097 | return false; |
| 1098 | |
| 1099 | seq1 = modulateBurst(gRACHSynchSequence.segment(0, 40), 0, sps, true); |
| 1100 | if (!seq1) { |
| 1101 | status = false; |
| 1102 | goto release; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1105 | conjugateVector(*seq1); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1106 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1107 | /* For SSE alignment, reallocate the midamble sequence on 16-byte boundary */ |
| 1108 | data = (complex *) convolve_h_alloc(seq1->size()); |
| 1109 | _seq1 = new signalVector(data, 0, seq1->size()); |
| 1110 | _seq1->setAligned(true); |
| 1111 | memcpy(_seq1->begin(), seq1->begin(), seq1->size() * sizeof(complex)); |
| 1112 | |
| 1113 | autocorr = convolve(seq0, _seq1, autocorr, NO_DELAY); |
| 1114 | if (!autocorr) { |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1115 | status = false; |
| 1116 | goto release; |
| 1117 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1118 | |
| 1119 | gRACHSequence = new CorrelationSequence; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1120 | gRACHSequence->sequence = _seq1; |
| 1121 | gRACHSequence->buffer = data; |
| 1122 | gRACHSequence->gain = peakDetect(*autocorr,&gRACHSequence->TOA, NULL); |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1123 | |
| 1124 | release: |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1125 | delete autocorr; |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1126 | delete seq0; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1127 | delete seq1; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1128 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1129 | if (!status) { |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1130 | delete _seq1; |
| 1131 | free(data); |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1132 | gRACHSequence = NULL; |
| 1133 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1134 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1135 | return status; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1136 | } |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1137 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1138 | static float computePeakRatio(signalVector *corr, |
| 1139 | int sps, float toa, complex amp) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1140 | { |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1141 | int num = 0; |
| 1142 | complex *peak; |
| 1143 | float rms, avg = 0.0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1144 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1145 | peak = corr->begin() + (int) rint(toa); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1146 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1147 | /* Check for bogus results */ |
| 1148 | if ((toa < 0.0) || (toa > corr->size())) |
| 1149 | return 0.0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1150 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1151 | for (int i = 2 * sps; i <= 5 * sps; i++) { |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1152 | if (peak - i >= corr->begin()) { |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1153 | avg += (peak - i)->norm2(); |
| 1154 | num++; |
| 1155 | } |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1156 | if (peak + i < corr->end()) { |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1157 | avg += (peak + i)->norm2(); |
| 1158 | num++; |
| 1159 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1162 | if (num < 2) |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1163 | return 0.0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1164 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1165 | rms = sqrtf(avg / (float) num) + 0.00001; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1166 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1167 | return (amp.abs()) / rms; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | bool energyDetect(signalVector &rxBurst, |
| 1171 | unsigned windowLength, |
| 1172 | float detectThreshold, |
| 1173 | float *avgPwr) |
| 1174 | { |
| 1175 | |
| 1176 | signalVector::const_iterator windowItr = rxBurst.begin(); //+rxBurst.size()/2 - 5*windowLength/2; |
| 1177 | float energy = 0.0; |
| 1178 | if (windowLength < 0) windowLength = 20; |
| 1179 | if (windowLength > rxBurst.size()) windowLength = rxBurst.size(); |
| 1180 | for (unsigned i = 0; i < windowLength; i++) { |
| 1181 | energy += windowItr->norm2(); |
| 1182 | windowItr+=4; |
| 1183 | } |
| 1184 | if (avgPwr) *avgPwr = energy/windowLength; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1185 | return (energy/windowLength > detectThreshold*detectThreshold); |
| 1186 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1187 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1188 | /* |
| 1189 | * Detect a burst based on correlation and peak-to-average ratio |
| 1190 | * |
| 1191 | * For one sampler-per-symbol, perform fast peak detection (no interpolation) |
| 1192 | * for initial gating. We do this because energy detection should be disabled. |
| 1193 | * For higher oversampling values, we assume the energy detector is in place |
| 1194 | * and we run full interpolating peak detection. |
| 1195 | */ |
| 1196 | static int detectBurst(signalVector &burst, |
| 1197 | signalVector &corr, CorrelationSequence *sync, |
| 1198 | float thresh, int sps, complex *amp, float *toa, |
| 1199 | int start, int len) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1200 | { |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1201 | /* Correlate */ |
| 1202 | if (!convolve(&burst, sync->sequence, &corr, |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1203 | CUSTOM, start, len, sps, 0)) { |
| 1204 | return -1; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1207 | /* Peak detection - place restrictions at correlation edges */ |
| 1208 | *amp = fastPeakDetect(corr, toa); |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1209 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1210 | if ((*toa < 3 * sps) || (*toa > len - 3 * sps)) |
| 1211 | return 0; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1212 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1213 | /* Peak -to-average ratio */ |
| 1214 | if (computePeakRatio(&corr, sps, *toa, *amp) < thresh) |
| 1215 | return 0; |
| 1216 | |
| 1217 | /* Compute peak-to-average ratio. Reject if we don't have enough values */ |
| 1218 | *amp = peakDetect(corr, toa, NULL); |
| 1219 | |
| 1220 | /* Normalize our channel gain */ |
| 1221 | *amp = *amp / sync->gain; |
| 1222 | |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 1223 | /* Compenate for residual rotation with dual Laurent pulse */ |
| 1224 | if (sps == 4) |
| 1225 | *amp = *amp * complex(0.0, 1.0); |
| 1226 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1227 | return 1; |
| 1228 | } |
| 1229 | |
| 1230 | /* |
| 1231 | * RACH burst detection |
| 1232 | * |
| 1233 | * Correlation window parameters: |
| 1234 | * target: Tail bits + RACH length (reduced from 41 to a multiple of 4) |
Thomas Tsou | dafb337 | 2013-09-18 16:21:26 -0400 | [diff] [blame] | 1235 | * head: Search 4 symbols before target |
| 1236 | * tail: Search 10 symbols after target |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1237 | */ |
| 1238 | int detectRACHBurst(signalVector &rxBurst, |
| 1239 | float thresh, |
| 1240 | int sps, |
| 1241 | complex *amp, |
| 1242 | float *toa) |
| 1243 | { |
| 1244 | int rc, start, target, head, tail, len; |
| 1245 | float _toa; |
| 1246 | complex _amp; |
| 1247 | signalVector corr; |
| 1248 | CorrelationSequence *sync; |
| 1249 | |
| 1250 | if ((sps != 1) && (sps != 4)) |
| 1251 | return -1; |
| 1252 | |
| 1253 | target = 8 + 40; |
Thomas Tsou | dafb337 | 2013-09-18 16:21:26 -0400 | [diff] [blame] | 1254 | head = 4; |
| 1255 | tail = 10; |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1256 | |
| 1257 | start = (target - head) * sps - 1; |
| 1258 | len = (head + tail) * sps; |
| 1259 | sync = gRACHSequence; |
| 1260 | corr = signalVector(len); |
| 1261 | |
| 1262 | rc = detectBurst(rxBurst, corr, sync, |
| 1263 | thresh, sps, &_amp, &_toa, start, len); |
| 1264 | if (rc < 0) { |
| 1265 | return -1; |
| 1266 | } else if (!rc) { |
| 1267 | if (amp) |
| 1268 | *amp = 0.0f; |
| 1269 | if (toa) |
| 1270 | *toa = 0.0f; |
| 1271 | return 0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1274 | /* Subtract forward search bits from delay */ |
| 1275 | if (toa) |
| 1276 | *toa = _toa - head * sps; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1277 | if (amp) |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1278 | *amp = _amp; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1279 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1280 | return 1; |
| 1281 | } |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1282 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1283 | /* |
| 1284 | * Normal burst detection |
| 1285 | * |
| 1286 | * Correlation window parameters: |
| 1287 | * target: Tail + data + mid-midamble + 1/2 remaining midamblebits |
Thomas Tsou | dafb337 | 2013-09-18 16:21:26 -0400 | [diff] [blame] | 1288 | * head: Search 4 symbols before target |
| 1289 | * tail: Search 4 symbols + maximum expected delay |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1290 | */ |
| 1291 | int analyzeTrafficBurst(signalVector &rxBurst, unsigned tsc, float thresh, |
| 1292 | int sps, complex *amp, float *toa, unsigned max_toa, |
| 1293 | bool chan_req, signalVector **chan, float *chan_offset) |
| 1294 | { |
| 1295 | int rc, start, target, head, tail, len; |
| 1296 | complex _amp; |
| 1297 | float _toa; |
| 1298 | signalVector corr; |
| 1299 | CorrelationSequence *sync; |
| 1300 | |
| 1301 | if ((tsc < 0) || (tsc > 7) || ((sps != 1) && (sps != 4))) |
| 1302 | return -1; |
| 1303 | |
| 1304 | target = 3 + 58 + 16 + 5; |
Thomas Tsou | dafb337 | 2013-09-18 16:21:26 -0400 | [diff] [blame] | 1305 | head = 4; |
| 1306 | tail = 4 + max_toa; |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1307 | |
| 1308 | start = (target - head) * sps - 1; |
| 1309 | len = (head + tail) * sps; |
| 1310 | sync = gMidambles[tsc]; |
| 1311 | corr = signalVector(len); |
| 1312 | |
| 1313 | rc = detectBurst(rxBurst, corr, sync, |
| 1314 | thresh, sps, &_amp, &_toa, start, len); |
| 1315 | if (rc < 0) { |
| 1316 | return -1; |
| 1317 | } else if (!rc) { |
| 1318 | if (amp) |
| 1319 | *amp = 0.0f; |
| 1320 | if (toa) |
| 1321 | *toa = 0.0f; |
| 1322 | return 0; |
| 1323 | } |
| 1324 | |
| 1325 | /* Subtract forward search bits from delay */ |
| 1326 | _toa -= head * sps; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1327 | if (toa) |
| 1328 | *toa = _toa; |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1329 | if (amp) |
| 1330 | *amp = _amp; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1331 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1332 | /* Equalization not currently supported */ |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1333 | if (chan_req) { |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1334 | *chan = new signalVector(6 * sps); |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1335 | |
| 1336 | if (chan_offset) |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1337 | *chan_offset = 0.0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1340 | return 1; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | signalVector *decimateVector(signalVector &wVector, |
| 1344 | int decimationFactor) |
| 1345 | { |
| 1346 | |
| 1347 | if (decimationFactor <= 1) return NULL; |
| 1348 | |
| 1349 | signalVector *decVector = new signalVector(wVector.size()/decimationFactor); |
| 1350 | decVector->isRealOnly(wVector.isRealOnly()); |
| 1351 | |
| 1352 | signalVector::iterator vecItr = decVector->begin(); |
| 1353 | for (unsigned int i = 0; i < wVector.size();i+=decimationFactor) |
| 1354 | *vecItr++ = wVector[i]; |
| 1355 | |
| 1356 | return decVector; |
| 1357 | } |
| 1358 | |
| 1359 | |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 1360 | SoftVector *demodulateBurst(signalVector &rxBurst, int sps, |
| 1361 | complex channel, float TOA) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1362 | { |
| 1363 | scaleVector(rxBurst,((complex) 1.0)/channel); |
| 1364 | delayVector(rxBurst,-TOA); |
| 1365 | |
| 1366 | signalVector *shapedBurst = &rxBurst; |
| 1367 | |
| 1368 | // shift up by a quarter of a frequency |
| 1369 | // ignore starting phase, since spec allows for discontinuous phase |
| 1370 | GMSKReverseRotate(*shapedBurst); |
| 1371 | |
| 1372 | // run through slicer |
Thomas Tsou | d24cc2c | 2013-08-20 15:41:45 -0400 | [diff] [blame] | 1373 | if (sps > 1) { |
| 1374 | signalVector *decShapedBurst = decimateVector(*shapedBurst, sps); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1375 | shapedBurst = decShapedBurst; |
| 1376 | } |
| 1377 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1378 | vectorSlicer(shapedBurst); |
| 1379 | |
| 1380 | SoftVector *burstBits = new SoftVector(shapedBurst->size()); |
| 1381 | |
| 1382 | SoftVector::iterator burstItr = burstBits->begin(); |
| 1383 | signalVector::iterator shapedItr = shapedBurst->begin(); |
| 1384 | for (; shapedItr < shapedBurst->end(); shapedItr++) |
| 1385 | *burstItr++ = shapedItr->real(); |
| 1386 | |
Thomas Tsou | d24cc2c | 2013-08-20 15:41:45 -0400 | [diff] [blame] | 1387 | if (sps > 1) |
| 1388 | delete shapedBurst; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1389 | |
| 1390 | return burstBits; |
| 1391 | |
| 1392 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1393 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1394 | // Assumes symbol-spaced sampling!!! |
| 1395 | // Based upon paper by Al-Dhahir and Cioffi |
| 1396 | bool designDFE(signalVector &channelResponse, |
| 1397 | float SNRestimate, |
| 1398 | int Nf, |
| 1399 | signalVector **feedForwardFilter, |
| 1400 | signalVector **feedbackFilter) |
| 1401 | { |
| 1402 | |
| 1403 | signalVector G0(Nf); |
| 1404 | signalVector G1(Nf); |
| 1405 | signalVector::iterator G0ptr = G0.begin(); |
| 1406 | signalVector::iterator G1ptr = G1.begin(); |
| 1407 | signalVector::iterator chanPtr = channelResponse.begin(); |
| 1408 | |
| 1409 | int nu = channelResponse.size()-1; |
| 1410 | |
| 1411 | *G0ptr = 1.0/sqrtf(SNRestimate); |
| 1412 | for(int j = 0; j <= nu; j++) { |
| 1413 | *G1ptr = chanPtr->conj(); |
| 1414 | G1ptr++; chanPtr++; |
| 1415 | } |
| 1416 | |
| 1417 | signalVector *L[Nf]; |
| 1418 | signalVector::iterator Lptr; |
| 1419 | float d; |
| 1420 | for(int i = 0; i < Nf; i++) { |
| 1421 | d = G0.begin()->norm2() + G1.begin()->norm2(); |
| 1422 | L[i] = new signalVector(Nf+nu); |
| 1423 | Lptr = L[i]->begin()+i; |
| 1424 | G0ptr = G0.begin(); G1ptr = G1.begin(); |
| 1425 | while ((G0ptr < G0.end()) && (Lptr < L[i]->end())) { |
| 1426 | *Lptr = (*G0ptr*(G0.begin()->conj()) + *G1ptr*(G1.begin()->conj()) )/d; |
| 1427 | Lptr++; |
| 1428 | G0ptr++; |
| 1429 | G1ptr++; |
| 1430 | } |
| 1431 | complex k = (*G1.begin())/(*G0.begin()); |
| 1432 | |
| 1433 | if (i != Nf-1) { |
| 1434 | signalVector G0new = G1; |
| 1435 | scaleVector(G0new,k.conj()); |
| 1436 | addVector(G0new,G0); |
| 1437 | |
| 1438 | signalVector G1new = G0; |
| 1439 | scaleVector(G1new,k*(-1.0)); |
| 1440 | addVector(G1new,G1); |
| 1441 | delayVector(G1new,-1.0); |
| 1442 | |
| 1443 | scaleVector(G0new,1.0/sqrtf(1.0+k.norm2())); |
| 1444 | scaleVector(G1new,1.0/sqrtf(1.0+k.norm2())); |
| 1445 | G0 = G0new; |
| 1446 | G1 = G1new; |
| 1447 | } |
| 1448 | } |
| 1449 | |
| 1450 | *feedbackFilter = new signalVector(nu); |
| 1451 | L[Nf-1]->segmentCopyTo(**feedbackFilter,Nf,nu); |
| 1452 | scaleVector(**feedbackFilter,(complex) -1.0); |
| 1453 | conjugateVector(**feedbackFilter); |
| 1454 | |
| 1455 | signalVector v(Nf); |
| 1456 | signalVector::iterator vStart = v.begin(); |
| 1457 | signalVector::iterator vPtr; |
| 1458 | *(vStart+Nf-1) = (complex) 1.0; |
| 1459 | for(int k = Nf-2; k >= 0; k--) { |
| 1460 | Lptr = L[k]->begin()+k+1; |
| 1461 | vPtr = vStart + k+1; |
| 1462 | complex v_k = 0.0; |
| 1463 | for (int j = k+1; j < Nf; j++) { |
| 1464 | v_k -= (*vPtr)*(*Lptr); |
| 1465 | vPtr++; Lptr++; |
| 1466 | } |
| 1467 | *(vStart + k) = v_k; |
| 1468 | } |
| 1469 | |
| 1470 | *feedForwardFilter = new signalVector(Nf); |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1471 | signalVector::iterator w = (*feedForwardFilter)->end(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1472 | for (int i = 0; i < Nf; i++) { |
| 1473 | delete L[i]; |
| 1474 | complex w_i = 0.0; |
| 1475 | int endPt = ( nu < (Nf-1-i) ) ? nu : (Nf-1-i); |
| 1476 | vPtr = vStart+i; |
| 1477 | chanPtr = channelResponse.begin(); |
| 1478 | for (int k = 0; k < endPt+1; k++) { |
| 1479 | w_i += (*vPtr)*(chanPtr->conj()); |
| 1480 | vPtr++; chanPtr++; |
| 1481 | } |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1482 | *--w = w_i/d; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | |
| 1486 | return true; |
| 1487 | |
| 1488 | } |
| 1489 | |
| 1490 | // Assumes symbol-rate sampling!!!! |
| 1491 | SoftVector *equalizeBurst(signalVector &rxBurst, |
| 1492 | float TOA, |
Thomas Tsou | d24cc2c | 2013-08-20 15:41:45 -0400 | [diff] [blame] | 1493 | int sps, |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1494 | signalVector &w, // feedforward filter |
| 1495 | signalVector &b) // feedback filter |
| 1496 | { |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1497 | signalVector *postForwardFull; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1498 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1499 | if (!delayVector(rxBurst, -TOA)) |
| 1500 | return NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1501 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1502 | postForwardFull = convolve(&rxBurst, &w, NULL, |
| 1503 | CUSTOM, 0, rxBurst.size() + w.size() - 1); |
| 1504 | if (!postForwardFull) |
| 1505 | return NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1506 | |
| 1507 | signalVector* postForward = new signalVector(rxBurst.size()); |
| 1508 | postForwardFull->segmentCopyTo(*postForward,w.size()-1,rxBurst.size()); |
| 1509 | delete postForwardFull; |
| 1510 | |
| 1511 | signalVector::iterator dPtr = postForward->begin(); |
| 1512 | signalVector::iterator dBackPtr; |
| 1513 | signalVector::iterator rotPtr = GMSKRotation->begin(); |
| 1514 | signalVector::iterator revRotPtr = GMSKReverseRotation->begin(); |
| 1515 | |
| 1516 | signalVector *DFEoutput = new signalVector(postForward->size()); |
| 1517 | signalVector::iterator DFEItr = DFEoutput->begin(); |
| 1518 | |
| 1519 | // NOTE: can insert the midamble and/or use midamble to estimate BER |
| 1520 | for (; dPtr < postForward->end(); dPtr++) { |
| 1521 | dBackPtr = dPtr-1; |
| 1522 | signalVector::iterator bPtr = b.begin(); |
| 1523 | while ( (bPtr < b.end()) && (dBackPtr >= postForward->begin()) ) { |
| 1524 | *dPtr = *dPtr + (*bPtr)*(*dBackPtr); |
| 1525 | bPtr++; |
| 1526 | dBackPtr--; |
| 1527 | } |
| 1528 | *dPtr = *dPtr * (*revRotPtr); |
| 1529 | *DFEItr = *dPtr; |
| 1530 | // make decision on symbol |
| 1531 | *dPtr = (dPtr->real() > 0.0) ? 1.0 : -1.0; |
| 1532 | //*DFEItr = *dPtr; |
| 1533 | *dPtr = *dPtr * (*rotPtr); |
| 1534 | DFEItr++; |
| 1535 | rotPtr++; |
| 1536 | revRotPtr++; |
| 1537 | } |
| 1538 | |
| 1539 | vectorSlicer(DFEoutput); |
| 1540 | |
| 1541 | SoftVector *burstBits = new SoftVector(postForward->size()); |
| 1542 | SoftVector::iterator burstItr = burstBits->begin(); |
| 1543 | DFEItr = DFEoutput->begin(); |
| 1544 | for (; DFEItr < DFEoutput->end(); DFEItr++) |
| 1545 | *burstItr++ = DFEItr->real(); |
| 1546 | |
| 1547 | delete postForward; |
| 1548 | |
| 1549 | delete DFEoutput; |
| 1550 | |
| 1551 | return burstBits; |
| 1552 | } |