Ln(x) approximation routines? - 9S12X

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Ln(x) approximation routines? - 9S12X

Jump to solution
2,661 Views
Pinhead
Contributor I

I am trying to calculate dewpoint in a 9S12X processor, and need a way to approximate the natural log function.  I have used a simple polynomial approximation, -x - x^2/2 - x^3/3..., but it breaks down below a certain relative humidity. 

 

Does anyone know of any methods or routines, preferrable in assembly?

 

Thanks,

Chris

 

 

Added p/n to subject.

Message Edited by NLFSJ on 2009-08-18 10:07 AM
Labels (1)
0 Kudos
Reply
1 Solution
728 Views
bigmac
Specialist III

Hello Chris, and welcome to the forum.

 

I assume that you require the natural log of the fractional relative humidity value.  The problem with using the Taylor series for ln(1-x) is that the series converges very slowly, and extremely slowly for low values of RH.

 

An alternative method may be to consider deriving the the base 2 log, and to then to use the identity:

ln(h) = ln(2) * log2(h) = 0.6931 * log2(h)

 

Since 0 < h < 1, the log value will always be negative, so the sign may be ignored within the derivation (and the result then subtracted, rather than added).  The log result will have an integer part and  a fractional part.

 

By initially using a base of 2, the integer part may be very easily derived by counting the number of left shifts of the binary h value, until there are no leading zeros remaining.  After shifting has occurred, the value will be within the range 0.5 <= h < 1, and the fractional part of the result will be derived from this value.  With this limited range, it would seem that a table lookup and interpolate method may be appropriate.  The use of linear interpolation between the the table values should give good accuracy for quite a small table.  My understanding is that the HCS12 has table lookup and interpolation instructions available, to simplify this process.

 

The result obtained would then be multiplied by the binary fractional value equivalent to 0.6931 ($B172 for a 16-bit fraction).

 

Regards,

Mac

View solution in original post

0 Kudos
Reply
3 Replies
729 Views
bigmac
Specialist III

Hello Chris, and welcome to the forum.

 

I assume that you require the natural log of the fractional relative humidity value.  The problem with using the Taylor series for ln(1-x) is that the series converges very slowly, and extremely slowly for low values of RH.

 

An alternative method may be to consider deriving the the base 2 log, and to then to use the identity:

ln(h) = ln(2) * log2(h) = 0.6931 * log2(h)

 

Since 0 < h < 1, the log value will always be negative, so the sign may be ignored within the derivation (and the result then subtracted, rather than added).  The log result will have an integer part and  a fractional part.

 

By initially using a base of 2, the integer part may be very easily derived by counting the number of left shifts of the binary h value, until there are no leading zeros remaining.  After shifting has occurred, the value will be within the range 0.5 <= h < 1, and the fractional part of the result will be derived from this value.  With this limited range, it would seem that a table lookup and interpolate method may be appropriate.  The use of linear interpolation between the the table values should give good accuracy for quite a small table.  My understanding is that the HCS12 has table lookup and interpolation instructions available, to simplify this process.

 

The result obtained would then be multiplied by the binary fractional value equivalent to 0.6931 ($B172 for a 16-bit fraction).

 

Regards,

Mac

0 Kudos
Reply
728 Views
Pinhead
Contributor I

It appears that this is the simplest way to do the approximation. 

It comes down to approximating the term:  LN(RH/100)

 

In order to shift and figure out the integer value, I started with 100/RH.  I made a table where one column held the numbers (100/0.1 to 100/99.9).  For each value I logged the shift remainder of that value and the mantissa of the log base 2 of that value.  I then decided on a resolution that I thought was acceptable for the remainder (0.500, 0.510, ... 1.000)  and made a table of remainder vs. log base 2.  In the end the integer remainder gets added to the mantissa from the table.   So my table is only 50 values long, or 100 bytes, and the calculations are all very simple.  Even a lot of approximations for these functions can be kind of large to deal with in HC12 assembly.

 

I could of course make a lookup table for 1000 rh values, but I am a bit freaked out about making such a large lookup table, so the marriage seems like the best option.

 

Thanks for the help!

0 Kudos
Reply
728 Views
WadeH
Contributor III

It depends on the size of your input. If it's 8 to 10 bits, the simplest way would be a look-up table.

If you have 512 bytes to spare, you could make a 256-word table from which an 8-bit ln(x) would

be available directly . Beyond that, an 8-bit table could still be used, but interpolation would be needed.

 

Composing the table would be an interesting hour's project with Excel or Basic

0 Kudos
Reply