# Android-Java: The reconstruction of the fast Fourier transform of a real signal using libgdx fft for android

Here is an example of calculating the Fourier Transform android using the FFT class from libgdx. The code I have provided is developed on eclipse. Before you start, be sure to download the libgdx jar library files.

Here is a good resource for learning how to install libgdx in eclipse along with some great tutorials on where to begin.

In this example, I also calculate the magnitude and phase of a real one dimensional array or signal. This is useful for many apps, especially those that do image/video or audio processing of some kind. Once the magnitude and phase signals are obtained, the program reconstructs the original array with the inverse Fourier transform. Feel free to use this code, but I ask please at least learn how it works before you use it.

Ensure you also follow this guide which teaches you how to set up a project with libgdx in java desktop and android by badlogic games

Here is the source code:

package com.spec.example; import android.app.Activity; import android.os.Bundle; import com.badlogic.gdx.audio.analysis.FFT; import java.lang.String; import android.util.FloatMath; import android.widget.TextView; public class spectrogram extends Activity { /** Called when the activity is first created. */ float[] array = {1, 6, 1, 4, 5, 0, 8, 7, 8, 6, 1,0, 5 ,6, 1,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; float[] array_hat,res=new float[array.length/2]; float[] fft_cpx,tmpr,tmpi; float[] mod_spec =new float[array.length/2]; float[] real_mod = new float[array.length]; float[] imag_mod = new float[array.length]; double[] real = new double[array.length]; double[] imag= new double[array.length]; double[] mag = new double[array.length]; double[] phase = new double[array.length]; int n; float tmp_val; String strings; FFT fft = new FFT(32, 8000); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); fft.forward(array); fft_cpx=fft.getSpectrum(); tmpi = fft.getImaginaryPart(); tmpr = fft.getRealPart(); for(int i=0;i<array.length;i++) { real[i] = (double) tmpr[i]; imag[i] = (double) tmpi[i]; mag[i] = Math.sqrt((real[i]*real[i]) + (imag[i]*imag[i])); phase[i]=Math.atan2(imag[i],real[i]); /****Reconstruction****/ real_mod[i] = (float) (mag[i] * Math.cos(phase[i])); imag_mod[i] = (float) (mag[i] * Math.sin(phase[i])); } fft.inverse(real_mod,imag_mod,res); } }

If you want an exact project example, I recommend taking a look at another post: simple fft exampling using libgdx where you can also download the source code.

To see a working example of this library for a signal processing application, you can take a look at my android app on the market, called ‘Enhancement‘.

This application is an educational platform for university students to learn about Digital Signal Processing (DSP) techniques and Speech Enhancement algorithms currently used in teleconferencing, radio communication, mobile phones and even forensic science. Alternatively you can check out the web site enhancementapp.com or even the ‘Enhancement Demo‘ App on the market.

Exactly what is the positive change concerning the iphone 4 and iphone five that are coming out for United states of america?

i’d really like to know if it’s really worth it to obtain the iphone4 or if i must wait for a apple iphone 5.

Well that is a common question. The fact is the technology turn around is every 6months, so the iphone 5 will have more advanced technology compared to the iphone 4. Notably it will have a dual core 1.2 Ghz CPU, a 8Mpixel camera, longer battery life (fingers crossed), new operating system etc. Read more here:

http://firstsearchblue.com/iphone-5-specs-release-date/

If you don’t NEED a smart phone right away, go the the iphone4. Otherwise I’d recommend holding out till the iphone5, set to be released in the USA july-september 2011, so you can expect in Au by the end of the year.

How long have you been running this blog? It appears like you have a nice web-site here and I’ll be bookmarking you. I will probably submit this site post to Digg to ensure that my followers can verify it out too.

Hi Dustin

Apologies for miss-leading you with the TextView. There are many applications you can use this Fourier transform framework. For instance extracting frequency content from an audio signal or an image. This alone is not an App, it is purely an inner class which can be used to calculate the magnitude and phase of a signal. If you want to display the reconstructed signal, you can use a TextView and iterate in a for loop to print out each reconstructed value on to the screen. If you need specific help, let me know.

Hi Roger

I am new to android development and I am doing an assignment about digital signal processing.

Could you give me some pointers about FFT and your method like fft.getspectrum.

Maybe you would like to give some code example, it will give a big help to me.

Sorry for the late reply, glad you worked it out! How’s your project going?

Oh I got it. I’ve forgot to build path for gdx-audio.jar.