B4A Class [B4x] xFFT Class and b4xlib

SimonElck

Member
Licensed User
Here is my small first tuner project. It analyzes the fundamental frequency of a soundwave in two steps:
  1. ...
Hi
I tried this (I am only beginning again with b4a after a long time).
I also tried
xFFTDemo.zip and FFT_Record.zip
But nothing works.
I did not change one line but get this error:

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Error occurred on line: 123 (Main)
java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.
at android.media.AudioRecord.startRecording(AudioRecord.java:1004)

as soon as I hit the 'Start' button

Is it to do with permissions? Or something else?
Thanks
 

klaus

Expert
Licensed User
Longtime User
I cannot answer for the Tuner project, you need to ask skrjabin.

I downloaded the xFFTDemo.zip from the forum and it works as expected.
What is your problem?

I downloaded the FFTRecord.zip file from the forum and I get the same error as you.
It is a permission problem, new Google rules, I hadn't tested this project for a wile

The project has been updated in the first post.
 

SimonElck

Member
Licensed User
Thanks, the permission thing was the problem. Same for the Tuner project.
Your new upload solved this

xFFTDemo worked well, sorry

Thanks again
 

drponciano

Active Member
Licensed User
Longtime User
Hello @klaus . I found a strange error with xFFT. I've been using it without problems until friday. My program uses it to make a high pass filter to a signal, it works perfectly in every computer I have used but, using Parallels in a Mac M2 with Windows 11 for ARM it failed miserably. I discovered the problem in this code:
Calculate power of 2:
   ld = Logarithm(N, 2)
   If ld - Floor(ld) > 0 Then
       Log("N must be a power of 2")
       Return fReal
   End If

My program , running In a "normal" Windows computers with Intel compatible processors, and in MacOS on M2 computers with 8192 signal samples, ld=13 but, in the same M2 computer under Parallels with Windows 11 ARM ld=12.99999999 so everything fails. I modified your code

Workarround for FFT:
' IsPowerOfTwo?
public Sub EsPotenciaDeDos(nn As Long) As Boolean
    ' Un número <= 0 no es potencia de 2
    If nn <= 0 Then Return False
 
    ' El truco bit a bit: n & (n-1) elimina el bit más a la derecha
    ' Si el resultado es 0, significa que solo había un bit (potencia de 2)
    Return (Bit.And(nn, nn - 1) = 0)
End Sub

...

If Not(EsPotenciaDeDos(N)) Then
        Log("N must be a power of 2")
        Return fReal
    End If
 
    ld = Logarithm(N, 2)
'    If ld - Floor(ld) > 0 Then
'        Log("N must be a power of 2")
'        Return fReal
'    End If

    Log("En FFT N= "&N&"   LD=  "&ld)
   
    NU =Round(ld)
    ...

I hope this helps users of your very useful xFFT library

Have a nice day
 
Last edited:

drponciano

Active Member
Licensed User
Longtime User
I suspect that in your case the problem are Floats instead of Double.
Not really. This is your original code:

Double or Float:
Public Sub Forward(Real() As Double) As Double()
    Private i As Int
    Private ld As Double

    N = Real.Length
   
    ld = Logarithm(N, 2)
    If ld - Floor(ld) > 0 Then
        Log("N must be a power of 2")
        Return fReal
    End If
   
    NU = ld

As 12.99999 is not 13 (a power of two) freal is returned with ZERO size and...
I think there is something wrong with Windows ARM or Parallels OR BOTH!
 

drponciano

Active Member
Licensed User
Longtime User
It seems that on this device:
Logarithm(8192, 2)
Returns 12.99999 instead of 13.
What is freal ?
freal is the array that contains the real part of the FFT transform. There is also a ffti for the imaginary part but, as 8192 "is not a power of two", freal returns with 0 length and the program fails when trying to access freal(0), freal(1),... That is what caused my program to crash ONLY in the Virtual Windows machine. The same thing happens as with FFT.Forward with FFT.inverse routines.
With the changes I made now works perfectly in Windows Intel computers, Mac with MacOS Tahoe 26.X and Parallels Windows 11 ARM virtual machine.
I thank you for the xFFT library which I have used many times.
 
Last edited:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…