% noiseTrim.m This function perform NOISE trimming

function [ spsFormat, referenceMic ] = noiseTrim( spsFormat, referenceMic )

spsMean         = mean( spsFormat, 2 );                                     % for plot      

ok              = 0;
    
while ok == 0

    close all
        
    fig             = figure( 'windowstate', 'maximized' );
    hax             = axes;
    plot( spsMean )
    grid on
    xlim( [ 0 size( spsMean, 1 ) ] ) 
    xlabel( 'Samples' )
    title( 'Click on first sample and hit ENTER...' )
    cursorobj       = datacursormode( fig );
    set( cursorobj, 'DisplayStyle', 'datatip', 'SnapToDataVertex', 'off', 'Enable', 'on' )
    pause
    mypoint         = getCursorInfo( cursorobj );
    startSample     = round( mypoint.Position( 1 ) );
    line( [ startSample startSample ], get( hax, 'YLim' ), 'Color', 'red' )
    title( 'Click on last sample and hit ENTER...' )
    set( cursorobj, 'DisplayStyle', 'datatip', 'SnapToDataVertex', 'off', 'Enable', 'on' )
    pause
    mypoint         = getCursorInfo( cursorobj );
    endSample       = round( mypoint.Position( 1 ) );
    line( [ endSample endSample ], get( hax, 'YLim' ), 'Color', 'red' )
    numSamples      = endSample - startSample;
    title( [ 'Selected ' num2str( numSamples ) ' samples' ] )
        
    prompt          = 'Is ok? (0->redo ; 1->ok): ';
    ok              = input( prompt );

    if ok == 1
        spsFormat   = spsFormat( startSample : endSample - 1, : );
    end

end
       
% Perform trimming on reference
if numel( referenceMic ) ~= 1
    referenceMic = referenceMic( startSample : endSample - 1, : );
end


close all
