B4A Library FFT Fast Fourier transform library

klaus

Expert
Licensed User
A new B4X FFT library does exits, the library in this thread is only valid for B4A.

The ownership of this post has been changed by Erel from agraham to klaus, with agrahams agreement, to allow me to edit this post with the update of the FFT library.

agrahams original text.
About nine months ago Klaus and I collaborated on a Basic4ppc library and demonstration program to perform forward and inverse Fast Fourier Transforms.

We have collaborated once again to port the library and demo to Basic4android.


Update 2014.08.03
The library has been updated after the bug report here.
The demo program has been 'rewritten' to take advantage of the B4A improvements since december 2010.
 

Attachments

Last edited:

Carol

Member
Licensed User
Fourier Transform and Filter

Hi, it is amazing to see FFT library here and thank you for the wonderful library. I am wondering if it is possible to perform high/low/bandpass filter with this library?
(not a lot of info about filters in the community..) Thank you!

Carol
 

klaus

Expert
Licensed User
I am wondering if it is possible to perform high/low/bandpass filter with this library?
There is no filter function in the library. Theoreticaly you can filter with FFT. If you have an FFT remove spectral lines and make an inverse FFT then you have a filetered signal without the frequencies you removed.
You can test this with the demo program.
- select 3 sin
- set the cursor on the right most spectral line (line 100)
- press delete, just as a reminder, the Delete is a toggle button.
- move the cursor over the other line (line 25) it will be removed also.
- now click on FFT I to calculate the inverse FFT
- you will see that only the fundamental sine remains, the two others have been 'filtered out', this is a low pass filter.

Best egards.
 

Tom Christman

Active Member
Licensed User
Klaus and Andrew: Thanks so much for the FFT, and all you contribute to the Forum. I know you have instructed and aided all of us who use B4A for development and continue to do so.:sign0098:
Tom
 

NeoTechni

Well-Known Member
Licensed User
I have a single array of integers containing the amplitude data from the microphone. How would I use this library to get a spectrum graph?
 

klaus

Expert
Licensed User
You need to transform the integers to doubles as the library expects doubles.
You need 3 arrays:
The time array sizes must be a power of 2 !
The size of the FFT arrays is half the size of the time array + 1.

Example:
Dim TimeAmplitude(256), the time domain amplitude, your array
Dim FFTReal(129), the real componants of the FFT
Dim FFTImag(129), the imaginary componants of the FFT

You can add two more arrays
Dim FFTAmpl(129)
Dim FFTPhase(129)


Then with FFT1.Transform2(TimeAmplitude, FFTReal, FFTImag) calculate the real and imaginary componants of the FFT.
Then with FFTAmpl = FFT1.ToAmplitude(FFTReal, FFTImag)
and FFTPhase = FFT1.ToPhase(FFTReal, FFTImag)
calculate the FFT amplitudes and phases.

You can also calculate the inverse FFT transform with
FFT1.Inverse2(FFTReal, FFTImag, TimeAmplitude)

Best regards.
 
Last edited:

NeoTechni

Well-Known Member
Licensed User
Thank you.

X = 2 ^ n
Y = X/2
Dim TimeAmplitude(X) - so this would be my data from the mic converted to doubles. Does it expect values between 0 and 1, or any number?
Dim FFTReal(Y) - empty, filled by Transform2
Dim FFTImag(Y) - empty, filled by Transform2

And I only need 1 of these, either of them
Dim FFTAmpl(Y) - empty, filled by ToAmplitude
Dim FFTPhase(Y) - empty, filled by ToPhase

And I don't need to know what real and imaginary mean, cause I'm only using the ampl/phase data?
 

klaus

Expert
Licensed User
Does it expect values between 0 and 1, or any number?
Any number.

You need FFTReal and FFTImaginary arrays because the Transform function calculates the real and imaginary parts.
To get the amplitude and or phase you must calculate them with ToAmplitude and or ToPhase.

Best regards.
 

NeoTechni

Well-Known Member
Licensed User
I know I need the arrays, what I meant is, I don't know what real/imaginary means (other than real being any number and imaginary being a square root of a negative number) and I don't need to know cause it handles them
 

klaus

Expert
Licensed User
The FFT can be expressed either as a :
- sum of sines expressed as amplitude and phase
- sum of cosines (real) and sines (imaginary)

Both express the same, it's a different mathematical writing.

Best regards.
 

NeoTechni

Well-Known Member
Licensed User
The FFT can be expressed either as a :
- sum of sines expressed as amplitude and phase
- sum of cosines (real) and sines (imaginary)

Both express the same, it's a different mathematical writing.

Best regards.
So I would use getamplitude and getphase
the same cell of the phases would be the frequency and the other array would be the amplitude? And these would be in order?
 

NeoTechni

Well-Known Member
Licensed User
It didnt answer my question, as that's what I had before I asked.

I want to know what to do with the data to visualize the audio by frequency+amplitude
 

bluejay

Active Member
Licensed User
I assume your question is "What is the frequency?"

The frequency is not stored in any of the arrays but calculated from the Sampling frequency Fs used used to record the N time samples.

N time samples get converted into N/2+1 frequency samples, each frequency sample has two values Amplitude and Phase. You can ignore the values in the Phase array and just use the Amplitude array.

The frequencies points go from 0 Hz to Fs/2 (the Nyquist frequency).

For example, if you measure the signal every 1msec for 256 samples then the Sampling Frequency Fs = 1000Hz so the FFT output points go from 0 to 500 Hz in steps of Fs/N or 1000/256 = 3.90625 Hz.

I assume in this FFT library the calculation results ignore the DC component ( 0 Hz) so the N/2 frequency points actually go from Fs/N to Fs/2 in steps of Fs/N.

Bluejay
 

lock255

Well-Known Member
Licensed User
I was wondering if with this library I can analyze a recording and get the second frequency per second.
 

riclp

New Member
Licensed User
I was wondering if with this library I can analyze a recording and get the second frequency per second.
I´m not sure what you mean, but it´s possible to apply FFT to sound segments and get the frequency with max amplitude.
Regards
 

lock255

Well-Known Member
Licensed User
It is possible, but the problem is that I can not use this library into, I need someone to write me a esmpio appropriate for my problem.
 

riclp

New Member
Licensed User
It is possible, but the problem is that I can not use this library into, I need someone to write me a esmpio appropriate for my problem.
B4X:
'' For a 16.384 samples 8 bit quantized sound
sample = 16384

Dim sound(sample) as Byte
Dim soundD(sample) as Double
'' Here you need to store your data in "sound"
'' .....
'' And convert them to Double
for cont= 0 to sample-1
    soundD(cont) = sound(cont)
'' Aply fft
Dim fft as FFT
Dim re(sample/2) as Double
Dim im(sample/2) as Double
Dim amp(sample/2) as Double
fft.Transform2(soundD, re, im)
amp = fft.ToAmplitude(re,im)
Dim max as int = 0
for cont= 0 to sample/2
    If (amp(cont)> amp(max)) Then
        max = cont
    End
Next
 
Top