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