Precision Bug in BlitzMax NG?

Started by Midimaster, April 05, 2023, 11:09:45

Previous topic - Next topic

Midimaster

At the moment I transfer my FFT-based apps from old BlitzMax Vanilla to BlitzMax NG. And in this context I ca observe a strange behavior and a difference between old and new BlitzMax.

On old BlitzMax the FFT-Algo work like charming. On BlitzMax-NG it produces only data-garbage and overrides the array borders.

I could reduce the problem to a short code, which runs on both versions. The test app tries to find out which frequency is in the sample. The result should look like this:

FFT-Test
Array was used until element 511
0    2.357   0.000   2.357
1    2.411   -1.05   2.633
2    2.588   -2.25   3.429
3    2.938   -3.78   4.789
4    3.598   -6.06   7.051
5    4.979   -10.2   11.39
6    8.982   -21.5   23.37
7    86.14   -233.   249.2
8    -10.4   31.19   32.88
9    -4.77   15.48   16.20
10   -3.06   10.58   11.02
11   -2.24   8.173   8.476
12   -1.77   6.719   6.949
13   -1.46   5.740   5.925
14   -1.25   5.033   5.187
15   -1.10   4.494   4.627
16   -0.98   4.068   4.186
17   -0.89   3.723   3.828
18   -0.81   3.436   3.532
19   -0.75   3.193   3.282
20   -0.71   2.984   3.068
...

You can see a clear maximum at position 7. This can be used to recalculate the given audio frequence.


On BlitzMax NG the result list short nowhere any peek, this goes down until 255:
FFT-Test
Array was used until element 766
0    -2.19   6.008   6.397
1    11.31   1.985   11.48
2    -3.88   -2.03   4.381
3    0.578   -5.54   5.575
4    7.393   2.259   7.731
5    -9.65   -2.28   9.916
6    -6.95   -2.34   7.342
7    10.14   13.15   16.61
8    11.55   -9.47   14.93
9    -2.53   -9.40   9.739
10   24.27   -12.2   27.18
11   -1.65   -17.0   17.17
12   -25.4   -7.02   26.37
13   -3.77   -7.93   8.788
14   1.075   0.753   0.000
15   -4.88   -3.24   5.863
16   -5.80   6.651   8.829
17   -0.71   8.540   8.570
18   12.24   -0.25   12.24
19   -0.31   -4.86   4.879

And the second probme is in NG the algo uses more than the given 512 array elements. A FFT-Algo alway only uses 512 elements!!!

Here is a runable example code that shows the problem. I expanded the arrays to unnecessary 1024 elements to prevent the crash on NG

Print "FFT-Test"
Global TestMax:Int
Global Sample:Float[1024]
Global Rex:Float[1024]
Global ImX:Float[1024]
For Local j:Int=0 To 512
sample[j]=Sin(j*5)
rex[j]=Sample[j]
imx[j]=0
Next

FastFourier 512, rex, imx
Print "Array was used until element " + TestMax

For Local i:Int=0 To 255
    Local out# = Sqr((Imx[i] * Imx[i]) + (Rex[i] * Rex[i]))
If out<2 Then out=0

Print Left((i) + "    ",2) + "   " + Left((rex[i]) + "    ",5) + "   " +Left((ImX[i]) + "    ",5) + "   " +Left((out) + "    ",5)

Next

End



Function FastFourier(n% , Rex#[] Var ,  Imx#[] Var)
Local Nm1%, Jm1%, Nd2%, M%, I%, j%, K%, L%, Le%, Le2%, Ip%
Local TR#, TI#, Ur#, Ui#, Sr#, Si#

  Nm1 = n - 1
  Nd2 = n / 2
  M = Int(Log(n) / Log(2))
  j = Nd2

  For I = 1 To n - 2 'Bit reversal sorting
    If I < j Then
      TR = Rex[j]
      TI = Imx[j]
      Rex[j] = Rex[I]
      Imx[j] = Imx[I]
      Rex[i] = TR
      Imx[i] = TI
    End If
    K = Nd2
    While K <= j
      j = j - K
      K = K / 2
    Wend
    j = j + K
  Next

'
  For L = 1 To M
    Le = Int(2 ^ L)
    Le2 = Le / 2
    Ur = 1
    Ui = 0
    Sr = Cos(180.0/ Le2)
    Si = -Sin(180.0 / Le2)
    For j = 1 To Le2
      Jm1 = j - 1
   I=Jm1
       While I <= Nm1
        Ip = I + Le2
If ip>TestMax
TestMax=ip
EndIf
        TR = Rex[ip] * Ur - Imx[ip] * Ui
        TI = Rex[ip] * Ui + Imx[ip] * Ur
        Rex[ip] = Rex[i] - TR
        Imx[ip] = Imx[i] - TI
        Rex[i] = Rex[i] + TR
        Imx[i] = Imx[i] + TI
I=I+ Le
      Wend
      TR = Ur
      Ur = TR * Sr - Ui * Si
      Ui = TR * Si + Ui * Sr
    Next
  Next
Return
 
End Function

...back from North Pole.

Derron

Once you updated NG to current code (via my "downloader" script -- or eg. Henris: https://github.com/HenriVi/BlitzMax-Install-Script ) and fixed the issues in bah.rtmidi (some casts missing) that code works as intended (Midimaster confirmed via discord).


bye
Ron