- DSP log - http://www.dsplog.com -

Linear to log conversion

Posted By Krishna Sankar On November 20, 2008 @ 6:40 am In DSP | 10 Comments

In signal processing blocks like power estimation used in digital communication, it may be required to represent the estimate in log scale. This post explains a simple linear to log conversion scheme proposed in the DSP Guru column on DSP Trick: Quick-and-Dirty Logarithms. [1]The scheme makes implementation of a linear to log conversion simple and small in a digital hardware like FPGA.

Consider an integer $N$. The floating point representation is,

$N=M2^E$

where,

$E$ is the exponent and

$M$is the mantissa.

Assume that $M$ is normalized, i.e $1\le M < 2$.

Taking logarithm to the base 2,

$\log_2N=\log_2M+E$.

In digital hardware implementations, finding the exponent $E$ is simple. Its just noting the index of the first bit which is 1 starting from MSB side.

For example consider an input number $N=12$.

Expressed in binary on 8 bit bus, $N_b=00001100$.

The value of $E$ in this example is 3.

Now, the part which remains to be computed is the mantissa $M$. In this example,

$M=\frac{12}{2^E} = 1.5$.

Given that $M$ lies in the range $1\le M < 2$. this can be computed using a Look Up Table. The LUT can store $\log_2$ values of input between 1 to 2. The precision requirement determines the number of elements in the LUT. Let us assume that we want to have a precision of $\frac{1}{2^k}=0.0625$, where $k=4$. The look up table values will be as follows:

 k=4 index. j Linear = 1+j/2^k LUT = log_2(Linear) 1 1.06250 0.0874628 2 1.12500 0.1699250 3 1.18750 0.2479275 4 1.25000 0.3219281 5 1.31250 0.3923174 6 1.37500 0.4594316 7 1.43750 0.5235620 8 1.50000 0.5849625 9 1.56250 0.6438562 10 1.62500 0.7004397 11 1.68750 0.7548875 12 1.75000 0.8073549 13 1.81250 0.8579810 14 1.87500 0.9068906 15 1.93750 0.9541963 16 2.00000 1.0000000

Table: Look up table values for logarithm computation

From the above look up table, we can see that mantissa of $M=\frac{12}{2^E} = 1.5$ corresponds to index of $j=8$. It is inituitive to note that the array index $j$ can be found out by the simple formula,

$j=\left(\frac{N}{2^E}-1\right)2^k$. To handle cases where this number can $j$ can be a fraction, the result is floored to the nearest integer, i.e.

$j=\lfloor\left(\frac{N}{2^E}-1\right)2^k\rfloor$

So the value of $N$ in $\log_2$ base is,

$N_{\log_2} = LUT(j) + E$.

Once we have the number in $\log_2$ base, conversion to any other base is simple.

$\log_b(N) = \frac{\log_c(N)}{\log_c(b)}$.

So the number $N$in $\log_{10}$ base is,

$N_{\log_{10}} = \frac{N_{\log_2}}{\log_2(10)}$.

## Simulation Model

Simple Matlab/Octave script for computing the logarithm via the LUT based approach is provided. Click here to download Matlab/Octave script for performing linear to log conversion using LUT based approach [2]

Figure: Linear to log conversion using LUT