blob: 04c4757952f219a2d3bccc5fc21db258832a086d [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"
Thomas Tsou20eb6d62013-11-09 14:30:41 -050021#include "signalVector.h"
dburgessb3a0ca42011-10-12 07:44:40 +000022
Tom Tsoub0aefcb2016-03-06 03:44:34 -080023/* Burst lengths */
24#define NORMAL_BURST_NBITS 148
25#define EDGE_BURST_NBITS 444
26#define EDGE_BURST_NSYMS (EDGE_BURST_NBITS / 3)
27
dburgessb3a0ca42011-10-12 07:44:40 +000028/** 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
Alexander Chemerisf9e78be2017-03-17 15:00:34 -070036/** Codes for burst types of received bursts*/
37enum CorrType{
38 OFF, ///< timeslot is off
39 TSC, ///< timeslot should contain a normal burst
40 RACH, ///< timeslot should contain an access burst
41 EDGE, ///< timeslot should contain an EDGE burst
42 IDLE ///< timeslot is an idle (or dummy) burst
43};
44
Alexander Chemeris4aa548f2017-03-17 15:23:06 -070045enum SignalError {
Tom Tsou577cd022015-05-18 13:57:54 -070046 SIGERR_NONE,
47 SIGERR_BOUNDS,
48 SIGERR_CLIP,
49 SIGERR_UNSUPPORTED,
50 SIGERR_INTERNAL,
51};
52
Alexander Chemerisb34e60c2017-03-17 15:29:09 -070053/*
54 * Burst detection threshold
55 *
56 * Decision threshold value for burst gating on peak-to-average value of
57 * correlated synchronization sequences. Lower values pass more bursts up
58 * to upper layers but will increase the false detection rate.
59 */
60#define BURST_THRESH 4.0
61
dburgessb3a0ca42011-10-12 07:44:40 +000062/** Convert a linear number to a dB value */
63float dB(float x);
64
65/** Convert a dB value into a linear value */
66float dBinv(float x);
67
68/** Compute the energy of a vector */
69float vectorNorm2(const signalVector &x);
70
71/** Compute the average power of a vector */
72float vectorPower(const signalVector &x);
73
74/** Setup the signal processing library */
Tom Tsou2079a3c2016-03-06 00:58:56 -080075bool sigProcLibSetup();
dburgessb3a0ca42011-10-12 07:44:40 +000076
77/** Destroy the signal processing library */
78void sigProcLibDestroy(void);
79
80/**
81 Convolve two vectors.
82 @param a,b The vectors to be convolved.
83 @param c, A preallocated vector to hold the convolution result.
84 @param spanType The type/span of the convolution.
Thomas Tsou3eaae802013-08-20 19:31:14 -040085 @return The convolution result or NULL on error.
dburgessb3a0ca42011-10-12 07:44:40 +000086*/
Thomas Tsou3f32ab52013-11-15 16:32:54 -050087signalVector *convolve(const signalVector *a, const signalVector *b,
88 signalVector *c, ConvType spanType,
89 size_t start = 0, size_t len = 0,
90 size_t step = 1, int offset = 0);
dburgessb3a0ca42011-10-12 07:44:40 +000091
92/**
dburgessb3a0ca42011-10-12 07:44:40 +000093 Frequency shift a vector.
94 @param y The frequency shifted vector.
95 @param x The vector to-be-shifted.
96 @param freq The digital frequency shift
97 @param startPhase The starting phase of the oscillator
98 @param finalPhase The final phase of the oscillator
99 @return The frequency shifted vector.
100*/
101signalVector* frequencyShift(signalVector *y,
102 signalVector *x,
103 float freq = 0.0,
104 float startPhase = 0.0,
105 float *finalPhase=NULL);
106
107/**
108 Correlate two vectors.
109 @param a,b The vectors to be correlated.
110 @param c, A preallocated vector to hold the correlation result.
111 @param spanType The type/span of the correlation.
112 @return The correlation result.
113*/
114signalVector* correlate(signalVector *a,
115 signalVector *b,
116 signalVector *c,
117 ConvType spanType,
118 bool bReversedConjugated = false,
119 unsigned startIx = 0,
120 unsigned len = 0);
121
Alexander Chemeris132fb242017-03-17 17:22:33 -0700122/** Operate soft slicer on a soft-bit vector */
123bool vectorSlicer(SoftVector *x);
dburgessb3a0ca42011-10-12 07:44:40 +0000124
125/** GMSK modulate a GSM burst of bits */
126signalVector *modulateBurst(const BitVector &wBurst,
dburgessb3a0ca42011-10-12 07:44:40 +0000127 int guardPeriodLength,
Thomas Tsou83e06892013-08-20 16:10:01 -0400128 int sps, bool emptyPulse = false);
dburgessb3a0ca42011-10-12 07:44:40 +0000129
Tom Tsoud3253432016-03-06 03:08:01 -0800130/** 8-PSK modulate a burst of bits */
131signalVector *modulateEdgeBurst(const BitVector &bits,
132 int sps, bool emptyPulse = false);
133
134/** Generate a EDGE burst with random payload - 4 SPS (625 samples) only */
135signalVector *generateEdgeBurst(int tsc);
136
Tom Tsou8ee2f382016-03-06 20:57:34 -0800137/** Generate an empty burst - 4 or 1 SPS */
138signalVector *generateEmptyBurst(int sps, int tn);
139
140/** Generate a normal GSM burst with random payload - 4 or 1 SPS */
141signalVector *genRandNormalBurst(int tsc, int sps, int tn);
142
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300143/** Generate an access GSM burst with random payload - 4 or 1 SPS */
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300144signalVector *genRandAccessBurst(int delay, int sps, int tn);
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300145
Tom Tsou8ee2f382016-03-06 20:57:34 -0800146/** Generate a dummy GSM burst - 4 or 1 SPS */
147signalVector *generateDummyBurst(int sps, int tn);
148
dburgessb3a0ca42011-10-12 07:44:40 +0000149/** Sinc function */
150float sinc(float x);
151
152/** Delay a vector */
Thomas Tsou94edaae2013-11-09 22:19:19 -0500153signalVector *delayVector(signalVector *in, signalVector *out, float delay);
dburgessb3a0ca42011-10-12 07:44:40 +0000154
155/** Add two vectors in-place */
156bool addVector(signalVector &x,
157 signalVector &y);
158
159/** Multiply two vectors in-place*/
160bool multVector(signalVector &x,
161 signalVector &y);
162
163/** Generate a vector of gaussian noise */
164signalVector *gaussianNoise(int length,
165 float variance = 1.0,
166 complex mean = complex(0.0));
167
168/**
169 Given a non-integer index, interpolate a sample.
170 @param inSig The signal from which to interpolate.
171 @param ix The index.
172 @return The interpolated signal value.
173*/
174complex interpolatePoint(const signalVector &inSig,
175 float ix);
176
177/**
178 Given a correlator output, locate the correlation peak.
179 @param rxBurst The correlator result.
180 @param peakIndex Pointer to value to receive interpolated peak index.
181 @param avgPower Power to value to receive mean power.
182 @return Peak value.
183*/
184complex peakDetect(const signalVector &rxBurst,
185 float *peakIndex,
186 float *avgPwr);
187
188/**
189 Apply a scalar to a vector.
190 @param x The vector of interest.
191 @param scale The scalar.
192*/
193void scaleVector(signalVector &x,
194 complex scale);
195
dburgessb3a0ca42011-10-12 07:44:40 +0000196/**
Alexander Chemeris1dd05cf2017-03-15 23:23:36 +0300197 Rough energy estimator.
198 @param rxBurst A GSM burst.
dburgessb3a0ca42011-10-12 07:44:40 +0000199 @param windowLength The number of burst samples used to compute burst energy
Alexander Chemeris1dd05cf2017-03-15 23:23:36 +0300200 @return The average power of the received burst.
dburgessb3a0ca42011-10-12 07:44:40 +0000201*/
Alexander Chemeris1dd05cf2017-03-15 23:23:36 +0300202float energyDetect(signalVector &rxBurst,
203 unsigned windowLength);
dburgessb3a0ca42011-10-12 07:44:40 +0000204
205/**
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700206 RACH aka Access Burst correlator/detector.
207 @param burst The received GSM burst of interest.
208 @param threshold The threshold that the received burst's post-correlator SNR is compared against to determine validity.
Thomas Tsoud24cc2c2013-08-20 15:41:45 -0400209 @param sps The number of samples per GSM symbol.
dburgessb3a0ca42011-10-12 07:44:40 +0000210 @param amplitude The estimated amplitude of received RACH burst.
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700211 @param toa The estimate time-of-arrival of received RACH burst.
212 @param max_toa The maximum expected time-of-arrival
213 @return 1 if threshold value is reached,
214 negative value (-SignalError) on error,
215 zero (SIGERR_NONE) if no burst is detected
dburgessb3a0ca42011-10-12 07:44:40 +0000216*/
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700217int detectRACHBurst(signalVector &burst,
218 float threshold,
Thomas Tsou3eaae802013-08-20 19:31:14 -0400219 int sps,
Alexander Chemeris130a8002015-06-09 20:52:11 -0400220 complex &amplitude,
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700221 float &toa,
222 unsigned max_toa);
dburgessb3a0ca42011-10-12 07:44:40 +0000223
224/**
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700225 GMSK Normal Burst correlator/detector.
dburgessb3a0ca42011-10-12 07:44:40 +0000226 @param rxBurst The received GSM burst of interest.
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700227 @param tsc Midamble type (0..7) also known as TSC
228 @param threshold The threshold that the received burst's post-correlator SNR is compared against to determine validity.
Thomas Tsoud24cc2c2013-08-20 15:41:45 -0400229 @param sps The number of samples per GSM symbol.
dburgessb3a0ca42011-10-12 07:44:40 +0000230 @param amplitude The estimated amplitude of received TSC burst.
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700231 @param toa The estimate time-of-arrival of received TSC burst.
232 @param max_toa The maximum expected time-of-arrival
233 @return 1 if threshold value is reached,
234 negative value (-SignalError) on error,
235 zero (SIGERR_NONE) if no burst is detected
dburgessb3a0ca42011-10-12 07:44:40 +0000236*/
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700237int analyzeTrafficBurst(signalVector &burst,
238 unsigned tsc,
239 float threshold,
Alexander Chemeris130a8002015-06-09 20:52:11 -0400240 int sps,
241 complex &amplitude,
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700242 float &toa,
243 unsigned max_toa);
dburgessb3a0ca42011-10-12 07:44:40 +0000244
245/**
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700246 EDGE/8-PSK Normal Burst correlator/detector
Tom Tsoud3253432016-03-06 03:08:01 -0800247 @param burst The received GSM burst of interest
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700248 @param tsc Midamble type (0..7) also known as TSC
249 @param threshold The threshold that the received burst's post-correlator SNR is compared against to determine validity.
Tom Tsoud3253432016-03-06 03:08:01 -0800250 @param sps The number of samples per GSM symbol.
251 @param amplitude The estimated amplitude of received TSC burst.
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700252 @param toa The estimate time-of-arrival of received TSC burst.
253 @param max_toa The maximum expected time-of-arrival
254 @return 1 if threshold value is reached,
255 negative value (-SignalError) on error,
256 zero (SIGERR_NONE) if no burst is detected
Tom Tsoud3253432016-03-06 03:08:01 -0800257*/
258int detectEdgeBurst(signalVector &burst,
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700259 unsigned tsc,
260 float threshold,
Tom Tsoud3253432016-03-06 03:08:01 -0800261 int sps,
262 complex &amplitude,
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700263 float &toa,
264 unsigned max_toa);
Tom Tsoud3253432016-03-06 03:08:01 -0800265
266/**
Alexander Chemeris4e6c9382017-03-17 15:24:18 -0700267 8-PSK/GMSK/RACH burst detector
268 @param burst The received GSM burst of interest
269 @param tsc Midamble type (0..7) also known as TSC
270 @param threshold The threshold that the received burst's post-correlator SNR is compared against to determine validity.
271 @param sps The number of samples per GSM symbol.
272 @param amplitude The estimated amplitude of received TSC burst.
273 @param toa The estimate time-of-arrival of received TSC burst (in symbols).
274 @param max_toa The maximum expected time-of-arrival (in symbols).
275 @return positive value (CorrType) if threshold value is reached,
276 negative value (-SignalError) on error,
277 zero (SIGERR_NONE) if no burst is detected
278*/
279int detectAnyBurst(signalVector &burst,
280 unsigned tsc,
281 float threshold,
282 int sps,
283 CorrType type,
284 complex &amp,
285 float &toa,
286 unsigned max_toa);
287
288/**
Tom Tsoud3253432016-03-06 03:08:01 -0800289 Downsample 4 SPS to 1 SPS using a polyphase filterbank
290 @param burst Input burst of at least 624 symbols
291 @return Decimated signal vector of 156 symbols
292*/
Tom Tsoud3253432016-03-06 03:08:01 -0800293signalVector *downsampleBurst(signalVector &burst);
294
295/**
dburgessb3a0ca42011-10-12 07:44:40 +0000296 Decimate a vector.
297 @param wVector The vector of interest.
Thomas Tsou94edaae2013-11-09 22:19:19 -0500298 @param factor Decimation factor.
dburgessb3a0ca42011-10-12 07:44:40 +0000299 @return The decimated signal vector.
300*/
Thomas Tsou94edaae2013-11-09 22:19:19 -0500301signalVector *decimateVector(signalVector &wVector, size_t factor);
dburgessb3a0ca42011-10-12 07:44:40 +0000302
303/**
Alexander Chemeris1c0b8b32017-03-17 16:12:47 -0700304 Demodulates a GMSK burst using a soft-slicer.
305 @param rxBurst The burst to be demodulated.
dburgessb3a0ca42011-10-12 07:44:40 +0000306 @param gsmPulse The GSM pulse.
Thomas Tsoud24cc2c2013-08-20 15:41:45 -0400307 @param sps The number of samples per GSM symbol.
dburgessb3a0ca42011-10-12 07:44:40 +0000308 @param channel The amplitude estimate of the received burst.
309 @param TOA The time-of-arrival of the received burst.
310 @return The demodulated bit sequence.
311*/
Alexander Chemeris1c0b8b32017-03-17 16:12:47 -0700312SoftVector *demodGmskBurst(signalVector &rxBurst, int sps,
313 complex channel, float TOA);
Tom Tsoud3253432016-03-06 03:08:01 -0800314
315/**
316 Demodulate 8-PSK EDGE burst with soft symbol ooutput
317 @param rxBurst The burst to be demodulated.
318 @param sps The number of samples per GSM symbol.
319 @param channel The amplitude estimate of the received burst.
320 @param TOA The time-of-arrival of the received burst.
321 @return The demodulated bit sequence.
322*/
323SoftVector *demodEdgeBurst(signalVector &rxBurst, int sps,
324 complex channel, float TOA);
325
kurtis.heimerl8aea56e2011-11-26 03:18:30 +0000326#endif /* SIGPROCLIB_H */