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; |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | |
| 620 | if (finalPhase) *finalPhase = phase; |
| 621 | |
| 622 | return y; |
| 623 | } |
| 624 | |
| 625 | signalVector* reverseConjugate(signalVector *b) |
| 626 | { |
| 627 | signalVector *tmp = new signalVector(b->size()); |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 628 | tmp->isReal(b->isReal()); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 629 | signalVector::iterator bP = b->begin(); |
| 630 | signalVector::iterator bPEnd = b->end(); |
| 631 | signalVector::iterator tmpP = tmp->end()-1; |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 632 | if (!b->isReal()) { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 633 | while (bP < bPEnd) { |
| 634 | *tmpP-- = bP->conj(); |
| 635 | bP++; |
| 636 | } |
| 637 | } |
| 638 | else { |
| 639 | while (bP < bPEnd) { |
| 640 | *tmpP-- = bP->real(); |
| 641 | bP++; |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | return tmp; |
| 646 | } |
| 647 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 648 | /* soft output slicer */ |
| 649 | bool vectorSlicer(signalVector *x) |
| 650 | { |
| 651 | |
| 652 | signalVector::iterator xP = x->begin(); |
| 653 | signalVector::iterator xPEnd = x->end(); |
| 654 | while (xP < xPEnd) { |
| 655 | *xP = (complex) (0.5*(xP->real()+1.0F)); |
| 656 | if (xP->real() > 1.0) *xP = 1.0; |
| 657 | if (xP->real() < 0.0) *xP = 0.0; |
| 658 | xP++; |
| 659 | } |
| 660 | return true; |
| 661 | } |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 662 | |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 663 | static signalVector *rotateBurst(const BitVector &wBurst, |
| 664 | int guardPeriodLength, int sps) |
| 665 | { |
| 666 | int burst_len; |
| 667 | signalVector *pulse, rotated, *shaped; |
| 668 | signalVector::iterator itr; |
| 669 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 670 | pulse = GSMPulse1->empty; |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 671 | burst_len = sps * (wBurst.size() + guardPeriodLength); |
| 672 | rotated = signalVector(burst_len); |
| 673 | itr = rotated.begin(); |
| 674 | |
| 675 | for (unsigned i = 0; i < wBurst.size(); i++) { |
| 676 | *itr = 2.0 * (wBurst[i] & 0x01) - 1.0; |
| 677 | itr += sps; |
| 678 | } |
| 679 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 680 | GMSKRotate(rotated, sps); |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 681 | rotated.isReal(false); |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 682 | |
| 683 | /* Dummy filter operation */ |
| 684 | shaped = convolve(&rotated, pulse, NULL, START_ONLY); |
| 685 | if (!shaped) |
| 686 | return NULL; |
| 687 | |
| 688 | return shaped; |
| 689 | } |
| 690 | |
| 691 | static signalVector *modulateBurstLaurent(const BitVector &bits, |
| 692 | int guard_len, int sps) |
| 693 | { |
| 694 | int burst_len; |
| 695 | float phase; |
Thomas Tsou | 6f4906e | 2013-11-09 02:40:18 -0500 | [diff] [blame] | 696 | signalVector *c0_pulse, *c1_pulse, *c0_burst; |
| 697 | signalVector *c1_burst, *c0_shaped, *c1_shaped; |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 698 | signalVector::iterator c0_itr, c1_itr; |
| 699 | |
| 700 | /* |
| 701 | * Apply before and after bits to reduce phase error at burst edges. |
| 702 | * Make sure there is enough room in the burst to accomodate all bits. |
| 703 | */ |
| 704 | if (guard_len < 4) |
| 705 | guard_len = 4; |
| 706 | |
| 707 | c0_pulse = GSMPulse->c0; |
| 708 | c1_pulse = GSMPulse->c1; |
| 709 | |
| 710 | burst_len = sps * (bits.size() + guard_len); |
| 711 | |
Thomas Tsou | 6f4906e | 2013-11-09 02:40:18 -0500 | [diff] [blame] | 712 | c0_burst = new signalVector(burst_len, c0_pulse->size()); |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 713 | c0_burst->isReal(true); |
Thomas Tsou | 6f4906e | 2013-11-09 02:40:18 -0500 | [diff] [blame] | 714 | c0_itr = c0_burst->begin(); |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 715 | |
Thomas Tsou | 6f4906e | 2013-11-09 02:40:18 -0500 | [diff] [blame] | 716 | c1_burst = new signalVector(burst_len, c1_pulse->size()); |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 717 | c1_burst->isReal(true); |
Thomas Tsou | 6f4906e | 2013-11-09 02:40:18 -0500 | [diff] [blame] | 718 | c1_itr = c1_burst->begin(); |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 719 | |
| 720 | /* Padded differential start bits */ |
| 721 | *c0_itr = 2.0 * (0x00 & 0x01) - 1.0; |
| 722 | c0_itr += sps; |
| 723 | |
| 724 | /* Main burst bits */ |
| 725 | for (unsigned i = 0; i < bits.size(); i++) { |
| 726 | *c0_itr = 2.0 * (bits[i] & 0x01) - 1.0; |
| 727 | c0_itr += sps; |
| 728 | } |
| 729 | |
| 730 | /* Padded differential end bits */ |
| 731 | *c0_itr = 2.0 * (0x01 & 0x01) - 1.0; |
| 732 | |
| 733 | /* Generate C0 phase coefficients */ |
Thomas Tsou | 6f4906e | 2013-11-09 02:40:18 -0500 | [diff] [blame] | 734 | GMSKRotate(*c0_burst, sps); |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 735 | c0_burst->isReal(false); |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 736 | |
Thomas Tsou | 6f4906e | 2013-11-09 02:40:18 -0500 | [diff] [blame] | 737 | c0_itr = c0_burst->begin(); |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 738 | c0_itr += sps * 2; |
| 739 | c1_itr += sps * 2; |
| 740 | |
| 741 | /* Start magic */ |
| 742 | phase = 2.0 * ((0x01 & 0x01) ^ (0x01 & 0x01)) - 1.0; |
| 743 | *c1_itr = *c0_itr * Complex<float>(0, phase); |
| 744 | c0_itr += sps; |
| 745 | c1_itr += sps; |
| 746 | |
| 747 | /* Generate C1 phase coefficients */ |
| 748 | for (unsigned i = 2; i < bits.size(); i++) { |
| 749 | phase = 2.0 * ((bits[i - 1] & 0x01) ^ (bits[i - 2] & 0x01)) - 1.0; |
| 750 | *c1_itr = *c0_itr * Complex<float>(0, phase); |
| 751 | |
| 752 | c0_itr += sps; |
| 753 | c1_itr += sps; |
| 754 | } |
| 755 | |
| 756 | /* End magic */ |
| 757 | int i = bits.size(); |
| 758 | phase = 2.0 * ((bits[i-1] & 0x01) ^ (bits[i-2] & 0x01)) - 1.0; |
| 759 | *c1_itr = *c0_itr * Complex<float>(0, phase); |
| 760 | |
| 761 | /* Primary (C0) and secondary (C1) pulse shaping */ |
Thomas Tsou | 6f4906e | 2013-11-09 02:40:18 -0500 | [diff] [blame] | 762 | c0_shaped = convolve(c0_burst, c0_pulse, NULL, START_ONLY); |
| 763 | c1_shaped = convolve(c1_burst, c1_pulse, NULL, START_ONLY); |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 764 | |
| 765 | /* Sum shaped outputs into C0 */ |
| 766 | c0_itr = c0_shaped->begin(); |
| 767 | c1_itr = c1_shaped->begin(); |
| 768 | for (unsigned i = 0; i < c0_shaped->size(); i++ ) |
| 769 | *c0_itr++ += *c1_itr++; |
| 770 | |
Thomas Tsou | 6f4906e | 2013-11-09 02:40:18 -0500 | [diff] [blame] | 771 | delete c0_burst; |
| 772 | delete c1_burst; |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 773 | delete c1_shaped; |
| 774 | |
| 775 | return c0_shaped; |
| 776 | } |
| 777 | |
| 778 | static signalVector *modulateBurstBasic(const BitVector &bits, |
| 779 | int guard_len, int sps) |
| 780 | { |
| 781 | int burst_len; |
Thomas Tsou | 6f4906e | 2013-11-09 02:40:18 -0500 | [diff] [blame] | 782 | signalVector *pulse, *burst, *shaped; |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 783 | signalVector::iterator burst_itr; |
| 784 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 785 | if (sps == 1) |
| 786 | pulse = GSMPulse1->c0; |
| 787 | else |
| 788 | pulse = GSMPulse->c0; |
| 789 | |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 790 | burst_len = sps * (bits.size() + guard_len); |
| 791 | |
Thomas Tsou | 6f4906e | 2013-11-09 02:40:18 -0500 | [diff] [blame] | 792 | burst = new signalVector(burst_len, pulse->size()); |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 793 | burst->isReal(true); |
Thomas Tsou | 6f4906e | 2013-11-09 02:40:18 -0500 | [diff] [blame] | 794 | burst_itr = burst->begin(); |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 795 | |
| 796 | /* Raw bits are not differentially encoded */ |
| 797 | for (unsigned i = 0; i < bits.size(); i++) { |
| 798 | *burst_itr = 2.0 * (bits[i] & 0x01) - 1.0; |
| 799 | burst_itr += sps; |
| 800 | } |
| 801 | |
Thomas Tsou | 6f4906e | 2013-11-09 02:40:18 -0500 | [diff] [blame] | 802 | GMSKRotate(*burst, sps); |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 803 | burst->isReal(false); |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 804 | |
| 805 | /* Single Gaussian pulse approximation shaping */ |
Thomas Tsou | 6f4906e | 2013-11-09 02:40:18 -0500 | [diff] [blame] | 806 | shaped = convolve(burst, pulse, NULL, START_ONLY); |
| 807 | |
| 808 | delete burst; |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 809 | |
| 810 | return shaped; |
| 811 | } |
| 812 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 813 | /* Assume input bits are not differentially encoded */ |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 814 | signalVector *modulateBurst(const BitVector &wBurst, int guardPeriodLength, |
| 815 | int sps, bool emptyPulse) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 816 | { |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 817 | if (emptyPulse) |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 818 | return rotateBurst(wBurst, guardPeriodLength, sps); |
| 819 | else if (sps == 4) |
| 820 | return modulateBurstLaurent(wBurst, guardPeriodLength, sps); |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 821 | else |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 822 | return modulateBurstBasic(wBurst, guardPeriodLength, sps); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 823 | } |
| 824 | |
Thomas Tsou | 0e0e1f4 | 2013-11-09 22:08:51 -0500 | [diff] [blame] | 825 | void generateSincTable() |
| 826 | { |
| 827 | float x; |
| 828 | |
| 829 | for (int i = 0; i < TABLESIZE; i++) { |
| 830 | x = (float) i / TABLESIZE * 8 * M_PI; |
| 831 | if (fabs(x) < 0.01) { |
| 832 | sincTable[i] = 1.0f; |
| 833 | continue; |
| 834 | } |
| 835 | |
| 836 | sincTable[i] = sinf(x) / x; |
| 837 | } |
| 838 | } |
| 839 | |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 840 | float sinc(float x) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 841 | { |
Thomas Tsou | 0e0e1f4 | 2013-11-09 22:08:51 -0500 | [diff] [blame] | 842 | if (fabs(x) >= 8 * M_PI) |
| 843 | return 0.0; |
| 844 | |
| 845 | int index = (int) floorf(fabs(x) / (8 * M_PI) * TABLESIZE); |
| 846 | |
| 847 | return sincTable[index]; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Thomas Tsou | f79c4d0 | 2013-11-09 15:51:56 -0600 | [diff] [blame] | 850 | /* |
| 851 | * Create fractional delay filterbank with Blackman-harris windowed |
| 852 | * sinc function generator. The number of filters generated is specified |
| 853 | * by the DELAYFILTS value. |
| 854 | */ |
| 855 | void generateDelayFilters() |
| 856 | { |
| 857 | int h_len = 20; |
| 858 | complex *data; |
| 859 | signalVector *h; |
| 860 | signalVector::iterator itr; |
| 861 | |
| 862 | float k, sum; |
| 863 | float a0 = 0.35875; |
| 864 | float a1 = 0.48829; |
| 865 | float a2 = 0.14128; |
| 866 | float a3 = 0.01168; |
| 867 | |
| 868 | for (int i = 0; i < DELAYFILTS; i++) { |
| 869 | data = (complex *) convolve_h_alloc(h_len); |
| 870 | h = new signalVector(data, 0, h_len); |
| 871 | h->setAligned(true); |
| 872 | h->isReal(true); |
| 873 | |
| 874 | sum = 0.0; |
| 875 | itr = h->end(); |
| 876 | for (int n = 0; n < h_len; n++) { |
| 877 | k = (float) n; |
| 878 | *--itr = (complex) sinc(M_PI_F * |
| 879 | (k - (float) h_len / 2.0 - (float) i / DELAYFILTS)); |
| 880 | *itr *= a0 - |
| 881 | a1 * cos(2 * M_PI * n / (h_len - 1)) + |
| 882 | a2 * cos(4 * M_PI * n / (h_len - 1)) - |
| 883 | a3 * cos(6 * M_PI * n / (h_len - 1)); |
| 884 | |
| 885 | sum += itr->real(); |
| 886 | } |
| 887 | |
| 888 | itr = h->begin(); |
| 889 | for (int n = 0; n < h_len; n++) |
| 890 | *itr++ /= sum; |
| 891 | |
| 892 | delayFilters[i] = h; |
| 893 | } |
| 894 | } |
| 895 | |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 896 | signalVector *delayVector(signalVector *in, signalVector *out, float delay) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 897 | { |
Thomas Tsou | f79c4d0 | 2013-11-09 15:51:56 -0600 | [diff] [blame] | 898 | int whole, index; |
Thomas Tsou | 2c282f5 | 2013-10-08 21:34:35 -0400 | [diff] [blame] | 899 | float frac; |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 900 | signalVector *h, *shift, *fshift = NULL; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 901 | |
Thomas Tsou | 2c282f5 | 2013-10-08 21:34:35 -0400 | [diff] [blame] | 902 | whole = floor(delay); |
| 903 | frac = delay - whole; |
| 904 | |
| 905 | /* Sinc interpolated fractional shift (if allowable) */ |
| 906 | if (fabs(frac) > 1e-2) { |
Thomas Tsou | f79c4d0 | 2013-11-09 15:51:56 -0600 | [diff] [blame] | 907 | index = floorf(frac * (float) DELAYFILTS); |
| 908 | h = delayFilters[index]; |
Thomas Tsou | 2c282f5 | 2013-10-08 21:34:35 -0400 | [diff] [blame] | 909 | |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 910 | fshift = convolve(in, h, NULL, NO_DELAY); |
| 911 | if (!fshift) |
| 912 | return NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 913 | } |
| 914 | |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 915 | if (!fshift) |
| 916 | shift = new signalVector(*in); |
| 917 | else |
| 918 | shift = fshift; |
| 919 | |
Thomas Tsou | 2c282f5 | 2013-10-08 21:34:35 -0400 | [diff] [blame] | 920 | /* Integer sample shift */ |
| 921 | if (whole < 0) { |
| 922 | whole = -whole; |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 923 | signalVector::iterator wBurstItr = shift->begin(); |
| 924 | signalVector::iterator shiftedItr = shift->begin() + whole; |
Thomas Tsou | 2c282f5 | 2013-10-08 21:34:35 -0400 | [diff] [blame] | 925 | |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 926 | while (shiftedItr < shift->end()) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 927 | *wBurstItr++ = *shiftedItr++; |
Thomas Tsou | 2c282f5 | 2013-10-08 21:34:35 -0400 | [diff] [blame] | 928 | |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 929 | while (wBurstItr < shift->end()) |
| 930 | *wBurstItr++ = 0.0; |
| 931 | } else if (whole >= 0) { |
| 932 | signalVector::iterator wBurstItr = shift->end() - 1; |
| 933 | signalVector::iterator shiftedItr = shift->end() - 1 - whole; |
| 934 | |
| 935 | while (shiftedItr >= shift->begin()) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 936 | *wBurstItr-- = *shiftedItr--; |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 937 | |
| 938 | while (wBurstItr >= shift->begin()) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 939 | *wBurstItr-- = 0.0; |
| 940 | } |
Thomas Tsou | 2c282f5 | 2013-10-08 21:34:35 -0400 | [diff] [blame] | 941 | |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 942 | if (!out) |
| 943 | return shift; |
| 944 | |
| 945 | out->clone(*shift); |
| 946 | delete shift; |
| 947 | return out; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 948 | } |
Thomas Tsou | 2c282f5 | 2013-10-08 21:34:35 -0400 | [diff] [blame] | 949 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 950 | signalVector *gaussianNoise(int length, |
| 951 | float variance, |
| 952 | complex mean) |
| 953 | { |
| 954 | |
| 955 | signalVector *noise = new signalVector(length); |
| 956 | signalVector::iterator nPtr = noise->begin(); |
| 957 | float stddev = sqrtf(variance); |
| 958 | while (nPtr < noise->end()) { |
| 959 | float u1 = (float) rand()/ (float) RAND_MAX; |
| 960 | while (u1==0.0) |
| 961 | u1 = (float) rand()/ (float) RAND_MAX; |
| 962 | float u2 = (float) rand()/ (float) RAND_MAX; |
| 963 | float arg = 2.0*M_PI*u2; |
| 964 | *nPtr = mean + stddev*complex(cos(arg),sin(arg))*sqrtf(-2.0*log(u1)); |
| 965 | nPtr++; |
| 966 | } |
| 967 | |
| 968 | return noise; |
| 969 | } |
| 970 | |
| 971 | complex interpolatePoint(const signalVector &inSig, |
| 972 | float ix) |
| 973 | { |
| 974 | |
| 975 | int start = (int) (floor(ix) - 10); |
| 976 | if (start < 0) start = 0; |
| 977 | int end = (int) (floor(ix) + 11); |
| 978 | if ((unsigned) end > inSig.size()-1) end = inSig.size()-1; |
| 979 | |
| 980 | complex pVal = 0.0; |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 981 | if (!inSig.isReal()) { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 982 | for (int i = start; i < end; i++) |
| 983 | pVal += inSig[i] * sinc(M_PI_F*(i-ix)); |
| 984 | } |
| 985 | else { |
| 986 | for (int i = start; i < end; i++) |
| 987 | pVal += inSig[i].real() * sinc(M_PI_F*(i-ix)); |
| 988 | } |
| 989 | |
| 990 | return pVal; |
| 991 | } |
| 992 | |
Thomas Tsou | 8181b01 | 2013-08-20 21:17:19 -0400 | [diff] [blame] | 993 | static complex fastPeakDetect(const signalVector &rxBurst, float *index) |
| 994 | { |
| 995 | float val, max = 0.0f; |
| 996 | complex amp; |
| 997 | int _index = -1; |
| 998 | |
| 999 | for (int i = 0; i < rxBurst.size(); i++) { |
| 1000 | val = rxBurst[i].norm2(); |
| 1001 | if (val > max) { |
| 1002 | max = val; |
| 1003 | _index = i; |
| 1004 | amp = rxBurst[i]; |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | if (index) |
| 1009 | *index = (float) _index; |
| 1010 | |
| 1011 | return amp; |
| 1012 | } |
| 1013 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1014 | complex peakDetect(const signalVector &rxBurst, |
| 1015 | float *peakIndex, |
| 1016 | float *avgPwr) |
| 1017 | { |
| 1018 | |
| 1019 | |
| 1020 | complex maxVal = 0.0; |
| 1021 | float maxIndex = -1; |
| 1022 | float sumPower = 0.0; |
| 1023 | |
| 1024 | for (unsigned int i = 0; i < rxBurst.size(); i++) { |
| 1025 | float samplePower = rxBurst[i].norm2(); |
| 1026 | if (samplePower > maxVal.real()) { |
| 1027 | maxVal = samplePower; |
| 1028 | maxIndex = i; |
| 1029 | } |
| 1030 | sumPower += samplePower; |
| 1031 | } |
| 1032 | |
| 1033 | // interpolate around the peak |
| 1034 | // to save computation, we'll use early-late balancing |
| 1035 | float earlyIndex = maxIndex-1; |
| 1036 | float lateIndex = maxIndex+1; |
| 1037 | |
| 1038 | float incr = 0.5; |
| 1039 | while (incr > 1.0/1024.0) { |
| 1040 | complex earlyP = interpolatePoint(rxBurst,earlyIndex); |
| 1041 | complex lateP = interpolatePoint(rxBurst,lateIndex); |
| 1042 | if (earlyP < lateP) |
| 1043 | earlyIndex += incr; |
| 1044 | else if (earlyP > lateP) |
| 1045 | earlyIndex -= incr; |
| 1046 | else break; |
| 1047 | incr /= 2.0; |
| 1048 | lateIndex = earlyIndex + 2.0; |
| 1049 | } |
| 1050 | |
| 1051 | maxIndex = earlyIndex + 1.0; |
| 1052 | maxVal = interpolatePoint(rxBurst,maxIndex); |
| 1053 | |
| 1054 | if (peakIndex!=NULL) |
| 1055 | *peakIndex = maxIndex; |
| 1056 | |
| 1057 | if (avgPwr!=NULL) |
| 1058 | *avgPwr = (sumPower-maxVal.norm2()) / (rxBurst.size()-1); |
| 1059 | |
| 1060 | return maxVal; |
| 1061 | |
| 1062 | } |
| 1063 | |
| 1064 | void scaleVector(signalVector &x, |
| 1065 | complex scale) |
| 1066 | { |
Thomas Tsou | 7e4e536 | 2013-10-30 21:18:55 -0400 | [diff] [blame] | 1067 | #ifdef HAVE_NEON |
| 1068 | int len = x.size(); |
| 1069 | |
| 1070 | scale_complex((float *) x.begin(), |
| 1071 | (float *) x.begin(), |
| 1072 | (float *) &scale, len); |
| 1073 | #else |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1074 | signalVector::iterator xP = x.begin(); |
| 1075 | signalVector::iterator xPEnd = x.end(); |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 1076 | if (!x.isReal()) { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1077 | while (xP < xPEnd) { |
| 1078 | *xP = *xP * scale; |
| 1079 | xP++; |
| 1080 | } |
| 1081 | } |
| 1082 | else { |
| 1083 | while (xP < xPEnd) { |
| 1084 | *xP = xP->real() * scale; |
| 1085 | xP++; |
| 1086 | } |
| 1087 | } |
Thomas Tsou | 7e4e536 | 2013-10-30 21:18:55 -0400 | [diff] [blame] | 1088 | #endif |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | /** in-place conjugation */ |
| 1092 | void conjugateVector(signalVector &x) |
| 1093 | { |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 1094 | if (x.isReal()) return; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1095 | signalVector::iterator xP = x.begin(); |
| 1096 | signalVector::iterator xPEnd = x.end(); |
| 1097 | while (xP < xPEnd) { |
| 1098 | *xP = xP->conj(); |
| 1099 | xP++; |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | |
| 1104 | // in-place addition!! |
| 1105 | bool addVector(signalVector &x, |
| 1106 | signalVector &y) |
| 1107 | { |
| 1108 | signalVector::iterator xP = x.begin(); |
| 1109 | signalVector::iterator yP = y.begin(); |
| 1110 | signalVector::iterator xPEnd = x.end(); |
| 1111 | signalVector::iterator yPEnd = y.end(); |
| 1112 | while ((xP < xPEnd) && (yP < yPEnd)) { |
| 1113 | *xP = *xP + *yP; |
| 1114 | xP++; yP++; |
| 1115 | } |
| 1116 | return true; |
| 1117 | } |
| 1118 | |
| 1119 | // in-place multiplication!! |
| 1120 | bool multVector(signalVector &x, |
| 1121 | signalVector &y) |
| 1122 | { |
| 1123 | signalVector::iterator xP = x.begin(); |
| 1124 | signalVector::iterator yP = y.begin(); |
| 1125 | signalVector::iterator xPEnd = x.end(); |
| 1126 | signalVector::iterator yPEnd = y.end(); |
| 1127 | while ((xP < xPEnd) && (yP < yPEnd)) { |
| 1128 | *xP = (*xP) * (*yP); |
| 1129 | xP++; yP++; |
| 1130 | } |
| 1131 | return true; |
| 1132 | } |
| 1133 | |
| 1134 | |
| 1135 | void offsetVector(signalVector &x, |
| 1136 | complex offset) |
| 1137 | { |
| 1138 | signalVector::iterator xP = x.begin(); |
| 1139 | signalVector::iterator xPEnd = x.end(); |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 1140 | if (!x.isReal()) { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1141 | while (xP < xPEnd) { |
| 1142 | *xP += offset; |
| 1143 | xP++; |
| 1144 | } |
| 1145 | } |
| 1146 | else { |
| 1147 | while (xP < xPEnd) { |
| 1148 | *xP = xP->real() + offset; |
| 1149 | xP++; |
| 1150 | } |
| 1151 | } |
| 1152 | } |
| 1153 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1154 | bool generateMidamble(int sps, int tsc) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1155 | { |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1156 | bool status = true; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 1157 | float toa; |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1158 | complex *data = NULL; |
| 1159 | signalVector *autocorr = NULL, *midamble = NULL; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1160 | signalVector *midMidamble = NULL, *_midMidamble = NULL; |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1161 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1162 | if ((tsc < 0) || (tsc > 7)) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1163 | return false; |
| 1164 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1165 | delete gMidambles[tsc]; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1166 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1167 | /* Use middle 16 bits of each TSC. Correlation sequence is not pulse shaped */ |
| 1168 | midMidamble = modulateBurst(gTrainingSequence[tsc].segment(5,16), 0, sps, true); |
| 1169 | if (!midMidamble) |
| 1170 | return false; |
| 1171 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1172 | /* Simulated receive sequence is pulse shaped */ |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1173 | midamble = modulateBurst(gTrainingSequence[tsc], 0, sps, false); |
| 1174 | if (!midamble) { |
| 1175 | status = false; |
| 1176 | goto release; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1177 | } |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1178 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1179 | // NOTE: Because ideal TSC 16-bit midamble is 66 symbols into burst, |
| 1180 | // the ideal TSC has an + 180 degree phase shift, |
| 1181 | // due to the pi/2 frequency shift, that |
| 1182 | // needs to be accounted for. |
| 1183 | // 26-midamble is 61 symbols into burst, has +90 degree phase shift. |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1184 | scaleVector(*midMidamble, complex(-1.0, 0.0)); |
| 1185 | scaleVector(*midamble, complex(0.0, 1.0)); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1186 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1187 | conjugateVector(*midMidamble); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1188 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1189 | /* For SSE alignment, reallocate the midamble sequence on 16-byte boundary */ |
| 1190 | data = (complex *) convolve_h_alloc(midMidamble->size()); |
| 1191 | _midMidamble = new signalVector(data, 0, midMidamble->size()); |
| 1192 | _midMidamble->setAligned(true); |
| 1193 | memcpy(_midMidamble->begin(), midMidamble->begin(), |
| 1194 | midMidamble->size() * sizeof(complex)); |
| 1195 | |
| 1196 | autocorr = convolve(midamble, _midMidamble, NULL, NO_DELAY); |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1197 | if (!autocorr) { |
| 1198 | status = false; |
| 1199 | goto release; |
| 1200 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1201 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1202 | gMidambles[tsc] = new CorrelationSequence; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1203 | gMidambles[tsc]->buffer = data; |
| 1204 | gMidambles[tsc]->sequence = _midMidamble; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 1205 | gMidambles[tsc]->gain = peakDetect(*autocorr, &toa, NULL); |
| 1206 | |
| 1207 | /* For 1 sps only |
| 1208 | * (Half of correlation length - 1) + midpoint of pulse shape + remainder |
| 1209 | * 13.5 = (16 / 2 - 1) + 1.5 + (26 - 10) / 2 |
| 1210 | */ |
| 1211 | if (sps == 1) |
| 1212 | gMidambles[tsc]->toa = toa - 13.5; |
| 1213 | else |
| 1214 | gMidambles[tsc]->toa = 0; |
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 | release: |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1217 | delete autocorr; |
| 1218 | delete midamble; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1219 | delete midMidamble; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1220 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1221 | if (!status) { |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1222 | delete _midMidamble; |
| 1223 | free(data); |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1224 | gMidambles[tsc] = NULL; |
| 1225 | } |
| 1226 | |
| 1227 | return status; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 1230 | bool generateRACHSequence(int sps) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1231 | { |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1232 | bool status = true; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 1233 | float toa; |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1234 | complex *data = NULL; |
| 1235 | signalVector *autocorr = NULL; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1236 | signalVector *seq0 = NULL, *seq1 = NULL, *_seq1 = NULL; |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1237 | |
| 1238 | delete gRACHSequence; |
| 1239 | |
| 1240 | seq0 = modulateBurst(gRACHSynchSequence, 0, sps, false); |
| 1241 | if (!seq0) |
| 1242 | return false; |
| 1243 | |
| 1244 | seq1 = modulateBurst(gRACHSynchSequence.segment(0, 40), 0, sps, true); |
| 1245 | if (!seq1) { |
| 1246 | status = false; |
| 1247 | goto release; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1250 | conjugateVector(*seq1); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1251 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1252 | /* For SSE alignment, reallocate the midamble sequence on 16-byte boundary */ |
| 1253 | data = (complex *) convolve_h_alloc(seq1->size()); |
| 1254 | _seq1 = new signalVector(data, 0, seq1->size()); |
| 1255 | _seq1->setAligned(true); |
| 1256 | memcpy(_seq1->begin(), seq1->begin(), seq1->size() * sizeof(complex)); |
| 1257 | |
| 1258 | autocorr = convolve(seq0, _seq1, autocorr, NO_DELAY); |
| 1259 | if (!autocorr) { |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1260 | status = false; |
| 1261 | goto release; |
| 1262 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1263 | |
| 1264 | gRACHSequence = new CorrelationSequence; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1265 | gRACHSequence->sequence = _seq1; |
| 1266 | gRACHSequence->buffer = data; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 1267 | gRACHSequence->gain = peakDetect(*autocorr, &toa, NULL); |
| 1268 | |
| 1269 | /* For 1 sps only |
| 1270 | * (Half of correlation length - 1) + midpoint of pulse shaping filer |
| 1271 | * 20.5 = (40 / 2 - 1) + 1.5 |
| 1272 | */ |
| 1273 | if (sps == 1) |
| 1274 | gRACHSequence->toa = toa - 20.5; |
| 1275 | else |
| 1276 | gRACHSequence->toa = 0.0; |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1277 | |
| 1278 | release: |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1279 | delete autocorr; |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1280 | delete seq0; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1281 | delete seq1; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1282 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1283 | if (!status) { |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1284 | delete _seq1; |
| 1285 | free(data); |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1286 | gRACHSequence = NULL; |
| 1287 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1288 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1289 | return status; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1290 | } |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1291 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1292 | static float computePeakRatio(signalVector *corr, |
| 1293 | int sps, float toa, complex amp) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1294 | { |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1295 | int num = 0; |
| 1296 | complex *peak; |
| 1297 | float rms, avg = 0.0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1298 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1299 | peak = corr->begin() + (int) rint(toa); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1300 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1301 | /* Check for bogus results */ |
| 1302 | if ((toa < 0.0) || (toa > corr->size())) |
| 1303 | return 0.0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1304 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1305 | for (int i = 2 * sps; i <= 5 * sps; i++) { |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1306 | if (peak - i >= corr->begin()) { |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1307 | avg += (peak - i)->norm2(); |
| 1308 | num++; |
| 1309 | } |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1310 | if (peak + i < corr->end()) { |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1311 | avg += (peak + i)->norm2(); |
| 1312 | num++; |
| 1313 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1316 | if (num < 2) |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1317 | return 0.0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1318 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1319 | rms = sqrtf(avg / (float) num) + 0.00001; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1320 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1321 | return (amp.abs()) / rms; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | bool energyDetect(signalVector &rxBurst, |
| 1325 | unsigned windowLength, |
| 1326 | float detectThreshold, |
| 1327 | float *avgPwr) |
| 1328 | { |
| 1329 | |
| 1330 | signalVector::const_iterator windowItr = rxBurst.begin(); //+rxBurst.size()/2 - 5*windowLength/2; |
| 1331 | float energy = 0.0; |
| 1332 | if (windowLength < 0) windowLength = 20; |
| 1333 | if (windowLength > rxBurst.size()) windowLength = rxBurst.size(); |
| 1334 | for (unsigned i = 0; i < windowLength; i++) { |
| 1335 | energy += windowItr->norm2(); |
| 1336 | windowItr+=4; |
| 1337 | } |
| 1338 | if (avgPwr) *avgPwr = energy/windowLength; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1339 | return (energy/windowLength > detectThreshold*detectThreshold); |
| 1340 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1341 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1342 | /* |
| 1343 | * Detect a burst based on correlation and peak-to-average ratio |
| 1344 | * |
| 1345 | * For one sampler-per-symbol, perform fast peak detection (no interpolation) |
| 1346 | * for initial gating. We do this because energy detection should be disabled. |
| 1347 | * For higher oversampling values, we assume the energy detector is in place |
| 1348 | * and we run full interpolating peak detection. |
| 1349 | */ |
| 1350 | static int detectBurst(signalVector &burst, |
| 1351 | signalVector &corr, CorrelationSequence *sync, |
| 1352 | float thresh, int sps, complex *amp, float *toa, |
| 1353 | int start, int len) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1354 | { |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1355 | /* Correlate */ |
| 1356 | if (!convolve(&burst, sync->sequence, &corr, |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1357 | CUSTOM, start, len, sps, 0)) { |
| 1358 | return -1; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1361 | /* Peak detection - place restrictions at correlation edges */ |
| 1362 | *amp = fastPeakDetect(corr, toa); |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1363 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1364 | if ((*toa < 3 * sps) || (*toa > len - 3 * sps)) |
| 1365 | return 0; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1366 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1367 | /* Peak -to-average ratio */ |
| 1368 | if (computePeakRatio(&corr, sps, *toa, *amp) < thresh) |
| 1369 | return 0; |
| 1370 | |
| 1371 | /* Compute peak-to-average ratio. Reject if we don't have enough values */ |
| 1372 | *amp = peakDetect(corr, toa, NULL); |
| 1373 | |
| 1374 | /* Normalize our channel gain */ |
| 1375 | *amp = *amp / sync->gain; |
| 1376 | |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 1377 | /* Compenate for residual rotation with dual Laurent pulse */ |
| 1378 | if (sps == 4) |
| 1379 | *amp = *amp * complex(0.0, 1.0); |
| 1380 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 1381 | /* Compensate for residuate time lag */ |
| 1382 | *toa = *toa - sync->toa; |
| 1383 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1384 | return 1; |
| 1385 | } |
| 1386 | |
| 1387 | /* |
| 1388 | * RACH burst detection |
| 1389 | * |
| 1390 | * Correlation window parameters: |
| 1391 | * 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] | 1392 | * head: Search 4 symbols before target |
| 1393 | * tail: Search 10 symbols after target |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1394 | */ |
| 1395 | int detectRACHBurst(signalVector &rxBurst, |
| 1396 | float thresh, |
| 1397 | int sps, |
| 1398 | complex *amp, |
| 1399 | float *toa) |
| 1400 | { |
| 1401 | int rc, start, target, head, tail, len; |
| 1402 | float _toa; |
| 1403 | complex _amp; |
Thomas Tsou | b075dd2 | 2013-11-09 22:25:46 -0500 | [diff] [blame^] | 1404 | signalVector *corr; |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1405 | CorrelationSequence *sync; |
| 1406 | |
| 1407 | if ((sps != 1) && (sps != 4)) |
| 1408 | return -1; |
| 1409 | |
| 1410 | target = 8 + 40; |
Thomas Tsou | dafb337 | 2013-09-18 16:21:26 -0400 | [diff] [blame] | 1411 | head = 4; |
| 1412 | tail = 10; |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1413 | |
| 1414 | start = (target - head) * sps - 1; |
| 1415 | len = (head + tail) * sps; |
| 1416 | sync = gRACHSequence; |
Thomas Tsou | b075dd2 | 2013-11-09 22:25:46 -0500 | [diff] [blame^] | 1417 | corr = new signalVector(len); |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1418 | |
Thomas Tsou | b075dd2 | 2013-11-09 22:25:46 -0500 | [diff] [blame^] | 1419 | rc = detectBurst(rxBurst, *corr, sync, |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1420 | thresh, sps, &_amp, &_toa, start, len); |
Thomas Tsou | b075dd2 | 2013-11-09 22:25:46 -0500 | [diff] [blame^] | 1421 | delete corr; |
| 1422 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1423 | if (rc < 0) { |
| 1424 | return -1; |
| 1425 | } else if (!rc) { |
| 1426 | if (amp) |
| 1427 | *amp = 0.0f; |
| 1428 | if (toa) |
| 1429 | *toa = 0.0f; |
| 1430 | return 0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1433 | /* Subtract forward search bits from delay */ |
| 1434 | if (toa) |
| 1435 | *toa = _toa - head * sps; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1436 | if (amp) |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1437 | *amp = _amp; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1438 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1439 | return 1; |
| 1440 | } |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1441 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1442 | /* |
| 1443 | * Normal burst detection |
| 1444 | * |
| 1445 | * Correlation window parameters: |
| 1446 | * target: Tail + data + mid-midamble + 1/2 remaining midamblebits |
Thomas Tsou | dafb337 | 2013-09-18 16:21:26 -0400 | [diff] [blame] | 1447 | * head: Search 4 symbols before target |
| 1448 | * tail: Search 4 symbols + maximum expected delay |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1449 | */ |
| 1450 | int analyzeTrafficBurst(signalVector &rxBurst, unsigned tsc, float thresh, |
| 1451 | int sps, complex *amp, float *toa, unsigned max_toa, |
| 1452 | bool chan_req, signalVector **chan, float *chan_offset) |
| 1453 | { |
| 1454 | int rc, start, target, head, tail, len; |
| 1455 | complex _amp; |
| 1456 | float _toa; |
Thomas Tsou | b075dd2 | 2013-11-09 22:25:46 -0500 | [diff] [blame^] | 1457 | signalVector *corr; |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1458 | CorrelationSequence *sync; |
| 1459 | |
| 1460 | if ((tsc < 0) || (tsc > 7) || ((sps != 1) && (sps != 4))) |
| 1461 | return -1; |
| 1462 | |
| 1463 | target = 3 + 58 + 16 + 5; |
Thomas Tsou | dafb337 | 2013-09-18 16:21:26 -0400 | [diff] [blame] | 1464 | head = 4; |
| 1465 | tail = 4 + max_toa; |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1466 | |
| 1467 | start = (target - head) * sps - 1; |
| 1468 | len = (head + tail) * sps; |
| 1469 | sync = gMidambles[tsc]; |
Thomas Tsou | b075dd2 | 2013-11-09 22:25:46 -0500 | [diff] [blame^] | 1470 | corr = new signalVector(len); |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1471 | |
Thomas Tsou | b075dd2 | 2013-11-09 22:25:46 -0500 | [diff] [blame^] | 1472 | rc = detectBurst(rxBurst, *corr, sync, |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1473 | thresh, sps, &_amp, &_toa, start, len); |
Thomas Tsou | b075dd2 | 2013-11-09 22:25:46 -0500 | [diff] [blame^] | 1474 | delete corr; |
| 1475 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1476 | if (rc < 0) { |
| 1477 | return -1; |
| 1478 | } else if (!rc) { |
| 1479 | if (amp) |
| 1480 | *amp = 0.0f; |
| 1481 | if (toa) |
| 1482 | *toa = 0.0f; |
| 1483 | return 0; |
| 1484 | } |
| 1485 | |
| 1486 | /* Subtract forward search bits from delay */ |
| 1487 | _toa -= head * sps; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1488 | if (toa) |
| 1489 | *toa = _toa; |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1490 | if (amp) |
| 1491 | *amp = _amp; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1492 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1493 | /* Equalization not currently supported */ |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1494 | if (chan_req) { |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1495 | *chan = new signalVector(6 * sps); |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1496 | |
| 1497 | if (chan_offset) |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1498 | *chan_offset = 0.0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1501 | return 1; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 1504 | signalVector *decimateVector(signalVector &wVector, size_t factor) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1505 | { |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 1506 | signalVector *dec; |
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 | if (factor <= 1) |
| 1509 | return NULL; |
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 | dec = new signalVector(wVector.size() / factor); |
| 1512 | dec->isReal(wVector.isReal()); |
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 | signalVector::iterator itr = dec->begin(); |
| 1515 | for (size_t i = 0; i < wVector.size(); i += factor) |
| 1516 | *itr++ = wVector[i]; |
| 1517 | |
| 1518 | return dec; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 1521 | SoftVector *demodulateBurst(signalVector &rxBurst, int sps, |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 1522 | complex channel, float TOA) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1523 | { |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 1524 | signalVector *delay, *dec = NULL; |
| 1525 | SoftVector *bits; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1526 | |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 1527 | scaleVector(rxBurst, ((complex) 1.0) / channel); |
| 1528 | delay = delayVector(&rxBurst, NULL, -TOA); |
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 | /* Shift up by a quarter of a frequency */ |
| 1531 | GMSKReverseRotate(*delay, sps); |
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 | /* Decimate and slice */ |
Thomas Tsou | d24cc2c | 2013-08-20 15:41:45 -0400 | [diff] [blame] | 1534 | if (sps > 1) { |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 1535 | dec = decimateVector(*delay, sps); |
| 1536 | delete delay; |
| 1537 | delay = NULL; |
| 1538 | } else { |
| 1539 | dec = delay; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 1542 | vectorSlicer(dec); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1543 | |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 1544 | bits = new SoftVector(dec->size()); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1545 | |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 1546 | SoftVector::iterator bit_itr = bits->begin(); |
| 1547 | signalVector::iterator burst_itr = dec->begin(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1548 | |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 1549 | for (; burst_itr < dec->end(); burst_itr++) |
| 1550 | *bit_itr++ = burst_itr->real(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1551 | |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 1552 | delete dec; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1553 | |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 1554 | return bits; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1555 | } |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 1556 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1557 | // Assumes symbol-spaced sampling!!! |
| 1558 | // Based upon paper by Al-Dhahir and Cioffi |
| 1559 | bool designDFE(signalVector &channelResponse, |
| 1560 | float SNRestimate, |
| 1561 | int Nf, |
| 1562 | signalVector **feedForwardFilter, |
| 1563 | signalVector **feedbackFilter) |
| 1564 | { |
| 1565 | |
| 1566 | signalVector G0(Nf); |
| 1567 | signalVector G1(Nf); |
| 1568 | signalVector::iterator G0ptr = G0.begin(); |
| 1569 | signalVector::iterator G1ptr = G1.begin(); |
| 1570 | signalVector::iterator chanPtr = channelResponse.begin(); |
| 1571 | |
| 1572 | int nu = channelResponse.size()-1; |
| 1573 | |
| 1574 | *G0ptr = 1.0/sqrtf(SNRestimate); |
| 1575 | for(int j = 0; j <= nu; j++) { |
| 1576 | *G1ptr = chanPtr->conj(); |
| 1577 | G1ptr++; chanPtr++; |
| 1578 | } |
| 1579 | |
| 1580 | signalVector *L[Nf]; |
| 1581 | signalVector::iterator Lptr; |
| 1582 | float d; |
| 1583 | for(int i = 0; i < Nf; i++) { |
| 1584 | d = G0.begin()->norm2() + G1.begin()->norm2(); |
| 1585 | L[i] = new signalVector(Nf+nu); |
| 1586 | Lptr = L[i]->begin()+i; |
| 1587 | G0ptr = G0.begin(); G1ptr = G1.begin(); |
| 1588 | while ((G0ptr < G0.end()) && (Lptr < L[i]->end())) { |
| 1589 | *Lptr = (*G0ptr*(G0.begin()->conj()) + *G1ptr*(G1.begin()->conj()) )/d; |
| 1590 | Lptr++; |
| 1591 | G0ptr++; |
| 1592 | G1ptr++; |
| 1593 | } |
| 1594 | complex k = (*G1.begin())/(*G0.begin()); |
| 1595 | |
| 1596 | if (i != Nf-1) { |
| 1597 | signalVector G0new = G1; |
| 1598 | scaleVector(G0new,k.conj()); |
| 1599 | addVector(G0new,G0); |
| 1600 | |
| 1601 | signalVector G1new = G0; |
| 1602 | scaleVector(G1new,k*(-1.0)); |
| 1603 | addVector(G1new,G1); |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 1604 | delayVector(&G1new, &G1new, -1.0); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1605 | |
| 1606 | scaleVector(G0new,1.0/sqrtf(1.0+k.norm2())); |
| 1607 | scaleVector(G1new,1.0/sqrtf(1.0+k.norm2())); |
| 1608 | G0 = G0new; |
| 1609 | G1 = G1new; |
| 1610 | } |
| 1611 | } |
| 1612 | |
| 1613 | *feedbackFilter = new signalVector(nu); |
| 1614 | L[Nf-1]->segmentCopyTo(**feedbackFilter,Nf,nu); |
| 1615 | scaleVector(**feedbackFilter,(complex) -1.0); |
| 1616 | conjugateVector(**feedbackFilter); |
| 1617 | |
| 1618 | signalVector v(Nf); |
| 1619 | signalVector::iterator vStart = v.begin(); |
| 1620 | signalVector::iterator vPtr; |
| 1621 | *(vStart+Nf-1) = (complex) 1.0; |
| 1622 | for(int k = Nf-2; k >= 0; k--) { |
| 1623 | Lptr = L[k]->begin()+k+1; |
| 1624 | vPtr = vStart + k+1; |
| 1625 | complex v_k = 0.0; |
| 1626 | for (int j = k+1; j < Nf; j++) { |
| 1627 | v_k -= (*vPtr)*(*Lptr); |
| 1628 | vPtr++; Lptr++; |
| 1629 | } |
| 1630 | *(vStart + k) = v_k; |
| 1631 | } |
| 1632 | |
| 1633 | *feedForwardFilter = new signalVector(Nf); |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1634 | signalVector::iterator w = (*feedForwardFilter)->end(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1635 | for (int i = 0; i < Nf; i++) { |
| 1636 | delete L[i]; |
| 1637 | complex w_i = 0.0; |
| 1638 | int endPt = ( nu < (Nf-1-i) ) ? nu : (Nf-1-i); |
| 1639 | vPtr = vStart+i; |
| 1640 | chanPtr = channelResponse.begin(); |
| 1641 | for (int k = 0; k < endPt+1; k++) { |
| 1642 | w_i += (*vPtr)*(chanPtr->conj()); |
| 1643 | vPtr++; chanPtr++; |
| 1644 | } |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1645 | *--w = w_i/d; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1646 | } |
| 1647 | |
| 1648 | |
| 1649 | return true; |
| 1650 | |
| 1651 | } |
| 1652 | |
| 1653 | // Assumes symbol-rate sampling!!!! |
| 1654 | SoftVector *equalizeBurst(signalVector &rxBurst, |
| 1655 | float TOA, |
Thomas Tsou | d24cc2c | 2013-08-20 15:41:45 -0400 | [diff] [blame] | 1656 | int sps, |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1657 | signalVector &w, // feedforward filter |
| 1658 | signalVector &b) // feedback filter |
| 1659 | { |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1660 | signalVector *postForwardFull; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1661 | |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 1662 | if (!delayVector(&rxBurst, &rxBurst, -TOA)) |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1663 | return NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1664 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1665 | postForwardFull = convolve(&rxBurst, &w, NULL, |
| 1666 | CUSTOM, 0, rxBurst.size() + w.size() - 1); |
| 1667 | if (!postForwardFull) |
| 1668 | return NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1669 | |
| 1670 | signalVector* postForward = new signalVector(rxBurst.size()); |
| 1671 | postForwardFull->segmentCopyTo(*postForward,w.size()-1,rxBurst.size()); |
| 1672 | delete postForwardFull; |
| 1673 | |
| 1674 | signalVector::iterator dPtr = postForward->begin(); |
| 1675 | signalVector::iterator dBackPtr; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 1676 | signalVector::iterator rotPtr = GMSKRotationN->begin(); |
| 1677 | signalVector::iterator revRotPtr = GMSKReverseRotationN->begin(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1678 | |
| 1679 | signalVector *DFEoutput = new signalVector(postForward->size()); |
| 1680 | signalVector::iterator DFEItr = DFEoutput->begin(); |
| 1681 | |
| 1682 | // NOTE: can insert the midamble and/or use midamble to estimate BER |
| 1683 | for (; dPtr < postForward->end(); dPtr++) { |
| 1684 | dBackPtr = dPtr-1; |
| 1685 | signalVector::iterator bPtr = b.begin(); |
| 1686 | while ( (bPtr < b.end()) && (dBackPtr >= postForward->begin()) ) { |
| 1687 | *dPtr = *dPtr + (*bPtr)*(*dBackPtr); |
| 1688 | bPtr++; |
| 1689 | dBackPtr--; |
| 1690 | } |
| 1691 | *dPtr = *dPtr * (*revRotPtr); |
| 1692 | *DFEItr = *dPtr; |
| 1693 | // make decision on symbol |
| 1694 | *dPtr = (dPtr->real() > 0.0) ? 1.0 : -1.0; |
| 1695 | //*DFEItr = *dPtr; |
| 1696 | *dPtr = *dPtr * (*rotPtr); |
| 1697 | DFEItr++; |
| 1698 | rotPtr++; |
| 1699 | revRotPtr++; |
| 1700 | } |
| 1701 | |
| 1702 | vectorSlicer(DFEoutput); |
| 1703 | |
| 1704 | SoftVector *burstBits = new SoftVector(postForward->size()); |
| 1705 | SoftVector::iterator burstItr = burstBits->begin(); |
| 1706 | DFEItr = DFEoutput->begin(); |
| 1707 | for (; DFEItr < DFEoutput->end(); DFEItr++) |
| 1708 | *burstItr++ = DFEItr->real(); |
| 1709 | |
| 1710 | delete postForward; |
| 1711 | |
| 1712 | delete DFEoutput; |
| 1713 | |
| 1714 | return burstBits; |
| 1715 | } |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 1716 | |
| 1717 | bool sigProcLibSetup(int sps) |
| 1718 | { |
| 1719 | if ((sps != 1) && (sps != 4)) |
| 1720 | return false; |
| 1721 | |
| 1722 | initTrigTables(); |
Thomas Tsou | 0e0e1f4 | 2013-11-09 22:08:51 -0500 | [diff] [blame] | 1723 | generateSincTable(); |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 1724 | initGMSKRotationTables(sps); |
| 1725 | |
| 1726 | GSMPulse1 = generateGSMPulse(1, 2); |
| 1727 | if (sps > 1) |
| 1728 | GSMPulse = generateGSMPulse(sps, 2); |
| 1729 | |
| 1730 | if (!generateRACHSequence(1)) { |
| 1731 | sigProcLibDestroy(); |
| 1732 | return false; |
| 1733 | } |
| 1734 | |
Thomas Tsou | f79c4d0 | 2013-11-09 15:51:56 -0600 | [diff] [blame] | 1735 | generateDelayFilters(); |
| 1736 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 1737 | return true; |
| 1738 | } |