Forums

Log table in Block Data?

Started by Tom Becker September 22, 2005
As nice as it is to have advanced math functions in BasicX, the hit on
the stack when you do something like 10.0^Log10(sX/10.0) and similar
functions is painfully large, 50 bytes or more just for the support
routines, and they are slow. I'm exploring options.

One is the venerable logarithm table. To convert a 10-bit analog input
to logarithms looks like a 1024*4-byte table of singles which can be
placed in EEPROM as Block Data. For this app, 4K of program space is
fine and it would be a lickety-split quick conversion. Has anyone done
that? Tom



Tom Becker wrote:

> One is the venerable logarithm table. To convert a 10-bit analog input
> to logarithms looks like a 1024*4-byte table of singles which can be
> placed in EEPROM as Block Data. For this app, 4K of program space is
> fine and it would be a lickety-split quick conversion. Has anyone done
> that?

I have done something similar with calculating Sines using a table
lookup. I explored various options and compared them to the original
BasicX code. See part 4 of my articles on the internals of BasicX. Here
is a more direct link to the section 4.4.6: http://tinyurl.com/8fnkk .

I would suggest before looking at alternatives you investigate the code
inside the log and log10 functions to see if they can be improved. For
example in the case of Sine, my rewritten Sin2 routine executes slightly
quicker and uses 21 bytes on the stack instead of the original 37. You
may be able to do the same for log10 by noting for example that is
simply calls log and uses the following equation to calculate log10(x)
= log(x)/log(10) = log(x)/2.30259.

Alternatively there are a number of shortcut methods of calculating
logarithms as well without needing such a large table. Here is an
excellent treatise on the subject by the same author who gave me ideas
about calculating Sine. For example here is a simple approximation that
might be all you need: ln(x) ~ 6*(x-1)/ ( x + 1 + 4*(x^0.5)). This page
also discusses several other algorithms with small tables.

Another source for ideas is Basic Stamp as it has more limited
arithmetic. Here is a table lookup and iterative algorithm written for
the Basic Stamp that may prove useful:
http://www.emesystems.com/BS2math3.htm#Logtable.

If you do some investigation, post your results. However the most
expedient solution that saves up to 13 bytes is to use the log/log10
equation listed above.

Mike


> ... 4K of program space [] would be a lickety-split quick conversion...

I think I've answered myself. The end result of this process is
linearization of a power function (y=a*x^b). I might as well just build
a ROM-based solution table in Block Data and skip the exponential
processes altogether. It's 1970 again! Tom


Mike Perks wrote:

> Alternatively there are a number of shortcut methods of calculating
> logarithms as well without needing such a large table. Here is an
> excellent treatise on the subject by the same author who gave me ideas
> about calculating Sine. For example here is a simple approximation
> that might be all you need: ln(x) ~ 6*(x-1)/ ( x + 1 + 4*(x^0.5)).
> This page also discusses several other algorithms with small tables.
>
I forgot the URL which is http://www.dattalo.com/technical/theory/logs.html

Mike


> ... Alternatively there are a number of shortcut methods of calculating logarithms...

Another handy tome is _Mastering Technical Mathematics, Second Edition_
by Gibilisco and Crowhurst, McGraw Hill 1999. Full of insight and
shortcuts. Tom



Mike, in the PIC world, Scott Dattalo is one of our 'gawds' ;-)

If it's on Scott's site, count on it. Over the years, and over a
decade, he and a few others have shown us PIC programmers remarkable
algorithms that are absolutely brilliant.

- Tom

--- In basicx@basi..., Mike Perks <basicx@a...> wrote:
> I forgot the URL which is
http://www.dattalo.com/technical/theory/logs.html
>
> Mike