% frameProcessing.m This function adjust length of signal for video analysis

function [ DoA ] = frameProcessing_ambisonics_fd( ambixFormat, blockIndex, stepSize, bufferSize, pwd, mvdr, music, ...
                                                  grid_dirs, sourceNum, window, energyWinFactor, index_o2_lowF, ...
                                                  index_o3_lowF, index_o4_lowF, index_o2_highF, ...
                                                  index_o3_highF, index_o4_highF, ambixOrder )

    % take a buffer
    ambixFormatChunk    = ambixFormat( (blockIndex-1) * stepSize + 1 : (blockIndex-1) * stepSize + bufferSize, : );

    % apply window and do fft
    ambixFormatChunkFft = fft( ambixFormatChunk .* window, bufferSize, 1 ) / bufferSize;
    ambixFormatChunkFft = 2 .* ambixFormatChunkFft( 1 : bufferSize/2 + 1, : ) * energyWinFactor;
    
    % apply frequency limit for orders > 1
    if ambixOrder == 2
        ambixFormatChunkFft( 1 : index_o2_lowF, 5 : 9 )         = 0;
        ambixFormatChunkFft( index_o2_highF : end, 5 : 9 )      = 0;
    elseif ambixOrder == 3
        ambixFormatChunkFft( 1 : index_o2_lowF, 5 : 9 )         = 0;
        ambixFormatChunkFft( index_o2_highF : end, 5 : 9 )      = 0;
        ambixFormatChunkFft( 1 : index_o3_lowF, 10 : 16 )       = 0;
        ambixFormatChunkFft( index_o3_highF : end, 10 : 16 )    = 0;
    elseif ambixOrder == 4
        ambixFormatChunkFft( 1 : index_o2_lowF, 5 : 9 )         = 0;
        ambixFormatChunkFft( index_o2_highF : end, 5 : 9 )      = 0;
        ambixFormatChunkFft( 1 : index_o3_lowF, 10 : 16 )       = 0;
        ambixFormatChunkFft( index_o3_highF : end, 10 : 16 )    = 0;
        ambixFormatChunkFft( 1 : index_o4_lowF, 17 : 25 )       = 0;
        ambixFormatChunkFft( index_o4_highF : end, 17 : 25 )    = 0;
    end

    % calculate spherical covariance
    sphCov              = cov( ambixFormatChunkFft );
    
    if pwd == 1
        [ DoA, ~ ]      = sphPWDmap( sphCov, grid_dirs, 0 );
    elseif mvdr == 1
        [ DoA, ~ ]      = sphMVDRmap( sphCov, grid_dirs, 0 );
    elseif music == 1
        [ DoA, ~ ]      = sphMUSIC( sphCov, grid_dirs, sourceNum );
    end

    DoA                 = abs( DoA );
    
end
