function navStartBit = findNavStartBit( msPerDataBit, secondarySeq, I_prompt )

%==============================================================================%
%                                                                              %
%   navStartBit = findNavStartBit( msPerDataBit, secondarySeq, I_prompt )      %
%                                                                              %
%   Find the navigation data bit transition index                              %
%   David S. De Lorenzo                                                        %
%   16 March 2006                                                              %
%                                                                              %
%------------------------------------------------------------------------------%
%                                                                              %
%   Input:                                                                     %
%        msPerDataBit  - milliseconds per data bit symbol                      %
%        secondarySeq  - secondary modulation on primary code                  %
%        I_prompt      - in-phase prompt correlator output                     %
%                                                                              %
%   Output:                                                                    %
%        navStartBit   - navigation data bit transition index                  %
%                                                                              %
%   Note: For real data files beginning with an extended sequence of           %
%         unchanging navigation data bits, this simple search/test             %
%         algorithm may incorrectly estimate the navigation data bit           %
%         transition boundary.  A quick work-around is to extend the           %
%         duration of the Pull-In Mode, keeping in mind that there             %
%         could be more than 400ms of unchanging navigation data               %
%         several times per 30 second C/A code navigation message.             %
%         Also, if carrier-phase lock is not established very early in         %
%         the pull-in mode, then there could be a moderate probability         %
%         of error in estimating the navigation data bit transition            %
%         index, since the I_prompt values will not cluster reliably           %
%         at +1 and -1 relative magnitudes.  The workaround again is           %
%         to extend the duration of the pull-in mode.                          %
%                                                                              %
%==============================================================================%
%                       GPS Software Receiver in Matlab                        %
%     Copyright (c) 2003-2017 David S. De Lorenzo / 2018-2023 Trimble Inc.     %
%==============================================================================%

% Dimension the test variable
testVec = zeros(1, msPerDataBit);

% Loop over navigation data bit index (e.g., from 1 to 20 ms for C/A-code)
for navBitIndex = 1:msPerDataBit
    
    % Loop over chunks (e.g., 20 ms chunks) in the tracked data record
    for chunkStart = 1:msPerDataBit:(length(I_prompt)-2*msPerDataBit)
        
        % Calculate & accumulate the correlator output total for this chunk
        idx = chunkStart + navBitIndex + (1:msPerDataBit) - 2;
        testVec(navBitIndex) = testVec(navBitIndex) ...
                               + abs( sum( secondarySeq .* I_prompt(idx) ) );
        
    end
    
end

% Determine the navigation data bit transition index
%   This is the index in the test vector with the highest
%   highest accumulation of I_prompt values.
[~,navStartBit] = max(testVec);

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