Truncated match.
PICList
Thread
'calculating log base 10'
1996\05\29@012202
by
Stuart Allman
I need to calculate the base 10 log of a 16 bit number in the range of 0 to
64k. Does anyone know a good approximation method?
Stuart Allman
Studio Sound Design
spam_OUTstudioTakeThisOuThalcyon.com
1996\05\29@024901
by
Steve Hardy
> From: Stuart Allman <.....studioKILLspam@spam@HALCYON.COM>
>
> I need to calculate the base 10 log of a 16 bit number in the range of 0 to
> 64k. Does anyone know a good approximation method?
>
> Stuart Allman
> Studio Sound Design
> studioKILLspamhalcyon.com
>
Depends how approximately you are willing to tolerate. Since you are
in the audio industry (?) perhaps +/ 1 or 2 dB is OK? If so, then
compute log (to base 2) by finding the bit number of the most significant
'1' bit, then use a lookup table on the most significant 4 bits to add
a bit more precision, and finally do a fixed point multiply to convert
into log base 10.
The precise routine details will include proper scaling for the fixed
point arithmetic, and also depend on the particular PIC chip. For more
details send email since this is a useful little building block which
I wouldn't mind fleshing out.
Regards,
SJH
Canberra, Australia
1996\05\29@045124
by
fastfwd

Stuart Allman <.....PICLISTKILLspam.....MITVMA.MIT.EDU> wrote:
> I need to calculate the base 10 log of a 16 bit number in the range
> of 0 to 64k. Does anyone know a good approximation method?
Stuart:
Here's one... It requires a short lookup table and isn't
extraordinarily accurate for small numbers, but it only requires
shifts, addition, and subtraction. I'm doing this from memory, but I
think it's accurate.
To find y = log10(x):
Make a table, thusly:
t(1) = log10(2/1)
t(2) = log10(4/3)
t(3) = log10(8/7)
....
t(n) = log10(2**n/2**n1)
[" ** " is my preferred notation for "raised to the power";
you may prefer " ^ ".]
The length of the table is dependent upon the precision that you
want; pick a level of precision and just keep adding entries to
the table until you get to one that, at your level of precision,
is equal to 0. For instance, if you only needed three or four
(decimal) digits of precision, I think eight eightbit entries
would be enough.
Ok. Now do this:
n = 1
y = 0
z = (x >> 1)
while x > 1,
{
if x  z < 1 then n = n + 1, z = (z >> 2).
otherwise, x = x  z, z = (x >> n), y = y + table(n).
}
["a >> b" means "shift 'a' rightward 'b' positions"]
When the routine completes, y will be (approximately, to the level of
precision you've chosen) equal to log10(x).
The table can be very small... 8 entries should be enough for 1%
accuracy over nearly the entire range [165535]. Note that the
accuracy will be worst at the low end of the range.
Andy
P.S. If you happen to have tables of ln(x) and log2(x), you can
also, surprisingly enough, calculate a really close
approximation of log10(x) like this:
log10(x) = log2(x)  ln(x)
Cool, huh?
Andrew Warren  EraseMEfastfwdspam_OUTTakeThisOuTix.netcom.com
Fast Forward Engineering, Vista, California
http://www.geocities.com/SiliconValley/2499
1996\05\29@094505
by
Stuart Allman
>Depends how approximately you are willing to tolerate. Since you are
>in the audio industry (?) perhaps +/ 1 or 2 dB is OK? If so, then
>compute log (to base 2) by finding the bit number of the most significant
>'1' bit, then use a lookup table on the most significant 4 bits to add
>a bit more precision, and finally do a fixed point multiply to convert
>into log base 10.
Would it help if I said it could be from ~0 to 1? I was just thinking about
the results and I wouldn't mind scaling the input to within 0 to 1/2 even.
Could you please explain the most significant 4bits thing to me. I
understand the most significant bit thing. I'm actually doing this with a
DSP attached to a PIC, so floating point stuff is OK by me.
Stuart Allman
Studio Sound Design
studiospam_OUThalcyon.com
More... (looser matching)
 Last day of these posts
 In 1996
, 1997 only
 Today
 New search...