% computeSPLbandpassfilt.m This function compute bandpass filtered spl from complex sum

function [ splBandPassFilt ] = computeSPLbandpassfilt( complexSum, fullScaleLevel, freqTot, bandpassLowCutFreq, bandpassHighCutFreq, ...
                               spsMean, fs, nfft, overlap, plotSignalEnable, limitFreq )
                                
    tmp                         = abs( freqTot - bandpassLowCutFreq );
    [~, idxLowBP]               = min( tmp );                                   % index of closest value
    tmp                         = abs( freqTot - bandpassHighCutFreq );
    [~, idxHighBP]              = min( tmp );                                   % index of closest value

    rmsBandPassFilt             = ( sqrt( sum( complexSum(:, idxLowBP:idxHighBP ), 2 ) ) / sqrt( 2 ) )';
    splBandPassFilt             = fullScaleLevel + 20 * log10( rmsBandPassFilt ); % 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( [ bandpassLowCutFreq bandpassLowCutFreq ], limitFreq, 'Color', 'red' );
        line( [ bandpassHighCutFreq bandpassHighCutFreq ], limitFreq, 'Color', 'red' );
        title( 'Band pass ')
        grid on
        xlim( [ 20 20000 ] )
        xlabel( 'Frequency [Hz]' )
        ylim( limitFreq )
        ylabel( '[dB]' )
        hold off

    end

end
