Post your best Lua script examples here

User avatar
By Musti
#12995 I am happy to share with you a function for approximate logarithm calculation on integer only versions on Lua without math library. In short there are two ways of calculating a logarithm, either with a look-up table or with iterative calculations. Both types of functions are introduced here, with very similar timing performance.

This particular code may be useful when you want to show analog values with a color-code on a led or similar.

Full details here: http://irnas.eu/2015/03/25/logarithm-integer-system/

Code: Select all-- ESP8266 running nodemcu lua - integer
-- 492us for x=1000 and 221us for x=1
function log_approx (x)
   E=272
   ln10=230
   ans=0
   res=0

   while x > 0 do
      res =res*100/E
      a1 = x*10000/E
      x = a1/100;
      res = res+a1-x*100;
      ans=ans+1;
      if res>100 then
         x=x+1;
         res=res-100; 
      end
   end

   res = res-100 - (100-res)*(100-res)/200
   ans = (ans*100+res)/100
   return ans*1000/ln10
end

-- ESP8266 running nodemcu lua - integer
-- 433us for x=1000 and 195us for x=1
input_val={2,3,4,5,6,7,9,12,16,20,25,32,40,50,63,79,100,126,158,200,251,316,398,501,631,794,1000,1025}
output_val={0,3,4,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30}

function log_approx2 (x)
   for i,v in ipairs(input_val) do
      if(x<v) then
         do return output_val[i] end
      end
   end
   return 0
end
User avatar
By Vitoa
#13066 Hi, very good math, if use more digits for e it would get much less error E=271.828183
Also can be done directly by equation.

Image

U can implement it and get something like this, didn't tested yet

function loge (z,resol)
--resol: number of iterations in the sum, default 20
local sum=0
if (not resol) then resol = 20 end
for n=0,resol do
sum = sum + (1/(2*n+1))*((z-1)/(z+1))^(2*n+1)
end
return 2*sum
end

Adding up only 20 items would cause the relative error to increase to 1% when z is greater than about 38.
It maybe a good idea to keep adding up until the item becomes less than 1e-6 or some tiny number
Indeed thanks for logarithm ideia, I wanted to dim a power led over time and it simplifies a lot

Regards,
Vitoa
User avatar
By picstart
#13074 The intensity of a led viewed by the eye is not logrithmic.....it is cie1963
the PWM maybe best done by the traditional lookup table.
Code: Select all// CIE1931 correction table


const unsigned char cie[256] = {
   0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
   1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
   2, 2, 2, 3, 3, 3, 3, 3, 3, 3,
   3, 4, 4, 4, 4, 4, 4, 5, 5, 5,
   5, 5, 6, 6, 6, 6, 6, 7, 7, 7,
   7, 8, 8, 8, 8, 9, 9, 9, 10, 10,
   10, 10, 11, 11, 11, 12, 12, 12, 13, 13,
   13, 14, 14, 15, 15, 15, 16, 16, 17, 17,
   17, 18, 18, 19, 19, 20, 20, 21, 21, 22,
   22, 23, 23, 24, 24, 25, 25, 26, 26, 27,
   28, 28, 29, 29, 30, 31, 31, 32, 32, 33,
   34, 34, 35, 36, 37, 37, 38, 39, 39, 40,
   41, 42, 43, 43, 44, 45, 46, 47, 47, 48,
   49, 50, 51, 52, 53, 54, 54, 55, 56, 57,
   58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
   68, 70, 71, 72, 73, 74, 75, 76, 77, 79,
   80, 81, 82, 83, 85, 86, 87, 88, 90, 91,
   92, 94, 95, 96, 98, 99, 100, 102, 103, 105,
   106, 108, 109, 110, 112, 113, 115, 116, 118, 120,
   121, 123, 124, 126, 128, 129, 131, 132, 134, 136,
   138, 139, 141, 143, 145, 146, 148, 150, 152, 154,
   155, 157, 159, 161, 163, 165, 167, 169, 171, 173,
   175, 177, 179, 181, 183, 185, 187, 189, 191, 193,
   196, 198, 200, 202, 204, 207, 209, 211, 214, 216,
   218, 220, 223, 225, 228, 230, 232, 235, 237, 240,
   242, 245, 247, 250, 252, 255,
};