dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 Free Software Foundation, Inc. |
| 3 | * |
| 4 | * This software is distributed under multiple licenses; see the COPYING file in the main directory for licensing information for this specific distribuion. |
| 5 | * |
| 6 | * This use of this software may be subject to additional restrictions. |
| 7 | * See the LEGAL file in the main directory for details. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | |
| 13 | */ |
| 14 | |
kurtis.heimerl | 8aea56e | 2011-11-26 03:18:30 +0000 | [diff] [blame] | 15 | #ifndef SIGPROCLIB_H |
| 16 | #define SIGPROCLIB_H |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 17 | |
| 18 | #include "Vector.h" |
| 19 | #include "Complex.h" |
Alexander Chemeris | d734e2d | 2013-06-16 14:30:58 +0400 | [diff] [blame] | 20 | #include "BitVector.h" |
Thomas Tsou | 20eb6d6 | 2013-11-09 14:30:41 -0500 | [diff] [blame] | 21 | #include "signalVector.h" |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 22 | |
Tom Tsou | b0aefcb | 2016-03-06 03:44:34 -0800 | [diff] [blame] | 23 | /* Burst lengths */ |
| 24 | #define NORMAL_BURST_NBITS 148 |
| 25 | #define EDGE_BURST_NBITS 444 |
| 26 | #define EDGE_BURST_NSYMS (EDGE_BURST_NBITS / 3) |
| 27 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 28 | /** Convolution type indicator */ |
kurtis.heimerl | 3b8ad24 | 2011-11-26 03:18:19 +0000 | [diff] [blame] | 29 | enum ConvType { |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 30 | START_ONLY, |
| 31 | NO_DELAY, |
| 32 | CUSTOM, |
| 33 | UNDEFINED, |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 34 | }; |
| 35 | |
Alexander Chemeris | f9e78be | 2017-03-17 15:00:34 -0700 | [diff] [blame] | 36 | /** Codes for burst types of received bursts*/ |
| 37 | enum CorrType{ |
| 38 | OFF, ///< timeslot is off |
| 39 | TSC, ///< timeslot should contain a normal burst |
| 40 | RACH, ///< timeslot should contain an access burst |
| 41 | EDGE, ///< timeslot should contain an EDGE burst |
| 42 | IDLE ///< timeslot is an idle (or dummy) burst |
| 43 | }; |
| 44 | |
Alexander Chemeris | 4aa548f | 2017-03-17 15:23:06 -0700 | [diff] [blame] | 45 | enum SignalError { |
Tom Tsou | 577cd02 | 2015-05-18 13:57:54 -0700 | [diff] [blame] | 46 | SIGERR_NONE, |
| 47 | SIGERR_BOUNDS, |
| 48 | SIGERR_CLIP, |
| 49 | SIGERR_UNSUPPORTED, |
| 50 | SIGERR_INTERNAL, |
| 51 | }; |
| 52 | |
Alexander Chemeris | b34e60c | 2017-03-17 15:29:09 -0700 | [diff] [blame^] | 53 | /* |
| 54 | * Burst detection threshold |
| 55 | * |
| 56 | * Decision threshold value for burst gating on peak-to-average value of |
| 57 | * correlated synchronization sequences. Lower values pass more bursts up |
| 58 | * to upper layers but will increase the false detection rate. |
| 59 | */ |
| 60 | #define BURST_THRESH 4.0 |
| 61 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 62 | /** Convert a linear number to a dB value */ |
| 63 | float dB(float x); |
| 64 | |
| 65 | /** Convert a dB value into a linear value */ |
| 66 | float dBinv(float x); |
| 67 | |
| 68 | /** Compute the energy of a vector */ |
| 69 | float vectorNorm2(const signalVector &x); |
| 70 | |
| 71 | /** Compute the average power of a vector */ |
| 72 | float vectorPower(const signalVector &x); |
| 73 | |
| 74 | /** Setup the signal processing library */ |
Tom Tsou | 2079a3c | 2016-03-06 00:58:56 -0800 | [diff] [blame] | 75 | bool sigProcLibSetup(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 76 | |
| 77 | /** Destroy the signal processing library */ |
| 78 | void sigProcLibDestroy(void); |
| 79 | |
| 80 | /** |
| 81 | Convolve two vectors. |
| 82 | @param a,b The vectors to be convolved. |
| 83 | @param c, A preallocated vector to hold the convolution result. |
| 84 | @param spanType The type/span of the convolution. |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 85 | @return The convolution result or NULL on error. |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 86 | */ |
Thomas Tsou | 3f32ab5 | 2013-11-15 16:32:54 -0500 | [diff] [blame] | 87 | signalVector *convolve(const signalVector *a, const signalVector *b, |
| 88 | signalVector *c, ConvType spanType, |
| 89 | size_t start = 0, size_t len = 0, |
| 90 | size_t step = 1, int offset = 0); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 91 | |
| 92 | /** |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 93 | Frequency shift a vector. |
| 94 | @param y The frequency shifted vector. |
| 95 | @param x The vector to-be-shifted. |
| 96 | @param freq The digital frequency shift |
| 97 | @param startPhase The starting phase of the oscillator |
| 98 | @param finalPhase The final phase of the oscillator |
| 99 | @return The frequency shifted vector. |
| 100 | */ |
| 101 | signalVector* frequencyShift(signalVector *y, |
| 102 | signalVector *x, |
| 103 | float freq = 0.0, |
| 104 | float startPhase = 0.0, |
| 105 | float *finalPhase=NULL); |
| 106 | |
| 107 | /** |
| 108 | Correlate two vectors. |
| 109 | @param a,b The vectors to be correlated. |
| 110 | @param c, A preallocated vector to hold the correlation result. |
| 111 | @param spanType The type/span of the correlation. |
| 112 | @return The correlation result. |
| 113 | */ |
| 114 | signalVector* correlate(signalVector *a, |
| 115 | signalVector *b, |
| 116 | signalVector *c, |
| 117 | ConvType spanType, |
| 118 | bool bReversedConjugated = false, |
| 119 | unsigned startIx = 0, |
| 120 | unsigned len = 0); |
| 121 | |
Alexander Chemeris | 132fb24 | 2017-03-17 17:22:33 -0700 | [diff] [blame] | 122 | /** Operate soft slicer on a soft-bit vector */ |
| 123 | bool vectorSlicer(SoftVector *x); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 124 | |
| 125 | /** GMSK modulate a GSM burst of bits */ |
| 126 | signalVector *modulateBurst(const BitVector &wBurst, |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 127 | int guardPeriodLength, |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 128 | int sps, bool emptyPulse = false); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 129 | |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 130 | /** 8-PSK modulate a burst of bits */ |
| 131 | signalVector *modulateEdgeBurst(const BitVector &bits, |
| 132 | int sps, bool emptyPulse = false); |
| 133 | |
| 134 | /** Generate a EDGE burst with random payload - 4 SPS (625 samples) only */ |
| 135 | signalVector *generateEdgeBurst(int tsc); |
| 136 | |
Tom Tsou | 8ee2f38 | 2016-03-06 20:57:34 -0800 | [diff] [blame] | 137 | /** Generate an empty burst - 4 or 1 SPS */ |
| 138 | signalVector *generateEmptyBurst(int sps, int tn); |
| 139 | |
| 140 | /** Generate a normal GSM burst with random payload - 4 or 1 SPS */ |
| 141 | signalVector *genRandNormalBurst(int tsc, int sps, int tn); |
| 142 | |
Alexander Chemeris | 5efe050 | 2016-03-23 17:06:32 +0300 | [diff] [blame] | 143 | /** Generate an access GSM burst with random payload - 4 or 1 SPS */ |
Alexander Chemeris | 37c52c7 | 2016-03-25 18:28:34 +0300 | [diff] [blame] | 144 | signalVector *genRandAccessBurst(int delay, int sps, int tn); |
Alexander Chemeris | 5efe050 | 2016-03-23 17:06:32 +0300 | [diff] [blame] | 145 | |
Tom Tsou | 8ee2f38 | 2016-03-06 20:57:34 -0800 | [diff] [blame] | 146 | /** Generate a dummy GSM burst - 4 or 1 SPS */ |
| 147 | signalVector *generateDummyBurst(int sps, int tn); |
| 148 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 149 | /** Sinc function */ |
| 150 | float sinc(float x); |
| 151 | |
| 152 | /** Delay a vector */ |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 153 | signalVector *delayVector(signalVector *in, signalVector *out, float delay); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 154 | |
| 155 | /** Add two vectors in-place */ |
| 156 | bool addVector(signalVector &x, |
| 157 | signalVector &y); |
| 158 | |
| 159 | /** Multiply two vectors in-place*/ |
| 160 | bool multVector(signalVector &x, |
| 161 | signalVector &y); |
| 162 | |
| 163 | /** Generate a vector of gaussian noise */ |
| 164 | signalVector *gaussianNoise(int length, |
| 165 | float variance = 1.0, |
| 166 | complex mean = complex(0.0)); |
| 167 | |
| 168 | /** |
| 169 | Given a non-integer index, interpolate a sample. |
| 170 | @param inSig The signal from which to interpolate. |
| 171 | @param ix The index. |
| 172 | @return The interpolated signal value. |
| 173 | */ |
| 174 | complex interpolatePoint(const signalVector &inSig, |
| 175 | float ix); |
| 176 | |
| 177 | /** |
| 178 | Given a correlator output, locate the correlation peak. |
| 179 | @param rxBurst The correlator result. |
| 180 | @param peakIndex Pointer to value to receive interpolated peak index. |
| 181 | @param avgPower Power to value to receive mean power. |
| 182 | @return Peak value. |
| 183 | */ |
| 184 | complex peakDetect(const signalVector &rxBurst, |
| 185 | float *peakIndex, |
| 186 | float *avgPwr); |
| 187 | |
| 188 | /** |
| 189 | Apply a scalar to a vector. |
| 190 | @param x The vector of interest. |
| 191 | @param scale The scalar. |
| 192 | */ |
| 193 | void scaleVector(signalVector &x, |
| 194 | complex scale); |
| 195 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 196 | /** |
Alexander Chemeris | 1dd05cf | 2017-03-15 23:23:36 +0300 | [diff] [blame] | 197 | Rough energy estimator. |
| 198 | @param rxBurst A GSM burst. |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 199 | @param windowLength The number of burst samples used to compute burst energy |
Alexander Chemeris | 1dd05cf | 2017-03-15 23:23:36 +0300 | [diff] [blame] | 200 | @return The average power of the received burst. |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 201 | */ |
Alexander Chemeris | 1dd05cf | 2017-03-15 23:23:36 +0300 | [diff] [blame] | 202 | float energyDetect(signalVector &rxBurst, |
| 203 | unsigned windowLength); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 204 | |
| 205 | /** |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 206 | RACH aka Access Burst correlator/detector. |
| 207 | @param burst The received GSM burst of interest. |
| 208 | @param threshold The threshold that the received burst's post-correlator SNR is compared against to determine validity. |
Thomas Tsou | d24cc2c | 2013-08-20 15:41:45 -0400 | [diff] [blame] | 209 | @param sps The number of samples per GSM symbol. |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 210 | @param amplitude The estimated amplitude of received RACH burst. |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 211 | @param toa The estimate time-of-arrival of received RACH burst. |
| 212 | @param max_toa The maximum expected time-of-arrival |
| 213 | @return 1 if threshold value is reached, |
| 214 | negative value (-SignalError) on error, |
| 215 | zero (SIGERR_NONE) if no burst is detected |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 216 | */ |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 217 | int detectRACHBurst(signalVector &burst, |
| 218 | float threshold, |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 219 | int sps, |
Alexander Chemeris | 130a800 | 2015-06-09 20:52:11 -0400 | [diff] [blame] | 220 | complex &litude, |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 221 | float &toa, |
| 222 | unsigned max_toa); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 223 | |
| 224 | /** |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 225 | GMSK Normal Burst correlator/detector. |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 226 | @param rxBurst The received GSM burst of interest. |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 227 | @param tsc Midamble type (0..7) also known as TSC |
| 228 | @param threshold The threshold that the received burst's post-correlator SNR is compared against to determine validity. |
Thomas Tsou | d24cc2c | 2013-08-20 15:41:45 -0400 | [diff] [blame] | 229 | @param sps The number of samples per GSM symbol. |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 230 | @param amplitude The estimated amplitude of received TSC burst. |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 231 | @param toa The estimate time-of-arrival of received TSC burst. |
| 232 | @param max_toa The maximum expected time-of-arrival |
| 233 | @return 1 if threshold value is reached, |
| 234 | negative value (-SignalError) on error, |
| 235 | zero (SIGERR_NONE) if no burst is detected |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 236 | */ |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 237 | int analyzeTrafficBurst(signalVector &burst, |
| 238 | unsigned tsc, |
| 239 | float threshold, |
Alexander Chemeris | 130a800 | 2015-06-09 20:52:11 -0400 | [diff] [blame] | 240 | int sps, |
| 241 | complex &litude, |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 242 | float &toa, |
| 243 | unsigned max_toa); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 244 | |
| 245 | /** |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 246 | EDGE/8-PSK Normal Burst correlator/detector |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 247 | @param burst The received GSM burst of interest |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 248 | @param tsc Midamble type (0..7) also known as TSC |
| 249 | @param threshold The threshold that the received burst's post-correlator SNR is compared against to determine validity. |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 250 | @param sps The number of samples per GSM symbol. |
| 251 | @param amplitude The estimated amplitude of received TSC burst. |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 252 | @param toa The estimate time-of-arrival of received TSC burst. |
| 253 | @param max_toa The maximum expected time-of-arrival |
| 254 | @return 1 if threshold value is reached, |
| 255 | negative value (-SignalError) on error, |
| 256 | zero (SIGERR_NONE) if no burst is detected |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 257 | */ |
| 258 | int detectEdgeBurst(signalVector &burst, |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 259 | unsigned tsc, |
| 260 | float threshold, |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 261 | int sps, |
| 262 | complex &litude, |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 263 | float &toa, |
| 264 | unsigned max_toa); |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 265 | |
| 266 | /** |
Alexander Chemeris | 4e6c938 | 2017-03-17 15:24:18 -0700 | [diff] [blame] | 267 | 8-PSK/GMSK/RACH burst detector |
| 268 | @param burst The received GSM burst of interest |
| 269 | @param tsc Midamble type (0..7) also known as TSC |
| 270 | @param threshold The threshold that the received burst's post-correlator SNR is compared against to determine validity. |
| 271 | @param sps The number of samples per GSM symbol. |
| 272 | @param amplitude The estimated amplitude of received TSC burst. |
| 273 | @param toa The estimate time-of-arrival of received TSC burst (in symbols). |
| 274 | @param max_toa The maximum expected time-of-arrival (in symbols). |
| 275 | @return positive value (CorrType) if threshold value is reached, |
| 276 | negative value (-SignalError) on error, |
| 277 | zero (SIGERR_NONE) if no burst is detected |
| 278 | */ |
| 279 | int detectAnyBurst(signalVector &burst, |
| 280 | unsigned tsc, |
| 281 | float threshold, |
| 282 | int sps, |
| 283 | CorrType type, |
| 284 | complex &, |
| 285 | float &toa, |
| 286 | unsigned max_toa); |
| 287 | |
| 288 | /** |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 289 | Downsample 4 SPS to 1 SPS using a polyphase filterbank |
| 290 | @param burst Input burst of at least 624 symbols |
| 291 | @return Decimated signal vector of 156 symbols |
| 292 | */ |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 293 | signalVector *downsampleBurst(signalVector &burst); |
| 294 | |
| 295 | /** |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 296 | Decimate a vector. |
| 297 | @param wVector The vector of interest. |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 298 | @param factor Decimation factor. |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 299 | @return The decimated signal vector. |
| 300 | */ |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 301 | signalVector *decimateVector(signalVector &wVector, size_t factor); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 302 | |
| 303 | /** |
Alexander Chemeris | 1c0b8b3 | 2017-03-17 16:12:47 -0700 | [diff] [blame] | 304 | Demodulates a GMSK burst using a soft-slicer. |
| 305 | @param rxBurst The burst to be demodulated. |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 306 | @param gsmPulse The GSM pulse. |
Thomas Tsou | d24cc2c | 2013-08-20 15:41:45 -0400 | [diff] [blame] | 307 | @param sps The number of samples per GSM symbol. |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 308 | @param channel The amplitude estimate of the received burst. |
| 309 | @param TOA The time-of-arrival of the received burst. |
| 310 | @return The demodulated bit sequence. |
| 311 | */ |
Alexander Chemeris | 1c0b8b3 | 2017-03-17 16:12:47 -0700 | [diff] [blame] | 312 | SoftVector *demodGmskBurst(signalVector &rxBurst, int sps, |
| 313 | complex channel, float TOA); |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 314 | |
| 315 | /** |
| 316 | Demodulate 8-PSK EDGE burst with soft symbol ooutput |
| 317 | @param rxBurst The burst to be demodulated. |
| 318 | @param sps The number of samples per GSM symbol. |
| 319 | @param channel The amplitude estimate of the received burst. |
| 320 | @param TOA The time-of-arrival of the received burst. |
| 321 | @return The demodulated bit sequence. |
| 322 | */ |
| 323 | SoftVector *demodEdgeBurst(signalVector &rxBurst, int sps, |
| 324 | complex channel, float TOA); |
| 325 | |
kurtis.heimerl | 8aea56e | 2011-11-26 03:18:30 +0000 | [diff] [blame] | 326 | #endif /* SIGPROCLIB_H */ |