# B4A Class[B4x] xFFT Class and b4xlib

This class performs Fast Fourier Transformations forward and inverse.

The class has been updated to a B4XLibrary.
The name hase changed from FFT to xFFT, you need to change it in the declaration if you update from the previous code module to the b4xlib.
The class is exactly the same for B4A, B4i and B4J.

A FFT library already exists, but only for B4A.

A Fast Fourier Transformation transforms a signal from its original domain (mostly the time domain) into its representation in the frequency domain.

The number N of samples in the original domain must be a power of 2?
Examples: 256, 1024, 2048, 4096 etc.

In theory, the source signal must have a real and an imaginary part.
In time signals the imaginary samples are all 0, this is treated internally in the class, no need to transmit them.
The result of an FFT is two signals real and imaginary.
Which can also be represented by magnitude and phase.

In concrete applications, the magnitude of the frequency signal is mostly used, therefore the Forward method returns the Magnitude.
The phase, real and imaginary signals can be accessed as properties.

Attached files:
xFFTDemo.zip contains three demo programs, one for each platform, these programs use the xFFT.b4xlib
xFFT.b4xlib the B4X Library file
xFFT.bas the Class file
FFT_Record.zip a concrete B4A project explained below.
xFFT.xml the xml help file, it is only needed for the B4X Help Viewer if you use it.
Don't copy this file to the AdditionalLibraries\B4X folder.
Copy it in the AdditionalLibraries\B4XlibXMLFiles folder, again, only if you use the B4X Help Viewer.

Methods:

Forward(Real() As Double) As Double() performs a FFT calculation
Real = an array of N Doubles representing the Real time signal.
Returns the Magnitude of the frequency domain N/2 + 1 samples.

Inverse(Real() As Double, Imag() As Double) As Double() performs an inverse FFT calculation
Real = an array of N/2 + 1 Doubles representing the real part of the frequency signal.
Imag = an array of N/2 + 1 Doubles representing the imaginary part of the frequency signal.
Returns the inverse frequency real signal, N samples.

Properties:

Magnitude = magnitude of the frequency signal N/2 + 1 samples, read only.

Phase = phase of the frequency signal N/2 + 1 samples, read only.

Real = real part of the frequency signal N/2 + 1 samples, read only.

Imag = imaginary part of the frequency signal N/2 + 1 samples, read only.

ModeDegrees = True (default), the phase signal is in degrees otherwise in radians.

Window = NONE or Hann, applies no window or a Hann window.

Demo programs:

Attached a demo project for each product B4A, B4i and B4J, they use the xFFT.b4xlib.

The program can:

Calculate different input signals.

Calculate forward and inverse FFTs.

Display the different signals.

Apply a Window.

For Android phones and iPhones tap on the chart to show the menu.

On Android and iOS tablets and in the B4J project the menu is always visible.

Concrete B4A application: FFT_Record

The program reads the microphone signal.

Start stop the reading

Switch between Time and FFT

Generate a sound composed of three frequencies: 500, 1000 and 2000

Change the scale up and down

Display the signal values when moving the finger over the diagram.

#### Attachments

• xFFTDemo_V1_2.zip
17 KB · Views: 545
• xFFT.b4xlib
2 KB · Views: 529
• xFFT.bas
6.2 KB · Views: 475
• xFFT.xml
3.1 KB · Views: 467
• FFT_Record.zip
54.9 KB · Views: 541
Last edited:

#### SimonElck

##### Member
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
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
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.
Thanks, the permission thing was the problem. Same for the Tuner project.

xFFTDemo worked well, sorry

Thanks again

Replies
34
Views
8K
Replies
39
Views
8K
Replies
12
Views
12K
Replies
106
Views
25K
Replies
669
Views
124K