Single Dimension and Multidimensional Fast Fourier Transform Using Apache Commons Math Library
in
Share your Work
•
1 year ago
Friends,
I had a hard time finding examples of complex arrays[], multidimensional complex arrays[][], single dimension and multidimensional ffts. Attached is the code that i came up to work. It is not clean and does declare some variables that are not used, but it does work. Also, I am neither a Java or Processing expert, so feel free to improve
. I used an image called square30.jpeg. You can find this and others on the web.
How to use:
1. Load Apache Commons Math Library. There are numerous threads on how to do this in the forum. Load this in your Processing Libraries. You need commons-math-2.2. See threads. When you save in library, remove dashes and periods from library and file names.
2. Must use an image that is a power of 2. If not, you need to pad the image to that size.
3. Image must be in local processing project folder.
Good luck!
- import org.apache.commons.math.stat.clustering.*;
- import org.apache.commons.math.stat.descriptive.summary.*;
- import org.apache.commons.math.analysis.polynomials.*;
- import org.apache.commons.math.analysis.integration.*;
- import org.apache.commons.math.stat.inference.*;
- import org.apache.commons.math.stat.*;
- import org.apache.commons.math.complex.*;
- import org.apache.commons.math.dfp.*;
- import org.apache.commons.math.stat.descriptive.*;
- import org.apache.commons.math.ode.jacobians.*;
- import org.apache.commons.math.special.*;
- import org.apache.commons.math.stat.descriptive.rank.*;
- import org.apache.commons.math.*;
- import org.apache.commons.math.exception.*;
- import org.apache.commons.math.stat.correlation.*;
- import org.apache.commons.math.stat.regression.*;
- import org.apache.commons.math.util.*;
- import org.apache.commons.math.analysis.solvers.*;
- import org.apache.commons.math.optimization.general.*;
- import org.apache.commons.math.analysis.interpolation.*;
- import org.apache.commons.math.analysis.*;
- import org.apache.commons.math.stat.descriptive.moment.*;
- import org.apache.commons.math.ode.sampling.*;
- import org.apache.commons.math.optimization.univariate.*;
- import org.apache.commons.math.estimation.*;
- import org.apache.commons.math.geometry.*;
- import org.apache.commons.math.fraction.*;
- import org.apache.commons.math.ode.events.*;
- import org.apache.commons.math.genetics.*;
- import org.apache.commons.math.ode.*;
- import org.apache.commons.math.random.*;
- import org.apache.commons.math.transform.*;
- import org.apache.commons.math.optimization.direct.*;
- import org.apache.commons.math.ode.nonstiff.*;
- import org.apache.commons.math.linear.*;
- import org.apache.commons.math.optimization.*;
- import org.apache.commons.math.exception.util.*;
- import org.apache.commons.math.distribution.*;
- import org.apache.commons.math.stat.ranking.*;
- import org.apache.commons.math.optimization.fitting.*;
- /*******************************************************************************************************
- Example of Complex Array, Multi-Dimensional Complex Array, Single dimensional and Multi dimensional
- fast fourier transform using the Apache Commons Math Library
- By john.d.engnr Feb. 2012
- Have fun!!!
- *******************************************************************************************************/
- PImage img;
- float rr,ri;
- int rs;
- int row,col, rw,cl,pos;
- double pix;
- FastFourierTransformer transformer = new FastFourierTransformer();
- FastFourierTransformer trans2 = new FastFourierTransformer();
- Complex result[];
- Complex realresult[][];
- void setup() {
- size(1024, 256);
- noFill();
- stroke(255);
- frameRate(30);
- img = loadImage("square30.jpg");
- col = img.width;
- row = img.height;
- double [] c = new double[col];
- Complex [][]realresult = new Complex[row][col];
- Complex [][]cc = new Complex[row][col];
- Complex z = new Complex(0.0,0.0);
- set(0,0,img);
- img.loadPixels();
- for( rw =0; rw < row; rw++)
- {
- for( cl =0; cl < col; cl++)
- {
- pos = (rw*col) + cl;
- c[cl] = red(img.pixels[pos]);
- }
- result = transformer.transform(c);
- for ( cl = 0; cl < col; cl++)
- {
- pos = (rw*col) + cl;
- rr =(float)result[cl].getReal();
- ri = (float)result[cl].getImaginary();
- rs = (int)sqrt(sq(rr) + sq(ri));
- img.pixels[pos] = color(rs);
- }
- }
- img.updatePixels();
- set(256,0,img);
- /*****************************************************************************
- reload image for multidimentional fft -- array has to be complex.
- Must reload img.pixels (single dimentional real) to multi dimentional complex)
- ******************************************************************************/
- img = loadImage("square30.jpg");
- img.loadPixels();
- for( rw =0; rw < row; rw++)
- {
- for( cl =0; cl < col; cl++)
- {
- pos = (rw*col) + cl;
- pix = (double) red(img.pixels[pos]); //load pixels from single dimension to two dimensional array
- cc[rw][cl] = new Complex(pix,0.00D);
- }
- }
- realresult = (Complex[][])trans2.mdfft(cc,true);
- for ( rw = 0; rw < row; rw++)
- {
- for ( cl = 0; cl < col; cl++)
- {
- pos = (rw*col) + cl;
- rr =(float)realresult[rw][cl].getReal();
- ri = (float)realresult[rw][cl].getImaginary();
- rs = (int)sqrt(sq(rr) + sq(ri));
- img.pixels[pos] = color(rs*20);// multiply color by 20 to intensify white
- }
- }
- img.updatePixels();
- set(512,0,img);
- /************************************************************************************************
- Convert Fourier transform from frequency domain back to time domain
- ************************************************************************************************/
- cc = (Complex[][])trans2.mdfft(realresult,false);
- for ( rw = 0; rw < row; rw++)
- {
- for ( cl = 0; cl < col; cl++)
- {
- pos = (rw*col) + cl;
- rr =(float)cc[rw][cl].getReal();
- ri = (float)cc[rw][cl].getImaginary();
- rs = (int)sqrt(sq(rr) + sq(ri));
- img.pixels[pos] = color(rs);
- }
- }
- img.updatePixels();
- set(768,0,img);
- }