avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-gcc-list] Re: AVR-GCC-list Digest, Vol 50, Issue 10


From: Nicholas Vinen
Subject: [avr-gcc-list] Re: AVR-GCC-list Digest, Vol 50, Issue 10
Date: Thu, 05 Mar 2009 07:29:43 +1100
User-agent: Thunderbird 2.0.0.19 (X11/20090103)

> Message: 2
> Date: Wed, 4 Mar 2009 13:05:39 -0500
> From: David VanHorn <address@hidden>
> Subject: [avr-gcc-list] Tables
> To: AVR-GCC <address@hidden>
> Message-ID:
>       <address@hidden>
> Content-Type: text/plain; charset="iso-8859-1"
>
> How would I construct a lookup table of logarithms in program memory?
> I'll need the logarithm of N entries between 0 and 1, ex:  If N = 10, then I
> need log(0), log(0.1), log(0.2) etc.
> I don't know what precision I'll need yet, but I'll need to be adjusting N,
> so manually creating the table with a spreadsheet would be one way, though
> icky.
> I'm hoping there's a way to have the compiler generate it for me?
>
>   
The C preprocessor is too basic to do this sort of thing. Functions like
logarithms come from the math library the the preprocessor can't make
use of libraries at compile time.

This is a PERL script I use to generate sine tables to load onto a
Mega48. It shouldn't be hard to adapt it to generate a log table too:

#!/usr/bin/perl
my $hz = 1000;
my $rate = 288000;
my $num = ($rate/$hz)>>0;
print "signed short sine_table[$num] = {\n";
my $pi = atan2(1, 1)*4;
for( my $i = 0; $i < $num; ++$i ) {
  my $pos = $i / ($rate/$hz);
  my $sine = sin(2 * $pi * $pos);
  printf("\t%d%s\n", ($sine*32767)>>0, $i < $num-1 ? "," : "");
}
print "};\n";


Then I just do ./sine.pl > sine.h and #include sine.h

Keep in mind there are algorithmic ways too do logs even without
floating point support, they're going to be more compact than a table,
but slower. If speed is your #1 concern then a table is best. Don't
forget to use the appropriate function to read the data out of flash,
you can't just index the array (unfortunately).


Nicholas





reply via email to

[Prev in Thread] Current Thread [Next in Thread]