Merge pull request #778 from csoler/v0.6-ImprovedGUI

V0.6 improved gui
This commit is contained in:
csoler 2017-04-19 18:53:49 +02:00 committed by GitHub
commit ccfd12b506
2 changed files with 865 additions and 157 deletions

View File

@ -0,0 +1,827 @@
/******************************************************************
Original FFT code Credits:
Copyright Takuya OOURA, 1996-2001
http://www.kurims.kyoto-u.ac.jp/~ooura/fft.html
******************************************************************/
/*
Fast Fourier/Cosine/Sine Transform
dimension :two
data length :power of 2
decimation :frequency
radix :4, 2, row-column
data :inplace
table :use
functions
cdft2d: Complex Discrete Fourier Transform
rdft2d: Real Discrete Fourier Transform
ddct2d: Discrete Cosine Transform
ddst2d: Discrete Sine Transform
function prototypes
void cdft2d(int, int, int, double **, int *, double *);
void rdft2d(int, int, int, double **, int *, double *);
void ddct2d(int, int, int, double **, double **, int *, double *);
void ddst2d(int, int, int, double **, double **, int *, double *);
-------- Complex DFT (Discrete Fourier Transform) --------
[definition]
<case1>
X[k1][k2] = sum_j1=0^n1-1 sum_j2=0^n2-1 x[j1][j2] *
exp(2*pi*i*j1*k1/n1) *
exp(2*pi*i*j2*k2/n2), 0<=k1<n1, 0<=k2<n2
<case2>
X[k1][k2] = sum_j1=0^n1-1 sum_j2=0^n2-1 x[j1][j2] *
exp(-2*pi*i*j1*k1/n1) *
exp(-2*pi*i*j2*k2/n2), 0<=k1<n1, 0<=k2<n2
(notes: sum_j=0^n-1 is a summation from j=0 to n-1)
[usage]
<case1>
ip[0] = 0; // first time only
cdft2d(n1, 2*n2, 1, a, ip, w);
<case2>
ip[0] = 0; // first time only
cdft2d(n1, 2*n2, -1, a, ip, w);
[parameters]
n1 :data length (int)
n1 >= 1, n1 = power of 2
2*n2 :data length (int)
n2 >= 1, n2 = power of 2
a[0...n1-1][0...2*n2-1]
:input/output data (double **)
input data
a[j1][2*j2] = Re(x[j1][j2]),
a[j1][2*j2+1] = Im(x[j1][j2]),
0<=j1<n1, 0<=j2<n2
output data
a[k1][2*k2] = Re(X[k1][k2]),
a[k1][2*k2+1] = Im(X[k1][k2]),
0<=k1<n1, 0<=k2<n2
ip[0...*]
:work area for bit reversal (int *)
length of ip >= 2+sqrt(n)
(n = max(n1, n2))
ip[0],ip[1] are pointers of the cos/sin table.
w[0...*]
:cos/sin table (double *)
length of w >= max(n1/2, n2/2)
w[],ip[] are initialized if ip[0] == 0.
[remark]
Inverse of
cdft2d(n1, 2*n2, -1, a, ip, w);
is
cdft2d(n1, 2*n2, 1, a, ip, w);
for (j1 = 0; j1 <= n1 - 1; j1++) {
for (j2 = 0; j2 <= 2 * n2 - 1; j2++) {
a[j1][j2] *= 1.0 / (n1 * n2);
}
}
*/
/* -------- initializing routines -------- */
#pragma once
#include <math.h>
class fft
{
public:
static void makewt(int nw, int *ip, double *w)
{
int nwh, j;
double delta, x, y;
ip[0] = nw;
ip[1] = 1;
if (nw > 2) {
nwh = nw >> 1;
delta = atan(1.0) / nwh;
w[0] = 1;
w[1] = 0;
w[nwh] = cos(delta * nwh);
w[nwh + 1] = w[nwh];
for (j = 2; j <= nwh - 2; j += 2) {
sincos(delta*j,&y,&x) ;
//x = cos(delta * j);
//y = sin(delta * j);
w[j] = x;
w[j + 1] = y;
w[nw - j] = y;
w[nw - j + 1] = x;
}
bitrv2(nw, ip + 2, w);
}
}
/* -------- child routines -------- */
static void bitrv2(int n, int *ip, double *a)
{
int j, j1, k, k1, l, m, m2;
double xr, xi;
ip[0] = 0;
l = n;
m = 1;
while ((m << 2) < l) {
l >>= 1;
for (j = 0; j <= m - 1; j++) {
ip[m + j] = ip[j] + l;
}
m <<= 1;
}
if ((m << 2) > l) {
for (k = 1; k <= m - 1; k++) {
for (j = 0; j <= k - 1; j++) {
j1 = (j << 1) + ip[k];
k1 = (k << 1) + ip[j];
xr = a[j1];
xi = a[j1 + 1];
a[j1] = a[k1];
a[j1 + 1] = a[k1 + 1];
a[k1] = xr;
a[k1 + 1] = xi;
}
}
} else {
m2 = m << 1;
for (k = 1; k <= m - 1; k++) {
for (j = 0; j <= k - 1; j++) {
j1 = (j << 1) + ip[k];
k1 = (k << 1) + ip[j];
xr = a[j1];
xi = a[j1 + 1];
a[j1] = a[k1];
a[j1 + 1] = a[k1 + 1];
a[k1] = xr;
a[k1 + 1] = xi;
j1 += m2;
k1 += m2;
xr = a[j1];
xi = a[j1 + 1];
a[j1] = a[k1];
a[j1 + 1] = a[k1 + 1];
a[k1] = xr;
a[k1 + 1] = xi;
}
}
}
}
static void bitrv2col(int n1, int n, int *ip, double **a)
{
int i, j, j1, k, k1, l, m, m2;
double xr, xi;
ip[0] = 0;
l = n;
m = 1;
while ((m << 2) < l) {
l >>= 1;
for (j = 0; j <= m - 1; j++) {
ip[m + j] = ip[j] + l;
}
m <<= 1;
}
if ((m << 2) > l) {
for (i = 0; i <= n1 - 1; i++) {
for (k = 1; k <= m - 1; k++) {
for (j = 0; j <= k - 1; j++) {
j1 = (j << 1) + ip[k];
k1 = (k << 1) + ip[j];
xr = a[i][j1];
xi = a[i][j1 + 1];
a[i][j1] = a[i][k1];
a[i][j1 + 1] = a[i][k1 + 1];
a[i][k1] = xr;
a[i][k1 + 1] = xi;
}
}
}
} else {
m2 = m << 1;
for (i = 0; i <= n1 - 1; i++) {
for (k = 1; k <= m - 1; k++) {
for (j = 0; j <= k - 1; j++) {
j1 = (j << 1) + ip[k];
k1 = (k << 1) + ip[j];
xr = a[i][j1];
xi = a[i][j1 + 1];
a[i][j1] = a[i][k1];
a[i][j1 + 1] = a[i][k1 + 1];
a[i][k1] = xr;
a[i][k1 + 1] = xi;
j1 += m2;
k1 += m2;
xr = a[i][j1];
xi = a[i][j1 + 1];
a[i][j1] = a[i][k1];
a[i][j1 + 1] = a[i][k1 + 1];
a[i][k1] = xr;
a[i][k1 + 1] = xi;
}
}
}
}
}
static void bitrv2row(int n, int n2, int *ip, double **a)
{
int i, j, j1, k, k1, l, m;
double xr, xi;
ip[0] = 0;
l = n;
m = 1;
while ((m << 1) < l) {
l >>= 1;
for (j = 0; j <= m - 1; j++) {
ip[m + j] = ip[j] + l;
}
m <<= 1;
}
if ((m << 1) > l) {
for (k = 1; k <= m - 1; k++) {
for (j = 0; j <= k - 1; j++) {
j1 = j + ip[k];
k1 = k + ip[j];
for (i = 0; i <= n2 - 2; i += 2) {
xr = a[j1][i];
xi = a[j1][i + 1];
a[j1][i] = a[k1][i];
a[j1][i + 1] = a[k1][i + 1];
a[k1][i] = xr;
a[k1][i + 1] = xi;
}
}
}
} else {
for (k = 1; k <= m - 1; k++) {
for (j = 0; j <= k - 1; j++) {
j1 = j + ip[k];
k1 = k + ip[j];
for (i = 0; i <= n2 - 2; i += 2) {
xr = a[j1][i];
xi = a[j1][i + 1];
a[j1][i] = a[k1][i];
a[j1][i + 1] = a[k1][i + 1];
a[k1][i] = xr;
a[k1][i + 1] = xi;
}
j1 += m;
k1 += m;
for (i = 0; i <= n2 - 2; i += 2) {
xr = a[j1][i];
xi = a[j1][i + 1];
a[j1][i] = a[k1][i];
a[j1][i + 1] = a[k1][i + 1];
a[k1][i] = xr;
a[k1][i + 1] = xi;
}
}
}
}
}
static void cftbcol(int n1, int n, double **a, double *w)
{
int i, j, j1, j2, j3, k, k1, ks, l, m;
double wk1r, wk1i, wk2r, wk2i, wk3r, wk3i;
double x0r, x0i, x1r, x1i, x2r, x2i, x3r, x3i;
for (i = 0; i <= n1 - 1; i++) {
l = 2;
while ((l << 1) < n) {
m = l << 2;
for (j = 0; j <= l - 2; j += 2) {
j1 = j + l;
j2 = j1 + l;
j3 = j2 + l;
x0r = a[i][j] + a[i][j1];
x0i = a[i][j + 1] + a[i][j1 + 1];
x1r = a[i][j] - a[i][j1];
x1i = a[i][j + 1] - a[i][j1 + 1];
x2r = a[i][j2] + a[i][j3];
x2i = a[i][j2 + 1] + a[i][j3 + 1];
x3r = a[i][j2] - a[i][j3];
x3i = a[i][j2 + 1] - a[i][j3 + 1];
a[i][j] = x0r + x2r;
a[i][j + 1] = x0i + x2i;
a[i][j2] = x0r - x2r;
a[i][j2 + 1] = x0i - x2i;
a[i][j1] = x1r - x3i;
a[i][j1 + 1] = x1i + x3r;
a[i][j3] = x1r + x3i;
a[i][j3 + 1] = x1i - x3r;
}
if (m < n) {
wk1r = w[2];
for (j = m; j <= l + m - 2; j += 2) {
j1 = j + l;
j2 = j1 + l;
j3 = j2 + l;
x0r = a[i][j] + a[i][j1];
x0i = a[i][j + 1] + a[i][j1 + 1];
x1r = a[i][j] - a[i][j1];
x1i = a[i][j + 1] - a[i][j1 + 1];
x2r = a[i][j2] + a[i][j3];
x2i = a[i][j2 + 1] + a[i][j3 + 1];
x3r = a[i][j2] - a[i][j3];
x3i = a[i][j2 + 1] - a[i][j3 + 1];
a[i][j] = x0r + x2r;
a[i][j + 1] = x0i + x2i;
a[i][j2] = x2i - x0i;
a[i][j2 + 1] = x0r - x2r;
x0r = x1r - x3i;
x0i = x1i + x3r;
a[i][j1] = wk1r * (x0r - x0i);
a[i][j1 + 1] = wk1r * (x0r + x0i);
x0r = x3i + x1r;
x0i = x3r - x1i;
a[i][j3] = wk1r * (x0i - x0r);
a[i][j3 + 1] = wk1r * (x0i + x0r);
}
k1 = 1;
ks = -1;
for (k = (m << 1); k <= n - m; k += m) {
k1++;
ks = -ks;
wk1r = w[k1 << 1];
wk1i = w[(k1 << 1) + 1];
wk2r = ks * w[k1];
wk2i = w[k1 + ks];
wk3r = wk1r - 2 * wk2i * wk1i;
wk3i = 2 * wk2i * wk1r - wk1i;
for (j = k; j <= l + k - 2; j += 2) {
j1 = j + l;
j2 = j1 + l;
j3 = j2 + l;
x0r = a[i][j] + a[i][j1];
x0i = a[i][j + 1] + a[i][j1 + 1];
x1r = a[i][j] - a[i][j1];
x1i = a[i][j + 1] - a[i][j1 + 1];
x2r = a[i][j2] + a[i][j3];
x2i = a[i][j2 + 1] + a[i][j3 + 1];
x3r = a[i][j2] - a[i][j3];
x3i = a[i][j2 + 1] - a[i][j3 + 1];
a[i][j] = x0r + x2r;
a[i][j + 1] = x0i + x2i;
x0r -= x2r;
x0i -= x2i;
a[i][j2] = wk2r * x0r - wk2i * x0i;
a[i][j2 + 1] = wk2r * x0i + wk2i * x0r;
x0r = x1r - x3i;
x0i = x1i + x3r;
a[i][j1] = wk1r * x0r - wk1i * x0i;
a[i][j1 + 1] = wk1r * x0i + wk1i * x0r;
x0r = x1r + x3i;
x0i = x1i - x3r;
a[i][j3] = wk3r * x0r - wk3i * x0i;
a[i][j3 + 1] = wk3r * x0i + wk3i * x0r;
}
}
}
l = m;
}
if (l < n) {
for (j = 0; j <= l - 2; j += 2) {
j1 = j + l;
x0r = a[i][j] - a[i][j1];
x0i = a[i][j + 1] - a[i][j1 + 1];
a[i][j] += a[i][j1];
a[i][j + 1] += a[i][j1 + 1];
a[i][j1] = x0r;
a[i][j1 + 1] = x0i;
}
}
}
}
static void cftbrow(int n, int n2, double **a, double *w)
{
int i, j, j1, j2, j3, k, k1, ks, l, m;
double wk1r, wk1i, wk2r, wk2i, wk3r, wk3i;
double x0r, x0i, x1r, x1i, x2r, x2i, x3r, x3i;
l = 1;
while ((l << 1) < n) {
m = l << 2;
for (j = 0; j <= l - 1; j++) {
j1 = j + l;
j2 = j1 + l;
j3 = j2 + l;
for (i = 0; i <= n2 - 2; i += 2) {
x0r = a[j][i] + a[j1][i];
x0i = a[j][i + 1] + a[j1][i + 1];
x1r = a[j][i] - a[j1][i];
x1i = a[j][i + 1] - a[j1][i + 1];
x2r = a[j2][i] + a[j3][i];
x2i = a[j2][i + 1] + a[j3][i + 1];
x3r = a[j2][i] - a[j3][i];
x3i = a[j2][i + 1] - a[j3][i + 1];
a[j][i] = x0r + x2r;
a[j][i + 1] = x0i + x2i;
a[j2][i] = x0r - x2r;
a[j2][i + 1] = x0i - x2i;
a[j1][i] = x1r - x3i;
a[j1][i + 1] = x1i + x3r;
a[j3][i] = x1r + x3i;
a[j3][i + 1] = x1i - x3r;
}
}
if (m < n) {
wk1r = w[2];
for (j = m; j <= l + m - 1; j++) {
j1 = j + l;
j2 = j1 + l;
j3 = j2 + l;
for (i = 0; i <= n2 - 2; i += 2) {
x0r = a[j][i] + a[j1][i];
x0i = a[j][i + 1] + a[j1][i + 1];
x1r = a[j][i] - a[j1][i];
x1i = a[j][i + 1] - a[j1][i + 1];
x2r = a[j2][i] + a[j3][i];
x2i = a[j2][i + 1] + a[j3][i + 1];
x3r = a[j2][i] - a[j3][i];
x3i = a[j2][i + 1] - a[j3][i + 1];
a[j][i] = x0r + x2r;
a[j][i + 1] = x0i + x2i;
a[j2][i] = x2i - x0i;
a[j2][i + 1] = x0r - x2r;
x0r = x1r - x3i;
x0i = x1i + x3r;
a[j1][i] = wk1r * (x0r - x0i);
a[j1][i + 1] = wk1r * (x0r + x0i);
x0r = x3i + x1r;
x0i = x3r - x1i;
a[j3][i] = wk1r * (x0i - x0r);
a[j3][i + 1] = wk1r * (x0i + x0r);
}
}
k1 = 1;
ks = -1;
for (k = (m << 1); k <= n - m; k += m) {
k1++;
ks = -ks;
wk1r = w[k1 << 1];
wk1i = w[(k1 << 1) + 1];
wk2r = ks * w[k1];
wk2i = w[k1 + ks];
wk3r = wk1r - 2 * wk2i * wk1i;
wk3i = 2 * wk2i * wk1r - wk1i;
for (j = k; j <= l + k - 1; j++) {
j1 = j + l;
j2 = j1 + l;
j3 = j2 + l;
for (i = 0; i <= n2 - 2; i += 2) {
x0r = a[j][i] + a[j1][i];
x0i = a[j][i + 1] + a[j1][i + 1];
x1r = a[j][i] - a[j1][i];
x1i = a[j][i + 1] - a[j1][i + 1];
x2r = a[j2][i] + a[j3][i];
x2i = a[j2][i + 1] + a[j3][i + 1];
x3r = a[j2][i] - a[j3][i];
x3i = a[j2][i + 1] - a[j3][i + 1];
a[j][i] = x0r + x2r;
a[j][i + 1] = x0i + x2i;
x0r -= x2r;
x0i -= x2i;
a[j2][i] = wk2r * x0r - wk2i * x0i;
a[j2][i + 1] = wk2r * x0i + wk2i * x0r;
x0r = x1r - x3i;
x0i = x1i + x3r;
a[j1][i] = wk1r * x0r - wk1i * x0i;
a[j1][i + 1] = wk1r * x0i + wk1i * x0r;
x0r = x1r + x3i;
x0i = x1i - x3r;
a[j3][i] = wk3r * x0r - wk3i * x0i;
a[j3][i + 1] = wk3r * x0i + wk3i * x0r;
}
}
}
}
l = m;
}
if (l < n) {
for (j = 0; j <= l - 1; j++) {
j1 = j + l;
for (i = 0; i <= n2 - 2; i += 2) {
x0r = a[j][i] - a[j1][i];
x0i = a[j][i + 1] - a[j1][i + 1];
a[j][i] += a[j1][i];
a[j][i + 1] += a[j1][i + 1];
a[j1][i] = x0r;
a[j1][i + 1] = x0i;
}
}
}
}
static void cftfcol(int n1, int n, double **a, double *w)
{
int i, j, j1, j2, j3, k, k1, ks, l, m;
double wk1r, wk1i, wk2r, wk2i, wk3r, wk3i;
double x0r, x0i, x1r, x1i, x2r, x2i, x3r, x3i;
for (i = 0; i <= n1 - 1; i++) {
l = 2;
while ((l << 1) < n) {
m = l << 2;
for (j = 0; j <= l - 2; j += 2) {
j1 = j + l;
j2 = j1 + l;
j3 = j2 + l;
x0r = a[i][j] + a[i][j1];
x0i = a[i][j + 1] + a[i][j1 + 1];
x1r = a[i][j] - a[i][j1];
x1i = a[i][j + 1] - a[i][j1 + 1];
x2r = a[i][j2] + a[i][j3];
x2i = a[i][j2 + 1] + a[i][j3 + 1];
x3r = a[i][j2] - a[i][j3];
x3i = a[i][j2 + 1] - a[i][j3 + 1];
a[i][j] = x0r + x2r;
a[i][j + 1] = x0i + x2i;
a[i][j2] = x0r - x2r;
a[i][j2 + 1] = x0i - x2i;
a[i][j1] = x1r + x3i;
a[i][j1 + 1] = x1i - x3r;
a[i][j3] = x1r - x3i;
a[i][j3 + 1] = x1i + x3r;
}
if (m < n) {
wk1r = w[2];
for (j = m; j <= l + m - 2; j += 2) {
j1 = j + l;
j2 = j1 + l;
j3 = j2 + l;
x0r = a[i][j] + a[i][j1];
x0i = a[i][j + 1] + a[i][j1 + 1];
x1r = a[i][j] - a[i][j1];
x1i = a[i][j + 1] - a[i][j1 + 1];
x2r = a[i][j2] + a[i][j3];
x2i = a[i][j2 + 1] + a[i][j3 + 1];
x3r = a[i][j2] - a[i][j3];
x3i = a[i][j2 + 1] - a[i][j3 + 1];
a[i][j] = x0r + x2r;
a[i][j + 1] = x0i + x2i;
a[i][j2] = x0i - x2i;
a[i][j2 + 1] = x2r - x0r;
x0r = x1r + x3i;
x0i = x1i - x3r;
a[i][j1] = wk1r * (x0i + x0r);
a[i][j1 + 1] = wk1r * (x0i - x0r);
x0r = x3i - x1r;
x0i = x3r + x1i;
a[i][j3] = wk1r * (x0r + x0i);
a[i][j3 + 1] = wk1r * (x0r - x0i);
}
k1 = 1;
ks = -1;
for (k = (m << 1); k <= n - m; k += m) {
k1++;
ks = -ks;
wk1r = w[k1 << 1];
wk1i = w[(k1 << 1) + 1];
wk2r = ks * w[k1];
wk2i = w[k1 + ks];
wk3r = wk1r - 2 * wk2i * wk1i;
wk3i = 2 * wk2i * wk1r - wk1i;
for (j = k; j <= l + k - 2; j += 2) {
j1 = j + l;
j2 = j1 + l;
j3 = j2 + l;
x0r = a[i][j] + a[i][j1];
x0i = a[i][j + 1] + a[i][j1 + 1];
x1r = a[i][j] - a[i][j1];
x1i = a[i][j + 1] - a[i][j1 + 1];
x2r = a[i][j2] + a[i][j3];
x2i = a[i][j2 + 1] + a[i][j3 + 1];
x3r = a[i][j2] - a[i][j3];
x3i = a[i][j2 + 1] - a[i][j3 + 1];
a[i][j] = x0r + x2r;
a[i][j + 1] = x0i + x2i;
x0r -= x2r;
x0i -= x2i;
a[i][j2] = wk2r * x0r + wk2i * x0i;
a[i][j2 + 1] = wk2r * x0i - wk2i * x0r;
x0r = x1r + x3i;
x0i = x1i - x3r;
a[i][j1] = wk1r * x0r + wk1i * x0i;
a[i][j1 + 1] = wk1r * x0i - wk1i * x0r;
x0r = x1r - x3i;
x0i = x1i + x3r;
a[i][j3] = wk3r * x0r + wk3i * x0i;
a[i][j3 + 1] = wk3r * x0i - wk3i * x0r;
}
}
}
l = m;
}
if (l < n) {
for (j = 0; j <= l - 2; j += 2) {
j1 = j + l;
x0r = a[i][j] - a[i][j1];
x0i = a[i][j + 1] - a[i][j1 + 1];
a[i][j] += a[i][j1];
a[i][j + 1] += a[i][j1 + 1];
a[i][j1] = x0r;
a[i][j1 + 1] = x0i;
}
}
}
}
static void cftfrow(int n, int n2, double **a, double *w)
{
int i, j, j1, j2, j3, k, k1, ks, l, m;
double wk1r, wk1i, wk2r, wk2i, wk3r, wk3i;
double x0r, x0i, x1r, x1i, x2r, x2i, x3r, x3i;
l = 1;
while ((l << 1) < n) {
m = l << 2;
for (j = 0; j <= l - 1; j++) {
j1 = j + l;
j2 = j1 + l;
j3 = j2 + l;
for (i = 0; i <= n2 - 2; i += 2) {
x0r = a[j][i] + a[j1][i];
x0i = a[j][i + 1] + a[j1][i + 1];
x1r = a[j][i] - a[j1][i];
x1i = a[j][i + 1] - a[j1][i + 1];
x2r = a[j2][i] + a[j3][i];
x2i = a[j2][i + 1] + a[j3][i + 1];
x3r = a[j2][i] - a[j3][i];
x3i = a[j2][i + 1] - a[j3][i + 1];
a[j][i] = x0r + x2r;
a[j][i + 1] = x0i + x2i;
a[j2][i] = x0r - x2r;
a[j2][i + 1] = x0i - x2i;
a[j1][i] = x1r + x3i;
a[j1][i + 1] = x1i - x3r;
a[j3][i] = x1r - x3i;
a[j3][i + 1] = x1i + x3r;
}
}
if (m < n) {
wk1r = w[2];
for (j = m; j <= l + m - 1; j++) {
j1 = j + l;
j2 = j1 + l;
j3 = j2 + l;
for (i = 0; i <= n2 - 2; i += 2) {
x0r = a[j][i] + a[j1][i];
x0i = a[j][i + 1] + a[j1][i + 1];
x1r = a[j][i] - a[j1][i];
x1i = a[j][i + 1] - a[j1][i + 1];
x2r = a[j2][i] + a[j3][i];
x2i = a[j2][i + 1] + a[j3][i + 1];
x3r = a[j2][i] - a[j3][i];
x3i = a[j2][i + 1] - a[j3][i + 1];
a[j][i] = x0r + x2r;
a[j][i + 1] = x0i + x2i;
a[j2][i] = x0i - x2i;
a[j2][i + 1] = x2r - x0r;
x0r = x1r + x3i;
x0i = x1i - x3r;
a[j1][i] = wk1r * (x0i + x0r);
a[j1][i + 1] = wk1r * (x0i - x0r);
x0r = x3i - x1r;
x0i = x3r + x1i;
a[j3][i] = wk1r * (x0r + x0i);
a[j3][i + 1] = wk1r * (x0r - x0i);
}
}
k1 = 1;
ks = -1;
for (k = (m << 1); k <= n - m; k += m) {
k1++;
ks = -ks;
wk1r = w[k1 << 1];
wk1i = w[(k1 << 1) + 1];
wk2r = ks * w[k1];
wk2i = w[k1 + ks];
wk3r = wk1r - 2 * wk2i * wk1i;
wk3i = 2 * wk2i * wk1r - wk1i;
for (j = k; j <= l + k - 1; j++) {
j1 = j + l;
j2 = j1 + l;
j3 = j2 + l;
for (i = 0; i <= n2 - 2; i += 2) {
x0r = a[j][i] + a[j1][i];
x0i = a[j][i + 1] + a[j1][i + 1];
x1r = a[j][i] - a[j1][i];
x1i = a[j][i + 1] - a[j1][i + 1];
x2r = a[j2][i] + a[j3][i];
x2i = a[j2][i + 1] + a[j3][i + 1];
x3r = a[j2][i] - a[j3][i];
x3i = a[j2][i + 1] - a[j3][i + 1];
a[j][i] = x0r + x2r;
a[j][i + 1] = x0i + x2i;
x0r -= x2r;
x0i -= x2i;
a[j2][i] = wk2r * x0r + wk2i * x0i;
a[j2][i + 1] = wk2r * x0i - wk2i * x0r;
x0r = x1r + x3i;
x0i = x1i - x3r;
a[j1][i] = wk1r * x0r + wk1i * x0i;
a[j1][i + 1] = wk1r * x0i - wk1i * x0r;
x0r = x1r - x3i;
x0i = x1i + x3r;
a[j3][i] = wk3r * x0r + wk3i * x0i;
a[j3][i + 1] = wk3r * x0i - wk3i * x0r;
}
}
}
}
l = m;
}
if (l < n) {
for (j = 0; j <= l - 1; j++) {
j1 = j + l;
for (i = 0; i <= n2 - 2; i += 2) {
x0r = a[j][i] - a[j1][i];
x0i = a[j][i + 1] - a[j1][i + 1];
a[j][i] += a[j1][i];
a[j][i + 1] += a[j1][i + 1];
a[j1][i] = x0r;
a[j1][i + 1] = x0i;
}
}
}
}
static int *alloc_1d_int(int n1)
{
int *i;
i = (int *) malloc(sizeof(int) * n1);
return i;
}
static void free_1d_int(int *i) { free(i); }
static double *alloc_1d_double(int n1)
{
double *d;
d = (double *) malloc(sizeof(double) * n1);
return d;
}
static void free_1d_double(double *d) { free(d); }
static double **alloc_2d_double(int n1, int n2)
{
double **dd, *d;
int j;
dd = (double **) malloc(sizeof(double *) * n1);
d = (double *) malloc(sizeof(double) * n1 * n2);
dd[0] = d;
for (j = 1; j < n1; j++) {
dd[j] = dd[j - 1] + n2;
}
return dd;
}
static void free_2d_double(double **dd) { free(dd[0]); free(dd); }
static void cdft2d(int n1, int n2, int isgn, double **a, int *ip, double *w)
{
int n;
n = n1 << 1;
if (n < n2) {
n = n2;
}
if (n > (ip[0] << 2)) {
makewt(n >> 2, ip, w);
}
if (n2 > 4) {
bitrv2col(n1, n2, ip + 2, a);
}
if (n1 > 2) {
bitrv2row(n1, n2, ip + 2, a);
}
if (isgn < 0) {
cftfcol(n1, n2, a, w);
cftfrow(n1, n2, a, w);
} else {
cftbcol(n1, n2, a, w);
cftbrow(n1, n2, a, w);
}
}
};

