FFT binary butterfly computing

Source: Internet
Author: User

G-spider @ 2011 initial version.

Void butterfly (unsigned int L, int flag, double * X, double * Y)
{
// Flag = 1, FFT
// Flag =-1, IFFT
Unsigned int I, j, R, tmpi, M1, M2, M3, M4, M5, N, N0, K1, K2;
Double N_1, U, V, tmptheta;

N = (unsigned INT) 1 <L;
N0 = n> 2;
N_1 = 1.0/(double) N;
Tmptheta = _ 2PI * N_1;

If (flag =-1)
{
For (I = 0; I <n; I ++)
{
Y [I] =-y [I];
}

}
// ================================================ ======================
// Obtain the positive cosine table
Arrsin [0] = 0.0;
Arrcos [0] = 1.0;
Arrsin [1] = sin (tmptheta );
Arrcos [1] = cos (tmptheta );
If (L> 1)
{
For (I = 2; I <= N0; I + = 2)
{// I = 2, 4, 6 ,....
Tmpi = I> 1;

Arrsin [I] = 2.0 * arrsin [tmpi] * arrcos [tmpi];
Arrcos [I] = 1.0-2.0 * arrsin [tmpi] * arrsin [tmpi];
// ================================================ ======================
Arrsin [I + 1] = arrcos [I] * arrsin [1] + arrsin [I] * arrcos [1];
Arrcos [I + 1] =-arrsin [I] * arrsin [1] + arrcos [I] * arrcos [1];
// Printf ("% 18.16lf % 18.16lf/N", arrsin [I], arrcos [I]);
}

N0 <= 1;
For (; I <N0; I + = 2)
{// I = n0' + 2, n0' + 4, n0' + 6,..., N0 (n0' = N0/2)
// Sin (PI/2-x) = cos (x); sin (N0-i) = cos (I );
// Cos (PI/2-x) = sin (x); cos (N0-i) = sin (I );
Tmpi = N0-i;
Arrsin [I] = arrcos [tmpi];
Arrcos [I] = arrsin [tmpi];
// ================================================ ======================
Tmpi --;
Arrsin [I + 1] = arrcos [tmpi];
Arrcos [I + 1] = arrsin [tmpi];
}
}
// ================================================ ======================
// Binary butterfly computing
For (j = 0; j <L ;)
{
M1 = (unsigned INT) 1 <j ++;
M2 = m1 <1;
M3 = n> J;
M4 = 0;
For (I = 0; I <m3; I ++)
{
M5 = 0;
For (r = 0; r <= (M1> 1); R ++)
{
K1 = M4 + R;
K2 = k1 + M1;

U = x [k2] * arrcos [M5] + Y [k2] * arrsin [M5];
V = Y [k2] * arrcos [M5]-X [k2] * arrsin [M5];
X [k2] = x [k1]-U;
X [k1] = x [k1] + U;
Y [k2] = Y [k1]-V;
Y [k1] = Y [k1] + V;

M5 + = m3;
}

M5 = m3;
For (; r <M1; r ++)
{
K1 = M4 + R;
K2 = k1 + M1;

U = Y [k2] * arrcos [M5]-X [k2] * arrsin [M5];
V =-(Y [k2] * arrsin [M5] + X [k2] * arrcos [M5]);
X [k2] = x [k1]-U;
X [k1] = x [k1] + U;
Y [k2] = Y [k1]-V;
Y [k1] = Y [k1] + V;

M5 + = m3;
}
M4 + = m2;
}
}
// ================================================ ======================
// Inverse Transformation
If (flag =-1)
{
For (I = 0; I <n; I ++)
{
X [I] = x [I] * N_1;
Y [I] =-y [I] * N_1;
}

}

}

 

// ======================================

// Dynamic Allocation

Void butterfly (unsigned int L, int flag, double * X, double * Y)
{
// Flag = 1, FFT
// Flag =-1, IFFT
Unsigned int I, j, R, tmpi, M1, M2, M3, M4, M5, N, N0, K1, K2;
Double N_1, U, V, tmptheta, * arrsin = NULL, * arrcos = NULL;

N = (unsigned INT) 1 <L;
N0 = n> 2;
N_1 = 1.0/(double) N;
Tmptheta = _ 2PI * N_1;

If (flag =-1)
{
For (I = 0; I <n; I ++)
{
Y [I] =-y [I];
}

}

// ================================================ ======================
// Obtain the positive cosine table
Arrsin = (double *) malloc (n> 1) + 1) * sizeof (double ));
Arrcos = (double *) malloc (n> 1) + 1) * sizeof (double ));
If (arrsin = NULL | arrcos = NULL)
Printf ("memory allocated error/N ");
Arrsin [0] = 0.0;
Arrcos [0] = 1.0;
Arrsin [1] = sin (tmptheta );
Arrcos [1] = cos (tmptheta );
If (L> 1)
{
For (I = 2; I <= N0; I + = 2)
{// I = 2, 4, 6 ,....
Tmpi = I> 1;

Arrsin [I] = 2.0 * arrsin [tmpi] * arrcos [tmpi];
Arrcos [I] = 1.0-2.0 * arrsin [tmpi] * arrsin [tmpi];
// ================================================ ======================
Arrsin [I + 1] = arrcos [I] * arrsin [1] + arrsin [I] * arrcos [1];
Arrcos [I + 1] =-arrsin [I] * arrsin [1] + arrcos [I] * arrcos [1];
}

N0 <= 1;
For (; I <N0; I + = 2)
{// I = n0' + 2, n0' + 4, n0' + 6,..., N0 (n0' = N0/2)
// Sin (PI/2-x) = cos (x); sin (N0-i) = cos (I );
// Cos (PI/2-x) = sin (x); cos (N0-i) = sin (I );
Tmpi = N0-i;
Arrsin [I] = arrcos [tmpi];
Arrcos [I] = arrsin [tmpi];
// ================================================ ======================
Tmpi --;
Arrsin [I + 1] = arrcos [tmpi];
Arrcos [I + 1] = arrsin [tmpi];
}
}
// ================================================ ======================
// Binary butterfly computing
For (j = 0; j <L ;)
{
M1 = (unsigned INT) 1 <j ++;
M2 = m1 <1;
M3 = n> J;
M4 = 0;
For (I = 0; I <m3; I ++)
{
M5 = 0;
For (r = 0; r <= (M1> 1); R ++)
{
K1 = M4 + R;
K2 = k1 + M1;

U = x [k2] * arrcos [M5] + Y [k2] * arrsin [M5];
V = Y [k2] * arrcos [M5]-X [k2] * arrsin [M5];
X [k2] = x [k1]-U;
X [k1] = x [k1] + U;
Y [k2] = Y [k1]-V;
Y [k1] = Y [k1] + V;

M5 + = m3;
}

M5 = m3;
For (; r <M1; r ++)
{
K1 = M4 + R;
K2 = k1 + M1;

U = Y [k2] * arrcos [M5]-X [k2] * arrsin [M5];
V =-(Y [k2] * arrsin [M5] + X [k2] * arrcos [M5]);
X [k2] = x [k1]-U;
X [k1] = x [k1] + U;
Y [k2] = Y [k1]-V;
Y [k1] = Y [k1] + V;

M5 + = m3;
}
M4 + = m2;
}
}
// ================================================ ======================
// Inverse Transformation
If (flag =-1)
{
For (I = 0; I <n; I ++)
{
X [I] = x [I] * N_1;
Y [I] =-y [I] * N_1;
}

}

}

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.