blob: 1b646cd7158e1cf663913e0ffc8110b63b865a1c [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
Tom Tsou577cd022015-05-18 13:57:54 -070036enum signalError {
37 SIGERR_NONE,
38 SIGERR_BOUNDS,
39 SIGERR_CLIP,
40 SIGERR_UNSUPPORTED,
41 SIGERR_INTERNAL,
42};
43
dburgessb3a0ca42011-10-12 07:44:40 +000044/** Convert a linear number to a dB value */
45float dB(float x);
46
47/** Convert a dB value into a linear value */
48float dBinv(float x);
49
50/** Compute the energy of a vector */
51float vectorNorm2(const signalVector &x);
52
53/** Compute the average power of a vector */
54float vectorPower(const signalVector &x);
55
56/** Setup the signal processing library */
Tom Tsou2079a3c2016-03-06 00:58:56 -080057bool sigProcLibSetup();
dburgessb3a0ca42011-10-12 07:44:40 +000058
59/** Destroy the signal processing library */
60void sigProcLibDestroy(void);
61
62/**
63 Convolve two vectors.
64 @param a,b The vectors to be convolved.
65 @param c, A preallocated vector to hold the convolution result.
66 @param spanType The type/span of the convolution.
Thomas Tsou3eaae802013-08-20 19:31:14 -040067 @return The convolution result or NULL on error.
dburgessb3a0ca42011-10-12 07:44:40 +000068*/
Thomas Tsou3f32ab52013-11-15 16:32:54 -050069signalVector *convolve(const signalVector *a, const signalVector *b,
70 signalVector *c, ConvType spanType,
71 size_t start = 0, size_t len = 0,
72 size_t step = 1, int offset = 0);
dburgessb3a0ca42011-10-12 07:44:40 +000073
74/**
dburgessb3a0ca42011-10-12 07:44:40 +000075 Frequency shift a vector.
76 @param y The frequency shifted vector.
77 @param x The vector to-be-shifted.
78 @param freq The digital frequency shift
79 @param startPhase The starting phase of the oscillator
80 @param finalPhase The final phase of the oscillator
81 @return The frequency shifted vector.
82*/
83signalVector* frequencyShift(signalVector *y,
84 signalVector *x,
85 float freq = 0.0,
86 float startPhase = 0.0,
87 float *finalPhase=NULL);
88
89/**
90 Correlate two vectors.
91 @param a,b The vectors to be correlated.
92 @param c, A preallocated vector to hold the correlation result.
93 @param spanType The type/span of the correlation.
94 @return The correlation result.
95*/
96signalVector* correlate(signalVector *a,
97 signalVector *b,
98 signalVector *c,
99 ConvType spanType,
100 bool bReversedConjugated = false,
101 unsigned startIx = 0,
102 unsigned len = 0);
103
104/** Operate soft slicer on real-valued portion of vector */
105bool vectorSlicer(signalVector *x);
106
107/** GMSK modulate a GSM burst of bits */
108signalVector *modulateBurst(const BitVector &wBurst,
dburgessb3a0ca42011-10-12 07:44:40 +0000109 int guardPeriodLength,
Thomas Tsou83e06892013-08-20 16:10:01 -0400110 int sps, bool emptyPulse = false);
dburgessb3a0ca42011-10-12 07:44:40 +0000111
Tom Tsoud3253432016-03-06 03:08:01 -0800112/** 8-PSK modulate a burst of bits */
113signalVector *modulateEdgeBurst(const BitVector &bits,
114 int sps, bool emptyPulse = false);
115
116/** Generate a EDGE burst with random payload - 4 SPS (625 samples) only */
117signalVector *generateEdgeBurst(int tsc);
118
Tom Tsou8ee2f382016-03-06 20:57:34 -0800119/** Generate an empty burst - 4 or 1 SPS */
120signalVector *generateEmptyBurst(int sps, int tn);
121
122/** Generate a normal GSM burst with random payload - 4 or 1 SPS */
123signalVector *genRandNormalBurst(int tsc, int sps, int tn);
124
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300125/** Generate an access GSM burst with random payload - 4 or 1 SPS */
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300126signalVector *genRandAccessBurst(int delay, int sps, int tn);
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300127
Tom Tsou8ee2f382016-03-06 20:57:34 -0800128/** Generate a dummy GSM burst - 4 or 1 SPS */
129signalVector *generateDummyBurst(int sps, int tn);
130
dburgessb3a0ca42011-10-12 07:44:40 +0000131/** Sinc function */
132float sinc(float x);
133
134/** Delay a vector */
Thomas Tsou94edaae2013-11-09 22:19:19 -0500135signalVector *delayVector(signalVector *in, signalVector *out, float delay);
dburgessb3a0ca42011-10-12 07:44:40 +0000136
137/** Add two vectors in-place */
138bool addVector(signalVector &x,
139 signalVector &y);
140
141/** Multiply two vectors in-place*/
142bool multVector(signalVector &x,
143 signalVector &y);
144
145/** Generate a vector of gaussian noise */
146signalVector *gaussianNoise(int length,
147 float variance = 1.0,
148 complex mean = complex(0.0));
149
150/**
151 Given a non-integer index, interpolate a sample.
152 @param inSig The signal from which to interpolate.
153 @param ix The index.
154 @return The interpolated signal value.
155*/
156complex interpolatePoint(const signalVector &inSig,
157 float ix);
158
159/**
160 Given a correlator output, locate the correlation peak.
161 @param rxBurst The correlator result.
162 @param peakIndex Pointer to value to receive interpolated peak index.
163 @param avgPower Power to value to receive mean power.
164 @return Peak value.
165*/
166complex peakDetect(const signalVector &rxBurst,
167 float *peakIndex,
168 float *avgPwr);
169
170/**
171 Apply a scalar to a vector.
172 @param x The vector of interest.
173 @param scale The scalar.
174*/
175void scaleVector(signalVector &x,
176 complex scale);
177
dburgessb3a0ca42011-10-12 07:44:40 +0000178/**
dburgessb3a0ca42011-10-12 07:44:40 +0000179 Energy detector, checks to see if received burst energy is above a threshold.
180 @param rxBurst The received GSM burst of interest.
181 @param windowLength The number of burst samples used to compute burst energy
182 @param detectThreshold The detection threshold, a linear value.
183 @param avgPwr The average power of the received burst.
184 @return True if burst energy is above threshold.
185*/
186bool energyDetect(signalVector &rxBurst,
187 unsigned windowLength,
188 float detectThreshold,
189 float *avgPwr = NULL);
190
191/**
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700192 RACH aka Access Burst correlator/detector.
193 @param burst The received GSM burst of interest.
194 @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 -0400195 @param sps The number of samples per GSM symbol.
dburgessb3a0ca42011-10-12 07:44:40 +0000196 @param amplitude The estimated amplitude of received RACH burst.
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700197 @param toa The estimate time-of-arrival of received RACH burst.
198 @param max_toa The maximum expected time-of-arrival
199 @return 1 if threshold value is reached,
200 negative value (-SignalError) on error,
201 zero (SIGERR_NONE) if no burst is detected
dburgessb3a0ca42011-10-12 07:44:40 +0000202*/
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700203int detectRACHBurst(signalVector &burst,
204 float threshold,
Thomas Tsou3eaae802013-08-20 19:31:14 -0400205 int sps,
Alexander Chemeris130a8002015-06-09 20:52:11 -0400206 complex &amplitude,
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700207 float &toa,
208 unsigned max_toa);
dburgessb3a0ca42011-10-12 07:44:40 +0000209
210/**
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700211 GMSK Normal Burst correlator/detector.
dburgessb3a0ca42011-10-12 07:44:40 +0000212 @param rxBurst The received GSM burst of interest.
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700213 @param tsc Midamble type (0..7) also known as TSC
214 @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 -0400215 @param sps The number of samples per GSM symbol.
dburgessb3a0ca42011-10-12 07:44:40 +0000216 @param amplitude The estimated amplitude of received TSC burst.
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700217 @param toa The estimate time-of-arrival of received TSC burst.
218 @param max_toa The maximum expected time-of-arrival
219 @return 1 if threshold value is reached,
220 negative value (-SignalError) on error,
221 zero (SIGERR_NONE) if no burst is detected
dburgessb3a0ca42011-10-12 07:44:40 +0000222*/
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700223int analyzeTrafficBurst(signalVector &burst,
224 unsigned tsc,
225 float threshold,
Alexander Chemeris130a8002015-06-09 20:52:11 -0400226 int sps,
227 complex &amplitude,
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700228 float &toa,
229 unsigned max_toa);
dburgessb3a0ca42011-10-12 07:44:40 +0000230
231/**
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700232 EDGE/8-PSK Normal Burst correlator/detector
Tom Tsoud3253432016-03-06 03:08:01 -0800233 @param burst The received GSM burst of interest
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700234 @param tsc Midamble type (0..7) also known as TSC
235 @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 -0800236 @param sps The number of samples per GSM symbol.
237 @param amplitude The estimated amplitude of received TSC burst.
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700238 @param toa The estimate time-of-arrival of received TSC burst.
239 @param max_toa The maximum expected time-of-arrival
240 @return 1 if threshold value is reached,
241 negative value (-SignalError) on error,
242 zero (SIGERR_NONE) if no burst is detected
Tom Tsoud3253432016-03-06 03:08:01 -0800243*/
244int detectEdgeBurst(signalVector &burst,
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700245 unsigned tsc,
246 float threshold,
Tom Tsoud3253432016-03-06 03:08:01 -0800247 int sps,
248 complex &amplitude,
Alexander Chemeris14d13b62017-03-17 15:12:17 -0700249 float &toa,
250 unsigned max_toa);
Tom Tsoud3253432016-03-06 03:08:01 -0800251
252/**
253 Downsample 4 SPS to 1 SPS using a polyphase filterbank
254 @param burst Input burst of at least 624 symbols
255 @return Decimated signal vector of 156 symbols
256*/
Tom Tsoud3253432016-03-06 03:08:01 -0800257signalVector *downsampleBurst(signalVector &burst);
258
259/**
dburgessb3a0ca42011-10-12 07:44:40 +0000260 Decimate a vector.
261 @param wVector The vector of interest.
Thomas Tsou94edaae2013-11-09 22:19:19 -0500262 @param factor Decimation factor.
dburgessb3a0ca42011-10-12 07:44:40 +0000263 @return The decimated signal vector.
264*/
Thomas Tsou94edaae2013-11-09 22:19:19 -0500265signalVector *decimateVector(signalVector &wVector, size_t factor);
dburgessb3a0ca42011-10-12 07:44:40 +0000266
267/**
268 Demodulates a received burst using a soft-slicer.
269 @param rxBurst The burst to be demodulated.
270 @param gsmPulse The GSM pulse.
Thomas Tsoud24cc2c2013-08-20 15:41:45 -0400271 @param sps The number of samples per GSM symbol.
dburgessb3a0ca42011-10-12 07:44:40 +0000272 @param channel The amplitude estimate of the received burst.
273 @param TOA The time-of-arrival of the received burst.
274 @return The demodulated bit sequence.
275*/
Thomas Tsou83e06892013-08-20 16:10:01 -0400276SoftVector *demodulateBurst(signalVector &rxBurst, int sps,
277 complex channel, float TOA);
Tom Tsoud3253432016-03-06 03:08:01 -0800278
279/**
280 Demodulate 8-PSK EDGE burst with soft symbol ooutput
281 @param rxBurst The burst to be demodulated.
282 @param sps The number of samples per GSM symbol.
283 @param channel The amplitude estimate of the received burst.
284 @param TOA The time-of-arrival of the received burst.
285 @return The demodulated bit sequence.
286*/
287SoftVector *demodEdgeBurst(signalVector &rxBurst, int sps,
288 complex channel, float TOA);
289
kurtis.heimerl8aea56e2011-11-26 03:18:30 +0000290#endif /* SIGPROCLIB_H */