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