function [ErrPct, ErrAboveThreshold] = hdlv_uvmtb_checker(PeakRef, LocationRef, FilterOut, MidSampleImpl, DetectedImpl, STime)

%#codegen

    ErrPct=0;
    
    % Find implementation peak
    PeakImpl = double(MidSampleImpl(DetectedImpl>0));
    
    PeakRefSq = PeakRef^2;
    
    if (PeakRef ~= 0)
        % Plot
        str = sprintf('[time=%f] Peak found in Ref at %f with a value of %.3f\n', STime, LocationRef, PeakRef);
        t = 1:length(FilterOut);
        subplot(313); plot(t,abs(FilterOut),LocationRef,PeakRef,'o'); title(str);
        
        fprintf('\n[time=%f] Peak location=%f, mag-squared=%.3f using global max\n', STime, LocationRef, PeakRefSq);
        
        if isempty(PeakImpl)
            ErrPct = 1;
            fprintf('[time=%f] No peak detected from impl error(pct)=%.3f\n', STime, ErrPct);
        else
            [~, LocationImpl] = max(DetectedImpl); 
            LocErr = abs(LocationImpl - LocationRef);
            fprintf('[time=%f] Peak detected from impl=%g error(abs)=%g\n', STime, LocationImpl, LocErr);
            
            ErrPct = max(abs(PeakRefSq-PeakImpl)/PeakRef);
            for n = 1:size(PeakImpl,1)
                fprintf('[time=%f] Peak mag-squared from impl=%.3f, error(abs)=%.3f error(pct)=%.3f\n',...
                    STime, PeakImpl(n),abs(PeakRefSq-PeakImpl(n)), ErrPct);
            end
        end
    end

    if ErrPct > 0.05
        ErrAboveThreshold = 1;
    else
        ErrAboveThreshold = 0;
    end
end
