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