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 | { |
| 769 | |
| 770 | int intOffset = (int) floor(delay); |
| 771 | float fracOffset = delay - intOffset; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 772 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 773 | // do fractional shift first, only do it for reasonable offsets |
| 774 | if (fabs(fracOffset) > 1e-2) { |
| 775 | // create sinc function |
| 776 | signalVector sincVector(21); |
| 777 | sincVector.isRealOnly(true); |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 778 | signalVector::iterator sincBurstItr = sincVector.end(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 779 | for (int i = 0; i < 21; i++) |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 780 | *--sincBurstItr = (complex) sinc(M_PI_F*(i-10-fracOffset)); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 781 | |
| 782 | signalVector shiftedBurst(wBurst.size()); |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 783 | if (!convolve(&wBurst, &sincVector, &shiftedBurst, NO_DELAY)) |
| 784 | return false; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 785 | wBurst.clone(shiftedBurst); |
| 786 | } |
| 787 | |
| 788 | if (intOffset < 0) { |
| 789 | intOffset = -intOffset; |
| 790 | signalVector::iterator wBurstItr = wBurst.begin(); |
| 791 | signalVector::iterator shiftedItr = wBurst.begin()+intOffset; |
| 792 | while (shiftedItr < wBurst.end()) |
| 793 | *wBurstItr++ = *shiftedItr++; |
| 794 | while (wBurstItr < wBurst.end()) |
| 795 | *wBurstItr++ = 0.0; |
| 796 | } |
| 797 | else { |
| 798 | signalVector::iterator wBurstItr = wBurst.end()-1; |
| 799 | signalVector::iterator shiftedItr = wBurst.end()-1-intOffset; |
| 800 | while (shiftedItr >= wBurst.begin()) |
| 801 | *wBurstItr-- = *shiftedItr--; |
| 802 | while (wBurstItr >= wBurst.begin()) |
| 803 | *wBurstItr-- = 0.0; |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | signalVector *gaussianNoise(int length, |
| 808 | float variance, |
| 809 | complex mean) |
| 810 | { |
| 811 | |
| 812 | signalVector *noise = new signalVector(length); |
| 813 | signalVector::iterator nPtr = noise->begin(); |
| 814 | float stddev = sqrtf(variance); |
| 815 | while (nPtr < noise->end()) { |
| 816 | float u1 = (float) rand()/ (float) RAND_MAX; |
| 817 | while (u1==0.0) |
| 818 | u1 = (float) rand()/ (float) RAND_MAX; |
| 819 | float u2 = (float) rand()/ (float) RAND_MAX; |
| 820 | float arg = 2.0*M_PI*u2; |
| 821 | *nPtr = mean + stddev*complex(cos(arg),sin(arg))*sqrtf(-2.0*log(u1)); |
| 822 | nPtr++; |
| 823 | } |
| 824 | |
| 825 | return noise; |
| 826 | } |
| 827 | |
| 828 | complex interpolatePoint(const signalVector &inSig, |
| 829 | float ix) |
| 830 | { |
| 831 | |
| 832 | int start = (int) (floor(ix) - 10); |
| 833 | if (start < 0) start = 0; |
| 834 | int end = (int) (floor(ix) + 11); |
| 835 | if ((unsigned) end > inSig.size()-1) end = inSig.size()-1; |
| 836 | |
| 837 | complex pVal = 0.0; |
| 838 | if (!inSig.isRealOnly()) { |
| 839 | for (int i = start; i < end; i++) |
| 840 | pVal += inSig[i] * sinc(M_PI_F*(i-ix)); |
| 841 | } |
| 842 | else { |
| 843 | for (int i = start; i < end; i++) |
| 844 | pVal += inSig[i].real() * sinc(M_PI_F*(i-ix)); |
| 845 | } |
| 846 | |
| 847 | return pVal; |
| 848 | } |
| 849 | |
Thomas Tsou | 8181b01 | 2013-08-20 21:17:19 -0400 | [diff] [blame] | 850 | static complex fastPeakDetect(const signalVector &rxBurst, float *index) |
| 851 | { |
| 852 | float val, max = 0.0f; |
| 853 | complex amp; |
| 854 | int _index = -1; |
| 855 | |
| 856 | for (int i = 0; i < rxBurst.size(); i++) { |
| 857 | val = rxBurst[i].norm2(); |
| 858 | if (val > max) { |
| 859 | max = val; |
| 860 | _index = i; |
| 861 | amp = rxBurst[i]; |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | if (index) |
| 866 | *index = (float) _index; |
| 867 | |
| 868 | return amp; |
| 869 | } |
| 870 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 871 | complex peakDetect(const signalVector &rxBurst, |
| 872 | float *peakIndex, |
| 873 | float *avgPwr) |
| 874 | { |
| 875 | |
| 876 | |
| 877 | complex maxVal = 0.0; |
| 878 | float maxIndex = -1; |
| 879 | float sumPower = 0.0; |
| 880 | |
| 881 | for (unsigned int i = 0; i < rxBurst.size(); i++) { |
| 882 | float samplePower = rxBurst[i].norm2(); |
| 883 | if (samplePower > maxVal.real()) { |
| 884 | maxVal = samplePower; |
| 885 | maxIndex = i; |
| 886 | } |
| 887 | sumPower += samplePower; |
| 888 | } |
| 889 | |
| 890 | // interpolate around the peak |
| 891 | // to save computation, we'll use early-late balancing |
| 892 | float earlyIndex = maxIndex-1; |
| 893 | float lateIndex = maxIndex+1; |
| 894 | |
| 895 | float incr = 0.5; |
| 896 | while (incr > 1.0/1024.0) { |
| 897 | complex earlyP = interpolatePoint(rxBurst,earlyIndex); |
| 898 | complex lateP = interpolatePoint(rxBurst,lateIndex); |
| 899 | if (earlyP < lateP) |
| 900 | earlyIndex += incr; |
| 901 | else if (earlyP > lateP) |
| 902 | earlyIndex -= incr; |
| 903 | else break; |
| 904 | incr /= 2.0; |
| 905 | lateIndex = earlyIndex + 2.0; |
| 906 | } |
| 907 | |
| 908 | maxIndex = earlyIndex + 1.0; |
| 909 | maxVal = interpolatePoint(rxBurst,maxIndex); |
| 910 | |
| 911 | if (peakIndex!=NULL) |
| 912 | *peakIndex = maxIndex; |
| 913 | |
| 914 | if (avgPwr!=NULL) |
| 915 | *avgPwr = (sumPower-maxVal.norm2()) / (rxBurst.size()-1); |
| 916 | |
| 917 | return maxVal; |
| 918 | |
| 919 | } |
| 920 | |
| 921 | void scaleVector(signalVector &x, |
| 922 | complex scale) |
| 923 | { |
| 924 | signalVector::iterator xP = x.begin(); |
| 925 | signalVector::iterator xPEnd = x.end(); |
| 926 | if (!x.isRealOnly()) { |
| 927 | while (xP < xPEnd) { |
| 928 | *xP = *xP * scale; |
| 929 | xP++; |
| 930 | } |
| 931 | } |
| 932 | else { |
| 933 | while (xP < xPEnd) { |
| 934 | *xP = xP->real() * scale; |
| 935 | xP++; |
| 936 | } |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | /** in-place conjugation */ |
| 941 | void conjugateVector(signalVector &x) |
| 942 | { |
| 943 | if (x.isRealOnly()) return; |
| 944 | signalVector::iterator xP = x.begin(); |
| 945 | signalVector::iterator xPEnd = x.end(); |
| 946 | while (xP < xPEnd) { |
| 947 | *xP = xP->conj(); |
| 948 | xP++; |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | |
| 953 | // in-place addition!! |
| 954 | bool addVector(signalVector &x, |
| 955 | signalVector &y) |
| 956 | { |
| 957 | signalVector::iterator xP = x.begin(); |
| 958 | signalVector::iterator yP = y.begin(); |
| 959 | signalVector::iterator xPEnd = x.end(); |
| 960 | signalVector::iterator yPEnd = y.end(); |
| 961 | while ((xP < xPEnd) && (yP < yPEnd)) { |
| 962 | *xP = *xP + *yP; |
| 963 | xP++; yP++; |
| 964 | } |
| 965 | return true; |
| 966 | } |
| 967 | |
| 968 | // in-place multiplication!! |
| 969 | bool multVector(signalVector &x, |
| 970 | signalVector &y) |
| 971 | { |
| 972 | signalVector::iterator xP = x.begin(); |
| 973 | signalVector::iterator yP = y.begin(); |
| 974 | signalVector::iterator xPEnd = x.end(); |
| 975 | signalVector::iterator yPEnd = y.end(); |
| 976 | while ((xP < xPEnd) && (yP < yPEnd)) { |
| 977 | *xP = (*xP) * (*yP); |
| 978 | xP++; yP++; |
| 979 | } |
| 980 | return true; |
| 981 | } |
| 982 | |
| 983 | |
| 984 | void offsetVector(signalVector &x, |
| 985 | complex offset) |
| 986 | { |
| 987 | signalVector::iterator xP = x.begin(); |
| 988 | signalVector::iterator xPEnd = x.end(); |
| 989 | if (!x.isRealOnly()) { |
| 990 | while (xP < xPEnd) { |
| 991 | *xP += offset; |
| 992 | xP++; |
| 993 | } |
| 994 | } |
| 995 | else { |
| 996 | while (xP < xPEnd) { |
| 997 | *xP = xP->real() + offset; |
| 998 | xP++; |
| 999 | } |
| 1000 | } |
| 1001 | } |
| 1002 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1003 | bool generateMidamble(int sps, int tsc) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1004 | { |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1005 | bool status = true; |
| 1006 | complex *data = NULL; |
| 1007 | signalVector *autocorr = NULL, *midamble = NULL; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1008 | signalVector *midMidamble = NULL, *_midMidamble = NULL; |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1009 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1010 | if ((tsc < 0) || (tsc > 7)) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1011 | return false; |
| 1012 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1013 | delete gMidambles[tsc]; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1014 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1015 | /* Use middle 16 bits of each TSC. Correlation sequence is not pulse shaped */ |
| 1016 | midMidamble = modulateBurst(gTrainingSequence[tsc].segment(5,16), 0, sps, true); |
| 1017 | if (!midMidamble) |
| 1018 | return false; |
| 1019 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1020 | /* Simulated receive sequence is pulse shaped */ |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1021 | midamble = modulateBurst(gTrainingSequence[tsc], 0, sps, false); |
| 1022 | if (!midamble) { |
| 1023 | status = false; |
| 1024 | goto release; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1025 | } |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1026 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1027 | // NOTE: Because ideal TSC 16-bit midamble is 66 symbols into burst, |
| 1028 | // the ideal TSC has an + 180 degree phase shift, |
| 1029 | // due to the pi/2 frequency shift, that |
| 1030 | // needs to be accounted for. |
| 1031 | // 26-midamble is 61 symbols into burst, has +90 degree phase shift. |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1032 | scaleVector(*midMidamble, complex(-1.0, 0.0)); |
| 1033 | scaleVector(*midamble, complex(0.0, 1.0)); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1034 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1035 | conjugateVector(*midMidamble); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1036 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1037 | /* For SSE alignment, reallocate the midamble sequence on 16-byte boundary */ |
| 1038 | data = (complex *) convolve_h_alloc(midMidamble->size()); |
| 1039 | _midMidamble = new signalVector(data, 0, midMidamble->size()); |
| 1040 | _midMidamble->setAligned(true); |
| 1041 | memcpy(_midMidamble->begin(), midMidamble->begin(), |
| 1042 | midMidamble->size() * sizeof(complex)); |
| 1043 | |
| 1044 | autocorr = convolve(midamble, _midMidamble, NULL, NO_DELAY); |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1045 | if (!autocorr) { |
| 1046 | status = false; |
| 1047 | goto release; |
| 1048 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1049 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1050 | gMidambles[tsc] = new CorrelationSequence; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1051 | gMidambles[tsc]->buffer = data; |
| 1052 | gMidambles[tsc]->sequence = _midMidamble; |
| 1053 | gMidambles[tsc]->gain = peakDetect(*autocorr,&gMidambles[tsc]->TOA, NULL); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1054 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1055 | release: |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1056 | delete autocorr; |
| 1057 | delete midamble; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1058 | delete midMidamble; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1059 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1060 | if (!status) { |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1061 | delete _midMidamble; |
| 1062 | free(data); |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1063 | gMidambles[tsc] = NULL; |
| 1064 | } |
| 1065 | |
| 1066 | return status; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 1069 | bool generateRACHSequence(int sps) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1070 | { |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1071 | bool status = true; |
| 1072 | complex *data = NULL; |
| 1073 | signalVector *autocorr = NULL; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1074 | signalVector *seq0 = NULL, *seq1 = NULL, *_seq1 = NULL; |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1075 | |
| 1076 | delete gRACHSequence; |
| 1077 | |
| 1078 | seq0 = modulateBurst(gRACHSynchSequence, 0, sps, false); |
| 1079 | if (!seq0) |
| 1080 | return false; |
| 1081 | |
| 1082 | seq1 = modulateBurst(gRACHSynchSequence.segment(0, 40), 0, sps, true); |
| 1083 | if (!seq1) { |
| 1084 | status = false; |
| 1085 | goto release; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1088 | conjugateVector(*seq1); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1089 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1090 | /* For SSE alignment, reallocate the midamble sequence on 16-byte boundary */ |
| 1091 | data = (complex *) convolve_h_alloc(seq1->size()); |
| 1092 | _seq1 = new signalVector(data, 0, seq1->size()); |
| 1093 | _seq1->setAligned(true); |
| 1094 | memcpy(_seq1->begin(), seq1->begin(), seq1->size() * sizeof(complex)); |
| 1095 | |
| 1096 | autocorr = convolve(seq0, _seq1, autocorr, NO_DELAY); |
| 1097 | if (!autocorr) { |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1098 | status = false; |
| 1099 | goto release; |
| 1100 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1101 | |
| 1102 | gRACHSequence = new CorrelationSequence; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1103 | gRACHSequence->sequence = _seq1; |
| 1104 | gRACHSequence->buffer = data; |
| 1105 | gRACHSequence->gain = peakDetect(*autocorr,&gRACHSequence->TOA, NULL); |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1106 | |
| 1107 | release: |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1108 | delete autocorr; |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1109 | delete seq0; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1110 | delete seq1; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1111 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1112 | if (!status) { |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1113 | delete _seq1; |
| 1114 | free(data); |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1115 | gRACHSequence = NULL; |
| 1116 | } |
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 | return status; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1119 | } |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1120 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1121 | static float computePeakRatio(signalVector *corr, |
| 1122 | int sps, float toa, complex amp) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1123 | { |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1124 | int num = 0; |
| 1125 | complex *peak; |
| 1126 | float rms, avg = 0.0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1127 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1128 | peak = corr->begin() + (int) rint(toa); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1129 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1130 | /* Check for bogus results */ |
| 1131 | if ((toa < 0.0) || (toa > corr->size())) |
| 1132 | return 0.0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1133 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1134 | for (int i = 2 * sps; i <= 5 * sps; i++) { |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1135 | if (peak - i >= corr->begin()) { |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1136 | avg += (peak - i)->norm2(); |
| 1137 | num++; |
| 1138 | } |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1139 | if (peak + i < corr->end()) { |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1140 | avg += (peak + i)->norm2(); |
| 1141 | num++; |
| 1142 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1145 | if (num < 2) |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1146 | return 0.0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1147 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1148 | rms = sqrtf(avg / (float) num) + 0.00001; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1149 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1150 | return (amp.abs()) / rms; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | bool energyDetect(signalVector &rxBurst, |
| 1154 | unsigned windowLength, |
| 1155 | float detectThreshold, |
| 1156 | float *avgPwr) |
| 1157 | { |
| 1158 | |
| 1159 | signalVector::const_iterator windowItr = rxBurst.begin(); //+rxBurst.size()/2 - 5*windowLength/2; |
| 1160 | float energy = 0.0; |
| 1161 | if (windowLength < 0) windowLength = 20; |
| 1162 | if (windowLength > rxBurst.size()) windowLength = rxBurst.size(); |
| 1163 | for (unsigned i = 0; i < windowLength; i++) { |
| 1164 | energy += windowItr->norm2(); |
| 1165 | windowItr+=4; |
| 1166 | } |
| 1167 | if (avgPwr) *avgPwr = energy/windowLength; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1168 | return (energy/windowLength > detectThreshold*detectThreshold); |
| 1169 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1170 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1171 | /* |
| 1172 | * Detect a burst based on correlation and peak-to-average ratio |
| 1173 | * |
| 1174 | * For one sampler-per-symbol, perform fast peak detection (no interpolation) |
| 1175 | * for initial gating. We do this because energy detection should be disabled. |
| 1176 | * For higher oversampling values, we assume the energy detector is in place |
| 1177 | * and we run full interpolating peak detection. |
| 1178 | */ |
| 1179 | static int detectBurst(signalVector &burst, |
| 1180 | signalVector &corr, CorrelationSequence *sync, |
| 1181 | float thresh, int sps, complex *amp, float *toa, |
| 1182 | int start, int len) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1183 | { |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1184 | /* Correlate */ |
| 1185 | if (!convolve(&burst, sync->sequence, &corr, |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1186 | CUSTOM, start, len, sps, 0)) { |
| 1187 | return -1; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1190 | /* Peak detection - place restrictions at correlation edges */ |
| 1191 | *amp = fastPeakDetect(corr, toa); |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1192 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1193 | if ((*toa < 3 * sps) || (*toa > len - 3 * sps)) |
| 1194 | return 0; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1195 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1196 | /* Peak -to-average ratio */ |
| 1197 | if (computePeakRatio(&corr, sps, *toa, *amp) < thresh) |
| 1198 | return 0; |
| 1199 | |
| 1200 | /* Compute peak-to-average ratio. Reject if we don't have enough values */ |
| 1201 | *amp = peakDetect(corr, toa, NULL); |
| 1202 | |
| 1203 | /* Normalize our channel gain */ |
| 1204 | *amp = *amp / sync->gain; |
| 1205 | |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame^] | 1206 | /* Compenate for residual rotation with dual Laurent pulse */ |
| 1207 | if (sps == 4) |
| 1208 | *amp = *amp * complex(0.0, 1.0); |
| 1209 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1210 | return 1; |
| 1211 | } |
| 1212 | |
| 1213 | /* |
| 1214 | * RACH burst detection |
| 1215 | * |
| 1216 | * Correlation window parameters: |
| 1217 | * target: Tail bits + RACH length (reduced from 41 to a multiple of 4) |
| 1218 | * head: Search 8 symbols before target |
| 1219 | * tail: Search 8 symbols after target |
| 1220 | */ |
| 1221 | int detectRACHBurst(signalVector &rxBurst, |
| 1222 | float thresh, |
| 1223 | int sps, |
| 1224 | complex *amp, |
| 1225 | float *toa) |
| 1226 | { |
| 1227 | int rc, start, target, head, tail, len; |
| 1228 | float _toa; |
| 1229 | complex _amp; |
| 1230 | signalVector corr; |
| 1231 | CorrelationSequence *sync; |
| 1232 | |
| 1233 | if ((sps != 1) && (sps != 4)) |
| 1234 | return -1; |
| 1235 | |
| 1236 | target = 8 + 40; |
| 1237 | head = 8; |
| 1238 | tail = head; |
| 1239 | |
| 1240 | start = (target - head) * sps - 1; |
| 1241 | len = (head + tail) * sps; |
| 1242 | sync = gRACHSequence; |
| 1243 | corr = signalVector(len); |
| 1244 | |
| 1245 | rc = detectBurst(rxBurst, corr, sync, |
| 1246 | thresh, sps, &_amp, &_toa, start, len); |
| 1247 | if (rc < 0) { |
| 1248 | return -1; |
| 1249 | } else if (!rc) { |
| 1250 | if (amp) |
| 1251 | *amp = 0.0f; |
| 1252 | if (toa) |
| 1253 | *toa = 0.0f; |
| 1254 | return 0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1257 | /* Subtract forward search bits from delay */ |
| 1258 | if (toa) |
| 1259 | *toa = _toa - head * sps; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1260 | if (amp) |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1261 | *amp = _amp; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1262 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1263 | return 1; |
| 1264 | } |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1265 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1266 | /* |
| 1267 | * Normal burst detection |
| 1268 | * |
| 1269 | * Correlation window parameters: |
| 1270 | * target: Tail + data + mid-midamble + 1/2 remaining midamblebits |
| 1271 | * head: Search 8 symbols before target |
| 1272 | * tail: Search 8 symbols + maximum expected delay |
| 1273 | */ |
| 1274 | int analyzeTrafficBurst(signalVector &rxBurst, unsigned tsc, float thresh, |
| 1275 | int sps, complex *amp, float *toa, unsigned max_toa, |
| 1276 | bool chan_req, signalVector **chan, float *chan_offset) |
| 1277 | { |
| 1278 | int rc, start, target, head, tail, len; |
| 1279 | complex _amp; |
| 1280 | float _toa; |
| 1281 | signalVector corr; |
| 1282 | CorrelationSequence *sync; |
| 1283 | |
| 1284 | if ((tsc < 0) || (tsc > 7) || ((sps != 1) && (sps != 4))) |
| 1285 | return -1; |
| 1286 | |
| 1287 | target = 3 + 58 + 16 + 5; |
| 1288 | head = 8; |
| 1289 | tail = head + max_toa; |
| 1290 | |
| 1291 | start = (target - head) * sps - 1; |
| 1292 | len = (head + tail) * sps; |
| 1293 | sync = gMidambles[tsc]; |
| 1294 | corr = signalVector(len); |
| 1295 | |
| 1296 | rc = detectBurst(rxBurst, corr, sync, |
| 1297 | thresh, sps, &_amp, &_toa, start, len); |
| 1298 | if (rc < 0) { |
| 1299 | return -1; |
| 1300 | } else if (!rc) { |
| 1301 | if (amp) |
| 1302 | *amp = 0.0f; |
| 1303 | if (toa) |
| 1304 | *toa = 0.0f; |
| 1305 | return 0; |
| 1306 | } |
| 1307 | |
| 1308 | /* Subtract forward search bits from delay */ |
| 1309 | _toa -= head * sps; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1310 | if (toa) |
| 1311 | *toa = _toa; |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1312 | if (amp) |
| 1313 | *amp = _amp; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1314 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1315 | /* Equalization not currently supported */ |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1316 | if (chan_req) { |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1317 | *chan = new signalVector(6 * sps); |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1318 | |
| 1319 | if (chan_offset) |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1320 | *chan_offset = 0.0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1323 | return 1; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | signalVector *decimateVector(signalVector &wVector, |
| 1327 | int decimationFactor) |
| 1328 | { |
| 1329 | |
| 1330 | if (decimationFactor <= 1) return NULL; |
| 1331 | |
| 1332 | signalVector *decVector = new signalVector(wVector.size()/decimationFactor); |
| 1333 | decVector->isRealOnly(wVector.isRealOnly()); |
| 1334 | |
| 1335 | signalVector::iterator vecItr = decVector->begin(); |
| 1336 | for (unsigned int i = 0; i < wVector.size();i+=decimationFactor) |
| 1337 | *vecItr++ = wVector[i]; |
| 1338 | |
| 1339 | return decVector; |
| 1340 | } |
| 1341 | |
| 1342 | |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 1343 | SoftVector *demodulateBurst(signalVector &rxBurst, int sps, |
| 1344 | complex channel, float TOA) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1345 | { |
| 1346 | scaleVector(rxBurst,((complex) 1.0)/channel); |
| 1347 | delayVector(rxBurst,-TOA); |
| 1348 | |
| 1349 | signalVector *shapedBurst = &rxBurst; |
| 1350 | |
| 1351 | // shift up by a quarter of a frequency |
| 1352 | // ignore starting phase, since spec allows for discontinuous phase |
| 1353 | GMSKReverseRotate(*shapedBurst); |
| 1354 | |
| 1355 | // run through slicer |
Thomas Tsou | d24cc2c | 2013-08-20 15:41:45 -0400 | [diff] [blame] | 1356 | if (sps > 1) { |
| 1357 | signalVector *decShapedBurst = decimateVector(*shapedBurst, sps); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1358 | shapedBurst = decShapedBurst; |
| 1359 | } |
| 1360 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1361 | vectorSlicer(shapedBurst); |
| 1362 | |
| 1363 | SoftVector *burstBits = new SoftVector(shapedBurst->size()); |
| 1364 | |
| 1365 | SoftVector::iterator burstItr = burstBits->begin(); |
| 1366 | signalVector::iterator shapedItr = shapedBurst->begin(); |
| 1367 | for (; shapedItr < shapedBurst->end(); shapedItr++) |
| 1368 | *burstItr++ = shapedItr->real(); |
| 1369 | |
Thomas Tsou | d24cc2c | 2013-08-20 15:41:45 -0400 | [diff] [blame] | 1370 | if (sps > 1) |
| 1371 | delete shapedBurst; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1372 | |
| 1373 | return burstBits; |
| 1374 | |
| 1375 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1376 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1377 | // Assumes symbol-spaced sampling!!! |
| 1378 | // Based upon paper by Al-Dhahir and Cioffi |
| 1379 | bool designDFE(signalVector &channelResponse, |
| 1380 | float SNRestimate, |
| 1381 | int Nf, |
| 1382 | signalVector **feedForwardFilter, |
| 1383 | signalVector **feedbackFilter) |
| 1384 | { |
| 1385 | |
| 1386 | signalVector G0(Nf); |
| 1387 | signalVector G1(Nf); |
| 1388 | signalVector::iterator G0ptr = G0.begin(); |
| 1389 | signalVector::iterator G1ptr = G1.begin(); |
| 1390 | signalVector::iterator chanPtr = channelResponse.begin(); |
| 1391 | |
| 1392 | int nu = channelResponse.size()-1; |
| 1393 | |
| 1394 | *G0ptr = 1.0/sqrtf(SNRestimate); |
| 1395 | for(int j = 0; j <= nu; j++) { |
| 1396 | *G1ptr = chanPtr->conj(); |
| 1397 | G1ptr++; chanPtr++; |
| 1398 | } |
| 1399 | |
| 1400 | signalVector *L[Nf]; |
| 1401 | signalVector::iterator Lptr; |
| 1402 | float d; |
| 1403 | for(int i = 0; i < Nf; i++) { |
| 1404 | d = G0.begin()->norm2() + G1.begin()->norm2(); |
| 1405 | L[i] = new signalVector(Nf+nu); |
| 1406 | Lptr = L[i]->begin()+i; |
| 1407 | G0ptr = G0.begin(); G1ptr = G1.begin(); |
| 1408 | while ((G0ptr < G0.end()) && (Lptr < L[i]->end())) { |
| 1409 | *Lptr = (*G0ptr*(G0.begin()->conj()) + *G1ptr*(G1.begin()->conj()) )/d; |
| 1410 | Lptr++; |
| 1411 | G0ptr++; |
| 1412 | G1ptr++; |
| 1413 | } |
| 1414 | complex k = (*G1.begin())/(*G0.begin()); |
| 1415 | |
| 1416 | if (i != Nf-1) { |
| 1417 | signalVector G0new = G1; |
| 1418 | scaleVector(G0new,k.conj()); |
| 1419 | addVector(G0new,G0); |
| 1420 | |
| 1421 | signalVector G1new = G0; |
| 1422 | scaleVector(G1new,k*(-1.0)); |
| 1423 | addVector(G1new,G1); |
| 1424 | delayVector(G1new,-1.0); |
| 1425 | |
| 1426 | scaleVector(G0new,1.0/sqrtf(1.0+k.norm2())); |
| 1427 | scaleVector(G1new,1.0/sqrtf(1.0+k.norm2())); |
| 1428 | G0 = G0new; |
| 1429 | G1 = G1new; |
| 1430 | } |
| 1431 | } |
| 1432 | |
| 1433 | *feedbackFilter = new signalVector(nu); |
| 1434 | L[Nf-1]->segmentCopyTo(**feedbackFilter,Nf,nu); |
| 1435 | scaleVector(**feedbackFilter,(complex) -1.0); |
| 1436 | conjugateVector(**feedbackFilter); |
| 1437 | |
| 1438 | signalVector v(Nf); |
| 1439 | signalVector::iterator vStart = v.begin(); |
| 1440 | signalVector::iterator vPtr; |
| 1441 | *(vStart+Nf-1) = (complex) 1.0; |
| 1442 | for(int k = Nf-2; k >= 0; k--) { |
| 1443 | Lptr = L[k]->begin()+k+1; |
| 1444 | vPtr = vStart + k+1; |
| 1445 | complex v_k = 0.0; |
| 1446 | for (int j = k+1; j < Nf; j++) { |
| 1447 | v_k -= (*vPtr)*(*Lptr); |
| 1448 | vPtr++; Lptr++; |
| 1449 | } |
| 1450 | *(vStart + k) = v_k; |
| 1451 | } |
| 1452 | |
| 1453 | *feedForwardFilter = new signalVector(Nf); |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1454 | signalVector::iterator w = (*feedForwardFilter)->end(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1455 | for (int i = 0; i < Nf; i++) { |
| 1456 | delete L[i]; |
| 1457 | complex w_i = 0.0; |
| 1458 | int endPt = ( nu < (Nf-1-i) ) ? nu : (Nf-1-i); |
| 1459 | vPtr = vStart+i; |
| 1460 | chanPtr = channelResponse.begin(); |
| 1461 | for (int k = 0; k < endPt+1; k++) { |
| 1462 | w_i += (*vPtr)*(chanPtr->conj()); |
| 1463 | vPtr++; chanPtr++; |
| 1464 | } |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1465 | *--w = w_i/d; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | |
| 1469 | return true; |
| 1470 | |
| 1471 | } |
| 1472 | |
| 1473 | // Assumes symbol-rate sampling!!!! |
| 1474 | SoftVector *equalizeBurst(signalVector &rxBurst, |
| 1475 | float TOA, |
Thomas Tsou | d24cc2c | 2013-08-20 15:41:45 -0400 | [diff] [blame] | 1476 | int sps, |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1477 | signalVector &w, // feedforward filter |
| 1478 | signalVector &b) // feedback filter |
| 1479 | { |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1480 | signalVector *postForwardFull; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1481 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1482 | if (!delayVector(rxBurst, -TOA)) |
| 1483 | return NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1484 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1485 | postForwardFull = convolve(&rxBurst, &w, NULL, |
| 1486 | CUSTOM, 0, rxBurst.size() + w.size() - 1); |
| 1487 | if (!postForwardFull) |
| 1488 | return NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1489 | |
| 1490 | signalVector* postForward = new signalVector(rxBurst.size()); |
| 1491 | postForwardFull->segmentCopyTo(*postForward,w.size()-1,rxBurst.size()); |
| 1492 | delete postForwardFull; |
| 1493 | |
| 1494 | signalVector::iterator dPtr = postForward->begin(); |
| 1495 | signalVector::iterator dBackPtr; |
| 1496 | signalVector::iterator rotPtr = GMSKRotation->begin(); |
| 1497 | signalVector::iterator revRotPtr = GMSKReverseRotation->begin(); |
| 1498 | |
| 1499 | signalVector *DFEoutput = new signalVector(postForward->size()); |
| 1500 | signalVector::iterator DFEItr = DFEoutput->begin(); |
| 1501 | |
| 1502 | // NOTE: can insert the midamble and/or use midamble to estimate BER |
| 1503 | for (; dPtr < postForward->end(); dPtr++) { |
| 1504 | dBackPtr = dPtr-1; |
| 1505 | signalVector::iterator bPtr = b.begin(); |
| 1506 | while ( (bPtr < b.end()) && (dBackPtr >= postForward->begin()) ) { |
| 1507 | *dPtr = *dPtr + (*bPtr)*(*dBackPtr); |
| 1508 | bPtr++; |
| 1509 | dBackPtr--; |
| 1510 | } |
| 1511 | *dPtr = *dPtr * (*revRotPtr); |
| 1512 | *DFEItr = *dPtr; |
| 1513 | // make decision on symbol |
| 1514 | *dPtr = (dPtr->real() > 0.0) ? 1.0 : -1.0; |
| 1515 | //*DFEItr = *dPtr; |
| 1516 | *dPtr = *dPtr * (*rotPtr); |
| 1517 | DFEItr++; |
| 1518 | rotPtr++; |
| 1519 | revRotPtr++; |
| 1520 | } |
| 1521 | |
| 1522 | vectorSlicer(DFEoutput); |
| 1523 | |
| 1524 | SoftVector *burstBits = new SoftVector(postForward->size()); |
| 1525 | SoftVector::iterator burstItr = burstBits->begin(); |
| 1526 | DFEItr = DFEoutput->begin(); |
| 1527 | for (; DFEItr < DFEoutput->end(); DFEItr++) |
| 1528 | *burstItr++ = DFEItr->real(); |
| 1529 | |
| 1530 | delete postForward; |
| 1531 | |
| 1532 | delete DFEoutput; |
| 1533 | |
| 1534 | return burstBits; |
| 1535 | } |