function settings = acquisitionSettingsSecondary( settings )

%==============================================================================%
%                                                                              %
%   settings = acquisitionSettingsSecondary( settings )                        %
%                                                                              %
%   Calculate secondary settings                                               %
%   David S. De Lorenzo                                                        %
%   30 January 2008                                                            %
%                                                                              %
%==============================================================================%
%                       GPS Software Receiver in Matlab                        %
%     Copyright (c) 2003-2017 David S. De Lorenzo / 2018-2023 Trimble Inc.     %
%==============================================================================%

%=== Integer number of samples in one millisecond ==============================
%    This is the number of samples that are read each ~1ms block.
%    Note:  Subtracting 3/4 samples before the 'round' operation
%           guarantees that the duration of each ~1ms block is
%           always __shorter__ than one millisecond, which causes
%           the code-phase rollover to march forwards until it
%           overruns the sample window.  This means that during
%           signal tracking, there might be a read/process of a
%           ~1ms signal block without a corresponding increment
%           of the tracking duration millisecond counter or a
%           call to the tracking loops function.
settings.samplesPer1ms = round(settings.samplingFreq * 0.001 - 3/4);

%=== Time in one millisecond ==================================================
%    This is the actual elapsed time in each ~1ms block.
settings.timePer1ms = settings.samplesPer1ms / settings.samplingFreq;

%=== Extra time dropped (or picked up) in each millisecond of signal ===========
settings.extraTimePer1ms = settings.timePer1ms - 0.001;

%=== Fractional sample dropped (or picked up) in each millisecond of signal ====
settings.extraSamplePer1ms = settings.extraTimePer1ms * settings.samplingFreq;

%=== Relative time/phase of each sample point w.r.t. the epoch of sampling =====
settings.timePts  = (0:settings.samplesPer1ms-1) / settings.samplingFreq;
settings.phasePts = 2 * pi * settings.timePts;

%=== Number of bytes per element ===============================================
switch settings.dataFormat
    case {'uchar', 'schar', 'int8', 'uint8'}
        bytesPerElement = 1;
    case {'int16', 'uint16'}
        bytesPerElement = 2;
    case {'int32', 'uint32', 'single', 'float32'}
        bytesPerElement = 4;
    case {'int64', 'uint64', 'double', 'float64'}
        bytesPerElement = 8;
    otherwise
        if strncmp( settings.dataFormat, 'bitN', 3 )
            bytesPerElement = str2double(settings.dataFormat(4:end))/8;
        elseif strncmp( settings.dataFormat, 'ubitN', 4 )
            bytesPerElement = str2double(settings.dataFormat(5:end))/8;
        else
            error('***  Data format not supported!!  ***');
        end
end

%=== Number of bytes per sample ================================================
switch settings.dataType
    case 'real'
        settings.bytesPerSample = bytesPerElement;
    case 'complex'
        settings.bytesPerSample = 2*bytesPerElement;
    otherwise
        error('***  Data type not supported!!  ***');
end

%=== Satellite signal parameters ===============================================
switch settings.signalType
    case 'L1-C/A'
        settings.signalFreq     = 1575.42e6;    %  [Hz]      (L1  == 1575.42e6)
        settings.codeFreq       =   1.023e6;    %  [Hz]      (C/A ==  1.023e6)
        settings.chipsPer1ms    =  1023;        % [chips]    (C/A ==   1023)
        settings.secondarySeq   = zeros(1,20);
    case 'L1-10Mchip_test_code'
        settings.signalFreq     = 1575.42e6;    %  [Hz]      (L1   == 1575.42e6)
        settings.codeFreq       =   10.23e6;    %  [Hz]      (test == 10.23e6)
        settings.chipsPer1ms    =  10230;       % [chips]    (test ==  10230)
        settings.secondarySeq   = zeros(1,20);
    case 'L5-Inphase'
        settings.signalFreq     = 1176.45e6;    %  [Hz]      (L5  == 1176.45e6)
        settings.codeFreq       =   10.23e6;    %  [Hz]      (L5  ==  10.23e6)
        settings.chipsPer1ms    =  10230;       % [chips]    (L5  ==   10230)
        % The 10-bit Neuman-Hofman code on the L5 in-phase data channel
        settings.secondarySeq   = [0 0 0 0 1 1 0 1 0 1];
    case 'L5-Quadrature'
        settings.signalFreq     = 1176.45e6;    %  [Hz]      (L5  == 1176.45e6)
        settings.codeFreq       =   10.23e6;    %  [Hz]      (L5  ==  10.23e6)
        settings.chipsPer1ms    =  10230;       % [chips]    (L5  ==   10230)
        % The 20-bit Neuman-Hofman code on the L5 quadrature pilot channel
        settings.secondarySeq   = [0 0 0 0 0 1 0 0 1 1 0 1 0 1 0 0 1 1 1 0];
    case 'GLONASS/G3-Data'
        settings.signalFreq     = 1202.025e6;   %  [Hz]      (L3  == 1202.025e6)
        settings.codeFreq       =   10.23e6;    %  [Hz]      (L3  ==  10.23e6)
        settings.chipsPer1ms    =  10230;       % [chips]    (L3  ==   10230)
        % The 5-bit Barker code on the L3 in-phase data channel
        settings.secondarySeq   = [0 0 0 1 0];
    case 'GLONASS/G3-Pilot'
        settings.signalFreq     = 1202.025e6;   %  [Hz]      (L3  == 1202.025e6)
        settings.codeFreq       =   10.23e6;    %  [Hz]      (L3  ==  10.23e6)
        settings.chipsPer1ms    =  10230;       % [chips]    (L3  ==   10230)
        % The 10-bit Neuman-Hofman code on the L3 quadrature pilot channel
        settings.secondarySeq   = [0 0 0 0 1 1 0 1 0 1];
    otherwise
        error('***  Invalid signal type!!  ***');
end
settings.secondarySeq = 2*settings.secondarySeq - 1;

%=== Milliseconds per data bit =================================================
%    The number of milliseconds per data bit is a receiver
%    setting that dictates how many 1ms signal blocks will
%    share carrier-phase polarity.  For acquisition, this
%    places a limit on the amount of coherent integration
%    that can occur before a potential navigation data bit
%    polarity reversal, and for tracking this places a
%    limit on how many milliseconds of accumulation can be
%    used in computing the carrier-phase and code-phase
%    discriminators for the PLL and DLL, respectively.
settings.msPerDataBit = length(settings.secondarySeq);

%=== Set scale factor for carrier-aiding of the code loop ======================
%    This is the ratio of the code frequency to the RF carrier frequency.
settings.carrierAidingSF = settings.codeFreq / settings.signalFreq;

%=== Doppler estimate - either one value for all satellites ====================
%    or separate values for each and every satellite.
if (length(settings.acq.dopplerEstimateVec) == 1)
    settings.acq.dopplerEstimateVec = ...
        repmat(settings.acq.dopplerEstimateVec, ...
               1, length(settings.acq.prnSearchVec));
elseif (length(settings.acq.dopplerEstimateVec) ~= ...
        length(settings.acq.prnSearchVec))
    error('***  Invalid Doppler estimate(s) specified!!  ***');
end

return
%===============================================================================