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