blob: 109ffa8cd84d0c715e55a4555da702a4e2c783a5 [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
dburgessb3a0ca42011-10-12 07:44:40 +000098 /** 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 Tsou3eaae802013-08-20 19:31:14 -0400105
106 /** alignment markers */
107 bool isAligned() const { return aligned; };
108 void setAligned(bool aligned) { this->aligned = aligned; };
dburgessb3a0ca42011-10-12 07:44:40 +0000109};
110
111/** Convert a linear number to a dB value */
112float dB(float x);
113
114/** Convert a dB value into a linear value */
115float dBinv(float x);
116
117/** Compute the energy of a vector */
118float vectorNorm2(const signalVector &x);
119
120/** Compute the average power of a vector */
121float vectorPower(const signalVector &x);
122
123/** Setup the signal processing library */
Thomas Tsoue57004d2013-08-20 18:55:33 -0400124bool sigProcLibSetup(int sps);
dburgessb3a0ca42011-10-12 07:44:40 +0000125
126/** Destroy the signal processing library */
127void 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 Tsou3eaae802013-08-20 19:31:14 -0400134 @return The convolution result or NULL on error.
dburgessb3a0ca42011-10-12 07:44:40 +0000135*/
Thomas Tsou3eaae802013-08-20 19:31:14 -0400136signalVector *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);
dburgessb3a0ca42011-10-12 07:44:40 +0000143
144/**
145 Generate the GSM pulse.
Thomas Tsoud24cc2c2013-08-20 15:41:45 -0400146 @param sps The number of samples per GSM symbol.
dburgessb3a0ca42011-10-12 07:44:40 +0000147 @param symbolLength The size of the pulse.
148 @return The GSM pulse.
149*/
Thomas Tsou83e06892013-08-20 16:10:01 -0400150void generateGSMPulse(int sps, int symbolLength);
dburgessb3a0ca42011-10-12 07:44:40 +0000151
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*/
161signalVector* 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*/
174signalVector* 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 */
183bool vectorSlicer(signalVector *x);
184
185/** GMSK modulate a GSM burst of bits */
186signalVector *modulateBurst(const BitVector &wBurst,
dburgessb3a0ca42011-10-12 07:44:40 +0000187 int guardPeriodLength,
Thomas Tsou83e06892013-08-20 16:10:01 -0400188 int sps, bool emptyPulse = false);
dburgessb3a0ca42011-10-12 07:44:40 +0000189
190/** Sinc function */
191float sinc(float x);
192
193/** Delay a vector */
Thomas Tsou3eaae802013-08-20 19:31:14 -0400194bool delayVector(signalVector &wBurst, float delay);
dburgessb3a0ca42011-10-12 07:44:40 +0000195
196/** Add two vectors in-place */
197bool addVector(signalVector &x,
198 signalVector &y);
199
200/** Multiply two vectors in-place*/
201bool multVector(signalVector &x,
202 signalVector &y);
203
204/** Generate a vector of gaussian noise */
205signalVector *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*/
215complex 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*/
225complex 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*/
234void 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*/
242void 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 Tsoud24cc2c2013-08-20 15:41:45 -0400248 @param sps The number of samples per GSM symbol.
dburgessb3a0ca42011-10-12 07:44:40 +0000249 @param TSC The training sequence [0..7]
250 @return Success.
251*/
Thomas Tsou83e06892013-08-20 16:10:01 -0400252bool generateMidamble(int sps, int tsc);
dburgessb3a0ca42011-10-12 07:44:40 +0000253/**
254 Generate a modulated RACH sequence, stored within the library.
255 @param gsmPulse The GSM pulse used for modulation.
Thomas Tsoud24cc2c2013-08-20 15:41:45 -0400256 @param sps The number of samples per GSM symbol.
dburgessb3a0ca42011-10-12 07:44:40 +0000257 @return Success.
258*/
Thomas Tsou83e06892013-08-20 16:10:01 -0400259bool generateRACHSequence(int sps);
dburgessb3a0ca42011-10-12 07:44:40 +0000260
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*/
269bool 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 Tsoud24cc2c2013-08-20 15:41:45 -0400278 @param sps The number of samples per GSM symbol.
dburgessb3a0ca42011-10-12 07:44:40 +0000279 @param amplitude The estimated amplitude of received RACH burst.
280 @param TOA The estimate time-of-arrival of received RACH burst.
Thomas Tsou3eaae802013-08-20 19:31:14 -0400281 @return positive if threshold value is reached, negative on error, zero otherwise
dburgessb3a0ca42011-10-12 07:44:40 +0000282*/
Thomas Tsou3eaae802013-08-20 19:31:14 -0400283int detectRACHBurst(signalVector &rxBurst,
284 float detectThreshold,
285 int sps,
286 complex *amplitude,
287 float* TOA);
dburgessb3a0ca42011-10-12 07:44:40 +0000288
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 Tsoud24cc2c2013-08-20 15:41:45 -0400294 @param sps The number of samples per GSM symbol.
dburgessb3a0ca42011-10-12 07:44:40 +0000295 @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 Tsou3eaae802013-08-20 19:31:14 -0400301 @return positive if threshold value is reached, negative on error, zero otherwise
dburgessb3a0ca42011-10-12 07:44:40 +0000302*/
Thomas Tsou3eaae802013-08-20 19:31:14 -0400303int 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);
dburgessb3a0ca42011-10-12 07:44:40 +0000313
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*/
320signalVector *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 Tsoud24cc2c2013-08-20 15:41:45 -0400327 @param sps The number of samples per GSM symbol.
dburgessb3a0ca42011-10-12 07:44:40 +0000328 @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 Tsou83e06892013-08-20 16:10:01 -0400332SoftVector *demodulateBurst(signalVector &rxBurst, int sps,
333 complex channel, float TOA);
dburgessb3a0ca42011-10-12 07:44:40 +0000334
335/**
dburgessb3a0ca42011-10-12 07:44:40 +0000336 Design the necessary filters for a decision-feedback equalizer.
337 @param channelResponse The multipath channel that we're mitigating.
338 @param SNRestimate The signal-to-noise estimate of the channel, a linear value
339 @param Nf The number of taps in the feedforward filter.
340 @param feedForwardFilter The designed feed forward filter.
341 @param feedbackFilter The designed feedback filter.
342 @return True if DFE can be designed.
343*/
344bool designDFE(signalVector &channelResponse,
345 float SNRestimate,
346 int Nf,
347 signalVector **feedForwardFilter,
348 signalVector **feedbackFilter);
349
350/**
351 Equalize/demodulate a received burst via a decision-feedback equalizer.
352 @param rxBurst The received burst to be demodulated.
353 @param TOA The time-of-arrival of the received burst.
Thomas Tsoud24cc2c2013-08-20 15:41:45 -0400354 @param sps The number of samples per GSM symbol.
dburgessb3a0ca42011-10-12 07:44:40 +0000355 @param w The feed forward filter of the DFE.
356 @param b The feedback filter of the DFE.
357 @return The demodulated bit sequence.
358*/
359SoftVector *equalizeBurst(signalVector &rxBurst,
360 float TOA,
Thomas Tsoud24cc2c2013-08-20 15:41:45 -0400361 int sps,
dburgessb3a0ca42011-10-12 07:44:40 +0000362 signalVector &w,
363 signalVector &b);
kurtis.heimerl8aea56e2011-11-26 03:18:30 +0000364
365#endif /* SIGPROCLIB_H */