blob: ebe7519ae3bfca3553b5e8b3790b601501d68b32 [file] [log] [blame]
dburgessb3a0ca42011-10-12 07:44:40 +00001/*
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.heimerl8aea56e2011-11-26 03:18:30 +000015#ifndef SIGPROCLIB_H
16#define SIGPROCLIB_H
dburgessb3a0ca42011-10-12 07:44:40 +000017
18#include "Vector.h"
19#include "Complex.h"
Alexander Chemerisd734e2d2013-06-16 14:30:58 +040020#include "BitVector.h"
dburgessb3a0ca42011-10-12 07:44:40 +000021
22/** Indicated signalVector symmetry */
kurtis.heimerl3b8ad242011-11-26 03:18:19 +000023enum Symmetry {
dburgessb3a0ca42011-10-12 07:44:40 +000024 NONE = 0,
25 ABSSYM = 1
26};
27
28/** Convolution type indicator */
kurtis.heimerl3b8ad242011-11-26 03:18:19 +000029enum ConvType {
Thomas Tsou3eaae802013-08-20 19:31:14 -040030 START_ONLY,
31 NO_DELAY,
32 CUSTOM,
33 UNDEFINED,
dburgessb3a0ca42011-10-12 07:44:40 +000034};
35
36/** the core data structure of the Transceiver */
37class 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 Tsou3eaae802013-08-20 19:31:14 -040044 bool aligned;
45
dburgessb3a0ca42011-10-12 07:44:40 +000046 public:
47
48 /** Constructors */
49 signalVector(int dSize=0, Symmetry wSymmetry = NONE):
50 Vector<complex>(dSize),
Thomas Tsou3eaae802013-08-20 19:31:14 -040051 realOnly(false), aligned(false)
dburgessb3a0ca42011-10-12 07:44:40 +000052 {
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 Tsou3eaae802013-08-20 19:31:14 -040059 realOnly(false), aligned(false)
dburgessb3a0ca42011-10-12 07:44:40 +000060 {
61 symmetry = wSymmetry;
62 };
63
64 signalVector(const signalVector &vec1, const signalVector &vec2):
65 Vector<complex>(vec1,vec2),
Thomas Tsou3eaae802013-08-20 19:31:14 -040066 realOnly(false), aligned(false)
dburgessb3a0ca42011-10-12 07:44:40 +000067 {
68 symmetry = vec1.symmetry;
69 };
70
71 signalVector(const signalVector &wVector):
72 Vector<complex>(wVector.size()),
Thomas Tsou3eaae802013-08-20 19:31:14 -040073 realOnly(false), aligned(false)
dburgessb3a0ca42011-10-12 07:44:40 +000074 {
75 wVector.copyTo(*this);
76 symmetry = wVector.getSymmetry();
77 };
78
Thomas Tsou3eaae802013-08-20 19:31:14 -040079 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
Thomas Tsou6f4906e2013-11-09 02:40:18 -050098 /** start index */
99 int getStartIndex() const { return mStart - mData; };
100
dburgessb3a0ca42011-10-12 07:44:40 +0000101 /** symmetry operators */
102 Symmetry getSymmetry() const { return symmetry;};
103 void setSymmetry(Symmetry wSymmetry) { symmetry = wSymmetry;};
104
105 /** real-valued operators */
106 bool isRealOnly() const { return realOnly;};
107 void isRealOnly(bool wOnly) { realOnly = wOnly;};
Thomas Tsou3eaae802013-08-20 19:31:14 -0400108
109 /** alignment markers */
110 bool isAligned() const { return aligned; };
111 void setAligned(bool aligned) { this->aligned = aligned; };
dburgessb3a0ca42011-10-12 07:44:40 +0000112};
113
114/** Convert a linear number to a dB value */
115float dB(float x);
116
117/** Convert a dB value into a linear value */
118float dBinv(float x);
119
120/** Compute the energy of a vector */
121float vectorNorm2(const signalVector &x);
122
123/** Compute the average power of a vector */
124float vectorPower(const signalVector &x);
125
126/** Setup the signal processing library */
Thomas Tsoue57004d2013-08-20 18:55:33 -0400127bool sigProcLibSetup(int sps);
dburgessb3a0ca42011-10-12 07:44:40 +0000128
129/** Destroy the signal processing library */
130void sigProcLibDestroy(void);
131
132/**
133 Convolve two vectors.
134 @param a,b The vectors to be convolved.
135 @param c, A preallocated vector to hold the convolution result.
136 @param spanType The type/span of the convolution.
Thomas Tsou3eaae802013-08-20 19:31:14 -0400137 @return The convolution result or NULL on error.
dburgessb3a0ca42011-10-12 07:44:40 +0000138*/
Thomas Tsou3eaae802013-08-20 19:31:14 -0400139signalVector *convolve(const signalVector *a,
140 const signalVector *b,
141 signalVector *c,
142 ConvType spanType,
143 int start = 0,
144 unsigned len = 0,
145 unsigned step = 1, int offset = 0);
dburgessb3a0ca42011-10-12 07:44:40 +0000146
147/**
dburgessb3a0ca42011-10-12 07:44:40 +0000148 Frequency shift a vector.
149 @param y The frequency shifted vector.
150 @param x The vector to-be-shifted.
151 @param freq The digital frequency shift
152 @param startPhase The starting phase of the oscillator
153 @param finalPhase The final phase of the oscillator
154 @return The frequency shifted vector.
155*/
156signalVector* frequencyShift(signalVector *y,
157 signalVector *x,
158 float freq = 0.0,
159 float startPhase = 0.0,
160 float *finalPhase=NULL);
161
162/**
163 Correlate two vectors.
164 @param a,b The vectors to be correlated.
165 @param c, A preallocated vector to hold the correlation result.
166 @param spanType The type/span of the correlation.
167 @return The correlation result.
168*/
169signalVector* correlate(signalVector *a,
170 signalVector *b,
171 signalVector *c,
172 ConvType spanType,
173 bool bReversedConjugated = false,
174 unsigned startIx = 0,
175 unsigned len = 0);
176
177/** Operate soft slicer on real-valued portion of vector */
178bool vectorSlicer(signalVector *x);
179
180/** GMSK modulate a GSM burst of bits */
181signalVector *modulateBurst(const BitVector &wBurst,
dburgessb3a0ca42011-10-12 07:44:40 +0000182 int guardPeriodLength,
Thomas Tsou83e06892013-08-20 16:10:01 -0400183 int sps, bool emptyPulse = false);
dburgessb3a0ca42011-10-12 07:44:40 +0000184
185/** Sinc function */
186float sinc(float x);
187
188/** Delay a vector */
Thomas Tsou3eaae802013-08-20 19:31:14 -0400189bool delayVector(signalVector &wBurst, float delay);
dburgessb3a0ca42011-10-12 07:44:40 +0000190
191/** Add two vectors in-place */
192bool addVector(signalVector &x,
193 signalVector &y);
194
195/** Multiply two vectors in-place*/
196bool multVector(signalVector &x,
197 signalVector &y);
198
199/** Generate a vector of gaussian noise */
200signalVector *gaussianNoise(int length,
201 float variance = 1.0,
202 complex mean = complex(0.0));
203
204/**
205 Given a non-integer index, interpolate a sample.
206 @param inSig The signal from which to interpolate.
207 @param ix The index.
208 @return The interpolated signal value.
209*/
210complex interpolatePoint(const signalVector &inSig,
211 float ix);
212
213/**
214 Given a correlator output, locate the correlation peak.
215 @param rxBurst The correlator result.
216 @param peakIndex Pointer to value to receive interpolated peak index.
217 @param avgPower Power to value to receive mean power.
218 @return Peak value.
219*/
220complex peakDetect(const signalVector &rxBurst,
221 float *peakIndex,
222 float *avgPwr);
223
224/**
225 Apply a scalar to a vector.
226 @param x The vector of interest.
227 @param scale The scalar.
228*/
229void scaleVector(signalVector &x,
230 complex scale);
231
232/**
233 Add a constant offset to a vecotr.
234 @param x The vector of interest.
235 @param offset The offset.
236*/
237void offsetVector(signalVector &x,
238 complex offset);
239
240/**
241 Generate a modulated GSM midamble, stored within the library.
242 @param gsmPulse The GSM pulse used for modulation.
Thomas Tsoud24cc2c2013-08-20 15:41:45 -0400243 @param sps The number of samples per GSM symbol.
dburgessb3a0ca42011-10-12 07:44:40 +0000244 @param TSC The training sequence [0..7]
245 @return Success.
246*/
Thomas Tsou83e06892013-08-20 16:10:01 -0400247bool generateMidamble(int sps, int tsc);
dburgessb3a0ca42011-10-12 07:44:40 +0000248/**
249 Generate a modulated RACH sequence, stored within the library.
250 @param gsmPulse The GSM pulse used for modulation.
Thomas Tsoud24cc2c2013-08-20 15:41:45 -0400251 @param sps The number of samples per GSM symbol.
dburgessb3a0ca42011-10-12 07:44:40 +0000252 @return Success.
253*/
Thomas Tsou83e06892013-08-20 16:10:01 -0400254bool generateRACHSequence(int sps);
dburgessb3a0ca42011-10-12 07:44:40 +0000255
256/**
257 Energy detector, checks to see if received burst energy is above a threshold.
258 @param rxBurst The received GSM burst of interest.
259 @param windowLength The number of burst samples used to compute burst energy
260 @param detectThreshold The detection threshold, a linear value.
261 @param avgPwr The average power of the received burst.
262 @return True if burst energy is above threshold.
263*/
264bool energyDetect(signalVector &rxBurst,
265 unsigned windowLength,
266 float detectThreshold,
267 float *avgPwr = NULL);
268
269/**
270 RACH correlator/detector.
271 @param rxBurst The received GSM burst of interest.
272 @param detectThreshold The threshold that the received burst's post-correlator SNR is compared against to determine validity.
Thomas Tsoud24cc2c2013-08-20 15:41:45 -0400273 @param sps The number of samples per GSM symbol.
dburgessb3a0ca42011-10-12 07:44:40 +0000274 @param amplitude The estimated amplitude of received RACH burst.
275 @param TOA The estimate time-of-arrival of received RACH burst.
Thomas Tsou3eaae802013-08-20 19:31:14 -0400276 @return positive if threshold value is reached, negative on error, zero otherwise
dburgessb3a0ca42011-10-12 07:44:40 +0000277*/
Thomas Tsou3eaae802013-08-20 19:31:14 -0400278int detectRACHBurst(signalVector &rxBurst,
279 float detectThreshold,
280 int sps,
281 complex *amplitude,
282 float* TOA);
dburgessb3a0ca42011-10-12 07:44:40 +0000283
284/**
285 Normal burst correlator, detector, channel estimator.
286 @param rxBurst The received GSM burst of interest.
287
288 @param detectThreshold The threshold that the received burst's post-correlator SNR is compared against to determine validity.
Thomas Tsoud24cc2c2013-08-20 15:41:45 -0400289 @param sps The number of samples per GSM symbol.
dburgessb3a0ca42011-10-12 07:44:40 +0000290 @param amplitude The estimated amplitude of received TSC burst.
291 @param TOA The estimate time-of-arrival of received TSC burst.
292 @param maxTOA The maximum expected time-of-arrival
293 @param requestChannel Set to true if channel estimation is desired.
294 @param channelResponse The estimated channel.
295 @param channelResponseOffset The time offset b/w the first sample of the channel response and the reported TOA.
Thomas Tsou3eaae802013-08-20 19:31:14 -0400296 @return positive if threshold value is reached, negative on error, zero otherwise
dburgessb3a0ca42011-10-12 07:44:40 +0000297*/
Thomas Tsou3eaae802013-08-20 19:31:14 -0400298int analyzeTrafficBurst(signalVector &rxBurst,
299 unsigned TSC,
300 float detectThreshold,
301 int sps,
302 complex *amplitude,
303 float *TOA,
304 unsigned maxTOA,
305 bool requestChannel = false,
306 signalVector** channelResponse = NULL,
307 float *channelResponseOffset = NULL);
dburgessb3a0ca42011-10-12 07:44:40 +0000308
309/**
310 Decimate a vector.
311 @param wVector The vector of interest.
312 @param decimationFactor The amount of decimation, i.e. the decimation factor.
313 @return The decimated signal vector.
314*/
315signalVector *decimateVector(signalVector &wVector,
316 int decimationFactor);
317
318/**
319 Demodulates a received burst using a soft-slicer.
320 @param rxBurst The burst to be demodulated.
321 @param gsmPulse The GSM pulse.
Thomas Tsoud24cc2c2013-08-20 15:41:45 -0400322 @param sps The number of samples per GSM symbol.
dburgessb3a0ca42011-10-12 07:44:40 +0000323 @param channel The amplitude estimate of the received burst.
324 @param TOA The time-of-arrival of the received burst.
325 @return The demodulated bit sequence.
326*/
Thomas Tsou83e06892013-08-20 16:10:01 -0400327SoftVector *demodulateBurst(signalVector &rxBurst, int sps,
328 complex channel, float TOA);
dburgessb3a0ca42011-10-12 07:44:40 +0000329
330/**
dburgessb3a0ca42011-10-12 07:44:40 +0000331 Design the necessary filters for a decision-feedback equalizer.
332 @param channelResponse The multipath channel that we're mitigating.
333 @param SNRestimate The signal-to-noise estimate of the channel, a linear value
334 @param Nf The number of taps in the feedforward filter.
335 @param feedForwardFilter The designed feed forward filter.
336 @param feedbackFilter The designed feedback filter.
337 @return True if DFE can be designed.
338*/
339bool designDFE(signalVector &channelResponse,
340 float SNRestimate,
341 int Nf,
342 signalVector **feedForwardFilter,
343 signalVector **feedbackFilter);
344
345/**
346 Equalize/demodulate a received burst via a decision-feedback equalizer.
347 @param rxBurst The received burst to be demodulated.
348 @param TOA The time-of-arrival of the received burst.
Thomas Tsoud24cc2c2013-08-20 15:41:45 -0400349 @param sps The number of samples per GSM symbol.
dburgessb3a0ca42011-10-12 07:44:40 +0000350 @param w The feed forward filter of the DFE.
351 @param b The feedback filter of the DFE.
352 @return The demodulated bit sequence.
353*/
354SoftVector *equalizeBurst(signalVector &rxBurst,
355 float TOA,
Thomas Tsoud24cc2c2013-08-20 15:41:45 -0400356 int sps,
dburgessb3a0ca42011-10-12 07:44:40 +0000357 signalVector &w,
358 signalVector &b);
kurtis.heimerl8aea56e2011-11-26 03:18:30 +0000359
360#endif /* SIGPROCLIB_H */