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