-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathTracking.py
executable file
·451 lines (345 loc) · 17.5 KB
/
Tracking.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#!/usr/bin/env python3
# This is based off of (essentially a Python port of) the tracking.m file included with SoftGNSS v3.0.
# The license that was included with that program is below:
#------------------------Original License----------------------------------
# SoftGNSS v3.0
#
# Copyright (C) Dennis M. Akos
# Written by Darius Plausinaitis and Dennis M. Akos
# Based on code by DMAkos Oct-1999
#--------------------------------------------------------------------------
#This program is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public License
#as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
#USA.
#--------------------------------------------------------------------------
import numpy as np
import matplotlib.pyplot as plt
import configparser
import Acquisition
import GoldCode
from GPSData import IQData
global GPS_conf
#np.set_printoptions(threshold=np.inf)
def main():
# Import data. Will read many ms at once, then process the blocks as needed.
# Need these to pass to importFile module
fs = 4.092*10**6 # Sampling Frequency [Hz]
numberOfMilliseconds = 350
sampleLength = numberOfMilliseconds*10**(-3)
bytesToSkip = 0
global GPS_conf
GPS_conf = configparser.ConfigParser()
GPS_conf.read('Settings.conf')
data = IQData()
# Uncomment one of these lines to choose between Launch12 or gps-sdr-sim data
# /home/evan/Capstone/gps/resources/[email protected]
#data.importFile('resources/[email protected]', fs, sampleLength, bytesToSkip)
#data.importFile('resources/[email protected]', fs, sampleLength, bytesToSkip)
#data.importFile('resources/test4092kHz.max', fs, sampleLength, bytesToSkip)
data.importFile('resources/Single4092KHz5s.max', fs, sampleLength, bytesToSkip)
RealDataOnly = True
#data.importFile('resources/Single4092KHz60s.max', fs, sampleLength, bytesToSkip, RealDataOnly)
#data.importFile('resources/Single4092KHz120s.max', fs, sampleLength, bytesToSkip, RealDataOnly)
# Set up demo acquisition result for file.
acqresult = Acquisition.SatStats()
theCodePhase = 630.251585
acqresult.CodePhaseSamples = int((1023.0 - theCodePhase)*4 + 1)
acqresult.FineFrequencyEstimate = -3363.8
acqresult.Sat = 1
chartOut = True
channel1 = Channel(data, acqresult, chartOut)
channel1.Track()
#channel1._writeBits()
channel1._writeBits2()
#channel1.GetEphemeris()
class Channel:
'''
Class that is a channel dedicated to tracking one satellite through a section of data.
At least 4 are required to get a pseudorange.
'''
def __init__(self, datain, acqData, chartoutput = True):
global GPS_conf
self.settings = GPS_conf['TRACKING']
#Acquisition inputs
self.data = datain
self.codePhase = acqData.CodePhaseSamples
self.acquiredCarrFreq = acqData.FineFrequencyEstimate
self.PRN = acqData.Sat # Value will be non-zero if Acquisition was successful for this channel
self.progress = True #Output progress
self.status = False # True if tracking was successful, False otherwise.
self.SamplesPerChip = int(float(GPS_conf['DATA']['fs']) /
float(self.settings['codeFreqBasis']))
#Tracking Result/Logging Parameters
self.outputChart = chartoutput
# We still need I_P for data processing, even without needing to plot.
#if self.outputChart:
if True:
#Preallocate space if charts are requested
self.absoluteSample = np.zeros(int(self.settings['msToProcess'])) # Sample that C/A code 1st starts.
self.codeFreq = np.zeros(int(self.settings['msToProcess'])) # C/A code frequency.
self.carrFreq = np.zeros(int(self.settings['msToProcess'])) # Frequency of tracked carrier.
self.I_P = np.zeros(int(self.settings['msToProcess'])) # Correlator outputs (resulting sum).
self.I_E = np.zeros(int(self.settings['msToProcess'])) # Correlator outputs (resulting sum).
self.I_L = np.zeros(int(self.settings['msToProcess'])) # Correlator outputs (resulting sum).
self.Q_P = np.zeros(int(self.settings['msToProcess'])) # Correlator outputs (resulting sum).
self.Q_E = np.zeros(int(self.settings['msToProcess'])) # Correlator outputs (resulting sum).
self.Q_L = np.zeros(int(self.settings['msToProcess'])) # Correlator outputs (resulting sum).
self.dllDiscr = np.zeros(int(self.settings['msToProcess'])) # Code-Loop discriminator
self.dllDiscrFilt = np.zeros(int(self.settings['msToProcess'])) # Code-Loop discriminator filter
self.pllDiscr = np.zeros(int(self.settings['msToProcess'])) # Carrier-Loop discriminator
self.pllDiscrFilt = np.zeros(int(self.settings['msToProcess'])) # Carrier-Loop discriminator filter
def Track(self):
'''
Retrieves data from data array (self.data), and aligns the replica code and
carrier to get navigation bits. Takes no arguments, but reads from self.data,
and outputs navigation data on self.I_P.
'''
global GPS_conf
# Calculate filter coefficient values for code loop
coeffCode1, coeffCode2 = self._calcLoopCoef(float(self.settings['codeLoopNoiseBandwidth'])
, float(self.settings['codeZeta']), float(self.settings['codeLoopGain']))
# Calculate filter coefficient values for carrier loop
coeffCar1, coeffCar2 = self._calcLoopCoef(float(self.settings['carrLoopNoiseBandwidth'])
, float(self.settings['carrZeta']), float(self.settings['carrLoopGain']))
# Process each channel (Will impliment loop in future. For now only processing one channel)
# Process channel if PRN is non-zero (Acquisition successful)
if self.PRN:
# Create instance of TrackingResults to store results into
CACode = GoldCode.getTrackingCode(self.PRN)
# Perform additional initializations:
codeFreq = float(self.settings['codeFreqBasis'])
# Residual code/carrier phase
remCodePhase = 0.0
# define residual carrier phase
remCarrPhase = 0.0
# code tracking loop parameters
oldCodeNco = 0.0
oldCodeError = 0.0
# carrier/Costas loop parameters
oldCarrNco = 0.0
oldCarrError = 0.0
dataPosition = 0
blksize = 0
carrFreq = self.acquiredCarrFreq
#Pre-cast configuration parameters
ms = int(self.settings['msToProcess'])
fs = float(GPS_conf['DATA']['fs'])
codeLength = int(self.settings['codeLength'])
earlyLateSpacing = float(self.settings['earlyLateSpacing'])
chippingRate = float(self.settings['codeFreqBasis'])
# Process the requested number of code periods (num of ms to process)
for loopCount in range(0, ms):
if self.progress:
print("------- %2.1f percent complete --------"%((loopCount/ms)*100), end = '\r')
# Update the phasestep based on code freq (variable) and
# sampling frequency (fixed)
codePhaseStep = np.real(codeFreq / fs)
#print("Old blksize: %d"%blksize)
blksize = int(np.ceil((codeLength-remCodePhase) / codePhaseStep))
#print("New blksize: %d"%blksize)
#print("Old remCodePhase: %f" %remCodePhase)
# Read in the appropriate number of samples to process this
# iteration
rawSignal = self.data.IData[self.codePhase + dataPosition: self.codePhase + dataPosition + blksize]
dataPosition = dataPosition + blksize
# Generate Early CA Code.
tStart = remCodePhase - earlyLateSpacing
tStep = codePhaseStep
tEnd = ((blksize-1)*codePhaseStep+remCodePhase) + codePhaseStep - earlyLateSpacing
tcode = np.linspace(tStart,tEnd,blksize,endpoint=False)
tcode2 = (np.ceil(tcode)).astype(int)
earlyCode = CACode[tcode2]
# Generate Late CA Code.
tStart = remCodePhase + earlyLateSpacing
tStep = codePhaseStep
tEnd = ((blksize-1)*codePhaseStep+remCodePhase) + codePhaseStep + earlyLateSpacing
tcode = np.linspace(tStart,tEnd,blksize,endpoint=False)
tcode2 = (np.ceil(tcode)).astype(int)
lateCode = CACode[tcode2]
# Generate Prompt CA Code.
tStart = remCodePhase
tStep = codePhaseStep
tEnd = ((blksize-1)*codePhaseStep+remCodePhase) + codePhaseStep
tcode = np.linspace(tStart,tEnd,blksize,endpoint=False)
tcode2 = (np.ceil(tcode)).astype(int)
promptCode = CACode[tcode2]
# Figure out remaining code phase (uses tcode from Prompt CA Code generation):
remCodePhase = (tcode[blksize-1]) - 1023.00
if abs(remCodePhase) > codePhaseStep:
remCodePhase = sign(remCodePhase)*codePhaseStep
else:
remCodePhase = 0
#print("remCodePhase: %f" %remCodePhase)
# Generate the carrier frequency to mix the signal to baseband
time = np.array(range(0,blksize+1))/fs
#print("Length of time array for cos and sin: %d" %len(time))
# Get the argument to sin/cos functions
trigarg = ((carrFreq * 2.0 * np.pi) * time) + remCarrPhase
# Carry the leftover phase to the next argument by looking at the last element
remCarrPhase = trigarg[blksize] % (2 * np.pi)
# Finally compute the signal to mix the collected data to baseband
carrCos = np.cos(trigarg[0:blksize])
carrSin = np.sin(trigarg[0:blksize])
# First mix to baseband
qBasebandSignal = carrCos * rawSignal
iBasebandSignal = carrSin * rawSignal
# Now get early, late, and prompt values for each
I_E = np.sum(earlyCode * iBasebandSignal)
Q_E = np.sum(earlyCode * qBasebandSignal)
I_P = np.sum(promptCode * iBasebandSignal)
Q_P = np.sum(promptCode * qBasebandSignal)
I_L = np.sum(lateCode * iBasebandSignal)
Q_L = np.sum(lateCode * qBasebandSignal)
# Find PLL error and update carrier NCO
# Implement carrier loop discriminator (phase detector)
carrError = np.arctan(Q_P / I_P) / (2.0 * np.pi)
# Implement carrier loop filter and generate NCO command
carrNco = oldCarrNco + coeffCar1 * (carrError - oldCarrError) + carrError * coeffCar2
oldCarrNco = carrNco
oldCarrError = carrError
# Modify carrier freq based on NCO command
carrFreq = self.acquiredCarrFreq + carrNco
# Find DLL error and update code NCO -------------------------------------
codeError = (np.sqrt(I_E * I_E + Q_E * Q_E) - np.sqrt(I_L * I_L + Q_L * Q_L)) /\
(np.sqrt(I_E * I_E + Q_E * Q_E) + np.sqrt(I_L * I_L + Q_L * Q_L))
# Implement code loop filter and generate NCO command
codeNco = oldCodeNco + coeffCode1 * (codeError - oldCodeError) + codeError * coeffCode2
oldCodeNco = codeNco
oldCodeError = codeError
# Modify code freq based on NCO command
codeFreq = chippingRate - codeNco
if self.outputChart:
self.pllDiscr[loopCount] = carrError
self.carrFreq[(loopCount)] = carrFreq # Return real value only?
self.codeFreq[(loopCount)] = codeFreq
self.I_E[loopCount] = I_E
self.I_P[loopCount] = I_P
self.I_L[loopCount] = I_L
self.Q_E[loopCount] = Q_E
self.Q_P[loopCount] = Q_P
self.Q_L[loopCount] = Q_L
if self.outputChart:
self._plotOutputs()
def _plotOutputs(self):
plt.plot(self.carrFreq)
plt.ylabel("PLL Frequency (Hz)")
plt.xlabel("t (ms)")
plt.title("Carrier frequency of NCO")
plt.show()
plt.subplot(2,1,1)
plt.plot(self.I_E**2,label="I_E")
plt.plot(self.I_P**2,label="I_P")
plt.plot(self.I_L**2,label="I_L")
plt.title("DLL Inphase")
plt.legend()
plt.subplot(2,1,2)
plt.plot(self.Q_E**2,label="Q_E")
plt.plot(self.Q_P**2,label="Q_P")
plt.plot(self.Q_L**2,label="Q_L")
plt.title("DLL Quadrature")
plt.xlabel("t (ms)")
plt.show()
SatelliteData = self.I_P
for ind,IP in enumerate(SatelliteData):
if IP > 0.1:
SatelliteData[ind] = 1
elif IP < 0.1:
SatelliteData[ind] = 0
plt.plot(SatelliteData)
plt.ylim([-.5,1.5])
plt.title("50bps Navigation Data (from I_P)")
plt.show()
#plt.plot(self.pllDiscr)
#plt.show()
def _calcLoopCoef(self, LoopNoiseBandwidth, Zeta, LoopGain):
'''
Calculates the loop coefficients tau1 and tau2.
This process is discussed in sections 7.1-7.3 of Borre.
'''
# Solve for the natural frequency
Wn = LoopNoiseBandwidth*8*Zeta / (4*Zeta**2 + 1)
# Solve for tau1 and tau2
tau1 = LoopGain / (Wn * Wn);
tau2 = (2.0 * Zeta) / Wn;
coeff1 = tau2/tau1
coeff2 = float(self.settings['sumInt'])/tau1
return (coeff1, coeff2)
def _writeBits(self, dr = '.', name = 'default'):
'''
Writes out the navigation data bits to a file for analysis using navigation tools. This version is
not fully working.
'''
if name == 'default':
name = 'SV%s.bin'%self.PRN
# First find a bit transition to be the starting index of the integration
start = np.sign(self.I_P[0])
startInd = 0
for samp in self.I_P:
if (np.sign(samp) != start ):
break
else:
startInd += 1
#Start integrating bits in groups of 20ms
ptr = 0
bits = np.zeros(len(self.I_P)/20)
for ind in range(startInd, len(self.I_P), 20):
m = np.mean(self.I_P[ind:ind+20])
if np.sign(m) == 1:
bits[ptr] = 1
elif np.sign(m) == -1:
bits[ptr] = 0
else:
pass
#raise BitsError(ind)
ptr += 1
#Write out the ms offset, followed by bitstream
with open( '%s/%s'%(dr, name),'w') as f:
f.write("%1d"%startInd)
f.writelines(["%3d" % item for item in bits])
print()
print("File written to: %s"%f.name)
def _writeBits2(self, dr = '.', name = 'default'):
'''
Writes out the navigation data bits to a file, revised from _writeBits()
# kwArgs
dr: directory where the file of bits ends up. Default is the cwd.
name: prefix of the name of the exported file to differetiate it from other channels.
default is to use SV followed by the identification number of the satellite.
# ToDo
- Add a timestamp option for the filename. This differetiates the file from other 'runs'
of the tracking algorithm.
'''
if name == 'default':
name = 'SV%s.bin'%self.PRN
# Quantize I_P levels to be either 0 or 1
SatelliteData = np.zeros(len(self.I_P), dtype=int)
self.SatelliteBits = []
for ind,IP in enumerate(self.I_P):
if IP >= 0.1:
SatelliteData[ind] = 1
elif IP < 0.1:
SatelliteData[ind] = 0
# Save value every 20ms (choose middle value)
if (ind%20 == 10): # If middle of 20ms chunk
self.SatelliteBits.append(SatelliteData[ind])# store bit value
fHandle = open(name,'wb')
fHandle.write(bytes(self.SatelliteBits))
print()
print("Total data bits: %d, written to file: %s."%(len(self.SatelliteBits),fHandle.name))
class BitsError(Exception):
def __init__(self, index):
self.message = "Integration Error at index %d"%index
print(self.message)
if __name__ == "__main__":
main()