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" |
Alexander Chemeris | 954b118 | 2015-06-04 15:39:41 -0400 | [diff] [blame] | 31 | #include "Logger.h" |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 32 | #include "Resampler.h" |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 33 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 34 | extern "C" { |
| 35 | #include "convolve.h" |
Thomas Tsou | 7e4e536 | 2013-10-30 21:18:55 -0400 | [diff] [blame] | 36 | #include "scale.h" |
Thomas Tsou | 0a3dc4c | 2013-11-09 02:29:55 -0500 | [diff] [blame] | 37 | #include "mult.h" |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 38 | } |
| 39 | |
Thomas Tsou | 7e4e536 | 2013-10-30 21:18:55 -0400 | [diff] [blame] | 40 | using namespace GSM; |
| 41 | |
Thomas Tsou | f79c4d0 | 2013-11-09 15:51:56 -0600 | [diff] [blame] | 42 | #define TABLESIZE 1024 |
| 43 | #define DELAYFILTS 64 |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 44 | |
Tom Tsou | 577cd02 | 2015-05-18 13:57:54 -0700 | [diff] [blame] | 45 | /* Clipping detection threshold */ |
| 46 | #define CLIP_THRESH 30000.0f |
| 47 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 48 | /** Lookup tables for trigonometric approximation */ |
| 49 | float cosTable[TABLESIZE+1]; // add 1 element for wrap around |
| 50 | float sinTable[TABLESIZE+1]; |
Thomas Tsou | 0e0e1f4 | 2013-11-09 22:08:51 -0500 | [diff] [blame] | 51 | float sincTable[TABLESIZE+1]; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 52 | |
| 53 | /** Constants */ |
| 54 | static const float M_PI_F = (float)M_PI; |
| 55 | static const float M_2PI_F = (float)(2.0*M_PI); |
| 56 | static const float M_1_2PI_F = 1/M_2PI_F; |
| 57 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 58 | /* Precomputed rotation vectors */ |
Tom Tsou | 2079a3c | 2016-03-06 00:58:56 -0800 | [diff] [blame] | 59 | static signalVector *GMSKRotation4 = NULL; |
| 60 | static signalVector *GMSKReverseRotation4 = NULL; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 61 | static signalVector *GMSKRotation1 = NULL; |
| 62 | static signalVector *GMSKReverseRotation1 = NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 63 | |
Thomas Tsou | f79c4d0 | 2013-11-09 15:51:56 -0600 | [diff] [blame] | 64 | /* Precomputed fractional delay filters */ |
| 65 | static signalVector *delayFilters[DELAYFILTS]; |
| 66 | |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 67 | static Complex<float> psk8_table[8] = { |
| 68 | Complex<float>(-0.70710678, 0.70710678), |
| 69 | Complex<float>( 0.0, -1.0), |
| 70 | Complex<float>( 0.0, 1.0), |
| 71 | Complex<float>( 0.70710678, -0.70710678), |
| 72 | Complex<float>(-1.0, 0.0), |
| 73 | Complex<float>(-0.70710678, -0.70710678), |
| 74 | Complex<float>( 0.70710678, 0.70710678), |
| 75 | Complex<float>( 1.0, 0.0), |
| 76 | }; |
| 77 | |
| 78 | /* Downsampling filterbank - 4 SPS to 1 SPS */ |
| 79 | #define DOWNSAMPLE_IN_LEN 624 |
| 80 | #define DOWNSAMPLE_OUT_LEN 156 |
| 81 | |
| 82 | static Resampler *dnsampler = NULL; |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 83 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 84 | /* |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 85 | * RACH and midamble correlation waveforms. Store the buffer separately |
| 86 | * because we need to allocate it explicitly outside of the signal vector |
| 87 | * constructor. This is because C++ (prior to C++11) is unable to natively |
| 88 | * perform 16-byte memory alignment required by many SSE instructions. |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 89 | */ |
| 90 | struct CorrelationSequence { |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 91 | CorrelationSequence() : sequence(NULL), buffer(NULL) |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 92 | { |
| 93 | } |
| 94 | |
| 95 | ~CorrelationSequence() |
| 96 | { |
| 97 | delete sequence; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 98 | free(buffer); |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 99 | } |
| 100 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 101 | signalVector *sequence; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 102 | void *buffer; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 103 | float toa; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 104 | complex gain; |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 105 | }; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 106 | |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 107 | /* |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 108 | * Gaussian and empty modulation pulses. Like the correlation sequences, |
| 109 | * store the runtime (Gaussian) buffer separately because of needed alignment |
| 110 | * for SSE instructions. |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 111 | */ |
| 112 | struct PulseSequence { |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 113 | PulseSequence() : c0(NULL), c1(NULL), c0_inv(NULL), empty(NULL), |
| 114 | c0_buffer(NULL), c1_buffer(NULL), c0_inv_buffer(NULL) |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 115 | { |
| 116 | } |
| 117 | |
| 118 | ~PulseSequence() |
| 119 | { |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 120 | delete c0; |
| 121 | delete c1; |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 122 | delete c0_inv; |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 123 | delete empty; |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 124 | free(c0_buffer); |
| 125 | free(c1_buffer); |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 126 | } |
| 127 | |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 128 | signalVector *c0; |
| 129 | signalVector *c1; |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 130 | signalVector *c0_inv; |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 131 | signalVector *empty; |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 132 | void *c0_buffer; |
| 133 | void *c1_buffer; |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 134 | void *c0_inv_buffer; |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 135 | }; |
| 136 | |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 137 | static CorrelationSequence *gMidambles[] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL}; |
| 138 | static CorrelationSequence *gEdgeMidambles[] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL}; |
| 139 | static CorrelationSequence *gRACHSequence = NULL; |
| 140 | static PulseSequence *GSMPulse1 = NULL; |
| 141 | static PulseSequence *GSMPulse4 = NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 142 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 143 | void sigProcLibDestroy() |
| 144 | { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 145 | for (int i = 0; i < 8; i++) { |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 146 | delete gMidambles[i]; |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 147 | delete gEdgeMidambles[i]; |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 148 | gMidambles[i] = NULL; |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 149 | gEdgeMidambles[i] = NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 150 | } |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 151 | |
Thomas Tsou | f79c4d0 | 2013-11-09 15:51:56 -0600 | [diff] [blame] | 152 | for (int i = 0; i < DELAYFILTS; i++) { |
| 153 | delete delayFilters[i]; |
| 154 | delayFilters[i] = NULL; |
| 155 | } |
| 156 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 157 | delete GMSKRotation1; |
| 158 | delete GMSKReverseRotation1; |
Tom Tsou | 2079a3c | 2016-03-06 00:58:56 -0800 | [diff] [blame] | 159 | delete GMSKRotation4; |
| 160 | delete GMSKReverseRotation4; |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 161 | delete gRACHSequence; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 162 | delete GSMPulse1; |
Tom Tsou | 2079a3c | 2016-03-06 00:58:56 -0800 | [diff] [blame] | 163 | delete GSMPulse4; |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 164 | delete dnsampler; |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 165 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 166 | GMSKRotation1 = NULL; |
Tom Tsou | 2079a3c | 2016-03-06 00:58:56 -0800 | [diff] [blame] | 167 | GMSKRotation4 = NULL; |
| 168 | GMSKReverseRotation4 = NULL; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 169 | GMSKReverseRotation1 = NULL; |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 170 | gRACHSequence = NULL; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 171 | GSMPulse1 = NULL; |
Tom Tsou | 2079a3c | 2016-03-06 00:58:56 -0800 | [diff] [blame] | 172 | GSMPulse4 = NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 173 | } |
| 174 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 175 | // dB relative to 1.0. |
| 176 | // if > 1.0, then return 0 dB |
| 177 | float dB(float x) { |
| 178 | |
| 179 | float arg = 1.0F; |
| 180 | float dB = 0.0F; |
| 181 | |
| 182 | if (x >= 1.0F) return 0.0F; |
| 183 | if (x <= 0.0F) return -200.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 (arg > x); |
| 196 | arg = prevArg; |
| 197 | dB = prevdB; |
| 198 | stepSize *= 0.5F; |
| 199 | dBstepSize -= 3.0F; |
| 200 | } |
| 201 | return ((arg-x)*(dB-3.0F) + (x-arg*0.5F)*dB)/(arg - arg*0.5F); |
| 202 | |
| 203 | } |
| 204 | |
| 205 | // 10^(-dB/10), inverse of dB func. |
| 206 | float dBinv(float x) { |
| 207 | |
| 208 | float arg = 1.0F; |
| 209 | float dB = 0.0F; |
| 210 | |
| 211 | if (x >= 0.0F) return 1.0F; |
| 212 | if (x <= -200.0F) return 0.0F; |
| 213 | |
| 214 | float prevArg = arg; |
| 215 | float prevdB = dB; |
| 216 | float stepSize = 16.0F; |
| 217 | float dBstepSize = 12.0F; |
| 218 | while (stepSize > 1.0F) { |
| 219 | do { |
| 220 | prevArg = arg; |
| 221 | prevdB = dB; |
| 222 | arg /= stepSize; |
| 223 | dB -= dBstepSize; |
| 224 | } while (dB > x); |
| 225 | arg = prevArg; |
| 226 | dB = prevdB; |
| 227 | stepSize *= 0.5F; |
| 228 | dBstepSize -= 3.0F; |
| 229 | } |
| 230 | |
| 231 | return ((dB-x)*(arg*0.5F)+(x-(dB-3.0F))*(arg))/3.0F; |
| 232 | |
| 233 | } |
| 234 | |
| 235 | float vectorNorm2(const signalVector &x) |
| 236 | { |
| 237 | signalVector::const_iterator xPtr = x.begin(); |
| 238 | float Energy = 0.0; |
| 239 | for (;xPtr != x.end();xPtr++) { |
| 240 | Energy += xPtr->norm2(); |
| 241 | } |
| 242 | return Energy; |
| 243 | } |
| 244 | |
| 245 | |
| 246 | float vectorPower(const signalVector &x) |
| 247 | { |
| 248 | return vectorNorm2(x)/x.size(); |
| 249 | } |
| 250 | |
| 251 | /** compute cosine via lookup table */ |
| 252 | float cosLookup(const 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 iDelta*cosTable[argI] + delta*cosTable[argI+1]; |
| 263 | } |
| 264 | |
| 265 | /** compute sine via lookup table */ |
| 266 | float sinLookup(const float x) |
| 267 | { |
| 268 | float arg = x*M_1_2PI_F; |
| 269 | while (arg > 1.0F) arg -= 1.0F; |
| 270 | while (arg < 0.0F) arg += 1.0F; |
| 271 | |
| 272 | const float argT = arg*((float)TABLESIZE); |
| 273 | const int argI = (int)argT; |
| 274 | const float delta = argT-argI; |
| 275 | const float iDelta = 1.0F-delta; |
| 276 | return iDelta*sinTable[argI] + delta*sinTable[argI+1]; |
| 277 | } |
| 278 | |
| 279 | |
| 280 | /** compute e^(-jx) via lookup table. */ |
Tom Tsou | 2079a3c | 2016-03-06 00:58:56 -0800 | [diff] [blame] | 281 | static complex expjLookup(float x) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 282 | { |
| 283 | float arg = x*M_1_2PI_F; |
| 284 | while (arg > 1.0F) arg -= 1.0F; |
| 285 | while (arg < 0.0F) arg += 1.0F; |
| 286 | |
| 287 | const float argT = arg*((float)TABLESIZE); |
| 288 | const int argI = (int)argT; |
| 289 | const float delta = argT-argI; |
| 290 | const float iDelta = 1.0F-delta; |
| 291 | return complex(iDelta*cosTable[argI] + delta*cosTable[argI+1], |
| 292 | iDelta*sinTable[argI] + delta*sinTable[argI+1]); |
| 293 | } |
| 294 | |
| 295 | /** Library setup functions */ |
Tom Tsou | 2079a3c | 2016-03-06 00:58:56 -0800 | [diff] [blame] | 296 | static void initTrigTables() { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 297 | for (int i = 0; i < TABLESIZE+1; i++) { |
| 298 | cosTable[i] = cos(2.0*M_PI*i/TABLESIZE); |
| 299 | sinTable[i] = sin(2.0*M_PI*i/TABLESIZE); |
| 300 | } |
| 301 | } |
| 302 | |
Tom Tsou | 2079a3c | 2016-03-06 00:58:56 -0800 | [diff] [blame] | 303 | /* |
| 304 | * Initialize 4 sps and 1 sps rotation tables |
| 305 | */ |
| 306 | static void initGMSKRotationTables() |
Thomas Tsou | d24cc2c | 2013-08-20 15:41:45 -0400 | [diff] [blame] | 307 | { |
Tom Tsou | 2079a3c | 2016-03-06 00:58:56 -0800 | [diff] [blame] | 308 | size_t len1 = 157, len4 = 625; |
| 309 | |
| 310 | GMSKRotation4 = new signalVector(len4); |
| 311 | GMSKReverseRotation4 = new signalVector(len4); |
| 312 | signalVector::iterator rotPtr = GMSKRotation4->begin(); |
| 313 | signalVector::iterator revPtr = GMSKReverseRotation4->begin(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 314 | float phase = 0.0; |
Tom Tsou | 2079a3c | 2016-03-06 00:58:56 -0800 | [diff] [blame] | 315 | while (rotPtr != GMSKRotation4->end()) { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 316 | *rotPtr++ = expjLookup(phase); |
| 317 | *revPtr++ = expjLookup(-phase); |
Tom Tsou | 2079a3c | 2016-03-06 00:58:56 -0800 | [diff] [blame] | 318 | phase += M_PI_F / 2.0F / 4.0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 319 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 320 | |
Tom Tsou | 2079a3c | 2016-03-06 00:58:56 -0800 | [diff] [blame] | 321 | GMSKRotation1 = new signalVector(len1); |
| 322 | GMSKReverseRotation1 = new signalVector(len1); |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 323 | rotPtr = GMSKRotation1->begin(); |
| 324 | revPtr = GMSKReverseRotation1->begin(); |
| 325 | phase = 0.0; |
| 326 | while (rotPtr != GMSKRotation1->end()) { |
| 327 | *rotPtr++ = expjLookup(phase); |
| 328 | *revPtr++ = expjLookup(-phase); |
| 329 | phase += M_PI_F / 2.0F; |
Thomas Tsou | e57004d | 2013-08-20 18:55:33 -0400 | [diff] [blame] | 330 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 333 | static void GMSKRotate(signalVector &x, int sps) |
| 334 | { |
Thomas Tsou | 0a3dc4c | 2013-11-09 02:29:55 -0500 | [diff] [blame] | 335 | #if HAVE_NEON |
| 336 | size_t len; |
| 337 | signalVector *a, *b, *out; |
| 338 | |
| 339 | a = &x; |
| 340 | out = &x; |
| 341 | len = out->size(); |
| 342 | |
| 343 | if (len == 157) |
| 344 | len--; |
| 345 | |
| 346 | if (sps == 1) |
| 347 | b = GMSKRotation1; |
| 348 | else |
Tom Tsou | 2079a3c | 2016-03-06 00:58:56 -0800 | [diff] [blame] | 349 | b = GMSKRotation4; |
Thomas Tsou | 0a3dc4c | 2013-11-09 02:29:55 -0500 | [diff] [blame] | 350 | |
| 351 | mul_complex((float *) out->begin(), |
| 352 | (float *) a->begin(), |
| 353 | (float *) b->begin(), len); |
| 354 | #else |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 355 | signalVector::iterator rotPtr, xPtr = x.begin(); |
| 356 | |
| 357 | if (sps == 1) |
| 358 | rotPtr = GMSKRotation1->begin(); |
| 359 | else |
Tom Tsou | 2079a3c | 2016-03-06 00:58:56 -0800 | [diff] [blame] | 360 | rotPtr = GMSKRotation4->begin(); |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 361 | |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 362 | if (x.isReal()) { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 363 | while (xPtr < x.end()) { |
| 364 | *xPtr = *rotPtr++ * (xPtr->real()); |
| 365 | xPtr++; |
| 366 | } |
| 367 | } |
| 368 | else { |
| 369 | while (xPtr < x.end()) { |
| 370 | *xPtr = *rotPtr++ * (*xPtr); |
| 371 | xPtr++; |
| 372 | } |
| 373 | } |
Thomas Tsou | 0a3dc4c | 2013-11-09 02:29:55 -0500 | [diff] [blame] | 374 | #endif |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Tom Tsou | 2079a3c | 2016-03-06 00:58:56 -0800 | [diff] [blame] | 377 | static bool GMSKReverseRotate(signalVector &x, int sps) |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 378 | { |
| 379 | signalVector::iterator rotPtr, xPtr= x.begin(); |
| 380 | |
| 381 | if (sps == 1) |
| 382 | rotPtr = GMSKReverseRotation1->begin(); |
Tom Tsou | 2079a3c | 2016-03-06 00:58:56 -0800 | [diff] [blame] | 383 | else if (sps == 4) |
| 384 | rotPtr = GMSKReverseRotation4->begin(); |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 385 | else |
Tom Tsou | 2079a3c | 2016-03-06 00:58:56 -0800 | [diff] [blame] | 386 | return false; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 387 | |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 388 | if (x.isReal()) { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 389 | while (xPtr < x.end()) { |
| 390 | *xPtr = *rotPtr++ * (xPtr->real()); |
| 391 | xPtr++; |
| 392 | } |
| 393 | } |
| 394 | else { |
| 395 | while (xPtr < x.end()) { |
| 396 | *xPtr = *rotPtr++ * (*xPtr); |
| 397 | xPtr++; |
| 398 | } |
| 399 | } |
Tom Tsou | 2079a3c | 2016-03-06 00:58:56 -0800 | [diff] [blame] | 400 | |
| 401 | return true; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 404 | signalVector *convolve(const signalVector *x, |
| 405 | const signalVector *h, |
| 406 | signalVector *y, |
Thomas Tsou | 3f32ab5 | 2013-11-15 16:32:54 -0500 | [diff] [blame] | 407 | ConvType spanType, size_t start, |
| 408 | size_t len, size_t step, int offset) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 409 | { |
Thomas Tsou | 3f32ab5 | 2013-11-15 16:32:54 -0500 | [diff] [blame] | 410 | int rc; |
| 411 | size_t head = 0, tail = 0; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 412 | bool alloc = false, append = false; |
| 413 | const signalVector *_x = NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 414 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 415 | if (!x || !h) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 416 | return NULL; |
| 417 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 418 | switch (spanType) { |
| 419 | case START_ONLY: |
| 420 | start = 0; |
Thomas Tsou | 6f4906e | 2013-11-09 02:40:18 -0500 | [diff] [blame] | 421 | head = h->size() - 1; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 422 | len = x->size(); |
Thomas Tsou | 6f4906e | 2013-11-09 02:40:18 -0500 | [diff] [blame] | 423 | |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 424 | if (x->getStart() < head) |
Thomas Tsou | 6f4906e | 2013-11-09 02:40:18 -0500 | [diff] [blame] | 425 | append = true; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 426 | break; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 427 | case NO_DELAY: |
| 428 | start = h->size() / 2; |
| 429 | head = start; |
| 430 | tail = start; |
| 431 | len = x->size(); |
| 432 | append = true; |
| 433 | break; |
| 434 | case CUSTOM: |
| 435 | if (start < h->size() - 1) { |
| 436 | head = h->size() - start; |
| 437 | append = true; |
| 438 | } |
| 439 | if (start + len > x->size()) { |
| 440 | tail = start + len - x->size(); |
| 441 | append = true; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 442 | } |
| 443 | break; |
| 444 | default: |
| 445 | return NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 446 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 447 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 448 | /* |
| 449 | * Error if the output vector is too small. Create the output vector |
| 450 | * if the pointer is NULL. |
| 451 | */ |
| 452 | if (y && (len > y->size())) |
| 453 | return NULL; |
| 454 | if (!y) { |
| 455 | y = new signalVector(len); |
| 456 | alloc = true; |
| 457 | } |
| 458 | |
| 459 | /* Prepend or post-pend the input vector if the parameters require it */ |
| 460 | if (append) |
| 461 | _x = new signalVector(*x, head, tail); |
| 462 | else |
| 463 | _x = x; |
| 464 | |
| 465 | /* |
| 466 | * Four convovle types: |
| 467 | * 1. Complex-Real (aligned) |
| 468 | * 2. Complex-Complex (aligned) |
| 469 | * 3. Complex-Real (!aligned) |
| 470 | * 4. Complex-Complex (!aligned) |
| 471 | */ |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 472 | if (h->isReal() && h->isAligned()) { |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 473 | rc = convolve_real((float *) _x->begin(), _x->size(), |
| 474 | (float *) h->begin(), h->size(), |
| 475 | (float *) y->begin(), y->size(), |
| 476 | start, len, step, offset); |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 477 | } else if (!h->isReal() && h->isAligned()) { |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 478 | rc = convolve_complex((float *) _x->begin(), _x->size(), |
| 479 | (float *) h->begin(), h->size(), |
| 480 | (float *) y->begin(), y->size(), |
| 481 | start, len, step, offset); |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 482 | } else if (h->isReal() && !h->isAligned()) { |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 483 | rc = base_convolve_real((float *) _x->begin(), _x->size(), |
| 484 | (float *) h->begin(), h->size(), |
| 485 | (float *) y->begin(), y->size(), |
| 486 | start, len, step, offset); |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 487 | } else if (!h->isReal() && !h->isAligned()) { |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 488 | rc = base_convolve_complex((float *) _x->begin(), _x->size(), |
| 489 | (float *) h->begin(), h->size(), |
| 490 | (float *) y->begin(), y->size(), |
| 491 | start, len, step, offset); |
| 492 | } else { |
| 493 | rc = -1; |
| 494 | } |
| 495 | |
| 496 | if (append) |
| 497 | delete _x; |
| 498 | |
| 499 | if (rc < 0) { |
| 500 | if (alloc) |
| 501 | delete y; |
| 502 | return NULL; |
| 503 | } |
| 504 | |
| 505 | return y; |
| 506 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 507 | |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 508 | /* |
| 509 | * Generate static EDGE linear equalizer. This equalizer is not adaptive. |
| 510 | * Filter taps are generated from the inverted 1 SPS impulse response of |
| 511 | * the EDGE pulse shape captured after the downsampling filter. |
| 512 | */ |
| 513 | static bool generateInvertC0Pulse(PulseSequence *pulse) |
| 514 | { |
| 515 | if (!pulse) |
| 516 | return false; |
| 517 | |
| 518 | pulse->c0_inv_buffer = convolve_h_alloc(5); |
| 519 | pulse->c0_inv = new signalVector((complex *) pulse->c0_inv_buffer, 0, 5); |
| 520 | pulse->c0_inv->isReal(true); |
| 521 | pulse->c0_inv->setAligned(false); |
| 522 | |
| 523 | signalVector::iterator xP = pulse->c0_inv->begin(); |
| 524 | *xP++ = 0.15884; |
| 525 | *xP++ = -0.43176; |
| 526 | *xP++ = 1.00000; |
| 527 | *xP++ = -0.42608; |
| 528 | *xP++ = 0.14882; |
| 529 | |
| 530 | return true; |
| 531 | } |
| 532 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 533 | static bool generateC1Pulse(int sps, PulseSequence *pulse) |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 534 | { |
| 535 | int len; |
| 536 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 537 | if (!pulse) |
| 538 | return false; |
| 539 | |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 540 | switch (sps) { |
| 541 | case 4: |
| 542 | len = 8; |
| 543 | break; |
| 544 | default: |
| 545 | return false; |
| 546 | } |
| 547 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 548 | pulse->c1_buffer = convolve_h_alloc(len); |
| 549 | pulse->c1 = new signalVector((complex *) |
| 550 | pulse->c1_buffer, 0, len); |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 551 | pulse->c1->isReal(true); |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 552 | |
| 553 | /* Enable alignment for SSE usage */ |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 554 | pulse->c1->setAligned(true); |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 555 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 556 | signalVector::iterator xP = pulse->c1->begin(); |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 557 | |
| 558 | switch (sps) { |
| 559 | case 4: |
| 560 | /* BT = 0.30 */ |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 561 | *xP++ = 0.0; |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 562 | *xP++ = 8.16373112e-03; |
| 563 | *xP++ = 2.84385729e-02; |
| 564 | *xP++ = 5.64158904e-02; |
| 565 | *xP++ = 7.05463553e-02; |
| 566 | *xP++ = 5.64158904e-02; |
| 567 | *xP++ = 2.84385729e-02; |
| 568 | *xP++ = 8.16373112e-03; |
| 569 | } |
| 570 | |
| 571 | return true; |
| 572 | } |
| 573 | |
Tom Tsou | 2079a3c | 2016-03-06 00:58:56 -0800 | [diff] [blame] | 574 | static PulseSequence *generateGSMPulse(int sps) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 575 | { |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 576 | int len; |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 577 | float arg, avg, center; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 578 | PulseSequence *pulse; |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 579 | |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 580 | if ((sps != 1) && (sps != 4)) |
| 581 | return NULL; |
| 582 | |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 583 | /* Store a single tap filter used for correlation sequence generation */ |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 584 | pulse = new PulseSequence(); |
| 585 | pulse->empty = new signalVector(1); |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 586 | pulse->empty->isReal(true); |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 587 | *(pulse->empty->begin()) = 1.0f; |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 588 | |
Thomas Tsou | 9ccd9f2 | 2013-08-21 13:59:52 -0400 | [diff] [blame] | 589 | /* |
| 590 | * For 4 samples-per-symbol use a precomputed single pulse Laurent |
| 591 | * approximation. This should yields below 2 degrees of phase error at |
| 592 | * the modulator output. Use the existing pulse approximation for all |
| 593 | * other oversampling factors. |
| 594 | */ |
| 595 | switch (sps) { |
| 596 | case 4: |
| 597 | len = 16; |
| 598 | break; |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 599 | case 1: |
Thomas Tsou | 9ccd9f2 | 2013-08-21 13:59:52 -0400 | [diff] [blame] | 600 | default: |
Tom Tsou | 2079a3c | 2016-03-06 00:58:56 -0800 | [diff] [blame] | 601 | len = 4; |
Thomas Tsou | 9ccd9f2 | 2013-08-21 13:59:52 -0400 | [diff] [blame] | 602 | } |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 603 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 604 | pulse->c0_buffer = convolve_h_alloc(len); |
| 605 | pulse->c0 = new signalVector((complex *) pulse->c0_buffer, 0, len); |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 606 | pulse->c0->isReal(true); |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 607 | |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 608 | /* Enable alingnment for SSE usage */ |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 609 | pulse->c0->setAligned(true); |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 610 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 611 | signalVector::iterator xP = pulse->c0->begin(); |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 612 | |
Thomas Tsou | 9ccd9f2 | 2013-08-21 13:59:52 -0400 | [diff] [blame] | 613 | if (sps == 4) { |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 614 | *xP++ = 0.0; |
Thomas Tsou | 9ccd9f2 | 2013-08-21 13:59:52 -0400 | [diff] [blame] | 615 | *xP++ = 4.46348606e-03; |
| 616 | *xP++ = 2.84385729e-02; |
| 617 | *xP++ = 1.03184855e-01; |
| 618 | *xP++ = 2.56065552e-01; |
| 619 | *xP++ = 4.76375085e-01; |
| 620 | *xP++ = 7.05961177e-01; |
| 621 | *xP++ = 8.71291644e-01; |
| 622 | *xP++ = 9.29453645e-01; |
| 623 | *xP++ = 8.71291644e-01; |
| 624 | *xP++ = 7.05961177e-01; |
| 625 | *xP++ = 4.76375085e-01; |
| 626 | *xP++ = 2.56065552e-01; |
| 627 | *xP++ = 1.03184855e-01; |
| 628 | *xP++ = 2.84385729e-02; |
| 629 | *xP++ = 4.46348606e-03; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 630 | generateC1Pulse(sps, pulse); |
Thomas Tsou | 9ccd9f2 | 2013-08-21 13:59:52 -0400 | [diff] [blame] | 631 | } else { |
| 632 | center = (float) (len - 1.0) / 2.0; |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 633 | |
Thomas Tsou | 9ccd9f2 | 2013-08-21 13:59:52 -0400 | [diff] [blame] | 634 | /* GSM pulse approximation */ |
| 635 | for (int i = 0; i < len; i++) { |
| 636 | arg = ((float) i - center) / (float) sps; |
| 637 | *xP++ = 0.96 * exp(-1.1380 * arg * arg - |
| 638 | 0.527 * arg * arg * arg * arg); |
| 639 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 640 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 641 | avg = sqrtf(vectorNorm2(*pulse->c0) / sps); |
| 642 | xP = pulse->c0->begin(); |
| 643 | for (int i = 0; i < len; i++) |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 644 | *xP++ /= avg; |
| 645 | } |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 646 | |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 647 | /* |
| 648 | * Current form of the EDGE equalization filter non-realizable at 4 SPS. |
| 649 | * Load the onto both 1 SPS and 4 SPS objects for convenience. Note that |
| 650 | * the EDGE demodulator downsamples to 1 SPS prior to equalization. |
| 651 | */ |
| 652 | generateInvertC0Pulse(pulse); |
| 653 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 654 | return pulse; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | signalVector* frequencyShift(signalVector *y, |
| 658 | signalVector *x, |
| 659 | float freq, |
| 660 | float startPhase, |
| 661 | float *finalPhase) |
| 662 | { |
| 663 | |
| 664 | if (!x) return NULL; |
| 665 | |
| 666 | if (y==NULL) { |
| 667 | y = new signalVector(x->size()); |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 668 | y->isReal(x->isReal()); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 669 | if (y==NULL) return NULL; |
| 670 | } |
| 671 | |
| 672 | if (y->size() < x->size()) return NULL; |
| 673 | |
| 674 | float phase = startPhase; |
| 675 | signalVector::iterator yP = y->begin(); |
| 676 | signalVector::iterator xPEnd = x->end(); |
| 677 | signalVector::iterator xP = x->begin(); |
| 678 | |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 679 | if (x->isReal()) { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 680 | while (xP < xPEnd) { |
| 681 | (*yP++) = expjLookup(phase)*( (xP++)->real() ); |
| 682 | phase += freq; |
| 683 | } |
| 684 | } |
| 685 | else { |
| 686 | while (xP < xPEnd) { |
| 687 | (*yP++) = (*xP++)*expjLookup(phase); |
| 688 | phase += freq; |
Thomas Tsou | 34bbef7 | 2013-11-13 22:58:15 -0500 | [diff] [blame] | 689 | if (phase > 2 * M_PI) |
| 690 | phase -= 2 * M_PI; |
| 691 | else if (phase < -2 * M_PI) |
| 692 | phase += 2 * M_PI; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | |
| 696 | |
| 697 | if (finalPhase) *finalPhase = phase; |
| 698 | |
| 699 | return y; |
| 700 | } |
| 701 | |
| 702 | signalVector* reverseConjugate(signalVector *b) |
| 703 | { |
| 704 | signalVector *tmp = new signalVector(b->size()); |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 705 | tmp->isReal(b->isReal()); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 706 | signalVector::iterator bP = b->begin(); |
| 707 | signalVector::iterator bPEnd = b->end(); |
| 708 | signalVector::iterator tmpP = tmp->end()-1; |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 709 | if (!b->isReal()) { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 710 | while (bP < bPEnd) { |
| 711 | *tmpP-- = bP->conj(); |
| 712 | bP++; |
| 713 | } |
| 714 | } |
| 715 | else { |
| 716 | while (bP < bPEnd) { |
| 717 | *tmpP-- = bP->real(); |
| 718 | bP++; |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | return tmp; |
| 723 | } |
| 724 | |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 725 | bool vectorSlicer(SoftVector *x) |
| 726 | { |
| 727 | SoftVector::iterator xP = x->begin(); |
| 728 | SoftVector::iterator xPEnd = x->end(); |
| 729 | while (xP < xPEnd) { |
| 730 | *xP = 0.5 * (*xP + 1.0f); |
| 731 | if (*xP > 1.0) |
| 732 | *xP = 1.0; |
| 733 | if (*xP < 0.0) |
| 734 | *xP = 0.0; |
| 735 | xP++; |
| 736 | } |
| 737 | return true; |
| 738 | } |
| 739 | |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 740 | static signalVector *rotateBurst(const BitVector &wBurst, |
| 741 | int guardPeriodLength, int sps) |
| 742 | { |
| 743 | int burst_len; |
| 744 | signalVector *pulse, rotated, *shaped; |
| 745 | signalVector::iterator itr; |
| 746 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 747 | pulse = GSMPulse1->empty; |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 748 | burst_len = sps * (wBurst.size() + guardPeriodLength); |
| 749 | rotated = signalVector(burst_len); |
| 750 | itr = rotated.begin(); |
| 751 | |
| 752 | for (unsigned i = 0; i < wBurst.size(); i++) { |
| 753 | *itr = 2.0 * (wBurst[i] & 0x01) - 1.0; |
| 754 | itr += sps; |
| 755 | } |
| 756 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 757 | GMSKRotate(rotated, sps); |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 758 | rotated.isReal(false); |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 759 | |
| 760 | /* Dummy filter operation */ |
| 761 | shaped = convolve(&rotated, pulse, NULL, START_ONLY); |
| 762 | if (!shaped) |
| 763 | return NULL; |
| 764 | |
| 765 | return shaped; |
| 766 | } |
| 767 | |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 768 | static void rotateBurst2(signalVector &burst, double phase) |
| 769 | { |
| 770 | Complex<float> rot = Complex<float>(cos(phase), sin(phase)); |
| 771 | |
| 772 | for (size_t i = 0; i < burst.size(); i++) |
| 773 | burst[i] = burst[i] * rot; |
| 774 | } |
| 775 | |
Tom Tsou | 4dfd64a | 2016-03-06 20:31:51 -0800 | [diff] [blame] | 776 | /* |
| 777 | * Ignore the guard length argument in the GMSK modulator interface |
| 778 | * because it results in 624/628 sized bursts instead of the preferred |
| 779 | * burst length of 625. Only 4 SPS is supported. |
| 780 | */ |
| 781 | static signalVector *modulateBurstLaurent(const BitVector &bits) |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 782 | { |
Tom Tsou | 4dfd64a | 2016-03-06 20:31:51 -0800 | [diff] [blame] | 783 | int burst_len, sps = 4; |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 784 | float phase; |
Thomas Tsou | 6f4906e | 2013-11-09 02:40:18 -0500 | [diff] [blame] | 785 | signalVector *c0_pulse, *c1_pulse, *c0_burst; |
| 786 | signalVector *c1_burst, *c0_shaped, *c1_shaped; |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 787 | signalVector::iterator c0_itr, c1_itr; |
| 788 | |
Tom Tsou | 2079a3c | 2016-03-06 00:58:56 -0800 | [diff] [blame] | 789 | c0_pulse = GSMPulse4->c0; |
| 790 | c1_pulse = GSMPulse4->c1; |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 791 | |
Tom Tsou | 4dfd64a | 2016-03-06 20:31:51 -0800 | [diff] [blame] | 792 | if (bits.size() > 156) |
| 793 | return NULL; |
| 794 | |
| 795 | burst_len = 625; |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 796 | |
Thomas Tsou | 6f4906e | 2013-11-09 02:40:18 -0500 | [diff] [blame] | 797 | c0_burst = new signalVector(burst_len, c0_pulse->size()); |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 798 | c0_burst->isReal(true); |
Thomas Tsou | 6f4906e | 2013-11-09 02:40:18 -0500 | [diff] [blame] | 799 | c0_itr = c0_burst->begin(); |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 800 | |
Thomas Tsou | 6f4906e | 2013-11-09 02:40:18 -0500 | [diff] [blame] | 801 | c1_burst = new signalVector(burst_len, c1_pulse->size()); |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 802 | c1_burst->isReal(true); |
Thomas Tsou | 6f4906e | 2013-11-09 02:40:18 -0500 | [diff] [blame] | 803 | c1_itr = c1_burst->begin(); |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 804 | |
Tom Tsou | aa15d62 | 2016-08-11 14:36:23 -0700 | [diff] [blame] | 805 | /* Padded differential tail bits */ |
| 806 | *c0_itr = 2.0 * (0x00 & 0x01) - 1.0; |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 807 | c0_itr += sps; |
| 808 | |
| 809 | /* Main burst bits */ |
| 810 | for (unsigned i = 0; i < bits.size(); i++) { |
| 811 | *c0_itr = 2.0 * (bits[i] & 0x01) - 1.0; |
| 812 | c0_itr += sps; |
| 813 | } |
| 814 | |
Tom Tsou | aa15d62 | 2016-08-11 14:36:23 -0700 | [diff] [blame] | 815 | /* Padded differential tail bits */ |
| 816 | *c0_itr = 2.0 * (0x00 & 0x01) - 1.0; |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 817 | |
| 818 | /* Generate C0 phase coefficients */ |
Thomas Tsou | 6f4906e | 2013-11-09 02:40:18 -0500 | [diff] [blame] | 819 | GMSKRotate(*c0_burst, sps); |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 820 | c0_burst->isReal(false); |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 821 | |
Thomas Tsou | 6f4906e | 2013-11-09 02:40:18 -0500 | [diff] [blame] | 822 | c0_itr = c0_burst->begin(); |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 823 | c0_itr += sps * 2; |
| 824 | c1_itr += sps * 2; |
| 825 | |
| 826 | /* Start magic */ |
| 827 | phase = 2.0 * ((0x01 & 0x01) ^ (0x01 & 0x01)) - 1.0; |
| 828 | *c1_itr = *c0_itr * Complex<float>(0, phase); |
| 829 | c0_itr += sps; |
| 830 | c1_itr += sps; |
| 831 | |
| 832 | /* Generate C1 phase coefficients */ |
| 833 | for (unsigned i = 2; i < bits.size(); i++) { |
| 834 | phase = 2.0 * ((bits[i - 1] & 0x01) ^ (bits[i - 2] & 0x01)) - 1.0; |
| 835 | *c1_itr = *c0_itr * Complex<float>(0, phase); |
| 836 | |
| 837 | c0_itr += sps; |
| 838 | c1_itr += sps; |
| 839 | } |
| 840 | |
| 841 | /* End magic */ |
| 842 | int i = bits.size(); |
| 843 | phase = 2.0 * ((bits[i-1] & 0x01) ^ (bits[i-2] & 0x01)) - 1.0; |
| 844 | *c1_itr = *c0_itr * Complex<float>(0, phase); |
| 845 | |
| 846 | /* Primary (C0) and secondary (C1) pulse shaping */ |
Thomas Tsou | 6f4906e | 2013-11-09 02:40:18 -0500 | [diff] [blame] | 847 | c0_shaped = convolve(c0_burst, c0_pulse, NULL, START_ONLY); |
| 848 | c1_shaped = convolve(c1_burst, c1_pulse, NULL, START_ONLY); |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 849 | |
| 850 | /* Sum shaped outputs into C0 */ |
| 851 | c0_itr = c0_shaped->begin(); |
| 852 | c1_itr = c1_shaped->begin(); |
| 853 | for (unsigned i = 0; i < c0_shaped->size(); i++ ) |
| 854 | *c0_itr++ += *c1_itr++; |
| 855 | |
Thomas Tsou | 6f4906e | 2013-11-09 02:40:18 -0500 | [diff] [blame] | 856 | delete c0_burst; |
| 857 | delete c1_burst; |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 858 | delete c1_shaped; |
| 859 | |
| 860 | return c0_shaped; |
| 861 | } |
| 862 | |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 863 | static signalVector *rotateEdgeBurst(const signalVector &symbols, int sps) |
| 864 | { |
| 865 | signalVector *burst; |
| 866 | signalVector::iterator burst_itr; |
| 867 | |
| 868 | burst = new signalVector(symbols.size() * sps); |
| 869 | burst_itr = burst->begin(); |
| 870 | |
| 871 | for (size_t i = 0; i < symbols.size(); i++) { |
| 872 | float phase = i * 3.0f * M_PI / 8.0f; |
| 873 | Complex<float> rot = Complex<float>(cos(phase), sin(phase)); |
| 874 | |
| 875 | *burst_itr = symbols[i] * rot; |
| 876 | burst_itr += sps; |
| 877 | } |
| 878 | |
| 879 | return burst; |
| 880 | } |
| 881 | |
| 882 | static signalVector *derotateEdgeBurst(const signalVector &symbols, int sps) |
| 883 | { |
| 884 | signalVector *burst; |
| 885 | signalVector::iterator burst_itr; |
| 886 | |
| 887 | if (symbols.size() % sps) |
| 888 | return NULL; |
| 889 | |
| 890 | burst = new signalVector(symbols.size() / sps); |
| 891 | burst_itr = burst->begin(); |
| 892 | |
| 893 | for (size_t i = 0; i < burst->size(); i++) { |
| 894 | float phase = (float) (i % 16) * 3.0f * M_PI / 8.0f; |
| 895 | Complex<float> rot = Complex<float>(cosf(phase), -sinf(phase)); |
| 896 | |
| 897 | *burst_itr = symbols[sps * i] * rot; |
| 898 | burst_itr++; |
| 899 | } |
| 900 | |
| 901 | return burst; |
| 902 | } |
| 903 | |
| 904 | static signalVector *mapEdgeSymbols(const BitVector &bits) |
| 905 | { |
| 906 | if (bits.size() % 3) |
| 907 | return NULL; |
| 908 | |
| 909 | signalVector *symbols = new signalVector(bits.size() / 3); |
| 910 | |
| 911 | for (size_t i = 0; i < symbols->size(); i++) { |
| 912 | unsigned index = (((unsigned) bits[3 * i + 0] & 0x01) << 0) | |
| 913 | (((unsigned) bits[3 * i + 1] & 0x01) << 1) | |
| 914 | (((unsigned) bits[3 * i + 2] & 0x01) << 2); |
| 915 | |
| 916 | (*symbols)[i] = psk8_table[index]; |
| 917 | } |
| 918 | |
| 919 | return symbols; |
| 920 | } |
| 921 | |
Tom Tsou | d2b0703 | 2016-04-26 19:28:59 -0700 | [diff] [blame] | 922 | /* |
| 923 | * EDGE 8-PSK rotate and pulse shape |
| 924 | * |
| 925 | * Delay the EDGE downlink bursts by one symbol in order to match GMSK pulse |
| 926 | * shaping group delay. The difference in group delay arises from the dual |
| 927 | * pulse filter combination of the GMSK Laurent represenation whereas 8-PSK |
| 928 | * uses a single pulse linear filter. |
| 929 | */ |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 930 | static signalVector *shapeEdgeBurst(const signalVector &symbols) |
| 931 | { |
Tom Tsou | d2b0703 | 2016-04-26 19:28:59 -0700 | [diff] [blame] | 932 | size_t nsyms, nsamps = 625, sps = 4; |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 933 | signalVector *burst, *shape; |
| 934 | signalVector::iterator burst_itr; |
| 935 | |
| 936 | nsyms = symbols.size(); |
| 937 | |
Tom Tsou | d2b0703 | 2016-04-26 19:28:59 -0700 | [diff] [blame] | 938 | if (nsyms * sps > nsamps) |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 939 | nsyms = 156; |
| 940 | |
| 941 | burst = new signalVector(nsamps, GSMPulse4->c0->size()); |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 942 | |
Tom Tsou | d2b0703 | 2016-04-26 19:28:59 -0700 | [diff] [blame] | 943 | /* Delay burst by 1 symbol */ |
| 944 | burst_itr = burst->begin() + sps; |
Tom Tsou | 06676ea | 2016-07-19 12:50:21 -0700 | [diff] [blame] | 945 | for (size_t i = 0; i < nsyms; i++) { |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 946 | float phase = i * 3.0f * M_PI / 8.0f; |
| 947 | Complex<float> rot = Complex<float>(cos(phase), sin(phase)); |
| 948 | |
| 949 | *burst_itr = symbols[i] * rot; |
Tom Tsou | d2b0703 | 2016-04-26 19:28:59 -0700 | [diff] [blame] | 950 | burst_itr += sps; |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | /* Single Gaussian pulse approximation shaping */ |
| 954 | shape = convolve(burst, GSMPulse4->c0, NULL, START_ONLY); |
| 955 | delete burst; |
| 956 | |
| 957 | return shape; |
| 958 | } |
| 959 | |
| 960 | /* |
Tom Tsou | 8ee2f38 | 2016-03-06 20:57:34 -0800 | [diff] [blame] | 961 | * Generate a random GSM normal burst. |
| 962 | */ |
| 963 | signalVector *genRandNormalBurst(int tsc, int sps, int tn) |
| 964 | { |
| 965 | if ((tsc < 0) || (tsc > 7) || (tn < 0) || (tn > 7)) |
| 966 | return NULL; |
| 967 | if ((sps != 1) && (sps != 4)) |
| 968 | return NULL; |
| 969 | |
| 970 | int i = 0; |
| 971 | BitVector *bits = new BitVector(148); |
| 972 | signalVector *burst; |
| 973 | |
| 974 | /* Tail bits */ |
| 975 | for (; i < 4; i++) |
| 976 | (*bits)[i] = 0; |
| 977 | |
| 978 | /* Random bits */ |
| 979 | for (; i < 61; i++) |
| 980 | (*bits)[i] = rand() % 2; |
| 981 | |
| 982 | /* Training sequence */ |
| 983 | for (int n = 0; i < 87; i++, n++) |
| 984 | (*bits)[i] = gTrainingSequence[tsc][n]; |
| 985 | |
| 986 | /* Random bits */ |
| 987 | for (; i < 144; i++) |
| 988 | (*bits)[i] = rand() % 2; |
| 989 | |
| 990 | /* Tail bits */ |
| 991 | for (; i < 148; i++) |
| 992 | (*bits)[i] = 0; |
| 993 | |
| 994 | int guard = 8 + !(tn % 4); |
| 995 | burst = modulateBurst(*bits, guard, sps); |
| 996 | delete bits; |
| 997 | |
| 998 | return burst; |
| 999 | } |
| 1000 | |
Alexander Chemeris | 5efe050 | 2016-03-23 17:06:32 +0300 | [diff] [blame] | 1001 | /* |
| 1002 | * Generate a random GSM access burst. |
| 1003 | */ |
Alexander Chemeris | 37c52c7 | 2016-03-25 18:28:34 +0300 | [diff] [blame] | 1004 | signalVector *genRandAccessBurst(int delay, int sps, int tn) |
Alexander Chemeris | 5efe050 | 2016-03-23 17:06:32 +0300 | [diff] [blame] | 1005 | { |
| 1006 | if ((tn < 0) || (tn > 7)) |
| 1007 | return NULL; |
| 1008 | if ((sps != 1) && (sps != 4)) |
| 1009 | return NULL; |
Alexander Chemeris | 37c52c7 | 2016-03-25 18:28:34 +0300 | [diff] [blame] | 1010 | if (delay > 68) |
| 1011 | return NULL; |
Alexander Chemeris | 5efe050 | 2016-03-23 17:06:32 +0300 | [diff] [blame] | 1012 | |
| 1013 | int i = 0; |
Alexander Chemeris | 37c52c7 | 2016-03-25 18:28:34 +0300 | [diff] [blame] | 1014 | BitVector *bits = new BitVector(88+delay); |
Alexander Chemeris | 5efe050 | 2016-03-23 17:06:32 +0300 | [diff] [blame] | 1015 | signalVector *burst; |
| 1016 | |
Alexander Chemeris | 37c52c7 | 2016-03-25 18:28:34 +0300 | [diff] [blame] | 1017 | /* delay */ |
| 1018 | for (; i < delay; i++) |
| 1019 | (*bits)[i] = 0; |
| 1020 | |
Alexander Chemeris | 5efe050 | 2016-03-23 17:06:32 +0300 | [diff] [blame] | 1021 | /* head and synch bits */ |
Alexander Chemeris | 37c52c7 | 2016-03-25 18:28:34 +0300 | [diff] [blame] | 1022 | for (int n = 0; i < 49+delay; i++, n++) |
Alexander Chemeris | 5efe050 | 2016-03-23 17:06:32 +0300 | [diff] [blame] | 1023 | (*bits)[i] = gRACHBurst[n]; |
| 1024 | |
| 1025 | /* Random bits */ |
Alexander Chemeris | 37c52c7 | 2016-03-25 18:28:34 +0300 | [diff] [blame] | 1026 | for (; i < 85+delay; i++) |
Alexander Chemeris | 5efe050 | 2016-03-23 17:06:32 +0300 | [diff] [blame] | 1027 | (*bits)[i] = rand() % 2; |
| 1028 | |
| 1029 | /* Tail bits */ |
Alexander Chemeris | 37c52c7 | 2016-03-25 18:28:34 +0300 | [diff] [blame] | 1030 | for (; i < 88+delay; i++) |
Alexander Chemeris | 5efe050 | 2016-03-23 17:06:32 +0300 | [diff] [blame] | 1031 | (*bits)[i] = 0; |
| 1032 | |
Alexander Chemeris | 37c52c7 | 2016-03-25 18:28:34 +0300 | [diff] [blame] | 1033 | int guard = 68-delay + !(tn % 4); |
Alexander Chemeris | 5efe050 | 2016-03-23 17:06:32 +0300 | [diff] [blame] | 1034 | burst = modulateBurst(*bits, guard, sps); |
| 1035 | delete bits; |
| 1036 | |
| 1037 | return burst; |
| 1038 | } |
| 1039 | |
Tom Tsou | 8ee2f38 | 2016-03-06 20:57:34 -0800 | [diff] [blame] | 1040 | signalVector *generateEmptyBurst(int sps, int tn) |
| 1041 | { |
| 1042 | if ((tn < 0) || (tn > 7)) |
| 1043 | return NULL; |
| 1044 | |
| 1045 | if (sps == 4) |
| 1046 | return new signalVector(625); |
| 1047 | else if (sps == 1) |
| 1048 | return new signalVector(148 + 8 + !(tn % 4)); |
| 1049 | else |
| 1050 | return NULL; |
| 1051 | } |
| 1052 | |
| 1053 | signalVector *generateDummyBurst(int sps, int tn) |
| 1054 | { |
| 1055 | if (((sps != 1) && (sps != 4)) || (tn < 0) || (tn > 7)) |
| 1056 | return NULL; |
| 1057 | |
| 1058 | return modulateBurst(gDummyBurst, 8 + !(tn % 4), sps); |
| 1059 | } |
| 1060 | |
| 1061 | /* |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 1062 | * Generate a random 8-PSK EDGE burst. Only 4 SPS is supported with |
| 1063 | * the returned burst being 625 samples in length. |
| 1064 | */ |
| 1065 | signalVector *generateEdgeBurst(int tsc) |
| 1066 | { |
| 1067 | int tail = 9 / 3; |
| 1068 | int data = 174 / 3; |
| 1069 | int train = 78 / 3; |
| 1070 | |
| 1071 | if ((tsc < 0) || (tsc > 7)) |
| 1072 | return NULL; |
| 1073 | |
| 1074 | signalVector *shape, *burst = new signalVector(148); |
| 1075 | const BitVector *midamble = &gEdgeTrainingSequence[tsc]; |
| 1076 | |
| 1077 | /* Tail */ |
| 1078 | int n, i = 0; |
| 1079 | for (; i < tail; i++) |
| 1080 | (*burst)[i] = psk8_table[7]; |
| 1081 | |
| 1082 | /* Body */ |
| 1083 | for (; i < tail + data; i++) |
| 1084 | (*burst)[i] = psk8_table[rand() % 8]; |
| 1085 | |
| 1086 | /* TSC */ |
| 1087 | for (n = 0; i < tail + data + train; i++, n++) { |
| 1088 | unsigned index = (((unsigned) (*midamble)[3 * n + 0] & 0x01) << 0) | |
| 1089 | (((unsigned) (*midamble)[3 * n + 1] & 0x01) << 1) | |
| 1090 | (((unsigned) (*midamble)[3 * n + 2] & 0x01) << 2); |
| 1091 | |
| 1092 | (*burst)[i] = psk8_table[index]; |
| 1093 | } |
| 1094 | |
| 1095 | /* Body */ |
| 1096 | for (; i < tail + data + train + data; i++) |
| 1097 | (*burst)[i] = psk8_table[rand() % 8]; |
| 1098 | |
| 1099 | /* Tail */ |
| 1100 | for (; i < tail + data + train + data + tail; i++) |
| 1101 | (*burst)[i] = psk8_table[7]; |
| 1102 | |
| 1103 | shape = shapeEdgeBurst(*burst); |
| 1104 | delete burst; |
| 1105 | |
| 1106 | return shape; |
| 1107 | } |
| 1108 | |
| 1109 | /* |
| 1110 | * Modulate 8-PSK burst. When empty pulse shaping (rotation only) |
| 1111 | * is enabled, the output vector length will be bit sequence length |
| 1112 | * times the SPS value. When pulse shaping is enabled, the output |
Alexander Chemeris | 9270a5a | 2017-03-17 13:03:41 -0700 | [diff] [blame] | 1113 | * vector length is fixed at 625 samples (156.25 symbols at 4 SPS). |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 1114 | * Pulse shaped bit sequences that go beyond one burst are truncated. |
| 1115 | * Pulse shaping at anything but 4 SPS is not supported. |
| 1116 | */ |
| 1117 | signalVector *modulateEdgeBurst(const BitVector &bits, |
| 1118 | int sps, bool empty) |
| 1119 | { |
| 1120 | signalVector *shape, *burst; |
| 1121 | |
| 1122 | if ((sps != 4) && !empty) |
| 1123 | return NULL; |
| 1124 | |
| 1125 | burst = mapEdgeSymbols(bits); |
| 1126 | if (!burst) |
| 1127 | return NULL; |
| 1128 | |
| 1129 | if (empty) |
| 1130 | shape = rotateEdgeBurst(*burst, sps); |
| 1131 | else |
| 1132 | shape = shapeEdgeBurst(*burst); |
| 1133 | |
| 1134 | delete burst; |
| 1135 | return shape; |
| 1136 | } |
| 1137 | |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 1138 | static signalVector *modulateBurstBasic(const BitVector &bits, |
| 1139 | int guard_len, int sps) |
| 1140 | { |
| 1141 | int burst_len; |
Thomas Tsou | 6f4906e | 2013-11-09 02:40:18 -0500 | [diff] [blame] | 1142 | signalVector *pulse, *burst, *shaped; |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 1143 | signalVector::iterator burst_itr; |
| 1144 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 1145 | if (sps == 1) |
| 1146 | pulse = GSMPulse1->c0; |
| 1147 | else |
Tom Tsou | 2079a3c | 2016-03-06 00:58:56 -0800 | [diff] [blame] | 1148 | pulse = GSMPulse4->c0; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 1149 | |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 1150 | burst_len = sps * (bits.size() + guard_len); |
| 1151 | |
Thomas Tsou | 6f4906e | 2013-11-09 02:40:18 -0500 | [diff] [blame] | 1152 | burst = new signalVector(burst_len, pulse->size()); |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 1153 | burst->isReal(true); |
Thomas Tsou | 6f4906e | 2013-11-09 02:40:18 -0500 | [diff] [blame] | 1154 | burst_itr = burst->begin(); |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 1155 | |
| 1156 | /* Raw bits are not differentially encoded */ |
| 1157 | for (unsigned i = 0; i < bits.size(); i++) { |
| 1158 | *burst_itr = 2.0 * (bits[i] & 0x01) - 1.0; |
| 1159 | burst_itr += sps; |
| 1160 | } |
| 1161 | |
Thomas Tsou | 6f4906e | 2013-11-09 02:40:18 -0500 | [diff] [blame] | 1162 | GMSKRotate(*burst, sps); |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 1163 | burst->isReal(false); |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 1164 | |
| 1165 | /* Single Gaussian pulse approximation shaping */ |
Thomas Tsou | 6f4906e | 2013-11-09 02:40:18 -0500 | [diff] [blame] | 1166 | shaped = convolve(burst, pulse, NULL, START_ONLY); |
| 1167 | |
| 1168 | delete burst; |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 1169 | |
| 1170 | return shaped; |
| 1171 | } |
| 1172 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1173 | /* Assume input bits are not differentially encoded */ |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 1174 | signalVector *modulateBurst(const BitVector &wBurst, int guardPeriodLength, |
| 1175 | int sps, bool emptyPulse) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1176 | { |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 1177 | if (emptyPulse) |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 1178 | return rotateBurst(wBurst, guardPeriodLength, sps); |
| 1179 | else if (sps == 4) |
Tom Tsou | 4dfd64a | 2016-03-06 20:31:51 -0800 | [diff] [blame] | 1180 | return modulateBurstLaurent(wBurst); |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 1181 | else |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 1182 | return modulateBurstBasic(wBurst, guardPeriodLength, sps); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
Tom Tsou | 2079a3c | 2016-03-06 00:58:56 -0800 | [diff] [blame] | 1185 | static void generateSincTable() |
Thomas Tsou | 0e0e1f4 | 2013-11-09 22:08:51 -0500 | [diff] [blame] | 1186 | { |
| 1187 | float x; |
| 1188 | |
| 1189 | for (int i = 0; i < TABLESIZE; i++) { |
| 1190 | x = (float) i / TABLESIZE * 8 * M_PI; |
| 1191 | if (fabs(x) < 0.01) { |
| 1192 | sincTable[i] = 1.0f; |
| 1193 | continue; |
| 1194 | } |
| 1195 | |
| 1196 | sincTable[i] = sinf(x) / x; |
| 1197 | } |
| 1198 | } |
| 1199 | |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 1200 | float sinc(float x) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1201 | { |
Thomas Tsou | 0e0e1f4 | 2013-11-09 22:08:51 -0500 | [diff] [blame] | 1202 | if (fabs(x) >= 8 * M_PI) |
| 1203 | return 0.0; |
| 1204 | |
| 1205 | int index = (int) floorf(fabs(x) / (8 * M_PI) * TABLESIZE); |
| 1206 | |
| 1207 | return sincTable[index]; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
Thomas Tsou | f79c4d0 | 2013-11-09 15:51:56 -0600 | [diff] [blame] | 1210 | /* |
| 1211 | * Create fractional delay filterbank with Blackman-harris windowed |
| 1212 | * sinc function generator. The number of filters generated is specified |
| 1213 | * by the DELAYFILTS value. |
| 1214 | */ |
| 1215 | void generateDelayFilters() |
| 1216 | { |
| 1217 | int h_len = 20; |
| 1218 | complex *data; |
| 1219 | signalVector *h; |
| 1220 | signalVector::iterator itr; |
| 1221 | |
| 1222 | float k, sum; |
| 1223 | float a0 = 0.35875; |
| 1224 | float a1 = 0.48829; |
| 1225 | float a2 = 0.14128; |
| 1226 | float a3 = 0.01168; |
| 1227 | |
| 1228 | for (int i = 0; i < DELAYFILTS; i++) { |
| 1229 | data = (complex *) convolve_h_alloc(h_len); |
| 1230 | h = new signalVector(data, 0, h_len); |
| 1231 | h->setAligned(true); |
| 1232 | h->isReal(true); |
| 1233 | |
| 1234 | sum = 0.0; |
| 1235 | itr = h->end(); |
| 1236 | for (int n = 0; n < h_len; n++) { |
| 1237 | k = (float) n; |
| 1238 | *--itr = (complex) sinc(M_PI_F * |
| 1239 | (k - (float) h_len / 2.0 - (float) i / DELAYFILTS)); |
| 1240 | *itr *= a0 - |
| 1241 | a1 * cos(2 * M_PI * n / (h_len - 1)) + |
| 1242 | a2 * cos(4 * M_PI * n / (h_len - 1)) - |
| 1243 | a3 * cos(6 * M_PI * n / (h_len - 1)); |
| 1244 | |
| 1245 | sum += itr->real(); |
| 1246 | } |
| 1247 | |
| 1248 | itr = h->begin(); |
| 1249 | for (int n = 0; n < h_len; n++) |
| 1250 | *itr++ /= sum; |
| 1251 | |
| 1252 | delayFilters[i] = h; |
| 1253 | } |
| 1254 | } |
| 1255 | |
Alexander Chemeris | e0c1218 | 2017-03-18 13:27:48 -0700 | [diff] [blame] | 1256 | signalVector *delayVector(const signalVector *in, signalVector *out, float delay) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1257 | { |
Thomas Tsou | f79c4d0 | 2013-11-09 15:51:56 -0600 | [diff] [blame] | 1258 | int whole, index; |
Thomas Tsou | 2c282f5 | 2013-10-08 21:34:35 -0400 | [diff] [blame] | 1259 | float frac; |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 1260 | signalVector *h, *shift, *fshift = NULL; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1261 | |
Thomas Tsou | 2c282f5 | 2013-10-08 21:34:35 -0400 | [diff] [blame] | 1262 | whole = floor(delay); |
| 1263 | frac = delay - whole; |
| 1264 | |
| 1265 | /* Sinc interpolated fractional shift (if allowable) */ |
| 1266 | if (fabs(frac) > 1e-2) { |
Thomas Tsou | f79c4d0 | 2013-11-09 15:51:56 -0600 | [diff] [blame] | 1267 | index = floorf(frac * (float) DELAYFILTS); |
| 1268 | h = delayFilters[index]; |
Thomas Tsou | 2c282f5 | 2013-10-08 21:34:35 -0400 | [diff] [blame] | 1269 | |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 1270 | fshift = convolve(in, h, NULL, NO_DELAY); |
| 1271 | if (!fshift) |
| 1272 | return NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 1275 | if (!fshift) |
| 1276 | shift = new signalVector(*in); |
| 1277 | else |
| 1278 | shift = fshift; |
| 1279 | |
Thomas Tsou | 2c282f5 | 2013-10-08 21:34:35 -0400 | [diff] [blame] | 1280 | /* Integer sample shift */ |
| 1281 | if (whole < 0) { |
| 1282 | whole = -whole; |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 1283 | signalVector::iterator wBurstItr = shift->begin(); |
| 1284 | signalVector::iterator shiftedItr = shift->begin() + whole; |
Thomas Tsou | 2c282f5 | 2013-10-08 21:34:35 -0400 | [diff] [blame] | 1285 | |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 1286 | while (shiftedItr < shift->end()) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1287 | *wBurstItr++ = *shiftedItr++; |
Thomas Tsou | 2c282f5 | 2013-10-08 21:34:35 -0400 | [diff] [blame] | 1288 | |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 1289 | while (wBurstItr < shift->end()) |
| 1290 | *wBurstItr++ = 0.0; |
| 1291 | } else if (whole >= 0) { |
| 1292 | signalVector::iterator wBurstItr = shift->end() - 1; |
| 1293 | signalVector::iterator shiftedItr = shift->end() - 1 - whole; |
| 1294 | |
| 1295 | while (shiftedItr >= shift->begin()) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1296 | *wBurstItr-- = *shiftedItr--; |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 1297 | |
| 1298 | while (wBurstItr >= shift->begin()) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1299 | *wBurstItr-- = 0.0; |
| 1300 | } |
Thomas Tsou | 2c282f5 | 2013-10-08 21:34:35 -0400 | [diff] [blame] | 1301 | |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 1302 | if (!out) |
| 1303 | return shift; |
| 1304 | |
| 1305 | out->clone(*shift); |
| 1306 | delete shift; |
| 1307 | return out; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1308 | } |
Thomas Tsou | 2c282f5 | 2013-10-08 21:34:35 -0400 | [diff] [blame] | 1309 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1310 | signalVector *gaussianNoise(int length, |
| 1311 | float variance, |
| 1312 | complex mean) |
| 1313 | { |
| 1314 | |
| 1315 | signalVector *noise = new signalVector(length); |
| 1316 | signalVector::iterator nPtr = noise->begin(); |
| 1317 | float stddev = sqrtf(variance); |
| 1318 | while (nPtr < noise->end()) { |
| 1319 | float u1 = (float) rand()/ (float) RAND_MAX; |
| 1320 | while (u1==0.0) |
| 1321 | u1 = (float) rand()/ (float) RAND_MAX; |
| 1322 | float u2 = (float) rand()/ (float) RAND_MAX; |
| 1323 | float arg = 2.0*M_PI*u2; |
| 1324 | *nPtr = mean + stddev*complex(cos(arg),sin(arg))*sqrtf(-2.0*log(u1)); |
| 1325 | nPtr++; |
| 1326 | } |
| 1327 | |
| 1328 | return noise; |
| 1329 | } |
| 1330 | |
| 1331 | complex interpolatePoint(const signalVector &inSig, |
| 1332 | float ix) |
| 1333 | { |
| 1334 | |
| 1335 | int start = (int) (floor(ix) - 10); |
| 1336 | if (start < 0) start = 0; |
| 1337 | int end = (int) (floor(ix) + 11); |
| 1338 | if ((unsigned) end > inSig.size()-1) end = inSig.size()-1; |
| 1339 | |
| 1340 | complex pVal = 0.0; |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 1341 | if (!inSig.isReal()) { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1342 | for (int i = start; i < end; i++) |
| 1343 | pVal += inSig[i] * sinc(M_PI_F*(i-ix)); |
| 1344 | } |
| 1345 | else { |
| 1346 | for (int i = start; i < end; i++) |
| 1347 | pVal += inSig[i].real() * sinc(M_PI_F*(i-ix)); |
| 1348 | } |
| 1349 | |
| 1350 | return pVal; |
| 1351 | } |
| 1352 | |
Thomas Tsou | 8181b01 | 2013-08-20 21:17:19 -0400 | [diff] [blame] | 1353 | static complex fastPeakDetect(const signalVector &rxBurst, float *index) |
| 1354 | { |
| 1355 | float val, max = 0.0f; |
| 1356 | complex amp; |
| 1357 | int _index = -1; |
| 1358 | |
Thomas Tsou | 3f32ab5 | 2013-11-15 16:32:54 -0500 | [diff] [blame] | 1359 | for (size_t i = 0; i < rxBurst.size(); i++) { |
Thomas Tsou | 8181b01 | 2013-08-20 21:17:19 -0400 | [diff] [blame] | 1360 | val = rxBurst[i].norm2(); |
| 1361 | if (val > max) { |
| 1362 | max = val; |
| 1363 | _index = i; |
| 1364 | amp = rxBurst[i]; |
| 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | if (index) |
| 1369 | *index = (float) _index; |
| 1370 | |
| 1371 | return amp; |
| 1372 | } |
| 1373 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1374 | complex peakDetect(const signalVector &rxBurst, |
| 1375 | float *peakIndex, |
| 1376 | float *avgPwr) |
| 1377 | { |
| 1378 | |
| 1379 | |
| 1380 | complex maxVal = 0.0; |
| 1381 | float maxIndex = -1; |
| 1382 | float sumPower = 0.0; |
| 1383 | |
| 1384 | for (unsigned int i = 0; i < rxBurst.size(); i++) { |
| 1385 | float samplePower = rxBurst[i].norm2(); |
| 1386 | if (samplePower > maxVal.real()) { |
| 1387 | maxVal = samplePower; |
| 1388 | maxIndex = i; |
| 1389 | } |
| 1390 | sumPower += samplePower; |
| 1391 | } |
| 1392 | |
| 1393 | // interpolate around the peak |
| 1394 | // to save computation, we'll use early-late balancing |
| 1395 | float earlyIndex = maxIndex-1; |
| 1396 | float lateIndex = maxIndex+1; |
| 1397 | |
| 1398 | float incr = 0.5; |
| 1399 | while (incr > 1.0/1024.0) { |
| 1400 | complex earlyP = interpolatePoint(rxBurst,earlyIndex); |
| 1401 | complex lateP = interpolatePoint(rxBurst,lateIndex); |
| 1402 | if (earlyP < lateP) |
| 1403 | earlyIndex += incr; |
| 1404 | else if (earlyP > lateP) |
| 1405 | earlyIndex -= incr; |
| 1406 | else break; |
| 1407 | incr /= 2.0; |
| 1408 | lateIndex = earlyIndex + 2.0; |
| 1409 | } |
| 1410 | |
| 1411 | maxIndex = earlyIndex + 1.0; |
| 1412 | maxVal = interpolatePoint(rxBurst,maxIndex); |
| 1413 | |
| 1414 | if (peakIndex!=NULL) |
| 1415 | *peakIndex = maxIndex; |
| 1416 | |
| 1417 | if (avgPwr!=NULL) |
| 1418 | *avgPwr = (sumPower-maxVal.norm2()) / (rxBurst.size()-1); |
| 1419 | |
| 1420 | return maxVal; |
| 1421 | |
| 1422 | } |
| 1423 | |
| 1424 | void scaleVector(signalVector &x, |
| 1425 | complex scale) |
| 1426 | { |
Thomas Tsou | 7e4e536 | 2013-10-30 21:18:55 -0400 | [diff] [blame] | 1427 | #ifdef HAVE_NEON |
| 1428 | int len = x.size(); |
| 1429 | |
| 1430 | scale_complex((float *) x.begin(), |
| 1431 | (float *) x.begin(), |
| 1432 | (float *) &scale, len); |
| 1433 | #else |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1434 | signalVector::iterator xP = x.begin(); |
| 1435 | signalVector::iterator xPEnd = x.end(); |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 1436 | if (!x.isReal()) { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1437 | while (xP < xPEnd) { |
| 1438 | *xP = *xP * scale; |
| 1439 | xP++; |
| 1440 | } |
| 1441 | } |
| 1442 | else { |
| 1443 | while (xP < xPEnd) { |
| 1444 | *xP = xP->real() * scale; |
| 1445 | xP++; |
| 1446 | } |
| 1447 | } |
Thomas Tsou | 7e4e536 | 2013-10-30 21:18:55 -0400 | [diff] [blame] | 1448 | #endif |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | /** in-place conjugation */ |
| 1452 | void conjugateVector(signalVector &x) |
| 1453 | { |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 1454 | if (x.isReal()) return; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1455 | signalVector::iterator xP = x.begin(); |
| 1456 | signalVector::iterator xPEnd = x.end(); |
| 1457 | while (xP < xPEnd) { |
| 1458 | *xP = xP->conj(); |
| 1459 | xP++; |
| 1460 | } |
| 1461 | } |
| 1462 | |
| 1463 | |
| 1464 | // in-place addition!! |
| 1465 | bool addVector(signalVector &x, |
| 1466 | signalVector &y) |
| 1467 | { |
| 1468 | signalVector::iterator xP = x.begin(); |
| 1469 | signalVector::iterator yP = y.begin(); |
| 1470 | signalVector::iterator xPEnd = x.end(); |
| 1471 | signalVector::iterator yPEnd = y.end(); |
| 1472 | while ((xP < xPEnd) && (yP < yPEnd)) { |
| 1473 | *xP = *xP + *yP; |
| 1474 | xP++; yP++; |
| 1475 | } |
| 1476 | return true; |
| 1477 | } |
| 1478 | |
| 1479 | // in-place multiplication!! |
| 1480 | bool multVector(signalVector &x, |
| 1481 | signalVector &y) |
| 1482 | { |
| 1483 | signalVector::iterator xP = x.begin(); |
| 1484 | signalVector::iterator yP = y.begin(); |
| 1485 | signalVector::iterator xPEnd = x.end(); |
| 1486 | signalVector::iterator yPEnd = y.end(); |
| 1487 | while ((xP < xPEnd) && (yP < yPEnd)) { |
| 1488 | *xP = (*xP) * (*yP); |
| 1489 | xP++; yP++; |
| 1490 | } |
| 1491 | return true; |
| 1492 | } |
| 1493 | |
Tom Tsou | 2079a3c | 2016-03-06 00:58:56 -0800 | [diff] [blame] | 1494 | static bool generateMidamble(int sps, int tsc) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1495 | { |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1496 | bool status = true; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 1497 | float toa; |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1498 | complex *data = NULL; |
| 1499 | signalVector *autocorr = NULL, *midamble = NULL; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1500 | signalVector *midMidamble = NULL, *_midMidamble = NULL; |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1501 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1502 | if ((tsc < 0) || (tsc > 7)) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1503 | return false; |
| 1504 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1505 | delete gMidambles[tsc]; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1506 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1507 | /* Use middle 16 bits of each TSC. Correlation sequence is not pulse shaped */ |
| 1508 | midMidamble = modulateBurst(gTrainingSequence[tsc].segment(5,16), 0, sps, true); |
| 1509 | if (!midMidamble) |
| 1510 | return false; |
| 1511 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1512 | /* Simulated receive sequence is pulse shaped */ |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1513 | midamble = modulateBurst(gTrainingSequence[tsc], 0, sps, false); |
| 1514 | if (!midamble) { |
| 1515 | status = false; |
| 1516 | goto release; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1517 | } |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1518 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1519 | // NOTE: Because ideal TSC 16-bit midamble is 66 symbols into burst, |
| 1520 | // the ideal TSC has an + 180 degree phase shift, |
| 1521 | // due to the pi/2 frequency shift, that |
| 1522 | // needs to be accounted for. |
| 1523 | // 26-midamble is 61 symbols into burst, has +90 degree phase shift. |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1524 | scaleVector(*midMidamble, complex(-1.0, 0.0)); |
| 1525 | scaleVector(*midamble, complex(0.0, 1.0)); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1526 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1527 | conjugateVector(*midMidamble); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1528 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1529 | /* For SSE alignment, reallocate the midamble sequence on 16-byte boundary */ |
| 1530 | data = (complex *) convolve_h_alloc(midMidamble->size()); |
| 1531 | _midMidamble = new signalVector(data, 0, midMidamble->size()); |
| 1532 | _midMidamble->setAligned(true); |
| 1533 | memcpy(_midMidamble->begin(), midMidamble->begin(), |
| 1534 | midMidamble->size() * sizeof(complex)); |
| 1535 | |
| 1536 | autocorr = convolve(midamble, _midMidamble, NULL, NO_DELAY); |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1537 | if (!autocorr) { |
| 1538 | status = false; |
| 1539 | goto release; |
| 1540 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1541 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1542 | gMidambles[tsc] = new CorrelationSequence; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1543 | gMidambles[tsc]->buffer = data; |
| 1544 | gMidambles[tsc]->sequence = _midMidamble; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 1545 | gMidambles[tsc]->gain = peakDetect(*autocorr, &toa, NULL); |
| 1546 | |
| 1547 | /* For 1 sps only |
| 1548 | * (Half of correlation length - 1) + midpoint of pulse shape + remainder |
| 1549 | * 13.5 = (16 / 2 - 1) + 1.5 + (26 - 10) / 2 |
| 1550 | */ |
| 1551 | if (sps == 1) |
| 1552 | gMidambles[tsc]->toa = toa - 13.5; |
| 1553 | else |
| 1554 | gMidambles[tsc]->toa = 0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1555 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1556 | release: |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1557 | delete autocorr; |
| 1558 | delete midamble; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1559 | delete midMidamble; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1560 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1561 | if (!status) { |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1562 | delete _midMidamble; |
| 1563 | free(data); |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1564 | gMidambles[tsc] = NULL; |
| 1565 | } |
| 1566 | |
| 1567 | return status; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1568 | } |
| 1569 | |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 1570 | CorrelationSequence *generateEdgeMidamble(int tsc) |
| 1571 | { |
| 1572 | complex *data = NULL; |
| 1573 | signalVector *midamble = NULL, *_midamble = NULL; |
| 1574 | CorrelationSequence *seq; |
| 1575 | |
| 1576 | if ((tsc < 0) || (tsc > 7)) |
| 1577 | return NULL; |
| 1578 | |
| 1579 | /* Use middle 48 bits of each TSC. Correlation sequence is not pulse shaped */ |
| 1580 | const BitVector *bits = &gEdgeTrainingSequence[tsc]; |
| 1581 | midamble = modulateEdgeBurst(bits->segment(15, 48), 1, true); |
| 1582 | if (!midamble) |
| 1583 | return NULL; |
| 1584 | |
| 1585 | conjugateVector(*midamble); |
| 1586 | |
| 1587 | data = (complex *) convolve_h_alloc(midamble->size()); |
| 1588 | _midamble = new signalVector(data, 0, midamble->size()); |
| 1589 | _midamble->setAligned(true); |
| 1590 | memcpy(_midamble->begin(), midamble->begin(), |
| 1591 | midamble->size() * sizeof(complex)); |
| 1592 | |
| 1593 | /* Channel gain is an empirically measured value */ |
| 1594 | seq = new CorrelationSequence; |
| 1595 | seq->buffer = data; |
| 1596 | seq->sequence = _midamble; |
| 1597 | seq->gain = Complex<float>(-19.6432, 19.5006) / 1.18; |
| 1598 | seq->toa = 0; |
| 1599 | |
| 1600 | delete midamble; |
| 1601 | |
| 1602 | return seq; |
| 1603 | } |
| 1604 | |
Tom Tsou | 2079a3c | 2016-03-06 00:58:56 -0800 | [diff] [blame] | 1605 | static bool generateRACHSequence(int sps) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1606 | { |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1607 | bool status = true; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 1608 | float toa; |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1609 | complex *data = NULL; |
| 1610 | signalVector *autocorr = NULL; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1611 | signalVector *seq0 = NULL, *seq1 = NULL, *_seq1 = NULL; |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1612 | |
| 1613 | delete gRACHSequence; |
| 1614 | |
| 1615 | seq0 = modulateBurst(gRACHSynchSequence, 0, sps, false); |
| 1616 | if (!seq0) |
| 1617 | return false; |
| 1618 | |
| 1619 | seq1 = modulateBurst(gRACHSynchSequence.segment(0, 40), 0, sps, true); |
| 1620 | if (!seq1) { |
| 1621 | status = false; |
| 1622 | goto release; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1623 | } |
| 1624 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1625 | conjugateVector(*seq1); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1626 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1627 | /* For SSE alignment, reallocate the midamble sequence on 16-byte boundary */ |
| 1628 | data = (complex *) convolve_h_alloc(seq1->size()); |
| 1629 | _seq1 = new signalVector(data, 0, seq1->size()); |
| 1630 | _seq1->setAligned(true); |
| 1631 | memcpy(_seq1->begin(), seq1->begin(), seq1->size() * sizeof(complex)); |
| 1632 | |
| 1633 | autocorr = convolve(seq0, _seq1, autocorr, NO_DELAY); |
| 1634 | if (!autocorr) { |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1635 | status = false; |
| 1636 | goto release; |
| 1637 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1638 | |
| 1639 | gRACHSequence = new CorrelationSequence; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1640 | gRACHSequence->sequence = _seq1; |
| 1641 | gRACHSequence->buffer = data; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 1642 | gRACHSequence->gain = peakDetect(*autocorr, &toa, NULL); |
| 1643 | |
| 1644 | /* For 1 sps only |
| 1645 | * (Half of correlation length - 1) + midpoint of pulse shaping filer |
| 1646 | * 20.5 = (40 / 2 - 1) + 1.5 |
| 1647 | */ |
| 1648 | if (sps == 1) |
| 1649 | gRACHSequence->toa = toa - 20.5; |
| 1650 | else |
| 1651 | gRACHSequence->toa = 0.0; |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1652 | |
| 1653 | release: |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1654 | delete autocorr; |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1655 | delete seq0; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1656 | delete seq1; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1657 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1658 | if (!status) { |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1659 | delete _seq1; |
| 1660 | free(data); |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1661 | gRACHSequence = NULL; |
| 1662 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1663 | |
Thomas Tsou | e5dcfc4 | 2013-08-20 16:27:12 -0400 | [diff] [blame] | 1664 | return status; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1665 | } |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1666 | |
Tom Tsou | a84e162 | 2016-06-29 14:50:25 -0700 | [diff] [blame] | 1667 | /* |
| 1668 | * Peak-to-average computation +/- range from peak in symbols |
| 1669 | */ |
| 1670 | #define COMPUTE_PEAK_MIN 2 |
| 1671 | #define COMPUTE_PEAK_MAX 5 |
| 1672 | |
| 1673 | /* |
| 1674 | * Minimum number of values needed to compute peak-to-average |
| 1675 | */ |
| 1676 | #define COMPUTE_PEAK_CNT 5 |
| 1677 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1678 | static float computePeakRatio(signalVector *corr, |
| 1679 | int sps, float toa, complex amp) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1680 | { |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1681 | int num = 0; |
| 1682 | complex *peak; |
| 1683 | float rms, avg = 0.0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1684 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1685 | /* Check for bogus results */ |
| 1686 | if ((toa < 0.0) || (toa > corr->size())) |
| 1687 | return 0.0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1688 | |
Alexander Chemeris | 1e9b4d5 | 2015-06-04 19:05:28 -0400 | [diff] [blame] | 1689 | peak = corr->begin() + (int) rint(toa); |
| 1690 | |
Tom Tsou | a84e162 | 2016-06-29 14:50:25 -0700 | [diff] [blame] | 1691 | for (int i = COMPUTE_PEAK_MIN * sps; i <= COMPUTE_PEAK_MAX * sps; i++) { |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1692 | if (peak - i >= corr->begin()) { |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1693 | avg += (peak - i)->norm2(); |
| 1694 | num++; |
| 1695 | } |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1696 | if (peak + i < corr->end()) { |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1697 | avg += (peak + i)->norm2(); |
| 1698 | num++; |
| 1699 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1700 | } |
| 1701 | |
Tom Tsou | a84e162 | 2016-06-29 14:50:25 -0700 | [diff] [blame] | 1702 | if (num < COMPUTE_PEAK_CNT) |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1703 | return 0.0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1704 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1705 | rms = sqrtf(avg / (float) num) + 0.00001; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1706 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1707 | return (amp.abs()) / rms; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
Alexander Chemeris | 1470fcd | 2017-03-17 22:35:02 -0700 | [diff] [blame] | 1710 | float energyDetect(const signalVector &rxBurst, unsigned windowLength) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1711 | { |
| 1712 | |
| 1713 | signalVector::const_iterator windowItr = rxBurst.begin(); //+rxBurst.size()/2 - 5*windowLength/2; |
| 1714 | float energy = 0.0; |
Tom Tsou | 2af1440 | 2017-03-23 14:54:00 -0700 | [diff] [blame] | 1715 | if (windowLength == 0) return 0.0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1716 | if (windowLength > rxBurst.size()) windowLength = rxBurst.size(); |
| 1717 | for (unsigned i = 0; i < windowLength; i++) { |
| 1718 | energy += windowItr->norm2(); |
| 1719 | windowItr+=4; |
| 1720 | } |
Alexander Chemeris | 1dd05cf | 2017-03-15 23:23:36 +0300 | [diff] [blame] | 1721 | return energy/windowLength; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1722 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1723 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1724 | /* |
| 1725 | * Detect a burst based on correlation and peak-to-average ratio |
| 1726 | * |
| 1727 | * For one sampler-per-symbol, perform fast peak detection (no interpolation) |
| 1728 | * for initial gating. We do this because energy detection should be disabled. |
| 1729 | * For higher oversampling values, we assume the energy detector is in place |
| 1730 | * and we run full interpolating peak detection. |
| 1731 | */ |
Alexander Chemeris | 1470fcd | 2017-03-17 22:35:02 -0700 | [diff] [blame] | 1732 | static int detectBurst(const signalVector &burst, |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1733 | signalVector &corr, CorrelationSequence *sync, |
| 1734 | float thresh, int sps, complex *amp, float *toa, |
| 1735 | int start, int len) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1736 | { |
Alexander Chemeris | 1470fcd | 2017-03-17 22:35:02 -0700 | [diff] [blame] | 1737 | const signalVector *corr_in; |
| 1738 | signalVector *dec = NULL; |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 1739 | |
| 1740 | if (sps == 4) { |
| 1741 | dec = downsampleBurst(burst); |
| 1742 | corr_in = dec; |
| 1743 | sps = 1; |
| 1744 | } else { |
| 1745 | corr_in = &burst; |
| 1746 | } |
| 1747 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1748 | /* Correlate */ |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 1749 | if (!convolve(corr_in, sync->sequence, &corr, |
| 1750 | CUSTOM, start, len, 1, 0)) { |
| 1751 | delete dec; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1752 | return -1; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 1755 | delete dec; |
| 1756 | |
| 1757 | /* Running at the downsampled rate at this point */ |
| 1758 | sps = 1; |
| 1759 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1760 | /* Peak detection - place restrictions at correlation edges */ |
| 1761 | *amp = fastPeakDetect(corr, toa); |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1762 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1763 | if ((*toa < 3 * sps) || (*toa > len - 3 * sps)) |
| 1764 | return 0; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1765 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1766 | /* Peak -to-average ratio */ |
| 1767 | if (computePeakRatio(&corr, sps, *toa, *amp) < thresh) |
| 1768 | return 0; |
| 1769 | |
| 1770 | /* Compute peak-to-average ratio. Reject if we don't have enough values */ |
| 1771 | *amp = peakDetect(corr, toa, NULL); |
| 1772 | |
| 1773 | /* Normalize our channel gain */ |
| 1774 | *amp = *amp / sync->gain; |
| 1775 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 1776 | /* Compensate for residuate time lag */ |
| 1777 | *toa = *toa - sync->toa; |
| 1778 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1779 | return 1; |
| 1780 | } |
| 1781 | |
Alexander Chemeris | 1470fcd | 2017-03-17 22:35:02 -0700 | [diff] [blame] | 1782 | static float maxAmplitude(const signalVector &burst) |
Tom Tsou | 577cd02 | 2015-05-18 13:57:54 -0700 | [diff] [blame] | 1783 | { |
Alexander Chemeris | 954b118 | 2015-06-04 15:39:41 -0400 | [diff] [blame] | 1784 | float max = 0.0; |
| 1785 | for (size_t i = 0; i < burst.size(); i++) { |
| 1786 | if (fabs(burst[i].real()) > max) |
| 1787 | max = fabs(burst[i].real()); |
| 1788 | if (fabs(burst[i].imag()) > max) |
| 1789 | max = fabs(burst[i].imag()); |
| 1790 | } |
Tom Tsou | 577cd02 | 2015-05-18 13:57:54 -0700 | [diff] [blame] | 1791 | |
Alexander Chemeris | 954b118 | 2015-06-04 15:39:41 -0400 | [diff] [blame] | 1792 | return max; |
Tom Tsou | 577cd02 | 2015-05-18 13:57:54 -0700 | [diff] [blame] | 1793 | } |
| 1794 | |
Alexander Chemeris | 130a800 | 2015-06-09 20:52:11 -0400 | [diff] [blame] | 1795 | /* |
| 1796 | * RACH/Normal burst detection with clipping detection |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1797 | * |
| 1798 | * Correlation window parameters: |
Alexander Chemeris | 130a800 | 2015-06-09 20:52:11 -0400 | [diff] [blame] | 1799 | * target: Tail bits + burst length |
| 1800 | * head: Search symbols before target |
| 1801 | * tail: Search symbols after target |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1802 | */ |
Alexander Chemeris | 1470fcd | 2017-03-17 22:35:02 -0700 | [diff] [blame] | 1803 | static int detectGeneralBurst(const signalVector &rxBurst, |
| 1804 | float thresh, |
| 1805 | int sps, |
| 1806 | complex &, |
| 1807 | float &toa, |
| 1808 | int target, int head, int tail, |
| 1809 | CorrelationSequence *sync) |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1810 | { |
Alexander Chemeris | 130a800 | 2015-06-09 20:52:11 -0400 | [diff] [blame] | 1811 | int rc, start, len; |
Alexander Chemeris | 954b118 | 2015-06-04 15:39:41 -0400 | [diff] [blame] | 1812 | bool clipping = false; |
Thomas Tsou | b075dd2 | 2013-11-09 22:25:46 -0500 | [diff] [blame] | 1813 | signalVector *corr; |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1814 | |
| 1815 | if ((sps != 1) && (sps != 4)) |
Tom Tsou | 577cd02 | 2015-05-18 13:57:54 -0700 | [diff] [blame] | 1816 | return -SIGERR_UNSUPPORTED; |
| 1817 | |
Alexander Chemeris | 954b118 | 2015-06-04 15:39:41 -0400 | [diff] [blame] | 1818 | // Detect potential clipping |
| 1819 | // We still may be able to demod the burst, so we'll give it a try |
| 1820 | // and only report clipping if we can't demod. |
| 1821 | float maxAmpl = maxAmplitude(rxBurst); |
| 1822 | if (maxAmpl > CLIP_THRESH) { |
| 1823 | LOG(DEBUG) << "max burst amplitude: " << maxAmpl << " is above the clipping threshold: " << CLIP_THRESH << std::endl; |
| 1824 | clipping = true; |
| 1825 | } |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1826 | |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 1827 | start = target - head - 1; |
| 1828 | len = head + tail; |
Thomas Tsou | b075dd2 | 2013-11-09 22:25:46 -0500 | [diff] [blame] | 1829 | corr = new signalVector(len); |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1830 | |
Thomas Tsou | b075dd2 | 2013-11-09 22:25:46 -0500 | [diff] [blame] | 1831 | rc = detectBurst(rxBurst, *corr, sync, |
Alexander Chemeris | 130a800 | 2015-06-09 20:52:11 -0400 | [diff] [blame] | 1832 | thresh, sps, &, &toa, start, len); |
Thomas Tsou | b075dd2 | 2013-11-09 22:25:46 -0500 | [diff] [blame] | 1833 | delete corr; |
| 1834 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1835 | if (rc < 0) { |
Tom Tsou | 577cd02 | 2015-05-18 13:57:54 -0700 | [diff] [blame] | 1836 | return -SIGERR_INTERNAL; |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1837 | } else if (!rc) { |
Alexander Chemeris | 130a800 | 2015-06-09 20:52:11 -0400 | [diff] [blame] | 1838 | amp = 0.0f; |
| 1839 | toa = 0.0f; |
Alexander Chemeris | 954b118 | 2015-06-04 15:39:41 -0400 | [diff] [blame] | 1840 | return clipping?-SIGERR_CLIP:SIGERR_NONE; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1843 | /* Subtract forward search bits from delay */ |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 1844 | toa -= head; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1845 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1846 | return 1; |
| 1847 | } |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1848 | |
Alexander Chemeris | 130a800 | 2015-06-09 20:52:11 -0400 | [diff] [blame] | 1849 | |
| 1850 | /* |
| 1851 | * RACH burst detection |
| 1852 | * |
| 1853 | * Correlation window parameters: |
| 1854 | * target: Tail bits + RACH length (reduced from 41 to a multiple of 4) |
Tom Tsou | e90c24c | 2016-06-21 16:14:39 -0700 | [diff] [blame] | 1855 | * head: Search 8 symbols before target |
| 1856 | * tail: Search 8 symbols + maximum expected delay |
Alexander Chemeris | 130a800 | 2015-06-09 20:52:11 -0400 | [diff] [blame] | 1857 | */ |
Alexander Chemeris | 1470fcd | 2017-03-17 22:35:02 -0700 | [diff] [blame] | 1858 | int detectRACHBurst(const signalVector &burst, |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 1859 | float threshold, |
Alexander Chemeris | 130a800 | 2015-06-09 20:52:11 -0400 | [diff] [blame] | 1860 | int sps, |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 1861 | complex &litude, |
Alexander Chemeris | 78d1fc9 | 2016-03-19 21:16:22 +0300 | [diff] [blame] | 1862 | float &toa, |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 1863 | unsigned max_toa) |
Alexander Chemeris | 130a800 | 2015-06-09 20:52:11 -0400 | [diff] [blame] | 1864 | { |
| 1865 | int rc, target, head, tail; |
| 1866 | CorrelationSequence *sync; |
| 1867 | |
| 1868 | target = 8 + 40; |
Tom Tsou | e90c24c | 2016-06-21 16:14:39 -0700 | [diff] [blame] | 1869 | head = 8; |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 1870 | tail = 8 + max_toa; |
Alexander Chemeris | 130a800 | 2015-06-09 20:52:11 -0400 | [diff] [blame] | 1871 | sync = gRACHSequence; |
| 1872 | |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 1873 | rc = detectGeneralBurst(burst, threshold, sps, amplitude, toa, |
Alexander Chemeris | 130a800 | 2015-06-09 20:52:11 -0400 | [diff] [blame] | 1874 | target, head, tail, sync); |
| 1875 | |
| 1876 | return rc; |
| 1877 | } |
| 1878 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1879 | /* |
| 1880 | * Normal burst detection |
| 1881 | * |
| 1882 | * Correlation window parameters: |
| 1883 | * target: Tail + data + mid-midamble + 1/2 remaining midamblebits |
Tom Tsou | e90c24c | 2016-06-21 16:14:39 -0700 | [diff] [blame] | 1884 | * head: Search 6 symbols before target |
| 1885 | * tail: Search 6 symbols + maximum expected delay |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1886 | */ |
Alexander Chemeris | 1470fcd | 2017-03-17 22:35:02 -0700 | [diff] [blame] | 1887 | int analyzeTrafficBurst(const signalVector &burst, unsigned tsc, float threshold, |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 1888 | int sps, complex &litude, float &toa, unsigned max_toa) |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1889 | { |
Alexander Chemeris | 130a800 | 2015-06-09 20:52:11 -0400 | [diff] [blame] | 1890 | int rc, target, head, tail; |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1891 | CorrelationSequence *sync; |
| 1892 | |
Tom Tsou | ae91f13 | 2017-03-28 14:40:38 -0700 | [diff] [blame] | 1893 | if (tsc > 7) |
Tom Tsou | 577cd02 | 2015-05-18 13:57:54 -0700 | [diff] [blame] | 1894 | return -SIGERR_UNSUPPORTED; |
| 1895 | |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1896 | target = 3 + 58 + 16 + 5; |
Tom Tsou | e90c24c | 2016-06-21 16:14:39 -0700 | [diff] [blame] | 1897 | head = 6; |
| 1898 | tail = 6 + max_toa; |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1899 | sync = gMidambles[tsc]; |
Thomas Tsou | 865bca4 | 2013-08-21 20:58:00 -0400 | [diff] [blame] | 1900 | |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 1901 | rc = detectGeneralBurst(burst, threshold, sps, amplitude, toa, |
Alexander Chemeris | 130a800 | 2015-06-09 20:52:11 -0400 | [diff] [blame] | 1902 | target, head, tail, sync); |
Alexander Chemeris | 130a800 | 2015-06-09 20:52:11 -0400 | [diff] [blame] | 1903 | return rc; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1904 | } |
| 1905 | |
Alexander Chemeris | 1470fcd | 2017-03-17 22:35:02 -0700 | [diff] [blame] | 1906 | int detectEdgeBurst(const signalVector &burst, unsigned tsc, float threshold, |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 1907 | int sps, complex &litude, float &toa, unsigned max_toa) |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 1908 | { |
| 1909 | int rc, target, head, tail; |
| 1910 | CorrelationSequence *sync; |
| 1911 | |
Tom Tsou | ae91f13 | 2017-03-28 14:40:38 -0700 | [diff] [blame] | 1912 | if (tsc > 7) |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 1913 | return -SIGERR_UNSUPPORTED; |
| 1914 | |
| 1915 | target = 3 + 58 + 16 + 5; |
Tom Tsou | e90c24c | 2016-06-21 16:14:39 -0700 | [diff] [blame] | 1916 | head = 6; |
| 1917 | tail = 6 + max_toa; |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 1918 | sync = gEdgeMidambles[tsc]; |
| 1919 | |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 1920 | rc = detectGeneralBurst(burst, threshold, sps, amplitude, toa, |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 1921 | target, head, tail, sync); |
| 1922 | return rc; |
| 1923 | } |
| 1924 | |
Alexander Chemeris | 1470fcd | 2017-03-17 22:35:02 -0700 | [diff] [blame] | 1925 | int detectAnyBurst(const signalVector &burst, unsigned tsc, float threshold, |
Alexander Chemeris | 4e6c938 | 2017-03-17 15:24:18 -0700 | [diff] [blame] | 1926 | int sps, CorrType type, complex &, float &toa, |
| 1927 | unsigned max_toa) |
| 1928 | { |
| 1929 | int rc = 0; |
| 1930 | |
| 1931 | switch (type) { |
| 1932 | case EDGE: |
| 1933 | rc = detectEdgeBurst(burst, tsc, threshold, sps, |
| 1934 | amp, toa, max_toa); |
| 1935 | if (rc > 0) |
| 1936 | break; |
| 1937 | else |
| 1938 | type = TSC; |
| 1939 | case TSC: |
| 1940 | rc = analyzeTrafficBurst(burst, tsc, threshold, sps, |
| 1941 | amp, toa, max_toa); |
| 1942 | break; |
| 1943 | case RACH: |
| 1944 | rc = detectRACHBurst(burst, threshold, sps, amp, toa, |
| 1945 | max_toa); |
| 1946 | break; |
| 1947 | default: |
| 1948 | LOG(ERR) << "Invalid correlation type"; |
| 1949 | } |
| 1950 | |
| 1951 | if (rc > 0) |
| 1952 | return type; |
| 1953 | |
| 1954 | return rc; |
| 1955 | } |
| 1956 | |
Alexander Chemeris | 1470fcd | 2017-03-17 22:35:02 -0700 | [diff] [blame] | 1957 | signalVector *downsampleBurst(const signalVector &burst) |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 1958 | { |
Tom Tsou | 7676427 | 2016-06-24 14:25:39 -0700 | [diff] [blame] | 1959 | signalVector *in, *out; |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 1960 | |
Tom Tsou | 7676427 | 2016-06-24 14:25:39 -0700 | [diff] [blame] | 1961 | in = new signalVector(DOWNSAMPLE_IN_LEN, dnsampler->len()); |
| 1962 | out = new signalVector(DOWNSAMPLE_OUT_LEN); |
| 1963 | memcpy(in->begin(), burst.begin(), DOWNSAMPLE_IN_LEN * 2 * sizeof(float)); |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 1964 | |
Tom Tsou | 92bdfb8 | 2017-03-28 15:22:01 -0700 | [diff] [blame] | 1965 | if (dnsampler->rotate((float *) in->begin(), DOWNSAMPLE_IN_LEN, |
| 1966 | (float *) out->begin(), DOWNSAMPLE_OUT_LEN) < 0) { |
| 1967 | delete out; |
| 1968 | out = NULL; |
| 1969 | } |
| 1970 | |
Tom Tsou | 7676427 | 2016-06-24 14:25:39 -0700 | [diff] [blame] | 1971 | delete in; |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 1972 | return out; |
| 1973 | }; |
| 1974 | |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 1975 | signalVector *decimateVector(signalVector &wVector, size_t factor) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1976 | { |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 1977 | signalVector *dec; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1978 | |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 1979 | if (factor <= 1) |
| 1980 | return NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1981 | |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 1982 | dec = new signalVector(wVector.size() / factor); |
| 1983 | dec->isReal(wVector.isReal()); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1984 | |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 1985 | signalVector::iterator itr = dec->begin(); |
| 1986 | for (size_t i = 0; i < wVector.size(); i += factor) |
| 1987 | *itr++ = wVector[i]; |
| 1988 | |
| 1989 | return dec; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 1992 | /* |
| 1993 | * Soft 8-PSK decoding using Manhattan distance metric |
| 1994 | */ |
| 1995 | static SoftVector *softSliceEdgeBurst(signalVector &burst) |
| 1996 | { |
| 1997 | size_t nsyms = 148; |
| 1998 | |
| 1999 | if (burst.size() < nsyms) |
| 2000 | return NULL; |
| 2001 | |
| 2002 | signalVector::iterator itr; |
| 2003 | SoftVector *bits = new SoftVector(nsyms * 3); |
| 2004 | |
| 2005 | /* |
| 2006 | * Bits 0 and 1 - First and second bits of the symbol respectively |
| 2007 | */ |
| 2008 | rotateBurst2(burst, -M_PI / 8.0); |
| 2009 | itr = burst.begin(); |
| 2010 | for (size_t i = 0; i < nsyms; i++) { |
| 2011 | (*bits)[3 * i + 0] = -itr->imag(); |
| 2012 | (*bits)[3 * i + 1] = itr->real(); |
| 2013 | itr++; |
| 2014 | } |
| 2015 | |
| 2016 | /* |
| 2017 | * Bit 2 - Collapse symbols into quadrant 0 (positive X and Y). |
| 2018 | * Decision area is then simplified to X=Y axis. Rotate again to |
| 2019 | * place decision boundary on X-axis. |
| 2020 | */ |
| 2021 | itr = burst.begin(); |
| 2022 | for (size_t i = 0; i < burst.size(); i++) { |
| 2023 | burst[i] = Complex<float>(fabs(itr->real()), fabs(itr->imag())); |
| 2024 | itr++; |
| 2025 | } |
| 2026 | |
| 2027 | rotateBurst2(burst, -M_PI / 4.0); |
| 2028 | itr = burst.begin(); |
| 2029 | for (size_t i = 0; i < nsyms; i++) { |
| 2030 | (*bits)[3 * i + 2] = -itr->imag(); |
| 2031 | itr++; |
| 2032 | } |
| 2033 | |
| 2034 | signalVector soft(bits->size()); |
| 2035 | for (size_t i = 0; i < bits->size(); i++) |
| 2036 | soft[i] = (*bits)[i]; |
| 2037 | |
| 2038 | return bits; |
| 2039 | } |
| 2040 | |
| 2041 | /* |
Alexander Chemeris | 132fb24 | 2017-03-17 17:22:33 -0700 | [diff] [blame] | 2042 | * Convert signalVector to SoftVector by taking real part of the signal. |
| 2043 | */ |
| 2044 | static SoftVector *signalToSoftVector(signalVector *dec) |
| 2045 | { |
| 2046 | SoftVector *bits = new SoftVector(dec->size()); |
| 2047 | |
| 2048 | SoftVector::iterator bit_itr = bits->begin(); |
| 2049 | signalVector::iterator burst_itr = dec->begin(); |
| 2050 | |
| 2051 | for (; burst_itr < dec->end(); burst_itr++) |
| 2052 | *bit_itr++ = burst_itr->real(); |
| 2053 | |
| 2054 | return bits; |
| 2055 | } |
| 2056 | |
| 2057 | /* |
Tom Tsou | 7fec303 | 2016-03-06 22:33:20 -0800 | [diff] [blame] | 2058 | * Shared portion of GMSK and EDGE demodulators consisting of timing |
| 2059 | * recovery and single tap channel correction. For 4 SPS (if activated), |
| 2060 | * the output is downsampled prior to the 1 SPS modulation specific |
| 2061 | * stages. |
| 2062 | */ |
Alexander Chemeris | e0c1218 | 2017-03-18 13:27:48 -0700 | [diff] [blame] | 2063 | static signalVector *demodCommon(const signalVector &burst, int sps, |
Tom Tsou | 7fec303 | 2016-03-06 22:33:20 -0800 | [diff] [blame] | 2064 | complex chan, float toa) |
| 2065 | { |
| 2066 | signalVector *delay, *dec; |
| 2067 | |
| 2068 | if ((sps != 1) && (sps != 4)) |
| 2069 | return NULL; |
| 2070 | |
Tom Tsou | 7fec303 | 2016-03-06 22:33:20 -0800 | [diff] [blame] | 2071 | delay = delayVector(&burst, NULL, -toa * (float) sps); |
Alexander Chemeris | e0c1218 | 2017-03-18 13:27:48 -0700 | [diff] [blame] | 2072 | scaleVector(*delay, (complex) 1.0 / chan); |
Tom Tsou | 7fec303 | 2016-03-06 22:33:20 -0800 | [diff] [blame] | 2073 | |
| 2074 | if (sps == 1) |
| 2075 | return delay; |
| 2076 | |
| 2077 | dec = downsampleBurst(*delay); |
| 2078 | |
| 2079 | delete delay; |
| 2080 | return dec; |
| 2081 | } |
| 2082 | |
| 2083 | /* |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 2084 | * Demodulate GSMK burst. Prior to symbol rotation, operate at |
| 2085 | * 4 SPS (if activated) to minimize distortion through the fractional |
| 2086 | * delay filters. Symbol rotation and after always operates at 1 SPS. |
| 2087 | */ |
Alexander Chemeris | e0c1218 | 2017-03-18 13:27:48 -0700 | [diff] [blame] | 2088 | SoftVector *demodGmskBurst(const signalVector &rxBurst, int sps, |
Alexander Chemeris | 1470fcd | 2017-03-17 22:35:02 -0700 | [diff] [blame] | 2089 | complex channel, float TOA) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 2090 | { |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 2091 | SoftVector *bits; |
Tom Tsou | 7fec303 | 2016-03-06 22:33:20 -0800 | [diff] [blame] | 2092 | signalVector *dec; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 2093 | |
Tom Tsou | 7fec303 | 2016-03-06 22:33:20 -0800 | [diff] [blame] | 2094 | dec = demodCommon(rxBurst, sps, channel, TOA); |
| 2095 | if (!dec) |
| 2096 | return NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 2097 | |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 2098 | /* Shift up by a quarter of a frequency */ |
| 2099 | GMSKReverseRotate(*dec, 1); |
Alexander Chemeris | 132fb24 | 2017-03-17 17:22:33 -0700 | [diff] [blame] | 2100 | /* Take real part of the signal */ |
| 2101 | bits = signalToSoftVector(dec); |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 2102 | delete dec; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 2103 | |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 2104 | return bits; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 2105 | } |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 2106 | |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 2107 | /* |
| 2108 | * Demodulate an 8-PSK burst. Prior to symbol rotation, operate at |
| 2109 | * 4 SPS (if activated) to minimize distortion through the fractional |
| 2110 | * delay filters. Symbol rotation and after always operates at 1 SPS. |
| 2111 | * |
| 2112 | * Allow 1 SPS demodulation here, but note that other parts of the |
| 2113 | * transceiver restrict EDGE operatoin to 4 SPS - 8-PSK distortion |
| 2114 | * through the fractional delay filters at 1 SPS renders signal |
| 2115 | * nearly unrecoverable. |
| 2116 | */ |
Alexander Chemeris | e0c1218 | 2017-03-18 13:27:48 -0700 | [diff] [blame] | 2117 | SoftVector *demodEdgeBurst(const signalVector &burst, int sps, |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 2118 | complex chan, float toa) |
| 2119 | { |
| 2120 | SoftVector *bits; |
Tom Tsou | 7fec303 | 2016-03-06 22:33:20 -0800 | [diff] [blame] | 2121 | signalVector *dec, *rot, *eq; |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 2122 | |
Tom Tsou | 7fec303 | 2016-03-06 22:33:20 -0800 | [diff] [blame] | 2123 | dec = demodCommon(burst, sps, chan, toa); |
| 2124 | if (!dec) |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 2125 | return NULL; |
| 2126 | |
Tom Tsou | 7fec303 | 2016-03-06 22:33:20 -0800 | [diff] [blame] | 2127 | /* Equalize and derotate */ |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 2128 | eq = convolve(dec, GSMPulse4->c0_inv, NULL, NO_DELAY); |
| 2129 | rot = derotateEdgeBurst(*eq, 1); |
| 2130 | |
Tom Tsou | 7fec303 | 2016-03-06 22:33:20 -0800 | [diff] [blame] | 2131 | /* Soft slice and normalize */ |
Tom Tsou | 0479562 | 2016-04-26 21:17:36 -0700 | [diff] [blame] | 2132 | bits = softSliceEdgeBurst(*rot); |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 2133 | |
| 2134 | delete dec; |
| 2135 | delete eq; |
| 2136 | delete rot; |
| 2137 | |
| 2138 | return bits; |
| 2139 | } |
| 2140 | |
Alexander Chemeris | e0c1218 | 2017-03-18 13:27:48 -0700 | [diff] [blame] | 2141 | SoftVector *demodAnyBurst(const signalVector &burst, int sps, complex amp, |
Alexander Chemeris | 6e1dffd | 2017-03-17 16:13:51 -0700 | [diff] [blame] | 2142 | float toa, CorrType type) |
| 2143 | { |
| 2144 | if (type == EDGE) |
| 2145 | return demodEdgeBurst(burst, sps, amp, toa); |
| 2146 | else |
| 2147 | return demodGmskBurst(burst, sps, amp, toa); |
| 2148 | } |
| 2149 | |
Tom Tsou | 2079a3c | 2016-03-06 00:58:56 -0800 | [diff] [blame] | 2150 | bool sigProcLibSetup() |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 2151 | { |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 2152 | initTrigTables(); |
Thomas Tsou | 0e0e1f4 | 2013-11-09 22:08:51 -0500 | [diff] [blame] | 2153 | generateSincTable(); |
Tom Tsou | 2079a3c | 2016-03-06 00:58:56 -0800 | [diff] [blame] | 2154 | initGMSKRotationTables(); |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 2155 | |
Tom Tsou | 2079a3c | 2016-03-06 00:58:56 -0800 | [diff] [blame] | 2156 | GSMPulse1 = generateGSMPulse(1); |
| 2157 | GSMPulse4 = generateGSMPulse(4); |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 2158 | |
Tom Tsou | 2079a3c | 2016-03-06 00:58:56 -0800 | [diff] [blame] | 2159 | generateRACHSequence(1); |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 2160 | for (int tsc = 0; tsc < 8; tsc++) { |
Tom Tsou | 2079a3c | 2016-03-06 00:58:56 -0800 | [diff] [blame] | 2161 | generateMidamble(1, tsc); |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 2162 | gEdgeMidambles[tsc] = generateEdgeMidamble(tsc); |
| 2163 | } |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 2164 | |
Thomas Tsou | f79c4d0 | 2013-11-09 15:51:56 -0600 | [diff] [blame] | 2165 | generateDelayFilters(); |
| 2166 | |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 2167 | dnsampler = new Resampler(1, 4); |
| 2168 | if (!dnsampler->init()) { |
| 2169 | LOG(ALERT) << "Rx resampler failed to initialize"; |
| 2170 | goto fail; |
| 2171 | } |
| 2172 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 2173 | return true; |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 2174 | |
| 2175 | fail: |
| 2176 | sigProcLibDestroy(); |
| 2177 | return false; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 2178 | } |
Alexander Chemeris | f7717ac | 2017-03-17 15:32:26 -0700 | [diff] [blame] | 2179 | |
| 2180 | std::string corrTypeToString(CorrType corr) { |
| 2181 | switch (corr) { |
| 2182 | case OFF: |
| 2183 | return "OFF"; |
| 2184 | case TSC: |
| 2185 | return "TSC"; |
| 2186 | case RACH: |
| 2187 | return "RACH"; |
| 2188 | case EDGE: |
| 2189 | return "EDGE"; |
| 2190 | case IDLE: |
| 2191 | return "IDLE"; |
| 2192 | default: |
| 2193 | return "unknown"; |
| 2194 | } |
| 2195 | } |
| 2196 | |
| 2197 | std::ostream& operator<<(std::ostream& os, CorrType corr) |
| 2198 | { |
| 2199 | os << corrTypeToString(corr); |
| 2200 | return os; |
| 2201 | } |