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. The scheme makes implementation of a linear to log conversion simple and small in a digital hardware like FPGA.
Consider an integer . The floating point representation is,
is the exponent and
is the mantissa.
Assume that is normalized, i.e .
Taking logarithm to the base 2,
In digital hardware implementations, finding the exponent is simple. Its just noting the index of the first bit which is 1 starting from MSB side.
For example consider an input number .
Expressed in binary on 8 bit bus, .
The value of in this example is 3.
Now, the part which remains to be computed is the mantissa . In this example,
Given that lies in the range . this can be computed using a Look Up Table. The LUT can store 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 , where . The look up table values will be as follows:
|index. j||Linear = 1+j/2^k||LUT = log_2(Linear)|
Table: Look up table values for logarithm computation
From the above look up table, we can see that mantissa of corresponds to index of . It is inituitive to note that the array index can be found out by the simple formula,
. To handle cases where this number can can be a fraction, the result is floored to the nearest integer, i.e.
So the value of in base is,
Once we have the number in base, conversion to any other base is simple.
So the number in base is,
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
Figure: Linear to log conversion using LUT