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