Java fft2 and FFT :)

Source: Internet
Author: User

FFT is used to change the C code on the Internet, and fft2 is written by itself. Who can give an FFT program of any length (I .e., the input does not need to be exponentially multiplied by 2 )?

Public class fftlist {// provides FFT output
Double [] fr;
Double [] Fi;
}

Public class fft2list {// provides fft2 output
Double [] [] outr;
Double [] [] Outi;
}

// INR and ini are the real and virtual parts of the input image. If there is no virtual part, you only need to make ini = NULL. W and H are input.
// The width and height of the input image. ncols and nrows are the width and height of the output image. Note that ncols and nrows must be 2
// Exponent, IFFT = true is the inverse fft2 operation
// This function is equivalent to fft2 (x, nrows, ncols) in MATLAB)
Public fft2list fft2 (double [] [] INR, double [] [] INI, int W, int H, int n
Cols, int nrows, Boolean IFFT)
{

Int I, J, K;
Double [] Pr = new double [ncols]; // Save the real part of each row
Double [] Pi = new double [ncols]; // Save the virtual part of each row
K = (INT) (math. Log (ncols)/Math. Log (2.0); // K = log2 (ncols)

Fft2list fft2list = new fft2list ();
Fft2list. outr = new double [ncols] [nrows];
Fft2list. Outi = new double [ncols] [nrows];

////////// Calculate the FFT for each line first /////////////
For (I = 0; I {
For (j = 0; j

// INR and ini are the real and virtual parts of the input image. If there is no virtual part, you only need to make ini = NULL. W and H are input.
// The width and height of the input image. ncols and nrows are the width and height of the output image. Note that ncols and nrows must be 2
// Exponent, IFFT = true is the inverse fft2 operation
// This function is equivalent to fft2 (x, nrows, ncols) in MATLAB)
Public fft2list fft2 (double [] [] INR, double [] [] INI, int W, int H, int n
Cols, int nrows, Boolean IFFT)
{

Int I, J, K;
Double [] Pr = new double [ncols]; // Save the real part of each row
Double [] Pi = new double [ncols]; // Save the virtual part of each row
K = (INT) (math. Log (ncols)/Math. Log (2.0); // K = log2 (ncols)

Fft2list fft2list = new fft2list ();
Fft2list. outr = new double [ncols] [nrows];
Fft2list. Outi = new double [ncols] [nrows];

////////// Calculate the FFT for each line first /////////////
For (I = 0; I {
For (j = 0; j <ncols; j ++) // clear 0
{
PR [J] = 0;
Pi [J] = 0;
}
For (j = 0; j <W; j ++) // assign the real and virtual parts of each row to PR and PI
{
PR [J] = INR [J] [I];
If (INI! = NULL) // The input may have no virtual part
Pi [J] = ini [J] [I];
}
Fftlist = This. FFT (PR, PI, ncols, K, IFFT );

For (j = 0; j <ncols; j ++) // Save the results of each row to outr and Outi.
{
Fft2list. outr [J] [I] = fftlist. FR [J];
Fft2list. Outi [J] [I] = fftlist. Fi [J];
}
}

/////////////////////////////////////////

PR = new double [nrows];
Pi = new double [NR
PR [J] = 0;
Pi [J] = 0;
}
For (j = 0; j <W; j ++) // assign the real and virtual parts of each row to PR and PI
{
PR [J] = INR [J] [I];
If (INI! = NULL) // The input may have no virtual part
Pi [J] = ini [J] [I];
}
Fftlist = This. FFT (PR, PI, ncols, K, IFFT );

For (j = 0; j <ncols; j ++) // Save the results of each row to outr and Outi.
{
Fft2list. outr [J] [I] = fftlist. FR [J];
Fft2list. Outi [J] [I] = fftlist. Fi [J];
}
}

/////////////////////////////////////////

PR = new double [nrows];
Pi = new double [nrows];
K = (INT) (math. Log (nrows)/Math. Log (2.0); // K = log2 (nrows)

//// // Then calculate the FFT for each column /////////////////

For (I = 0; I <ncols; I ++)
{
For (j = 0; j <nrows; j ++) // clear 0
{
PR [J] = 0;
Pi [J] = 0;
}
For (j = 0; j {
PR [J] = fft2list. outr [I] [J]; // convert each of the preceding results
// Columns are assigned to PR and PI.
Pi [J] = fft2list. Outi [I] [J];
}
Fftlist = This. FFT (PR, PI, nrows, K, IFFT );
For (j = 0; j <nrows; j ++)
{
Fft2list. outr [I] [J] = fftlist. FR [J];
Fft2list. Outi [I] [J] = fftlist. Fi [J];
} NT) (math. Log (nrows)/Math. Log (2.0); // K = log2 (nrows)

//// // Then calculate the FFT for each column /////////////////

For (I = 0; I <ncols; I ++)
{
For (j = 0; j <nrows; j ++) // clear 0
{
PR [J] = 0;
Pi [J] = 0;
}
For (j = 0; j {
PR [J] = fft2list. outr [I] [J]; // convert each of the preceding results
// Columns are assigned to PR and PI.
Pi [J] = fft2list. Outi [I] [J];
}
Fftlist = This. FFT (PR, PI, nrows, K, IFFT );
For (j = 0; j <nrows; j ++)
{
Fft2list. outr [I] [J] = fftlist. FR [J];
Fft2list. Outi [I] [J] = fftlist. Fi [J];
}
}
Return fft2list;
}

// N = 2 ^ K. Other computation errors may occur.
Public fftlist FFT (double [] PR, double [] Pi, int N, int K, Boolean IFFT)
{
Int it, m, is, I, J, NV, l0;
Double P, Q, S, VR, Vi, poddr, poddi;
Fftlist Cl = new fftlist ();
Cl. Fr = new double [N];
Cl. Fi = new double [N];

For (IT = 0; it <= n-1; it ++)
{
M = it; Is = 0;
For (I = 0; I <= K-1; I ++)
{J = m/2; Is = 2 * is + (m-2 * j); M = J ;}
Cl. FR [it] = Pr [is]; CL. Fi [it] = PI [is];
}

PR [0] = 1.0; Pi [0
Return fft2list;
}

// N = 2 ^ K. Other computation errors may occur.
Public fftlist FFT (double [] PR, double [] Pi, int N, int K, Boolean IFFT)
{
Int it, m, is, I, J, NV, l0;
Double P, Q, S, VR, Vi, poddr, poddi;
Fftlist Cl = new fftlist ();
Cl. Fr = new double [N];
Cl. Fi = new double [N];

For (IT = 0; it <= n-1; it ++)
{
M = it; Is = 0;
For (I = 0; I <= K-1; I ++)
{J = m/2; Is = 2 * is + (m-2 * j); M = J ;}
Cl. FR [it] = Pr [is]; CL. Fi [it] = PI [is];
}

PR [0] = 1.0; Pi [0] = 0.0;
P = 2 * Math. PI/(1.0 * n );

PR [1] = math. Cos (p); Pi [1] =-math. Sin (P );

If (IFFT) PI [1] =-Pi [1]; // inverse FFT

For (I = 2; I <= n-1; I ++)
{
P = Pr [I-1] * PR [1]; q = PI [I-1] * PI [1];
S = (PR [I-1] + PI [I-1]) * (PR [1] + PI [1]);
PR [I] = p-Q; Pi [I] = s-p-Q;
}

For (IT = 0; it <= n-2; it = It + 2)
{
Vr = Cl. FR [it]; VI = Cl. Fi [it];
Cl. FR [it] = VR + Cl. FR [It + 1]; CL. Fi [it] = vi + Cl. Fi [It + 1];
Cl. FR [It + 1] = vr-cl.fr [It + 1]; CL. Fi [It + 1] = vi-cl.fi [It + 1];
}

M = n/2; NV = 2;
For (L0 = K-2; l0> = 0; l0 --)
{
M = m/2; NV = 2 * NV;
PR [1] = math. Cos (p); Pi [1] =-math. Sin (P );

If (IFFT) PI [1] =-Pi [1]; // inverse FFT

For (I = 2; I <= n-1; I ++)
{
P = Pr [I-1] * PR [1]; q = PI [I-1] * PI [1];
S = (PR [I-1] + PI [I-1]) * (PR [1] + PI [1]);
PR [I] = p-Q; Pi [I] = s-p-Q;
}

For (IT = 0; it <= n-2; it = It + 2)
{
Vr = Cl. FR [it]; VI = Cl. Fi [it];
Cl. FR [it] = VR + Cl. FR [It + 1]; CL. Fi [it] = vi + Cl. Fi [It + 1];
Cl. FR [It + 1] = vr-cl.fr [It + 1]; CL. Fi [It + 1] = vi-cl.fi [It + 1];
}

M = n/2; NV = 2;
For (L0 = K-2; l0> = 0; l0 --)
{
M = m/2; NV = 2 * NV;
For (IT = 0; it <= (m-1) * NV; it = It + NV)
For (j = 0; j <= (NV/2)-1; j ++)
{
P = Pr [M * j] * Cl. FR [It + J + NV/2];
Q = PI [M * j] * Cl. Fi [It + J + NV/2];
S = Pr [M * j] + PI [M * j];
S = S * (Cl. FR [It + J + NV/2] + Cl. Fi [It + J + NV/2]);
Poddr = p-Q; poddi = s-p-Q;
Cl. FR [It + J + NV/2] = Cl. FR [It + J]-poddr;
Cl. Fi [It + J + NV/2] = Cl. Fi [It + J]-poddi;
Cl. FR [It + J] = Cl. FR [It + J] + poddr;
Cl. Fi [It + J] = Cl. Fi [It + J] + poddi;
}
}

If (IFFT)
For (I = 0;

M = n/2; NV = 2;
For (L0 = K-2; l0> = 0; l0 --)
{
M = m/2; NV = 2 * NV;
For (IT = 0; it <= (m-1) * NV; it = It + NV)
For (j = 0; j <= (NV/2)-1; j ++)
{
P = Pr [M * j] * Cl. FR [It + J + NV/2];
Q = PI [M * j] * Cl. Fi [It + J + NV/2];
S = Pr [M * j] + PI [M * j];
S = S * (Cl. FR [It + J + NV/2] + Cl. Fi [It + J + NV/2]);
Poddr = p-Q; poddi = s-p-Q;
Cl. FR [It + J + NV/2] = Cl. FR [It + J]-poddr;
Cl. Fi [It + J + NV/2] = Cl. Fi [It + J]-poddi;
Cl. FR [It + J] = Cl. FR [It + J] + poddr;
Cl. Fi [It + J] = Cl. Fi [It + J] + poddi;
}
}

If (IFFT)
For (I = 0; I <= n-1; I ++)
{
Cl. FR [I] = Cl. FR [I]/(1.0 * n );
Cl. Fi [I] = Cl. Fi [I]/(1.0 * n );
}

Return Cl;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.