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