View File

@ -42,6 +42,7 @@
#include "graphwidget.h"
#include "edge.h"
#include "node.h"
#include "fft.h"
#include <iostream>
#include <QDebug>
@ -50,86 +51,9 @@
#include <math.h>
#define SWAP(a,b) tempr=(a);(a)=(b);(b)=tempr
void fourn(double data[],unsigned long nn[],unsigned long ndim,int isign)
{
int i1,i2,i3,i2rev,i3rev,ip1,ip2,ip3,ifp1,ifp2;
int ibit,idim,k1,k2,n,nprev,nrem,ntot;
double tempi,tempr;
double theta,wi,wpi,wpr,wr,wtemp;
ntot=1;
for (idim=1;idim<=(long)ndim;++idim)
ntot *= nn[idim];
nprev=1;
for (idim=ndim;idim>=1;idim--) {
n=nn[idim];
nrem=ntot/(n*nprev);
ip1=nprev << 1;
ip2=ip1*n;
ip3=ip2*nrem;
i2rev=1;
for (i2=1;i2<=ip2;i2+=ip1) {
if (i2 < i2rev) {
for (i1=i2;i1<=i2+ip1-2;i1+=2) {
for (i3=i1;i3<=ip3;i3+=ip2) {
i3rev=i2rev+i3-i2;
SWAP(data[i3],data[i3rev]);
SWAP(data[i3+1],data[i3rev+1]);
}
}
}
ibit=ip2 >> 1;
while (ibit >= ip1 && i2rev > ibit) {
i2rev -= ibit;
ibit >>= 1;
}
i2rev += ibit;
}
ifp1=ip1;
while (ifp1 < ip2) {
ifp2=ifp1 << 1;
theta=isign*6.28318530717959/(ifp2/ip1);
wtemp=sin(0.5*theta);
wpr = -2.0*wtemp*wtemp;
wpi=sin(theta);
wr=1.0;
wi=0.0;
for (i3=1;i3<=ifp1;i3+=ip1) {
for (i1=i3;i1<=i3+ip1-2;i1+=2) {
for (i2=i1;i2<=ip3;i2+=ifp2) {
k1=i2;
k2=k1+ifp1;
tempr=wr*data[k2]-wi*data[k2+1];
tempi=wr*data[k2+1]+wi*data[k2];
data[k2]=data[k1]-tempr;
data[k2+1]=data[k1+1]-tempi;
data[k1] += tempr;
data[k1+1] += tempi;
}
}
wr=(wtemp=wr)*wpr-wi*wpi+wr;
wi=wi*wpr+wtemp*wpi+wi;
}
ifp1=ifp2;
}
nprev *= n;
}
}
#undef SWAP
GraphWidget::GraphWidget(QWidget *)
: timerId(0), mIsFrozen(false)
{
// QGraphicsScene *scene = new QGraphicsScene(QRectF(0,0,500,500),this);
// scene->setItemIndexMethod(QGraphicsScene::NoIndex);
// scene->clear() ;
// setScene(scene);
// scene()->setSceneRect(0, 0, width(), height());
setCacheMode(CacheBackground);
setViewportUpdateMode(BoundingRectViewportUpdate);
setRenderHint(QPainter::Antialiasing);
@ -142,18 +66,6 @@ GraphWidget::GraphWidget(QWidget *)
void GraphWidget::clearGraph()
{
// QGraphicsScene *scene = new QGraphicsScene(this);
// scene->setItemIndexMethod(QGraphicsScene::NoIndex);
// setScene(scene);
// scene->addItem(centerNode);
// centerNode->setPos(0, 0);
// if (oldscene != NULL)
// {
// delete oldscene;
// }
scene()->clear();
scene()->setSceneRect(0, 0, width(), height());
@ -229,18 +141,6 @@ void GraphWidget::itemMoved()
void GraphWidget::keyPressEvent(QKeyEvent *event)
{
switch (event->key()) {
// case Qt::Key_Up:
// centerNode->moveBy(0, -20);
// break;
// case Qt::Key_Down:
// centerNode->moveBy(0, 20);
// break;
// case Qt::Key_Left:
// centerNode->moveBy(-20, 0);
// break;
// case Qt::Key_Right:
// centerNode->moveBy(20, 0);
// break;
case Qt::Key_Plus:
scaleView(qreal(1.2));
break;
@ -259,45 +159,63 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
}
}
static void convolveWithGaussian(double *forceMap,unsigned int S,int /*s*/)
static void convolveWithForce(double *forceMap,unsigned int S,int /*s*/)
{
static double *bf = NULL ;
static double **bf = NULL ;
static double **tmp = NULL ;
static int *ip = NULL ;
static double *w = NULL ;
static uint32_t last_S = 0 ;
if(bf == NULL)
{
bf = new double[S*S*2] ;
bf = fft::alloc_2d_double(S, 2*S);
for(unsigned int i=0;i<S;++i)
for(unsigned int j=0;j<S;++j)
{
int x = (i<S/2)?i:(S-i) ;
int y = (j<S/2)?j:(S-j) ;
// int l=2*(x*x+y*y);
bf[2*(i+S*j)] = log(sqrtf(0.1 + x*x+y*y)); // linear -> derivative is constant
bf[2*(i+S*j)+1] = 0 ;
bf[i][j*2+0] = log(sqrtf(0.1 + x*x+y*y)); // linear -> derivative is constant
bf[i][j*2+1] = 0 ;
}
unsigned long nn[2] = {S,S};
fourn(&bf[-1],&nn[-1],2,1) ;
ip = fft::alloc_1d_int(2 + (int) sqrt(S + 0.5));
w = fft::alloc_1d_double(S/2+S);
ip[0] = 0;
fft::cdft2d(S, 2*S, 1, bf, ip, w);
}
unsigned long nn[2] = {S,S};
fourn(&forceMap[-1],&nn[-1],2,1) ;
if(last_S != S)
{
if(tmp)
fft::free_2d_double(tmp) ;
tmp = fft::alloc_2d_double(S, 2*S);
last_S = S ;
}
memcpy(tmp[0],forceMap,S*S*2*sizeof(double)) ;
fft::cdft2d(S, 2*S, 1, tmp, ip, w);
for (unsigned int i=0;i<S;++i)
for (unsigned int j=0;j<S;++j)
{
float a = forceMap[2*(i+S*j)]*bf[2*(i+S*j)] - forceMap[2*(i+S*j)+1]*bf[2*(i+S*j)+1] ;
float b = forceMap[2*(i+S*j)]*bf[2*(i+S*j)+1] + forceMap[2*(i+S*j)+1]*bf[2*(i+S*j)] ;
float a = tmp[i][2*j+0]*bf[i][2*j+0] - tmp[i][2*j+1]*bf[i][2*j+1] ;
float b = tmp[i][2*j+0]*bf[i][2*j+1] + tmp[i][2*j+1]*bf[i][2*j+0] ;
forceMap[2*(i+S*j)] = a ;
forceMap[2*(i+S*j)+1] = b ;
tmp[i][2*j+0] = a ;
tmp[i][2*j+1] = b ;
}
fourn(&forceMap[-1],&nn[-1],2,-1) ;
fft::cdft2d(S, 2*S,-1, tmp, ip, w);
for(unsigned int i=0;i<S*S*2;++i)
forceMap[i] /= S*S;
memcpy(forceMap,tmp[0],S*S*2*sizeof(double)) ;
for(uint32_t i=0;i<2*S*S;++i)
forceMap[i] /= S*S;
}
void GraphWidget::timerEvent(QTimerEvent *event)
@ -322,7 +240,7 @@ void GraphWidget::timerEvent(QTimerEvent *event)
QRectF R(scene()->sceneRect()) ;
if( (hit++ & 7) == 0)
if( (hit++ & 3) == 0)
{
memset(forceMap,0,2*S*S*sizeof(double)) ;
@ -348,7 +266,7 @@ void GraphWidget::timerEvent(QTimerEvent *event)
}
// compute convolution with 1/omega kernel.
convolveWithGaussian(forceMap,S,20) ;
convolveWithForce(forceMap,S,20) ;
}
foreach (Node *node, _nodes)
@ -435,43 +353,6 @@ void GraphWidget::wheelEvent(QWheelEvent *event)
scaleView(pow((double)2, -event->delta() / 240.0));
}
//void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
//{
// Q_UNUSED(rect);
//
// // Shadow
// QRectF sceneRect = this->sceneRect();
// QRectF rightShadow(sceneRect.right(), sceneRect.top() + 5, 5, sceneRect.height());
// QRectF bottomShadow(sceneRect.left() + 5, sceneRect.bottom(), sceneRect.width(), 5);
// if (rightShadow.intersects(rect) || rightShadow.contains(rect))
// painter->fillRect(rightShadow, Qt::darkGray);
// if (bottomShadow.intersects(rect) || bottomShadow.contains(rect))
// painter->fillRect(bottomShadow, Qt::darkGray);
//
// // Fill
// QLinearGradient gradient(sceneRect.topLeft(), sceneRect.bottomRight());
// gradient.setColorAt(0, Qt::white);
// gradient.setColorAt(1, Qt::lightGray);
// painter->fillRect(rect.intersected(sceneRect), gradient);
// painter->setBrush(Qt::NoBrush);
// painter->drawRect(sceneRect);
//
// // Text
// QRectF textRect(sceneRect.left() + 4, sceneRect.top() + 4,
// sceneRect.width() - 4, sceneRect.height() - 4);
// QString message(tr("Click and drag the nodes around, and zoom with the mouse "
// "wheel or the '+' and '-' keys"));
//
// QFont font = painter->font();
// font.setBold(true);
// font.setPointSize(14);
// painter->setFont(font);
// painter->setPen(Qt::lightGray);
// painter->drawText(textRect.translated(2, 2), message);
// painter->setPen(Qt::black);
// painter->drawText(textRect, message);
//}
void GraphWidget::scaleView(qreal scaleFactor)
{
qreal factor = matrix().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();