%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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 : 20 Novemeber 2008
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Script for computing log2 and log10 values of a number
% using the LUT based approach
clear
N = 1:1000; % input
k = 4; % precision requirements for LUT
LUT = log2([1:1/2^k:2]);
LUT = LUT(2:end); % LUT values
% computing the logarithm to base 2
E = floor(log2(N)); % finding the exponent
j = floor((N./2.^E - 1).*2.^k);
% finding the final number
N_log2 = E;
N_log2(find(j~=0)) = N_log2(find(j~=0)) + LUT(j(find(j~=0))); % updating the mantissa only for j > 0
N_log2_theory = log2(N); % theory
N_log10 = N_log2/log2(10); % converting to log10 base
N_log10_theory = log10(N); % theory
close all
plot(N,N_log2,'b-','LineWidth',2);
hold on
plot(N, N_log2_theory,'m-','LineWidth',2);
plot(N,N_log10,'g','LineWidth',2);
plot(N,N_log10_theory,'c','LineWidth',2);
xlabel('N');
ylabel('log value');
title('log conversion');
grid on
legend('log2-sim','log2-theory','log10-sim','log10-theory');