-
Notifications
You must be signed in to change notification settings - Fork 6
/
acquisition.m
331 lines (263 loc) · 13.8 KB
/
acquisition.m
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
function acqResults = acquisition(longSignal, settings)
%Function performs cold start acquisition on the collected "data". It
%searches for GPS signals of all satellites, which are listed in field
%"acqSatelliteList" in the settings structure. Function saves code phase
%and frequency of the detected signals in the "acqResults" structure.
%
%acqResults = acquisition(longSignal, settings)
%
% Inputs:
% longSignal - 11 ms of raw signal from the front-end
% settings - Receiver settings. Provides information about
% sampling and intermediate frequencies and other
% parameters including the list of the satellites to
% be acquired.
% Outputs:
% acqResults - Function saves code phases and frequencies of the
% detected signals in the "acqResults" structure. The
% field "carrFreq" is set to 0 if the signal is not
% detected for the given PRN number.
%--------------------------------------------------------------------------
% SoftGNSS v3.0
%
% Copyright (C) Darius Plausinaitis and Dennis M. Akos
% Written by Darius Plausinaitis and Dennis M. Akos
% Based on Peter Rinder and Nicolaj Bertelsen
%--------------------------------------------------------------------------
%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.
%--------------------------------------------------------------------------
%CVS record:
%$Id: acquisition.m,v 1.1.2.12 2006/08/14 12:08:03 dpl Exp $
%% Condition input signal to speed up acquisition =========================
% If input IF signal freq. is too high, a resampling strategy is applied
% to speed up the acquisition. This is user selectable.
if (settings.samplingFreq > settings.resamplingThreshold && ...
settings.resamplingflag == 1)
%--- Filiter out signal power outside the main lobe of CM code --------
fs = settings.samplingFreq;
IF = settings.IF;
% Bandwidth of CA mian lobe: 0.5e6 is a margin to make sure most of CA
% code power will not filtered out
BW = settings.codeFreqBasis*2 + 0.5e6;
% Filter parameter
w1 = (IF)-BW/2;
w2 = (IF)+BW/2;
wp = [w1*2/fs w2*2/fs];
% Filter coefficients
b = fir1(700,wp);
% Filter operation
longSignal = filtfilt(b,1,longSignal);
% --- Find resample frequency -----------------------------------------
% Refer to bandpass sampling theorem(Yi-Ran Sun,Generalized Bandpass
% Sampling Receivers for Software Defined Radio)
% Upper boundary frequency of the bandpass IF signal
fu = settings.IF + BW/2;
% Lower freq. of the acceptable sampling Freq. range
n = floor(fu/BW);
if (n<1)
n = 1;
end
lowerFreq = 2*fu/n;
% Lower boundary frequency of the bandpass IF signal
fl = settings.IF - BW/2;
% Upper boundary frequency of the acceptable sampling Freq. range
if(n>1)
upperFreq = 2*fl/(n-1);
else
upperFreq = lowerFreq;
end
% Save orignal Freq. for later use
oldFreq = settings.samplingFreq;
% Take the center of the acceptable sampling Freq. range as
% resampling frequency. As settings are used to generate local
% CM code samples, so assign the resampling freq. to settings.
% This can not change the settings.samplingFreq outside this
% acquisition function.
settings.samplingFreq = ceil((lowerFreq + upperFreq)/2);
%--- Downsample input IF signal -------------------------------------------------
% Signal length after resampling
signalLen = floor((length(longSignal)-1) /oldFreq * settings.samplingFreq);
% Resampled signal index
index = ceil((0:signalLen-1)/settings.samplingFreq * oldFreq);
index(1) = 1;
% Resampled signal
longSignal = longSignal(index);
% For later use
oldIF = settings.IF;
% Resampling is equivalent to down-converting the original IF by integer
% times of resampling freq.. So the IF after resampling is equivalent to:
settings.IF = rem(settings.IF,settings.samplingFreq);
end % resampling input IF signals
%% Acquisition initialization =============================================
% Find number of samples per spreading code
samplesPerCode = round(settings.samplingFreq / ...
(settings.codeFreqBasis / settings.codeLength));
% Create two 1msec vectors of data to correlate with and one with zero DC
signal1 = longSignal(1 : samplesPerCode);
signal2 = longSignal(samplesPerCode+1 : 2*samplesPerCode);
signal0DC = longSignal - mean(longSignal); %%Problems here....
% Find sampling period
ts = 1 / settings.samplingFreq;
% Find phase points of the local carrier wave
phasePoints = (0 : (samplesPerCode-1)) * 2 * pi * ts;
% Number of the frequency bins for the given acquisition band (500Hz steps)
numberOfFrqBins = round(settings.acqSearchBand * 2/500) + 1;
% Generate all C/A codes and sample them according to the sampling freq.
caCodesTable = makeCaTable(settings);
%--- Initialize arrays to speed up the code -------------------------------
% Search results of all frequency bins and code shifts (for one satellite)
results = zeros(numberOfFrqBins, samplesPerCode);
% Carrier frequencies of the frequency bins
frqBins = zeros(1, numberOfFrqBins);
%--- Initialize acqResults ------------------------------------------------
% Carrier frequencies of detected signals
acqResults.carrFreq = zeros(1, 32);
% PRN code phases of detected signals
acqResults.codePhase = zeros(1, 32);
% Correlation peak ratios of the detected signals
acqResults.peakMetric = zeros(1, 32);
fprintf('(');
% Perform search for all listed PRN numbers ...
for PRN = settings.acqSatelliteList
%% Coarse Acquisition ==============================================
%--- Perform DFT of PRN code ------------------------------------------
caCodeFreqDom = conj(fft(caCodesTable(PRN, :)));
%--- Make the correlation for whole frequency band (for all freq. bins)
for frqBinIndex = 1:numberOfFrqBins
%--- Generate carrier wave frequency grid (0.5kHz step) -----------
frqBins(frqBinIndex) = settings.IF - settings.acqSearchBand + ...
0.5e3 * (frqBinIndex - 1);
%--- Generate local sine and cosine -------------------------------
sigCarr = exp(1i*frqBins(frqBinIndex) * phasePoints);
%--- "Remove carrier" from the signal -----------------------------
I1 = real(sigCarr .* signal1);
Q1 = imag(sigCarr .* signal1);
I2 = real(sigCarr .* signal2);
Q2 = imag(sigCarr .* signal2);
%--- Convert the baseband signal to frequency domain --------------
IQfreqDom1 = fft(I1 + 1i*Q1);
IQfreqDom2 = fft(I2 + 1i*Q2);
%--- Multiplication in the frequency domain (correlation in time
%domain)
convCodeIQ1 = IQfreqDom1 .* caCodeFreqDom;
convCodeIQ2 = IQfreqDom2 .* caCodeFreqDom;
%--- Perform inverse DFT and store correlation results ------------
acqRes1 = abs(ifft(convCodeIQ1));
acqRes2 = abs(ifft(convCodeIQ2));
%--- Check which msec had the greater power and save that, will
%"blend" 1st and 2nd msec but will correct data bit issues
if (max(acqRes1) > max(acqRes2))
results(frqBinIndex, :) = acqRes1;
else
results(frqBinIndex, :) = acqRes2;
end
end % frqBinIndex = 1:numberOfFrqBins
%% Look for correlation peaks in the results ==============================
% Find the highest peak and compare it to the second highest peak
% The second peak is chosen not closer than 1 chip to the highest peak
%--- Find the correlation peak and the carrier frequency --------------
[~, frequencyBinIndex] = max(max(results, [], 2));
%--- Find code phase of the same correlation peak ---------------------
[peakSize, codePhase] = max(max(results));
%--- Find 1 chip wide C/A code phase exclude range around the peak ----
samplesPerCodeChip = round(settings.samplingFreq / settings.codeFreqBasis);
excludeRangeIndex1 = codePhase - samplesPerCodeChip;
excludeRangeIndex2 = codePhase + samplesPerCodeChip;
%--- Correct PRN code phase exclude range if the range includes array
%boundaries
if excludeRangeIndex1 < 1
codePhaseRange = excludeRangeIndex2 : ...
(samplesPerCode + excludeRangeIndex1);
elseif excludeRangeIndex2 >= samplesPerCode
codePhaseRange = (excludeRangeIndex2 - samplesPerCode) : ...
excludeRangeIndex1;
else
codePhaseRange = [1:excludeRangeIndex1, ...
excludeRangeIndex2 : samplesPerCode];
end
%--- Find the second highest correlation peak in the same freq. bin ---
secondPeakSize = max(results(frequencyBinIndex, codePhaseRange));
%--- Store result -----------------------------------------------------
acqResults.peakMetric(PRN) = peakSize/secondPeakSize;
% If the result is above threshold, then there is a signal ...
if (peakSize/secondPeakSize) > settings.acqThreshold
%% Fine resolution frequency search =======================================
%--- Indicate PRN number of the detected signal -------------------
fprintf('%02d ', PRN);
%--- Generate 10msec long C/A codes sequence for given PRN --------
caCode = generateCAcode(PRN);
codeValueIndex = floor((ts * (1:10*samplesPerCode)) / ...
(1/settings.codeFreqBasis));
longCaCode = caCode((rem(codeValueIndex, 1023) + 1));
%--- Remove C/A code modulation from the original signal ----------
% (Using detected C/A code phase)
xCarrier = ...
signal0DC(codePhase:(codePhase + 10*samplesPerCode-1)) ...
.* longCaCode;
%--- Compute the magnitude of the FFT, find maximum and the
%associated carrier frequency
%--- Find the next highest power of two and increase by 8x --------
fftNumPts = 8*(2^(nextpow2(length(xCarrier))));
%--- Compute the magnitude of the FFT, find maximum and the
%associated carrier frequency
fftxc = abs(fft(xCarrier, fftNumPts));
uniqFftPts = ceil((fftNumPts + 1) / 2);
[~, fftMaxIndex] = max(fftxc);
fftFreqBins = (0 : uniqFftPts-1) * settings.samplingFreq/fftNumPts;
if (fftMaxIndex > uniqFftPts) %and should validate using complex data
if (rem(fftNumPts,2)==0) %even number of points, so DC and Fs/2 computed
fftFreqBinsRev=-fftFreqBins((uniqFftPts-1):-1:2);
[~, fftMaxIndex] = max(fftxc((uniqFftPts+1):length(fftxc)));
acqResults.carrFreq(PRN) = -fftFreqBinsRev(fftMaxIndex);
else %odd points so only DC is not included
fftFreqBinsRev=-fftFreqBins((uniqFftPts):-1:2);
[~, fftMaxIndex] = max(fftxc((uniqFftPts+1):length(fftxc)));
acqResults.carrFreq(PRN) = fftFreqBinsRev(fftMaxIndex);
end
else
acqResults.carrFreq(PRN) = (-1)^(settings.fileType-1)*fftFreqBins(fftMaxIndex);
end
%signal found, if IF =0 just change to 1 Hz to allow processing
if(acqResults.carrFreq(PRN) == 0)
acqResults.carrFreq(PRN) = 1;
end
acqResults.codePhase(PRN) = codePhase;
%% Downsampling recovery ============================================
% Find acquisition results corresponding to orignal sampling freq
if (exist('oldFreq', 'var') && settings.resamplingflag == 1)
% Find code phase
acqResults.codePhase(PRN) = floor((codePhase - 1)/ ...
settings.samplingFreq * oldFreq)+1;
% Doppler frequency
if (settings.IF >= settings.samplingFreq/2)
% In this condition, the FFT computed freq. is symmetric
% with the true frequemcy about half of the sampling
% frequency, so we have the following:
IF_temp = settings.samplingFreq - settings.IF;
doppler = IF_temp - acqResults.carrFreq(PRN);
else
doppler = acqResults.carrFreq(PRN) - settings.IF;
end
% Carrier freq. corresponding to orignal sampling freq
acqResults.carrFreq(PRN) = doppler + oldIF;
end
else
%--- No signal with this PRN --------------------------------------
fprintf('. ');
end % if (peakSize/secondPeakSize) > settings.acqThreshold
end % for PRN = satelliteList
%=== Acquisition is over ==================================================
fprintf(')\n');