-- -- complex.lua - plots the numbers as -- Description: This script plots the ADSL SHOW CHANNEL output. -- -- To run: I use lua5.1exe and type dofile("complex.lua") -- -- See: www.lua.org and http://lua-gd.luaforge.net/ for lua downloads -- -- dofile("complex.lua") -- Copyright: Doug Rice 2007 -- lua-GD: http://lua-gd.luaforge.net/manual.html#license.luagd -- http://www.lua.org/license.html -- -- -- -- -------------------------------------------------- -- Sec 0.0 includes -- -------------------------------------------------- require "gd" -- -------------------------------------------------- -- Sec 1.0 functions - complex numbers -- -------------------------------------------------- function add( re1, im1, re2, im2 ) local re , im re = re1 + re2 im = im1 + im2 return re , im end function mult( re1,im1,re2,im2 ) local re , im re = re1*re2-im1*im2 im = re1*im2+im1*re2 return re , im end -- -------------------------------------------------- -- Sec 1.1 functions - plot -- -------------------------------------------------- function plotSetup( ) xOff = 10 xScale = 20 xMid = 200 + xOff xMax = xMid*2 xMid = xMid + xOff yOff = 10 yScale = 20 yMid = 200 + yOff yMax = yMid*2 yMid = yMid + yOff im = gd.createTrueColor( xMax+4+xOff*2, yMax+yOff*2) end function plotSetup2( ) xOff = 10 xScale = 18 xMid = 300 + xOff xMax = xMid*2 xMid = xMid + xOff yOff = 10 yScale = 18 yMid = 300 + yOff yMax = yMid*2 yMid = yMid + yOff im = gd.createTrueColor( xMax+4+xOff*2, yMax+yOff*2) end function plotPass( dataFile,inst ) fname = dataFile:match( "([^.]*)") bg = im:colorAllocateAlpha(250, 250, 250 , 0) im:filledRectangle(0,0, xMax * xScale+4+xOff*2, yMax*yScale+yOff*2, bg ) bg = im:colorAllocateAlpha(127, 127, 127 , 0) labelClr = im:colorAllocateAlpha(32, 32, 0 , 0) -- im:filledRectangle( 1,1,260,150, bg ) -- first allocated color defines the background. white = im:colorAllocate(255, 255, 255) black = im:colorAllocate(0, 0, 0) red = im:colorAllocate(255, 0, 0) yellow = im:colorAllocate(255, 255, 0) green = im:colorAllocate(0, 255, 0) cyan = im:colorAllocate(0, 255, 255 ) blue = im:colorAllocate(0, 0, 255) mag = im:colorAllocate(255, 0, 255) -- gray = im:colorAllocateAlpha(255, 255, 0xCC, 64) gray = im:colorAllocateAlpha(128, 128, 128, 64) coloursA = { black,red,yellow,green, cyan,blue,mag,white,black,red,yellow,green, cyan,blue,mag,white } im:string(gd.FONT_LARGE, 10, 5, "dataFile: ".. fname .."_" .. inst .. ".png", labelClr) -- --------------- -- plot grid -- --------------- print( "a_"..fname.."_" .. inst .. ".png" ) -- axis im:line( xMid-xMax/2, yMid , xMid+xMax/2, yMid , black ) im:line( xMid, yMid-yMax/2 , xMid, yMid+yMax/2 , black ) are = 0.0 aim = 0.0 lastre3 = 0.0 lastim3 = 0.0 for x3 = 0 , 5 , 1 do for x2 = 0 , 5 , 1 do lastre3 = nil lastim3 = nil for x1 = 0 , 5 , 1 do are = 0.0 aim = 0.0 cmare = 1.0 cmaim = 0.0 plotDigit( x3 ) plotDigit( x2 ) plotDigit( x1 ) re3 = are im3 = aim if ( lastre3 ) then -- im:line( xMid + lastre3*xScale, yMid + lastim3*yScale , xMid + re3*xScale, yMid + im3*yScale , gray ) end lastre3 = re3 lastim3 = im3 opStr = x3..x2..x1 im:rectangle( re3*xScale+ xMid-1, im3*yScale+yMid-1, re3*xScale+xMid+1, im3*yScale+yMid+1, mag ) im:string(gd.FONT_TINY, re3*xScale+ xMid-1, im3*yScale+yMid-1, opStr, labelClr) -- im:string(gd.FONT_LARGE, 10, 5, "dataFile: ".. fname .."_" .. inst .. ".png" .. " @ " .. timeStamp .. downStr , labelClr) end if ( lastre3a ) then -- im:line( xMid + lastre3a*xScale, yMid + lastim3a*yScale , xMid + re3*xScale, yMid + im3*yScale , blue ) end lastre3a = re3 lastim3a = im3 end end end function plotDigit( digit ) -- -- Take a number 123 -- -- re,im = ((( '1'*cm)+'2')*cm + '3') -- a = cma*cm + digit*cma -- local dre,dim -- raise to next power are , aim = mult( are, aim, cmre, cmim ) are , aim = add( are, aim, digit, 0 ) end function plotEnd( dataFile,inst ) fname = dataFile:match( "([^.]*)") print( "a_"..fname.."_" .. inst .. ".png" ) im:png("./a_"..fname.."_" .. inst .. ".png") end -- -------------------------------------------------- -- Sec 2.0 Main -- -------------------------------------------------- a = math.atan(1) -- print( a ) baseMag = 1.5 baseAngle = 30 function plot( baseMag, baseAngle ) cmre = baseMag * math.cos( baseAngle * a/45 ) cmim = baseMag * math.sin( baseAngle * a/45 ) print( re , im , a* 4 ) print( mult( cmre, cmim, cmre, cmim ) ) GraphLable = "base_"..baseMag.."_"..baseAngle plotSetup2( ) plotPass( "complex",GraphLable) plotEnd( "complex",GraphLable) end baseMag = 2 baseAngle = 30 plot( baseMag, 0 ) plot( baseMag, 30 ) plot( baseMag, 45 ) plot( baseMag, 60 ) plot( baseMag, 90 ) plot( baseMag, 120 ) plot( baseMag, 180 ) baseMag = 3 plot( baseMag, 0 ) plot( baseMag, 30 ) plot( baseMag, 45 ) plot( baseMag, 60 ) plot( baseMag, 90 ) plot( baseMag, 120 ) plot( baseMag, 180 ) baseMag = 6 plot( baseMag, 0 ) plot( baseMag, 30 ) plot( baseMag, 45 ) plot( baseMag, 60 ) plot( baseMag, 90 ) plot( baseMag, 120 ) plot( baseMag, 180 ) baseMag = 4/3 plot( baseMag, 0 ) plot( baseMag, 30 ) plot( baseMag, 45 ) plot( baseMag, 60 ) plot( baseMag, 90 ) plot( baseMag, 120 ) plot( baseMag, 180 )