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