% computeSPLlowpassfilt.m This function compute lowpass filtered spl from complex sum

function [ splLowPassFilt ] = computeSPLlowpassfilt( complexSum, fullScaleLevel, freqTot, lowPassCutFreq, spsMean, ...
                              fs, nfft, overlap, plotSignalEnable, limitFreq )
                                
    tmp                         = abs( freqTot - lowPassCutFreq );
    [ ~, idxLP ]                = min( tmp );                                   % index of closest value

    rmsLowPassFilt              = ( sqrt( sum( complexSum( :, 1:idxLP ), 2 ) ) / sqrt( 2 ) )';
    splLowPassFilt              = fullScaleLevel + 20 * log10( rmsLowPassFilt );  % convert rms to SPL

    if plotSignalEnable

        figure    
        halfFrf                 = avgFft( spsMean, nfft, overlap );
        amplitudeFrfdB          = 20 * log10( mean( abs( halfFrf ), 3 ) );
        freq                    = linspace( 0, fs/2, nfft/2+1 ); 
        semilogx( freq, amplitudeFrfdB )
        hold on
        line( [ lowPassCutFreq lowPassCutFreq ], limitFreq, 'Color', 'red' );
        title( 'Low pass' )
        grid on
        xlim( [ 20 20000 ] )
        xlabel( 'Frequency [Hz]' )
        ylim( limitFreq )
        ylabel( '[dB]' )
        hold off

    end

end
