blob: a10d55135b799b70a044aac86aec334188e76c59 [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 */
Alexander Chemeris0229d222017-03-17 16:03:17 -070024#define NORMAL_BURST_NBITS 148
25#define EDGE_BURST_NBITS 444
26#define EDGE_BURST_NSYMS (EDGE_BURST_NBITS / 3)
Tom Tsoub0aefcb2016-03-06 03:44:34 -080027
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};
Alexander Chemerisf7717ac2017-03-17 15:32:26 -070044std::string corrTypeToString(CorrType corr);
45std::ostream& operator<<(std::ostream& os, CorrType corr);
Alexander Chemerisf9e78be2017-03-17 15:00:34 -070046
Alexander Chemeris4aa548f2017-03-17 15:23:06 -070047enum SignalError {
Tom Tsou577cd022015-05-18 13:57:54 -070048 SIGERR_NONE,
49 SIGERR_BOUNDS,
50 SIGERR_CLIP,
51 SIGERR_UNSUPPORTED,
52 SIGERR_INTERNAL,
53};
54
Alexander Chemerisb34e60c2017-03-17 15:29:09 -070055/*
56 * Burst detection threshold
57 *
58 * Decision threshold value for burst gating on peak-to-average value of
59 * correlated synchronization sequences. Lower values pass more bursts up
60 * to upper layers but will increase the false detection rate.
61 */
62#define BURST_THRESH 4.0
63
dburgessb3a0ca42011-10-12 07:44:40 +000064/** Convert a linear number to a dB value */
65float dB(float x);
66
67/** Convert a dB value into a linear value */
68float dBinv(float x);
69
70/** Compute the energy of a vector */
71float vectorNorm2(const signalVector &x);
72
73/** Compute the average power of a vector */
74float vectorPower(const signalVector &x);
75
76/** Setup the signal processing library */
Tom Tsou2079a3c2016-03-06 00:58:56 -080077bool sigProcLibSetup();
dburgessb3a0ca42011-10-12 07:44:40 +000078
79/** Destroy the signal processing library */
80void sigProcLibDestroy(void);
81
Alexander Chemeris0229d222017-03-17 16:03:17 -070082/**
83 Convolve two vectors.
84 @param a,b The vectors to be convolved.
85 @param c, A preallocated vector to hold the convolution result.
86 @param spanType The type/span of the convolution.
87 @return The convolution result or NULL on error.
dburgessb3a0ca42011-10-12 07:44:40 +000088*/
Thomas Tsou3f32ab52013-11-15 16:32:54 -050089signalVector *convolve(const signalVector *a, const signalVector *b,
90 signalVector *c, ConvType spanType,
91 size_t start = 0, size_t len = 0,
92 size_t step = 1, int offset = 0);
dburgessb3a0ca42011-10-12 07:44:40 +000093
Alexander Chemeris0229d222017-03-17 16:03:17 -070094/**
dburgessb3a0ca42011-10-12 07:44:40 +000095 Frequency shift a vector.
Alexander Chemeris0229d222017-03-17 16:03:17 -070096 @param y The frequency shifted vector.
97 @param x The vector to-be-shifted.
98 @param freq The digital frequency shift
99 @param startPhase The starting phase of the oscillator
100 @param finalPhase The final phase of the oscillator
101 @return The frequency shifted vector.
dburgessb3a0ca42011-10-12 07:44:40 +0000102*/
103signalVector* frequencyShift(signalVector *y,
Alexander Chemeris0229d222017-03-17 16:03:17 -0700104 signalVector *x,
105 float freq = 0.0,
106 float startPhase = 0.0,
107 float *finalPhase=NULL);
dburgessb3a0ca42011-10-12 07:44:40 +0000108
Alexander Chemeris0229d222017-03-17 16:03:17 -0700109/**
110 Correlate two vectors.
dburgessb3a0ca42011-10-12 07:44:40 +0000111 @param a,b The vectors to be correlated.
112 @param c, A preallocated vector to hold the correlation result.
113 @param spanType The type/span of the correlation.
114 @return The correlation result.
115*/
116signalVector* correlate(signalVector *a,
Alexander Chemeris0229d222017-03-17 16:03:17 -0700117 signalVector *b,
118 signalVector *c,
119 ConvType spanType,
dburgessb3a0ca42011-10-12 07:44:40 +0000120 bool bReversedConjugated = false,
Alexander Chemeris0229d222017-03-17 16:03:17 -0700121 unsigned startIx = 0,
122 unsigned len = 0);
dburgessb3a0ca42011-10-12 07:44:40 +0000123
Alexander Chemeris132fb242017-03-17 17:22:33 -0700124/** Operate soft slicer on a soft-bit vector */
125bool vectorSlicer(SoftVector *x);
dburgessb3a0ca42011-10-12 07:44:40 +0000126
127/** GMSK modulate a GSM burst of bits */
128signalVector *modulateBurst(const BitVector &wBurst,
Alexander Chemeris0229d222017-03-17 16:03:17 -0700129 int guardPeriodLength,
130 int sps, bool emptyPulse = false);
dburgessb3a0ca42011-10-12 07:44:40 +0000131
Tom Tsoud3253432016-03-06 03:08:01 -0800132/** 8-PSK modulate a burst of bits */
133signalVector *modulateEdgeBurst(const BitVector &bits,
134 int sps, bool emptyPulse = false);
135
136/** Generate a EDGE burst with random payload - 4 SPS (625 samples) only */
137signalVector *generateEdgeBurst(int tsc);
138
Tom Tsou8ee2f382016-03-06 20:57:34 -0800139/** Generate an empty burst - 4 or 1 SPS */
140signalVector *generateEmptyBurst(int sps, int tn);
141
142/** Generate a normal GSM burst with random payload - 4 or 1 SPS */
143signalVector *genRandNormalBurst(int tsc, int sps, int tn);
144
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300145/** Generate an access GSM burst with random payload - 4 or 1 SPS */
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300146signalVector *genRandAccessBurst(int delay, int sps, int tn);
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300147
Tom Tsou8ee2f382016-03-06 20:57:34 -0800148/** Generate a dummy GSM burst - 4 or 1 SPS */
149signalVector *generateDummyBurst(int sps, int tn);
150
dburgessb3a0ca42011-10-12 07:44:40 +0000151/** Sinc function */
152float sinc(float x);
153
154/** Delay a vector */
Thomas Tsou94edaae2013-11-09 22:19:19 -0500155signalVector *delayVector(signalVector *in, signalVector *out, float delay);
dburgessb3a0ca42011-10-12 07:44:40 +0000156
157/** Add two vectors in-place */
158bool addVector(signalVector &x,
Alexander Chemeris0229d222017-03-17 16:03:17 -0700159 signalVector &y);
dburgessb3a0ca42011-10-12 07:44:40 +0000160
161/** Multiply two vectors in-place*/
162bool multVector(signalVector &x,
163 signalVector &y);
164
165/** Generate a vector of gaussian noise */
166signalVector *gaussianNoise(int length,
167 float variance = 1.0,
168 complex mean = complex(0.0));
169
170/**
Alexander Chemeris0229d222017-03-17 16:03:17 -0700171 Given a non-integer index, interpolate a sample.
172 @param inSig The signal from which to interpolate.
173 @param ix The index.
174 @return The interpolated signal value.
dburgessb3a0ca42011-10-12 07:44:40 +0000175*/
176complex interpolatePoint(const signalVector &inSig,
Alexander Chemeris0229d222017-03-17 16:03:17 -0700177 float ix);
dburgessb3a0ca42011-10-12 07:44:40 +0000178
179/**
Alexander Chemeris0229d222017-03-17 16:03:17 -0700180 Given a correlator output, locate the correlation peak.
181 @param rxBurst The correlator result.
182 @param peakIndex Pointer to value to receive interpolated peak index.
183 @param avgPower Power to value to receive mean power.
184 @return Peak value.
dburgessb3a0ca42011-10-12 07:44:40 +0000185*/
186complex peakDetect(const signalVector &rxBurst,
Alexander Chemeris0229d222017-03-17 16:03:17 -0700187 float *peakIndex,
188 float *avgPwr);
dburgessb3a0ca42011-10-12 07:44:40 +0000189
190/**
191 Apply a scalar to a vector.
192 @param x The vector of interest.
193 @param scale The scalar.
194*/
195void scaleVector(signalVector &x,
Alexander Chemeris0229d222017-03-17 16:03:17 -0700196 complex scale);
dburgessb3a0ca42011-10-12 07:44:40 +0000197
dburgessb3a0ca42011-10-12 07:44:40 +0000198/**
Alexander Chemeris1dd05cf2017-03-15 23:23:36 +0300199 Rough energy estimator.
200 @param rxBurst A GSM burst.
dburgessb3a0ca42011-10-12 07:44:40 +0000201 @param windowLength The number of burst samples used to compute burst energy
Alexander Chemeris1dd05cf2017-03-15 23:23:36 +0300202 @return The average power of the received burst.
dburgessb3a0ca42011-10-12 07:44:40 +0000203*/
Alexander Chemeris1dd05cf2017-03-15 23:23:36 +0300204float energyDetect(signalVector &rxBurst,
205 unsigned windowLength);
dburgessb3a0ca42011-10-12 07:44:40 +0000206
207/**
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700208 RACH aka Access Burst correlator/detector.
209 @param burst The received GSM burst of interest.
210 @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 -0400211 @param sps The number of samples per GSM symbol.
dburgessb3a0ca42011-10-12 07:44:40 +0000212 @param amplitude The estimated amplitude of received RACH burst.
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700213 @param toa The estimate time-of-arrival of received RACH burst.
214 @param max_toa The maximum expected time-of-arrival
215 @return 1 if threshold value is reached,
216 negative value (-SignalError) on error,
217 zero (SIGERR_NONE) if no burst is detected
dburgessb3a0ca42011-10-12 07:44:40 +0000218*/
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700219int detectRACHBurst(signalVector &burst,
220 float threshold,
Thomas Tsou3eaae802013-08-20 19:31:14 -0400221 int sps,
Alexander Chemeris130a8002015-06-09 20:52:11 -0400222 complex &amplitude,
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700223 float &toa,
224 unsigned max_toa);
dburgessb3a0ca42011-10-12 07:44:40 +0000225
226/**
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700227 GMSK Normal Burst correlator/detector.
dburgessb3a0ca42011-10-12 07:44:40 +0000228 @param rxBurst The received GSM burst of interest.
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700229 @param tsc Midamble type (0..7) also known as TSC
230 @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 -0400231 @param sps The number of samples per GSM symbol.
dburgessb3a0ca42011-10-12 07:44:40 +0000232 @param amplitude The estimated amplitude of received TSC burst.
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700233 @param toa The estimate time-of-arrival of received TSC burst.
234 @param max_toa The maximum expected time-of-arrival
235 @return 1 if threshold value is reached,
236 negative value (-SignalError) on error,
237 zero (SIGERR_NONE) if no burst is detected
dburgessb3a0ca42011-10-12 07:44:40 +0000238*/
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700239int analyzeTrafficBurst(signalVector &burst,
240 unsigned tsc,
241 float threshold,
Alexander Chemeris130a8002015-06-09 20:52:11 -0400242 int sps,
243 complex &amplitude,
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700244 float &toa,
245 unsigned max_toa);
dburgessb3a0ca42011-10-12 07:44:40 +0000246
247/**
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700248 EDGE/8-PSK Normal Burst correlator/detector
Tom Tsoud3253432016-03-06 03:08:01 -0800249 @param burst The received GSM burst of interest
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700250 @param tsc Midamble type (0..7) also known as TSC
251 @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 -0800252 @param sps The number of samples per GSM symbol.
253 @param amplitude The estimated amplitude of received TSC burst.
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700254 @param toa The estimate time-of-arrival of received TSC burst.
255 @param max_toa The maximum expected time-of-arrival
256 @return 1 if threshold value is reached,
257 negative value (-SignalError) on error,
258 zero (SIGERR_NONE) if no burst is detected
Tom Tsoud3253432016-03-06 03:08:01 -0800259*/
260int detectEdgeBurst(signalVector &burst,
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700261 unsigned tsc,
262 float threshold,
Tom Tsoud3253432016-03-06 03:08:01 -0800263 int sps,
264 complex &amplitude,
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700265 float &toa,
266 unsigned max_toa);
Tom Tsoud3253432016-03-06 03:08:01 -0800267
268/**
Alexander Chemeris4e6c9382017-03-17 15:24:18 -0700269 8-PSK/GMSK/RACH burst detector
270 @param burst The received GSM burst of interest
271 @param tsc Midamble type (0..7) also known as TSC
272 @param threshold The threshold that the received burst's post-correlator SNR is compared against to determine validity.
273 @param sps The number of samples per GSM symbol.
274 @param amplitude The estimated amplitude of received TSC burst.
275 @param toa The estimate time-of-arrival of received TSC burst (in symbols).
276 @param max_toa The maximum expected time-of-arrival (in symbols).
277 @return positive value (CorrType) if threshold value is reached,
278 negative value (-SignalError) on error,
279 zero (SIGERR_NONE) if no burst is detected
280*/
281int detectAnyBurst(signalVector &burst,
282 unsigned tsc,
283 float threshold,
284 int sps,
285 CorrType type,
286 complex &amp,
287 float &toa,
288 unsigned max_toa);
289
290/**
Alexander Chemeris0229d222017-03-17 16:03:17 -0700291 Downsample 4 SPS to 1 SPS using a polyphase filterbank
Tom Tsoud3253432016-03-06 03:08:01 -0800292 @param burst Input burst of at least 624 symbols
293 @return Decimated signal vector of 156 symbols
294*/
Tom Tsoud3253432016-03-06 03:08:01 -0800295signalVector *downsampleBurst(signalVector &burst);
296
297/**
Alexander Chemeris0229d222017-03-17 16:03:17 -0700298 Decimate a vector.
dburgessb3a0ca42011-10-12 07:44:40 +0000299 @param wVector The vector of interest.
Thomas Tsou94edaae2013-11-09 22:19:19 -0500300 @param factor Decimation factor.
dburgessb3a0ca42011-10-12 07:44:40 +0000301 @return The decimated signal vector.
302*/
Thomas Tsou94edaae2013-11-09 22:19:19 -0500303signalVector *decimateVector(signalVector &wVector, size_t factor);
dburgessb3a0ca42011-10-12 07:44:40 +0000304
305/**
Alexander Chemeris1c0b8b32017-03-17 16:12:47 -0700306 Demodulates a GMSK burst using a soft-slicer.
307 @param rxBurst The burst to be demodulated.
dburgessb3a0ca42011-10-12 07:44:40 +0000308 @param gsmPulse The GSM pulse.
Thomas Tsoud24cc2c2013-08-20 15:41:45 -0400309 @param sps The number of samples per GSM symbol.
dburgessb3a0ca42011-10-12 07:44:40 +0000310 @param channel The amplitude estimate of the received burst.
311 @param TOA The time-of-arrival of the received burst.
312 @return The demodulated bit sequence.
313*/
Alexander Chemeris1c0b8b32017-03-17 16:12:47 -0700314SoftVector *demodGmskBurst(signalVector &rxBurst, int sps,
315 complex channel, float TOA);
Tom Tsoud3253432016-03-06 03:08:01 -0800316
317/**
318 Demodulate 8-PSK EDGE burst with soft symbol ooutput
Alexander Chemeris0229d222017-03-17 16:03:17 -0700319 @param rxBurst The burst to be demodulated.
Tom Tsoud3253432016-03-06 03:08:01 -0800320 @param sps The number of samples per GSM symbol.
321 @param channel The amplitude estimate of the received burst.
322 @param TOA The time-of-arrival of the received burst.
323 @return The demodulated bit sequence.
324*/
325SoftVector *demodEdgeBurst(signalVector &rxBurst, int sps,
326 complex channel, float TOA);
327
Alexander Chemeris6e1dffd2017-03-17 16:13:51 -0700328/** Demodulate burst basde on type and output soft bits */
329SoftVector *demodAnyBurst(signalVector &burst, int sps,
330 complex amp, float toa, CorrType type);
331
kurtis.heimerl8aea56e2011-11-26 03:18:30 +0000332#endif /* SIGPROCLIB_H */