Question

I've written some simple audio processing algorithms in MATLAB that I will be using for an Android application. I'm having some trouble translating the FFT implementation to Java.

I'm trying to use the Java version of fftpack, which is reportedly efficient, but is also a bit cryptic to me, perhaps because my lack of a firm grasp on FFT in general.

I simply have a double array from which I want to produce a corresponding array of Complex numbers representing the FFT. The only calculation I will be doing on the FFT is taking the absolute value of its elements.

For clarity's sake, here is the essential MATLAB code that I would like to replicate using Java fftpack:

X = fft(myDoubleArray);
abs(X[i]);

I expect this is rather simple, but I can't identify entry points into fftpack.


SOLVED:

The answer below from LutzL works, but creating a Complex1D object is actually unnecessary as fftpack supports a double array as input:

RealDoubleFFT rdfft = new RealDoubleFFT(myArray.length);
rdfft.ft(myArray);
Was it helpful?

Solution

Reading from sourceforge jfftpack repository

You initialize a new fft object via

fft = new ComplexDoubleFFT(N);

Then you use the forward transform of the fft object with

Complex1D X;
X.x=new double[N];
X.y=new double[N];
for(k=0;k<N;k++) X.x[k]=x[k];

fft.ft(X);

and can then access the absolute values as

x=X.x[k]; y=X.y[k]; a=Math.sqrt(x*x+y*y);

Optionally divide by N to normalize the FFT.

Add appropriate variable declarations.


The usual conventions apply, if the signal is sampled with sampling frequency fs, the entry X.(xy)[k] represents the frequency component at k*fs/N for k=N/2 to N-1.


You may want to look for a different, better supported FFT implementation that is done in pure java, it is not that complicated to implement the FFT for dyadic lengths. The only complication comes when implementing the transformation in place to get the last speedup factor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top