%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% All rights reserved by Krishna Pillai, http://www.dsplog.com
% The file may not be re-distributed without explicit authorization
% from Krishna Pillai.
% Checked for proper operation with Octave Version 3.0.0
% Author        : Krishna Pillai
% Email         : krishna@dsplog.com
% Version       : 1.0
% Date          : 14th December 2008
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Script for computing average, max and rms error using the 
% approximate magnitude computation algorithm.

clear all
ipPhase = [0:0.001:pi];
ip = exp(j*ipPhase);

maxVal = max(abs(real(ip)),abs(imag(ip)));
minVal = min(abs(real(ip)),abs(imag(ip)));

alpha  = [  1   1   1  7/8 15/16];
beta   = [1/2 1/4 3/8 7/16 15/32];

for ii = 1:length(alpha)
    absCompute(ii,:) = alpha(ii)*maxVal + beta(ii)*minVal;
    err = absCompute(ii,:) - 1;
    sgn = 2*(max(err) > -1*min(err))-1;
    maxError(ii)     = sgn*max(abs(err));
    avgError(ii)     = mean(err);
    rmsError(ii)     = sqrt(mean(err.^2));
end

close all
plot(ipPhase*180/pi,abs(ip),'k','LineWidth',2);
hold on
plot(ipPhase*180/pi,absCompute,'LineWidth',2);
legend('ideal','\alpha=1,\beta=1/2','\alpha=1,\beta=1/4','\alpha=1,\beta=3/8','\alpha=7/8,\beta=7/16','\alpha=15/16,\beta=15/32');
grid on
xlabel('ip phase (degrees)');
ylabel('absolute value');
title('absolute value estimate');

errorSummary = [alpha;beta;maxError*100; avgError*100; rmsError*100].'








