B4A Library FFT Fast Fourier transform library

Discussion in 'Additional libraries, classes and official updates' started by klaus, Dec 20, 2010.

  1. daniel69

    daniel69 Member Licensed User

    Thanks Klaus ... I'll try to make a post in the forum (in the B4i wishlist forum)
     
  2. sorex

    sorex Expert Licensed User

    @klaus : can we use this lib on a returned soundbuffer to display a waveform or frequency bars? (from the MIC or stream)

    or will it be too slow?
     
  3. klaus

    klaus Expert Licensed User

    Waveform no.

    Frequency bars, I don't know, I have never tested it.
    It depends also on the number of time signal items you want to take into account.
     
  4. NeoTechni

    NeoTechni Well-Known Member Licensed User

    Using another library on this site somewhere to get the audio data in real time, I use this to get the frequency bars

    Code:
    Type Histogram(Values As List, Highest As Int, Inc As Int)
        
    Dim TimeMin As Double=1000000,TimeMax As Double =-1000000, FFTMax As Double ,  FFTLimit As Double, FFTThreshold = 0.8 As Double, N_Samples As Int , CursorScaleFreq As Double , FFTprev As Int 
        
    Dim FFT1 As FFT

    Sub RecData2Histogram(SampleRate As Int, recData() As Short) As Histogram ', recData() As Double =  fft1.CopyArray(srcData)
        Dim tempHist As Histogram ,  soundD() As Double = CopyRecDataD(SampleRate, recData), N_2 As Int = soundD.Length * 0.5 ,FFTPeaks As List 
        
    Dim FFTReal(N_2) As Double, FFTImg(N_2) As Double, FFTAmp(N_2) As Double
        
        tempHist.Initialize 
        tempHist.Values.Initialize 
        
        FFT1.Transform2(soundD, FFTReal, FFTImg )
        FFTAmp = FFT1.ToAmplitude(FFTReal,FFTImg)
        PeakDetect(FFTAmp)
        
        tempHist.Inc = 
    1
        tempHist.Highest = FFTMax
    ' TimeMax
        tempHist.values.AddAll(FFTAmp)
        
        
    Return tempHist
    End Sub
     
  5. daniel69

    daniel69 Member Licensed User

    Hi Klaus!! Let me know when you start to make the ios class of this project ... badly need it ...
     
  6. klaus

    klaus Expert Licensed User

    Unfortunately you need to wait a bit, I will be on travel for one week.
    I will have a look at it at the end of next week.
     
    daniel69 likes this.
  7. padvou

    padvou Active Member Licensed User

    Thank you very much for the library and the test project posted.
    Could you please help me figure out why I get this?
    Code:
    ** Activity (main) Create, isFirst = true **


    ** 
    Activity (main) Resume **


    Start Recording


    Sampling...


    32768


    AR state: 
    1


    java.lang.RuntimeException: 
    FFT: Real and Imaginary arrays are incorrect length!


        at anywheresoftware.b4a.agraham.fft.FFT.Transform2(
    FFT.java:253)
        at b4a.example.main._initcalulation(main.java:
    739)
        at b4a.example.main._btncalc_click(main.java:
    458)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    515)
        at anywheresoftware.b4a.BA.raiseEvent2(BA.java:
    175)
        at anywheresoftware.b4a.BA.raiseEvent2(BA.java:
    163)
        at anywheresoftware.b4a.BA.raiseEvent(BA.java:
    159)
        at anywheresoftware.b4a.objects.ViewWrapper$
    1.onClick(ViewWrapper.java:66)
        at android.view.View.performClick(
    View.java:4424)
        at android.view.View$PerformClick.run(
    View.java:18383)
        at android.os.Handler.handleCallback(Handler.java:
    733)
        at android.os.Handler.dispatchMessage(Handler.java:
    95)
        at android.os.Looper.loop(Looper.java:
    137)
        at android.app.ActivityThread.main(ActivityThread.java:
    4998)
        at java.lang.reflect.Method.invokeNative(Native Method)


        at java.lang.reflect.Method.invoke(Method.java:
    515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:
    811)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
    627)
        at dalvik.system.NativeStart.main(Native Method)
    java.lang.RuntimeException: 
    FFT: Real and Imaginary arrays are incorrect length!
     
  8. padvou

    padvou Active Member Licensed User

    Hello and thank you for posting this code.
    How is this implemented in a project?
    I mean I would like to get the frequency of a tone recorded by the device mic.
     
  9. padvou

    padvou Active Member Licensed User

    Would the following do the trick?
    Code:
    Public N = 16384 As Int
        
    Public N_2 = N / 2 As Int
       
        
    Public soundD(N) As Double

        
    Public FFT1 As FFT
        
    Public FFTReal(N_2+1As Double 'added +1
        Public FFTImg(N_2+1As Double 'added +1
        Public FFTAmp(N_2) As Double
     
  10. klaus

    klaus Expert Licensed User

    @padvou
    Sorry for answering only today, but I was on travel for a week so no B4x.
    The declarations must be:
    Code:
    Public N = 16384 As Int
    Public N_2 = N / 2 As Int
    Public N_21 = N_2 + 1 As Int
    Public soundD(N) As Double
    Public FFT1 As FFT
    Public FFTReal(N_21) As Double
    Public FFTImg(N_21) As Double
    Public FFTAmp(N_21) As Double
    ' Public FFTPhase(N_21) As Double
    for use with Transform2 and Inverse2.
     
    Last edited: Mar 18, 2015
    padvou likes this.
  11. klaus

    klaus Expert Licensed User

    @NewTechni
    Be careful, the FFT library had been updated.
    In the new version the array dimensions must be N / 2 + 1 !
    Look above.
     
  12. padvou

    padvou Active Member Licensed User

    @klaus
    Thank you very much for responding to me.
    Could you please also tell me what this is:
    What is the threshold for peak detection?
     
  13. Medel

    Medel Member Licensed User

    Hi, my name's Luis, i've been trying to use the FreqDetect.zip but i get the error:
    "An error has occurred in sub: java.lang.RuntimeException: FFT: Real and Imaginary arrays are incorrect lenght! Continue?"

    I also tried the FFT_Freq_Detect and i also get the same error...

    This is the sound file that i've been using to check if the frequency is correct:



    Does anyone have the same error? any suggestion? Thanks for any tip in advance
     
  14. Medel

    Medel Member Licensed User

    Hi again, i've solve my problem reading the past post from Klaus, just change it like in the picture.

    Thanks Klaus and Graham, awesome work!!!
     

    Attached Files:

  15. klaus

    klaus Expert Licensed User

    I have updated the projects in post #42 and #51.
     
  16. daniel69

    daniel69 Member Licensed User

    Hi Klaus! Any news about the ios FFT Class? Can I help somehow?

    y
     
  17. techknight

    techknight Well-Known Member Licensed User

    I like your FreqDetect Zip, but I need to modify it somehow and I am not sure how.

    I dont need graphs or anything, but what I do need is realtime monitoring of the mic for frequency peaks and amplitudes. Then I have a stored list with the certain frequencies and amplitudes stored, soon as I hit a match between stored list and FFT list, it fires an event.

    Your FFT program can basically do that, but it requires "recording" which I dont want. I need realtime live monitoring so the instant the sound is heard, analyzed, then matched it fires an event. Event runs, and then it keeps on moving until its heard again.
     
  18. techknight

    techknight Well-Known Member Licensed User

    Just tried the FFT detect and I dont think it works. Both of my phones keep showing the same 3 peaks, no matter if the room is silent or if I am playing something, plus I have to keep hitting record over and over. its NOT realtime!

    any thoughts???

    Peaks are 500.65, 899.1, and 1300.7 no matter what.
     
  19. Johan Hormaza

    Johan Hormaza Active Member Licensed User

    And how would it be for a Z transform?
     
  20. klaus

    klaus Expert Licensed User

    You would need to write it.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice