function [trackResults, channel]= tracking_CAUC(fid_real,fid_imag, channel, settings)
% Performs code and carrier tracking for all channels.
%
%[trackResults, channel] = tracking(fid, channel, settings)
%
% Inputs:
% fid - file identifier of the signal record.
% channel - PRN, carrier frequencies and code phases of all
% satellites to be tracked (prepared by preRum.m from
% acquisition results).
% settings - receiver settings.
% Outputs:
% trackResults - tracking results (structure array). Contains
% in-phase prompt outputs and absolute starting
% positions of spreading codes, together with other
% observation data from the tracking loops. All are
% saved every millisecond.
%--------------------------------------------------------------------------
% 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.
%--------------------------------------------------------------------------
%CVS record:
%$Id: tracking.m,v 1.14.2.32 2007/01/30 09:45:12 dpl Exp $
%% Initialize result structure ============================================
% Channel status
trackResults.status = '-'; % No tracked signal, or lost lock
% The absolute sample in the record of the C/A code start:
trackResults.absoluteSample = zeros(1, settings.msToProcess);
% Freq of the C/A code:
trackResults.codeFreq = inf(1, settings.msToProcess);
% Frequency of the tracked carrier wave:
trackResults.carrFreq = inf(1, settings.msToProcess);
% Outputs from the correlators (In-phase):
trackResults.I_P = zeros(1, settings.msToProcess);
trackResults.I_E = zeros(1, settings.msToProcess);
trackResults.I_L = zeros(1, settings.msToProcess);
% Outputs from the correlators (Quadrature-phase):
trackResults.Q_E = zeros(1, settings.msToProcess);
trackResults.Q_P = zeros(1, settings.msToProcess);
trackResults.Q_L = zeros(1, settings.msToProcess);
% Loop discriminators
trackResults.dllDiscr = inf(1, settings.msToProcess);
trackResults.dllDiscrFilt = inf(1, settings.msToProcess);
trackResults.pllDiscr = inf(1, settings.msToProcess);
trackResults.pllDiscrFilt = inf(1, settings.msToProcess);
%--- Copy initial settings for all channels -------------------------------
trackResults = repmat(trackResults, 1, settings.numberOfChannels);
%% Initialize tracking variables ==========================================
codePeriods = settings.msToProcess; % For GPS one C/A code is one ms
%--- DLL variables --------------------------------------------------------
% Define early-late offset (in chips)
earlyLateSpc = settings.dllCorrelatorSpacing;
% Summation interval
PDIcode = 0.001;
% Calculate filter coefficient values
[tau1code, tau2code] = calcLoopCoef(settings.dllNoiseBandwidth, ...
settings.dllDampingRatio, ...
1.0);
%--- PLL variables --------------------------------------------------------
% Summation interval
PDIcarr = 0.001;
% Calculate filter coefficient values
[tau1carr, tau2carr] = calcLoopCoef(settings.pllNoiseBandwidth, ...
settings.pllDampingRatio, ...
0.25);
hwb = waitbar(0,'Tracking...');
%% Start processing channels ==============================================
for channelNr =1:settings.numberOfChannels
% Only process if PRN is non zero (acquisition was successful)
if (channel(channelNr).PRN ~= 0)
% channel(channelNr).PRN = 7;
% Save additional information - each channel's tracked PRN
trackResults(channelNr).PRN = channel(channelNr).PRN;
% Move the starting point of processing. Can be used to start the
% signal processing at any point in the data record (e.g. for long
% records). In addition skip through that data file to start at the
% appropriate sample (corresponding to code phase). Assumes sample
% type is schar (or 1 byte per sample)
fseek(fid_real, ...
settings.skipNumberOfBytes + channel(channelNr).codePhase-1, ...
'bof');
fseek(fid_imag, ...
settings.skipNumberOfBytes + channel(channelNr).codePhase-1, ...
'bof');
% Get a vector with the C/A code sampled 1x/chip
caCode = generateCAcode(channel(channelNr).PRN);
% Then make it possible to do early and late versions
caCode = [caCode(1023) caCode caCode(1)];
%--- Perform various initializations ------------------------------
% define initial code frequency basis of NCO
codeFreq = settings.codeFreqBasis;
% define residual code phase (in chips)
remCodePhase = 0.0;
% define carrier frequency which is used over whole tracking period
carrFreq = channel(channelNr).acquiredFreq;
carrFreqBasis = channel(channelNr).acquiredFreq;
% 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;
%=== Process the number of specified code periods =================
for loopCnt = 1:codePeriods
%% GUI update -------------------------------------------------------------
% The GUI is updated every 50ms. This way Matlab GUI is still
% responsive enough. At the same time Matlab is not occupied
% all the time with GUI task.
if (rem(loopCnt, 50) == 0)
try
waitbar(loopCnt/codePeriods, ...
hwb, ...
['Tracking: Ch ', int2str(channelNr), ...
' of ', int2str(settings.numberOfChannels), ...
'; PRN#', int2str(channel(channelNr).PRN), ...
'; Completed ',int2str(loopCnt), ...
' of ', int2str(codePeriods), ' msec']);
catch
% The progress bar was closed. It is used as a signal
% to stop, "cancel" processing. Exit.
disp('Progress bar closed, exiting...');
return
end
end
%% Read next block of data ------------------------------------------------
% Find the size of a "block" or code period in whole samples
% Update the phasestep based on code freq (variable) and
% sampling frequency (fixed)
codePhaseStep = codeFreq / settings.samplingFr