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 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 53 | /** Convert a linear number to a dB value */ |
| 54 | float dB(float x); |
| 55 | |
| 56 | /** Convert a dB value into a linear value */ |
| 57 | float dBinv(float x); |
| 58 | |
| 59 | /** Compute the energy of a vector */ |
| 60 | float vectorNorm2(const signalVector &x); |
| 61 | |
| 62 | /** Compute the average power of a vector */ |
| 63 | float vectorPower(const signalVector &x); |
| 64 | |
| 65 | /** Setup the signal processing library */ |
Tom Tsou | 2079a3c | 2016-03-06 00:58:56 -0800 | [diff] [blame] | 66 | bool sigProcLibSetup(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 67 | |
| 68 | /** Destroy the signal processing library */ |
| 69 | void sigProcLibDestroy(void); |
| 70 | |
| 71 | /** |
| 72 | Convolve two vectors. |
| 73 | @param a,b The vectors to be convolved. |
| 74 | @param c, A preallocated vector to hold the convolution result. |
| 75 | @param spanType The type/span of the convolution. |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 76 | @return The convolution result or NULL on error. |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 77 | */ |
Thomas Tsou | 3f32ab5 | 2013-11-15 16:32:54 -0500 | [diff] [blame] | 78 | signalVector *convolve(const signalVector *a, const signalVector *b, |
| 79 | signalVector *c, ConvType spanType, |
| 80 | size_t start = 0, size_t len = 0, |
| 81 | size_t step = 1, int offset = 0); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 82 | |
| 83 | /** |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 84 | Frequency shift a vector. |
| 85 | @param y The frequency shifted vector. |
| 86 | @param x The vector to-be-shifted. |
| 87 | @param freq The digital frequency shift |
| 88 | @param startPhase The starting phase of the oscillator |
| 89 | @param finalPhase The final phase of the oscillator |
| 90 | @return The frequency shifted vector. |
| 91 | */ |
| 92 | signalVector* frequencyShift(signalVector *y, |
| 93 | signalVector *x, |
| 94 | float freq = 0.0, |
| 95 | float startPhase = 0.0, |
| 96 | float *finalPhase=NULL); |
| 97 | |
| 98 | /** |
| 99 | Correlate two vectors. |
| 100 | @param a,b The vectors to be correlated. |
| 101 | @param c, A preallocated vector to hold the correlation result. |
| 102 | @param spanType The type/span of the correlation. |
| 103 | @return The correlation result. |
| 104 | */ |
| 105 | signalVector* correlate(signalVector *a, |
| 106 | signalVector *b, |
| 107 | signalVector *c, |
| 108 | ConvType spanType, |
| 109 | bool bReversedConjugated = false, |
| 110 | unsigned startIx = 0, |
| 111 | unsigned len = 0); |
| 112 | |
Alexander Chemeris | 132fb24 | 2017-03-17 17:22:33 -0700 | [diff] [blame] | 113 | /** Operate soft slicer on a soft-bit vector */ |
| 114 | bool vectorSlicer(SoftVector *x); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 115 | |
| 116 | /** GMSK modulate a GSM burst of bits */ |
| 117 | signalVector *modulateBurst(const BitVector &wBurst, |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 118 | int guardPeriodLength, |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 119 | int sps, bool emptyPulse = false); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 120 | |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 121 | /** 8-PSK modulate a burst of bits */ |
| 122 | signalVector *modulateEdgeBurst(const BitVector &bits, |
| 123 | int sps, bool emptyPulse = false); |
| 124 | |
| 125 | /** Generate a EDGE burst with random payload - 4 SPS (625 samples) only */ |
| 126 | signalVector *generateEdgeBurst(int tsc); |
| 127 | |
Tom Tsou | 8ee2f38 | 2016-03-06 20:57:34 -0800 | [diff] [blame] | 128 | /** Generate an empty burst - 4 or 1 SPS */ |
| 129 | signalVector *generateEmptyBurst(int sps, int tn); |
| 130 | |
| 131 | /** Generate a normal GSM burst with random payload - 4 or 1 SPS */ |
| 132 | signalVector *genRandNormalBurst(int tsc, int sps, int tn); |
| 133 | |
Alexander Chemeris | 5efe050 | 2016-03-23 17:06:32 +0300 | [diff] [blame] | 134 | /** Generate an access GSM burst with random payload - 4 or 1 SPS */ |
Alexander Chemeris | 37c52c7 | 2016-03-25 18:28:34 +0300 | [diff] [blame] | 135 | signalVector *genRandAccessBurst(int delay, int sps, int tn); |
Alexander Chemeris | 5efe050 | 2016-03-23 17:06:32 +0300 | [diff] [blame] | 136 | |
Tom Tsou | 8ee2f38 | 2016-03-06 20:57:34 -0800 | [diff] [blame] | 137 | /** Generate a dummy GSM burst - 4 or 1 SPS */ |
| 138 | signalVector *generateDummyBurst(int sps, int tn); |
| 139 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 140 | /** Sinc function */ |
| 141 | float sinc(float x); |
| 142 | |
| 143 | /** Delay a vector */ |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 144 | signalVector *delayVector(signalVector *in, signalVector *out, float delay); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 145 | |
| 146 | /** Add two vectors in-place */ |
| 147 | bool addVector(signalVector &x, |
| 148 | signalVector &y); |
| 149 | |
| 150 | /** Multiply two vectors in-place*/ |
| 151 | bool multVector(signalVector &x, |
| 152 | signalVector &y); |
| 153 | |
| 154 | /** Generate a vector of gaussian noise */ |
| 155 | signalVector *gaussianNoise(int length, |
| 156 | float variance = 1.0, |
| 157 | complex mean = complex(0.0)); |
| 158 | |
| 159 | /** |
| 160 | Given a non-integer index, interpolate a sample. |
| 161 | @param inSig The signal from which to interpolate. |
| 162 | @param ix The index. |
| 163 | @return The interpolated signal value. |
| 164 | */ |
| 165 | complex interpolatePoint(const signalVector &inSig, |
| 166 | float ix); |
| 167 | |
| 168 | /** |
| 169 | Given a correlator output, locate the correlation peak. |
| 170 | @param rxBurst The correlator result. |
| 171 | @param peakIndex Pointer to value to receive interpolated peak index. |
| 172 | @param avgPower Power to value to receive mean power. |
| 173 | @return Peak value. |
| 174 | */ |
| 175 | complex peakDetect(const signalVector &rxBurst, |
| 176 | float *peakIndex, |
| 177 | float *avgPwr); |
| 178 | |
| 179 | /** |
| 180 | Apply a scalar to a vector. |
| 181 | @param x The vector of interest. |
| 182 | @param scale The scalar. |
| 183 | */ |
| 184 | void scaleVector(signalVector &x, |
| 185 | complex scale); |
| 186 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 187 | /** |
Alexander Chemeris | 1dd05cf | 2017-03-15 23:23:36 +0300 | [diff] [blame] | 188 | Rough energy estimator. |
| 189 | @param rxBurst A GSM burst. |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 190 | @param windowLength The number of burst samples used to compute burst energy |
Alexander Chemeris | 1dd05cf | 2017-03-15 23:23:36 +0300 | [diff] [blame] | 191 | @return The average power of the received burst. |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 192 | */ |
Alexander Chemeris | 1dd05cf | 2017-03-15 23:23:36 +0300 | [diff] [blame] | 193 | float energyDetect(signalVector &rxBurst, |
| 194 | unsigned windowLength); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 195 | |
| 196 | /** |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 197 | RACH aka Access Burst correlator/detector. |
| 198 | @param burst The received GSM burst of interest. |
| 199 | @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] | 200 | @param sps The number of samples per GSM symbol. |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 201 | @param amplitude The estimated amplitude of received RACH burst. |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 202 | @param toa The estimate time-of-arrival of received RACH burst. |
| 203 | @param max_toa The maximum expected time-of-arrival |
| 204 | @return 1 if threshold value is reached, |
| 205 | negative value (-SignalError) on error, |
| 206 | zero (SIGERR_NONE) if no burst is detected |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 207 | */ |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 208 | int detectRACHBurst(signalVector &burst, |
| 209 | float threshold, |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 210 | int sps, |
Alexander Chemeris | 130a800 | 2015-06-09 20:52:11 -0400 | [diff] [blame] | 211 | complex &litude, |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 212 | float &toa, |
| 213 | unsigned max_toa); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 214 | |
| 215 | /** |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 216 | GMSK Normal Burst correlator/detector. |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 217 | @param rxBurst The received GSM burst of interest. |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 218 | @param tsc Midamble type (0..7) also known as TSC |
| 219 | @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] | 220 | @param sps The number of samples per GSM symbol. |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 221 | @param amplitude The estimated amplitude of received TSC burst. |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 222 | @param toa The estimate time-of-arrival of received TSC burst. |
| 223 | @param max_toa The maximum expected time-of-arrival |
| 224 | @return 1 if threshold value is reached, |
| 225 | negative value (-SignalError) on error, |
| 226 | zero (SIGERR_NONE) if no burst is detected |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 227 | */ |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 228 | int analyzeTrafficBurst(signalVector &burst, |
| 229 | unsigned tsc, |
| 230 | float threshold, |
Alexander Chemeris | 130a800 | 2015-06-09 20:52:11 -0400 | [diff] [blame] | 231 | int sps, |
| 232 | complex &litude, |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 233 | float &toa, |
| 234 | unsigned max_toa); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 235 | |
| 236 | /** |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 237 | EDGE/8-PSK Normal Burst correlator/detector |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 238 | @param burst The received GSM burst of interest |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 239 | @param tsc Midamble type (0..7) also known as TSC |
| 240 | @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] | 241 | @param sps The number of samples per GSM symbol. |
| 242 | @param amplitude The estimated amplitude of received TSC burst. |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 243 | @param toa The estimate time-of-arrival of received TSC burst. |
| 244 | @param max_toa The maximum expected time-of-arrival |
| 245 | @return 1 if threshold value is reached, |
| 246 | negative value (-SignalError) on error, |
| 247 | zero (SIGERR_NONE) if no burst is detected |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 248 | */ |
| 249 | int detectEdgeBurst(signalVector &burst, |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 250 | unsigned tsc, |
| 251 | float threshold, |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 252 | int sps, |
| 253 | complex &litude, |
Alexander Chemeris | 14d13b6 | 2017-03-17 15:12:17 -0700 | [diff] [blame] | 254 | float &toa, |
| 255 | unsigned max_toa); |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 256 | |
| 257 | /** |
| 258 | Downsample 4 SPS to 1 SPS using a polyphase filterbank |
| 259 | @param burst Input burst of at least 624 symbols |
| 260 | @return Decimated signal vector of 156 symbols |
| 261 | */ |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 262 | signalVector *downsampleBurst(signalVector &burst); |
| 263 | |
| 264 | /** |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 265 | Decimate a vector. |
| 266 | @param wVector The vector of interest. |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 267 | @param factor Decimation factor. |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 268 | @return The decimated signal vector. |
| 269 | */ |
Thomas Tsou | 94edaae | 2013-11-09 22:19:19 -0500 | [diff] [blame] | 270 | signalVector *decimateVector(signalVector &wVector, size_t factor); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 271 | |
| 272 | /** |
Alexander Chemeris | 1c0b8b3 | 2017-03-17 16:12:47 -0700 | [diff] [blame] | 273 | Demodulates a GMSK burst using a soft-slicer. |
| 274 | @param rxBurst The burst to be demodulated. |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 275 | @param gsmPulse The GSM pulse. |
Thomas Tsou | d24cc2c | 2013-08-20 15:41:45 -0400 | [diff] [blame] | 276 | @param sps The number of samples per GSM symbol. |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 277 | @param channel The amplitude estimate of the received burst. |
| 278 | @param TOA The time-of-arrival of the received burst. |
| 279 | @return The demodulated bit sequence. |
| 280 | */ |
Alexander Chemeris | 1c0b8b3 | 2017-03-17 16:12:47 -0700 | [diff] [blame] | 281 | SoftVector *demodGmskBurst(signalVector &rxBurst, int sps, |
| 282 | complex channel, float TOA); |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 283 | |
| 284 | /** |
| 285 | Demodulate 8-PSK EDGE burst with soft symbol ooutput |
| 286 | @param rxBurst The burst to be demodulated. |
| 287 | @param sps The number of samples per GSM symbol. |
| 288 | @param channel The amplitude estimate of the received burst. |
| 289 | @param TOA The time-of-arrival of the received burst. |
| 290 | @return The demodulated bit sequence. |
| 291 | */ |
| 292 | SoftVector *demodEdgeBurst(signalVector &rxBurst, int sps, |
| 293 | complex channel, float TOA); |
| 294 | |
kurtis.heimerl | 8aea56e | 2011-11-26 03:18:30 +0000 | [diff] [blame] | 295 | #endif /* SIGPROCLIB_H */ |