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