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" |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 21 | |
| 22 | /** Indicated signalVector symmetry */ |
kurtis.heimerl | 3b8ad24 | 2011-11-26 03:18:19 +0000 | [diff] [blame] | 23 | enum Symmetry { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 24 | NONE = 0, |
| 25 | ABSSYM = 1 |
| 26 | }; |
| 27 | |
| 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 | |
| 36 | /** the core data structure of the Transceiver */ |
| 37 | class signalVector: public Vector<complex> |
| 38 | { |
| 39 | |
| 40 | private: |
| 41 | |
| 42 | Symmetry symmetry; ///< the symmetry of the vector |
| 43 | bool realOnly; ///< true if vector is real-valued, not complex-valued |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame^] | 44 | bool aligned; |
| 45 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 46 | public: |
| 47 | |
| 48 | /** Constructors */ |
| 49 | signalVector(int dSize=0, Symmetry wSymmetry = NONE): |
| 50 | Vector<complex>(dSize), |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame^] | 51 | realOnly(false), aligned(false) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 52 | { |
| 53 | symmetry = wSymmetry; |
| 54 | }; |
| 55 | |
| 56 | signalVector(complex* wData, size_t start, |
| 57 | size_t span, Symmetry wSymmetry = NONE): |
| 58 | Vector<complex>(NULL,wData+start,wData+start+span), |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame^] | 59 | realOnly(false), aligned(false) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 60 | { |
| 61 | symmetry = wSymmetry; |
| 62 | }; |
| 63 | |
| 64 | signalVector(const signalVector &vec1, const signalVector &vec2): |
| 65 | Vector<complex>(vec1,vec2), |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame^] | 66 | realOnly(false), aligned(false) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 67 | { |
| 68 | symmetry = vec1.symmetry; |
| 69 | }; |
| 70 | |
| 71 | signalVector(const signalVector &wVector): |
| 72 | Vector<complex>(wVector.size()), |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame^] | 73 | realOnly(false), aligned(false) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 74 | { |
| 75 | wVector.copyTo(*this); |
| 76 | symmetry = wVector.getSymmetry(); |
| 77 | }; |
| 78 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame^] | 79 | signalVector(size_t size, size_t start): |
| 80 | Vector<complex>(size + start), |
| 81 | realOnly(false), aligned(false) |
| 82 | { |
| 83 | mStart = mData + start; |
| 84 | symmetry = NONE; |
| 85 | }; |
| 86 | |
| 87 | signalVector(const signalVector &wVector, size_t start, size_t tail = 0): |
| 88 | Vector<complex>(start + wVector.size() + tail), |
| 89 | realOnly(false), aligned(false) |
| 90 | { |
| 91 | mStart = mData + start; |
| 92 | wVector.copyTo(*this); |
| 93 | memset(mData, 0, start * sizeof(complex)); |
| 94 | memset(mStart + wVector.size(), 0, tail * sizeof(complex)); |
| 95 | symmetry = NONE; |
| 96 | }; |
| 97 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 98 | /** symmetry operators */ |
| 99 | Symmetry getSymmetry() const { return symmetry;}; |
| 100 | void setSymmetry(Symmetry wSymmetry) { symmetry = wSymmetry;}; |
| 101 | |
| 102 | /** real-valued operators */ |
| 103 | bool isRealOnly() const { return realOnly;}; |
| 104 | void isRealOnly(bool wOnly) { realOnly = wOnly;}; |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame^] | 105 | |
| 106 | /** alignment markers */ |
| 107 | bool isAligned() const { return aligned; }; |
| 108 | void setAligned(bool aligned) { this->aligned = aligned; }; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | /** Convert a linear number to a dB value */ |
| 112 | float dB(float x); |
| 113 | |
| 114 | /** Convert a dB value into a linear value */ |
| 115 | float dBinv(float x); |
| 116 | |
| 117 | /** Compute the energy of a vector */ |
| 118 | float vectorNorm2(const signalVector &x); |
| 119 | |
| 120 | /** Compute the average power of a vector */ |
| 121 | float vectorPower(const signalVector &x); |
| 122 | |
| 123 | /** Setup the signal processing library */ |
Thomas Tsou | e57004d | 2013-08-20 18:55:33 -0400 | [diff] [blame] | 124 | bool sigProcLibSetup(int sps); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 125 | |
| 126 | /** Destroy the signal processing library */ |
| 127 | void sigProcLibDestroy(void); |
| 128 | |
| 129 | /** |
| 130 | Convolve two vectors. |
| 131 | @param a,b The vectors to be convolved. |
| 132 | @param c, A preallocated vector to hold the convolution result. |
| 133 | @param spanType The type/span of the convolution. |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame^] | 134 | @return The convolution result or NULL on error. |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 135 | */ |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame^] | 136 | signalVector *convolve(const signalVector *a, |
| 137 | const signalVector *b, |
| 138 | signalVector *c, |
| 139 | ConvType spanType, |
| 140 | int start = 0, |
| 141 | unsigned len = 0, |
| 142 | unsigned step = 1, int offset = 0); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 143 | |
| 144 | /** |
| 145 | Generate the GSM pulse. |
Thomas Tsou | d24cc2c | 2013-08-20 15:41:45 -0400 | [diff] [blame] | 146 | @param sps The number of samples per GSM symbol. |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 147 | @param symbolLength The size of the pulse. |
| 148 | @return The GSM pulse. |
| 149 | */ |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 150 | void generateGSMPulse(int sps, int symbolLength); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 151 | |
| 152 | /** |
| 153 | Frequency shift a vector. |
| 154 | @param y The frequency shifted vector. |
| 155 | @param x The vector to-be-shifted. |
| 156 | @param freq The digital frequency shift |
| 157 | @param startPhase The starting phase of the oscillator |
| 158 | @param finalPhase The final phase of the oscillator |
| 159 | @return The frequency shifted vector. |
| 160 | */ |
| 161 | signalVector* frequencyShift(signalVector *y, |
| 162 | signalVector *x, |
| 163 | float freq = 0.0, |
| 164 | float startPhase = 0.0, |
| 165 | float *finalPhase=NULL); |
| 166 | |
| 167 | /** |
| 168 | Correlate two vectors. |
| 169 | @param a,b The vectors to be correlated. |
| 170 | @param c, A preallocated vector to hold the correlation result. |
| 171 | @param spanType The type/span of the correlation. |
| 172 | @return The correlation result. |
| 173 | */ |
| 174 | signalVector* correlate(signalVector *a, |
| 175 | signalVector *b, |
| 176 | signalVector *c, |
| 177 | ConvType spanType, |
| 178 | bool bReversedConjugated = false, |
| 179 | unsigned startIx = 0, |
| 180 | unsigned len = 0); |
| 181 | |
| 182 | /** Operate soft slicer on real-valued portion of vector */ |
| 183 | bool vectorSlicer(signalVector *x); |
| 184 | |
| 185 | /** GMSK modulate a GSM burst of bits */ |
| 186 | signalVector *modulateBurst(const BitVector &wBurst, |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 187 | int guardPeriodLength, |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 188 | int sps, bool emptyPulse = false); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 189 | |
| 190 | /** Sinc function */ |
| 191 | float sinc(float x); |
| 192 | |
| 193 | /** Delay a vector */ |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame^] | 194 | bool delayVector(signalVector &wBurst, float delay); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 195 | |
| 196 | /** Add two vectors in-place */ |
| 197 | bool addVector(signalVector &x, |
| 198 | signalVector &y); |
| 199 | |
| 200 | /** Multiply two vectors in-place*/ |
| 201 | bool multVector(signalVector &x, |
| 202 | signalVector &y); |
| 203 | |
| 204 | /** Generate a vector of gaussian noise */ |
| 205 | signalVector *gaussianNoise(int length, |
| 206 | float variance = 1.0, |
| 207 | complex mean = complex(0.0)); |
| 208 | |
| 209 | /** |
| 210 | Given a non-integer index, interpolate a sample. |
| 211 | @param inSig The signal from which to interpolate. |
| 212 | @param ix The index. |
| 213 | @return The interpolated signal value. |
| 214 | */ |
| 215 | complex interpolatePoint(const signalVector &inSig, |
| 216 | float ix); |
| 217 | |
| 218 | /** |
| 219 | Given a correlator output, locate the correlation peak. |
| 220 | @param rxBurst The correlator result. |
| 221 | @param peakIndex Pointer to value to receive interpolated peak index. |
| 222 | @param avgPower Power to value to receive mean power. |
| 223 | @return Peak value. |
| 224 | */ |
| 225 | complex peakDetect(const signalVector &rxBurst, |
| 226 | float *peakIndex, |
| 227 | float *avgPwr); |
| 228 | |
| 229 | /** |
| 230 | Apply a scalar to a vector. |
| 231 | @param x The vector of interest. |
| 232 | @param scale The scalar. |
| 233 | */ |
| 234 | void scaleVector(signalVector &x, |
| 235 | complex scale); |
| 236 | |
| 237 | /** |
| 238 | Add a constant offset to a vecotr. |
| 239 | @param x The vector of interest. |
| 240 | @param offset The offset. |
| 241 | */ |
| 242 | void offsetVector(signalVector &x, |
| 243 | complex offset); |
| 244 | |
| 245 | /** |
| 246 | Generate a modulated GSM midamble, stored within the library. |
| 247 | @param gsmPulse The GSM pulse used for modulation. |
Thomas Tsou | d24cc2c | 2013-08-20 15:41:45 -0400 | [diff] [blame] | 248 | @param sps The number of samples per GSM symbol. |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 249 | @param TSC The training sequence [0..7] |
| 250 | @return Success. |
| 251 | */ |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 252 | bool generateMidamble(int sps, int tsc); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 253 | /** |
| 254 | Generate a modulated RACH sequence, stored within the library. |
| 255 | @param gsmPulse The GSM pulse used for modulation. |
Thomas Tsou | d24cc2c | 2013-08-20 15:41:45 -0400 | [diff] [blame] | 256 | @param sps The number of samples per GSM symbol. |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 257 | @return Success. |
| 258 | */ |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 259 | bool generateRACHSequence(int sps); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 260 | |
| 261 | /** |
| 262 | Energy detector, checks to see if received burst energy is above a threshold. |
| 263 | @param rxBurst The received GSM burst of interest. |
| 264 | @param windowLength The number of burst samples used to compute burst energy |
| 265 | @param detectThreshold The detection threshold, a linear value. |
| 266 | @param avgPwr The average power of the received burst. |
| 267 | @return True if burst energy is above threshold. |
| 268 | */ |
| 269 | bool energyDetect(signalVector &rxBurst, |
| 270 | unsigned windowLength, |
| 271 | float detectThreshold, |
| 272 | float *avgPwr = NULL); |
| 273 | |
| 274 | /** |
| 275 | RACH correlator/detector. |
| 276 | @param rxBurst The received GSM burst of interest. |
| 277 | @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] | 278 | @param sps The number of samples per GSM symbol. |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 279 | @param amplitude The estimated amplitude of received RACH burst. |
| 280 | @param TOA The estimate time-of-arrival of received RACH burst. |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame^] | 281 | @return positive if threshold value is reached, negative on error, zero otherwise |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 282 | */ |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame^] | 283 | int detectRACHBurst(signalVector &rxBurst, |
| 284 | float detectThreshold, |
| 285 | int sps, |
| 286 | complex *amplitude, |
| 287 | float* TOA); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 288 | |
| 289 | /** |
| 290 | Normal burst correlator, detector, channel estimator. |
| 291 | @param rxBurst The received GSM burst of interest. |
| 292 | |
| 293 | @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] | 294 | @param sps The number of samples per GSM symbol. |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 295 | @param amplitude The estimated amplitude of received TSC burst. |
| 296 | @param TOA The estimate time-of-arrival of received TSC burst. |
| 297 | @param maxTOA The maximum expected time-of-arrival |
| 298 | @param requestChannel Set to true if channel estimation is desired. |
| 299 | @param channelResponse The estimated channel. |
| 300 | @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^] | 301 | @return positive if threshold value is reached, negative on error, zero otherwise |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 302 | */ |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame^] | 303 | int analyzeTrafficBurst(signalVector &rxBurst, |
| 304 | unsigned TSC, |
| 305 | float detectThreshold, |
| 306 | int sps, |
| 307 | complex *amplitude, |
| 308 | float *TOA, |
| 309 | unsigned maxTOA, |
| 310 | bool requestChannel = false, |
| 311 | signalVector** channelResponse = NULL, |
| 312 | float *channelResponseOffset = NULL); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 313 | |
| 314 | /** |
| 315 | Decimate a vector. |
| 316 | @param wVector The vector of interest. |
| 317 | @param decimationFactor The amount of decimation, i.e. the decimation factor. |
| 318 | @return The decimated signal vector. |
| 319 | */ |
| 320 | signalVector *decimateVector(signalVector &wVector, |
| 321 | int decimationFactor); |
| 322 | |
| 323 | /** |
| 324 | Demodulates a received burst using a soft-slicer. |
| 325 | @param rxBurst The burst to be demodulated. |
| 326 | @param gsmPulse The GSM pulse. |
Thomas Tsou | d24cc2c | 2013-08-20 15:41:45 -0400 | [diff] [blame] | 327 | @param sps The number of samples per GSM symbol. |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 328 | @param channel The amplitude estimate of the received burst. |
| 329 | @param TOA The time-of-arrival of the received burst. |
| 330 | @return The demodulated bit sequence. |
| 331 | */ |
Thomas Tsou | 83e0689 | 2013-08-20 16:10:01 -0400 | [diff] [blame] | 332 | SoftVector *demodulateBurst(signalVector &rxBurst, int sps, |
| 333 | complex channel, float TOA); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 334 | |
| 335 | /** |
| 336 | Creates a simple Kaiser-windowed low-pass FIR filter. |
| 337 | @param cutoffFreq The digital 3dB bandwidth of the filter. |
| 338 | @param filterLen The number of taps in the filter. |
| 339 | @param gainDC The DC gain of the filter. |
| 340 | @return The desired LPF |
| 341 | */ |
| 342 | signalVector *createLPF(float cutoffFreq, |
| 343 | int filterLen, |
| 344 | float gainDC = 1.0); |
| 345 | |
| 346 | /** |
| 347 | Change sampling rate of a vector via polyphase resampling. |
| 348 | @param wVector The vector to be resampled. |
| 349 | @param P The numerator, i.e. the amount of upsampling. |
| 350 | @param Q The denominator, i.e. the amount of downsampling. |
| 351 | @param LPF An optional low-pass filter used in the resampling process. |
| 352 | @return A vector resampled at P/Q of the original sampling rate. |
| 353 | */ |
| 354 | signalVector *polyphaseResampleVector(signalVector &wVector, |
| 355 | int P, int Q, |
| 356 | signalVector *LPF); |
| 357 | |
| 358 | /** |
| 359 | Change the sampling rate of a vector via linear interpolation. |
| 360 | @param wVector The vector to be resampled. |
| 361 | @param expFactor Ratio of new sampling rate/original sampling rate. |
| 362 | @param endPoint ??? |
| 363 | @return A vector resampled a expFactor*original sampling rate. |
| 364 | */ |
| 365 | signalVector *resampleVector(signalVector &wVector, |
| 366 | float expFactor, |
| 367 | complex endPoint); |
| 368 | |
| 369 | /** |
| 370 | Design the necessary filters for a decision-feedback equalizer. |
| 371 | @param channelResponse The multipath channel that we're mitigating. |
| 372 | @param SNRestimate The signal-to-noise estimate of the channel, a linear value |
| 373 | @param Nf The number of taps in the feedforward filter. |
| 374 | @param feedForwardFilter The designed feed forward filter. |
| 375 | @param feedbackFilter The designed feedback filter. |
| 376 | @return True if DFE can be designed. |
| 377 | */ |
| 378 | bool designDFE(signalVector &channelResponse, |
| 379 | float SNRestimate, |
| 380 | int Nf, |
| 381 | signalVector **feedForwardFilter, |
| 382 | signalVector **feedbackFilter); |
| 383 | |
| 384 | /** |
| 385 | Equalize/demodulate a received burst via a decision-feedback equalizer. |
| 386 | @param rxBurst The received burst to be demodulated. |
| 387 | @param TOA The time-of-arrival of the received burst. |
Thomas Tsou | d24cc2c | 2013-08-20 15:41:45 -0400 | [diff] [blame] | 388 | @param sps The number of samples per GSM symbol. |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 389 | @param w The feed forward filter of the DFE. |
| 390 | @param b The feedback filter of the DFE. |
| 391 | @return The demodulated bit sequence. |
| 392 | */ |
| 393 | SoftVector *equalizeBurst(signalVector &rxBurst, |
| 394 | float TOA, |
Thomas Tsou | d24cc2c | 2013-08-20 15:41:45 -0400 | [diff] [blame] | 395 | int sps, |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 396 | signalVector &w, |
| 397 | signalVector &b); |
kurtis.heimerl | 8aea56e | 2011-11-26 03:18:30 +0000 | [diff] [blame] | 398 | |
| 399 | #endif /* SIGPROCLIB_H */ |