Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 01:12 20 Apr 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : MMBasic for Linux (a possiblity - don't get too excited)

     Page 5 of 5    
Author Message
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 3831
Posted: 03:57pm 17 Sep 2021
Copy link to clipboard 
Print this post

  lizby said  Will the command set include the SYSTEM command (I hope)?


Dee-dee-dee-dee, dee-dee-dee-dee, dee-dee-dee-dee, you have entered the Twilight Zone

But in case you missed it, "Yes, yes it will"

Tom
Edited 2021-09-18 02:42 by thwill
Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3008
Posted: 08:51pm 17 Sep 2021
Copy link to clipboard 
Print this post

Oops, don't know how I double-posted. Thanks. Looking forward to it.
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 3496
Posted: 07:29pm 18 Sep 2021
Copy link to clipboard 
Print this post

Hi Will,

For the commands that I know how they work I have a list of 20 tests.
Maybe this is something you can use. Most of the tests have no comments, since they simply compare results from a command or function with their theoretical (see wikipedia) answer within the accuracy set.

For some, where the method is important, there are comments. It is tested on a CMM2, since I am not sure if all these are implemented on PICO.

I hope this is to some use...

Volhout

EDIT: after reading the code, I think some more comments will be needed.

' test sequence of several math commands and functions

'math function tests defines
score=0
tests=0
accuracy = 1e-10

'check used basic functions to ensure the basic math is OK
if abs(-3)<>3 then print "basic function ABS fail":end
if abs(exp(1)-2.718281828459045) > accuracy then print "basic function EXP fail":end
if abs(log(exp(3))-3) > accuracy then print "basic function LOG fail":end
if abs(sin(pi/4)-sqr(1/2)) > accuracy then print "basic function SIN fail":end



'test hyperbolic functions
'check the numerical difference between the hyperbolic function an it's mathmatical formula

dim float x=3 'just any value

tests=tests+1
' test WIKI definition of sinh
if abs(math(sinh x) - (exp(x)-exp(-x))/2) < accuracy then
 score=score+1
else
 ? "MATH(SINH x) function fault"
endif

tests=tests+1
' test WIKI definition of cosh
if abs(math(cosh x) - (exp(x)+exp(-x))/2) < accuracy then
 score=score+1
else
 ? "MATH(COSH x) function fault"
endif

tests=tests+1
' test WIKI definition of tanh
if abs(math(tanh x) - (exp(x)-exp(-x))/(exp(x)+exp(-x))) < accuracy then
 score=score+1
else
 ? "MATH(TANH x) function fault"
endif

tests=tests+1
' log10 of 1000 is 3
if abs(math(log10 1000) - 3) < accuracy then
 score=score+1
else
 ? "MATH(LOG10 x) function fault"
endif

'no test for MATH(ATAN3 x,y) because I only ATAN2 is described in literature



'Simple statistics: a mix of functions ans commands
'use a linear array a size n filled with samples of a sine wave
'a second array b same size is needed.
option base 0
n=512
dim float a(n-1):for i=0 to n-1: a(i)=sin(2*pi*i/n):next i
dim float b(n-1)

tests=tests+1
' standard deviation of a sine wave is equal to the square root of 1/2 (0.707)
if abs(math(sd a()) - sqr(1/2)) < accuracy then
 score=score+1
else
 ? "MATH(SD a()) function fault"
endif

tests=tests+1
' set all values in a array to 3, and sum up all values in normal basic code.
' compare if array size * value
x=0
math set 3,b()
for i=0 to n-1: x=x+b(i) : next i
if abs(x - 3*n) < accuracy then
 score=score+1
else
 ? "MATH SET x,a() command fault"
endif

tests=tests+1
' calculate the mean of an array of identical values. should be equal to that value
if abs(math(mean b()) - 3) < accuracy then
 score=score+1
else
 ? "MATH(MEAN a()) function fault"
endif

tests=tests+1
' add a fixed value to each cell, and redo the mean test. The mean should be equal to
' previous result added to the fixed value
math add b(),3,b()
if abs(math(mean b()) - 3 - 3) < accuracy then
 score=score+1
else
 ? "MATH ADD a(),x,a() command fault"
endif

tests=tests+1
' calculate the sum in the MATH SUM function, should be previous value * array size
if abs(math(sum b()) - 6*n) < accuracy then
 score=score+1
else
 ? "MATH(SUM a()) function fault"
endif

tests=tests+1
' multiply each value in the sine wave array with 2, amplitude should be equal to
' 2x the square root of 1/2 (that is equal to the square root of 2)
math scale a(),2,a()
if abs(math(sd a()) - sqr(2)) < accuracy then
 score=score+1
else
 ? "MATH SCALE a(),x,a() command fault"
endif

tests=tests+1
' a sine wave with amplitude 2 has a peak of +2, find it with MATH MAX
if abs(math(max a()) - 2) < accuracy then
 score=score+1
else
 ? "MATH(MAX a()) function fault"
endif

tests=tests+1
' a sine wave with amplitude 2 has a lowest value of -2, find it with MATH MIN
if abs(math(min a()) + 2) < accuracy then
 score=score+1
else
 ? "MATH(MIN a()) function fault"
endif

tests=tests+1
' perform an FFT on the sine wave array.
dim float dft(1,n-1)
math fft a(),dft()
' to test the FFT a pure sine wave amplitude 2 is converted with 512 samples
' the output spectrum should contain 2 peaks (symetrical spectrum) each containing
' half the samples summed = 512 * 2 / 2 = 512. Other spectral components
' should be rounding errors. The phase part of the array has values between -pi and pi
' and these will be far lower than the peaks in the amplitude section of the dft array
if abs(math(max dft()) - n) < accuracy then
 score=score+1
else
 ? "MATH FFT a(),b() command fault"
endif

tests=tests+1
math fft inverse dft(),b()
' this test uses the output of FFT and converts it back to a signal array b(). The
' array should be a copy of a(), so contain a sine wave amplitude 2.
if abs(math(max b()) - 2) < accuracy then
 score=score+1
else
 ? "MATH FFT INVERSE a(),b() command fault"
endif

tests=tests+1
' check how much a() and b() are alike through correlation
if abs(math(correl b(),a()) - 1) < accuracy then
 score=score+1
else
 ? "MATH(CORREL a(),b()) function fault"
endif

tests=tests+1
' check how much a() and b() are alike through correlation when a fault is introduced
b(123)=10
'? math(correl a(),b())
if math(correl b(),a()) <> 1 then
 score=score+1
else
 ? "MATH(CORREL a(),b()) function fault"
endif

tests=tests+1
dim float c(n-1)
math interpolate b(),a(),1,c()
' the elements in a() and b() (should be identical) are subtracted, resulting in zero
' multiplied by 1, and a() is added, resulting in c() being a copy of a() if b() is
' a copy of a(). So the amplitude should still be 2.
if abs(math(max c()) - 2) < accuracy then
 score=score+1
else
 ? "MATH INTERPOLATE a(),b() command fault"
endif

tests=tests+1
math set 0,dft()
math insert dft(),0,,a()
' zero the dft(1,n-1), the insert array b() into dft(0,n-1)
' then test if values have arrived
if abs(math(max dft()) - 2) < accuracy then
 score=score+1
else
 ? "MATH INSERT a(),,,,b() command fault"
endif

tests=tests+1
math set 0,c()
math slice dft(),0,,c()
' zero the c(), the slice data from array dft(0,n-1) into c()
' then test if values have arrived
if abs(math(max c()) - 2) < accuracy then
 score=score+1
else
 ? "MATH SLICE a(),,,,b() command fault"
endif

tests=tests+1
math set 0,c()
c(123)=1
' all samples are 0 except one that is 1. The mean value will be 1/n
' the median value will be 0 (most samples are 0)
if abs(math(median c())) < accuracy then
 score=score+1
else
 ? "MATH(MEDIAN a()) function fault"
endif


? tests;" tests performed, ";score;" where PASS"
if tests=score then print "simple MATH commands and functions are PASS"
end



Edited 2021-09-19 05:45 by Volhout
PicomiteVGA PETSCII ROBOTS
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 3496
Posted: 07:47pm 18 Sep 2021
Copy link to clipboard 
Print this post

If this is of any use, I may spend some time trying to check the multi dimension MATH functions, but that will take some learning...
PicomiteVGA PETSCII ROBOTS
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 3831
Posted: 10:52pm 18 Sep 2021
Copy link to clipboard 
Print this post

Many thanks Volhout, that's very helpful and I know it must have taken you a considerable time as it's taken me over an hour to convert into the format used by my tests - and I'm not quite done yet. The good news is they all pass with MMB4L and that gives me confidence that just blindly copying Peter's MATH implementation from the CMM2 is likely to be valid.

If you do end up writing tests for the multi-dimensional MATH functions then it would be really helpful if each test were a separate SUB and did not depend on the prior result of any other test.

Thanks again.

Best wishes,

Tom
Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 3831
Posted: 08:27am 19 Sep 2021
Copy link to clipboard 
Print this post

  thwill said  Many thanks Volhout ...


And I think with the help of your unit-tests I may have found a new bug with MATH FFT on the CMM2/PicoMite/MMB4L:

 https://www.thebackshed.com/forum/ViewTopic.php?PID=175160

Best wishes,

Tom
Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 3831
Posted: 09:11am 19 Sep 2021
Copy link to clipboard 
Print this post

  thwill said  And I think with the help of your unit-tests I may have found a new bug with MATH FFT on the CMM2/PicoMite/MMB4L


Or I may have just made a foolish mistake .

Tom
Edited 2021-09-19 19:11 by thwill
Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
     Page 5 of 5    
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024