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