/
ff_fft.c
206 lines (169 loc) · 4.73 KB
/
ff_fft.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "parser.h"
#include "fft.h"
#include <math.h>
/**
**/
void cdft(int n, double wr, double wi, double* a);
void rdft(int n, double wr, double wi, double* a);
int mayer_fht(double* fz, int n);
int mayer_ifft(int n, double* real, double* imag);
int mayer_realfft(int n, double* real);
int mayer_fft(int n, double* real, double* imag);
int mayer_realifft(int n, double* real);
Var* ff_fft(vfuncptr func, Var* arg)
{
Var *real = NULL, *img = NULL;
double* data;
int i, j, n, x, y, z;
COMPLEX *in, *out;
Alist alist[4];
alist[0] = make_alist("real", ID_VAL, NULL, &real);
alist[1] = make_alist("img", ID_VAL, NULL, &img);
alist[2].name = NULL;
if (parse_args(func, arg, alist) == 0) return (NULL);
if (real == NULL && img == NULL) {
parse_error("%s: No real or imaginary objects specified\n", func->name);
return (NULL);
}
x = GetSamples(V_SIZE(real), V_ORG(real));
y = GetLines(V_SIZE(real), V_ORG(real));
z = GetBands(V_SIZE(real), V_ORG(real));
if (img == NULL && x == 2) {
n = y * z;
in = (COMPLEX*)calloc(n, sizeof(COMPLEX));
out = (COMPLEX*)calloc(n, sizeof(COMPLEX));
for (i = 0; i < y; i++) {
for (j = 0; j < z; j++) {
in[i].re = extract_double(real, cpos(0, i, j, real));
in[i].im = extract_double(real, cpos(1, i, j, real));
}
}
} else {
n = V_DSIZE(real);
in = (COMPLEX*)calloc(n, sizeof(COMPLEX));
out = (COMPLEX*)calloc(n, sizeof(COMPLEX));
for (i = 0; i < n; i++) {
in[i].re = extract_double(real, i);
in[i].im = (img == NULL ? 0.0 : extract_double(img, i));
}
}
if (func->fdata == (void*)1) {
fft(in, n, out);
} else {
rft(in, n, out);
}
data = (double*)calloc(n * 2, sizeof(double));
for (i = 0; i < n; i++) {
data[i * 2] = out[i].re;
data[i * 2 + 1] = out[i].im;
}
return (newVal(BSQ, 2, n, 1, DV_DOUBLE, data));
}
Var* ff_realfft(vfuncptr func, Var* arg)
{
Var* obj = NULL;
int i, n;
double *in, *out;
Alist alist[3];
alist[0] = make_alist("obj", ID_VAL, NULL, &obj);
alist[1].name = NULL;
if (parse_args(func, arg, alist) == 0) return (NULL);
if (obj == NULL) {
parse_error("%s: No object specified\n", func->name);
return (NULL);
}
n = V_DSIZE(obj);
in = (double*)calloc(n, sizeof(double));
out = (double*)calloc(n, sizeof(double));
for (i = 0; i < n; i++) {
in[i] = extract_double(obj, i);
}
if (func->fdata == (void*)1) {
realfft(in, n, out);
} else {
realrft(in, n, out);
}
return (newVal(BSQ, 1, n, 1, DV_DOUBLE, out));
}
Var* ff_realfft2(vfuncptr func, Var* arg)
{
Var* obj = NULL;
int i, j, n, x;
double* in;
Alist alist[3];
alist[0] = make_alist("obj", ID_VAL, NULL, &obj);
alist[1].name = NULL;
if (parse_args(func, arg, alist) == 0) return (NULL);
if (obj == NULL) {
parse_error("%s: No object specified\n", func->name);
return (NULL);
}
n = V_DSIZE(obj);
for (x = n; (x & 1) == 0; x >>= 1)
;
if (x != 1) {
parse_error("dimension not a power of 2. Use version 0.\n");
return (NULL);
}
in = (double*)calloc(n, sizeof(double));
for (i = 0; i < n; i++) {
in[i] = extract_double(obj, i);
}
if (func->fdata == (void*)1) {
rdft(n, cos(M_PI / n), sin(M_PI / n), in);
} else {
rdft(n, cos(M_PI / n), -sin(M_PI / n), in);
for (j = 0; j <= n - 1; j++) {
in[j] *= 2.0 / n;
}
}
return (newVal(BSQ, 1, n, 1, DV_DOUBLE, in));
}
/** fft(n,real,imag)
** Does a fourier transform of "n" points of the "real" and
** "imag" arrays.
** ifft(n,real,imag)
** Does an inverse fourier transform of "n" points of the "real"
** and "imag" arrays.
** realfft(n,real)
** Does a real-valued fourier transform of "n" points of the
** "real" and "imag" arrays. The real part of the transform ends
** up in the first half of the array and the imaginary part of the
** transform ends up in the second half of the array.
** realifft(n,real)
** The inverse of the realfft() routine above.
**/
Var* ff_realfft3(vfuncptr func, Var* arg)
{
Var* obj = NULL;
size_t i, n;
double* in;
Alist alist[3];
alist[0] = make_alist("obj", ID_VAL, NULL, &obj);
alist[1].name = NULL;
if (parse_args(func, arg, alist) == 0) return (NULL);
if (obj == NULL) {
parse_error("%s: No object specified\n", func->name);
return (NULL);
}
n = V_DSIZE(obj);
if (n > INT_MAX) {
parse_error("%s: fft function does not handle objects greater than %ld bytes.\n",
func->name, INT_MAX);
return NULL;
}
in = (double*)calloc(n, sizeof(double));
if (in == NULL) {
parse_error("%s: Unable to alloc %ld bytes.\n", func->name, n * sizeof(double));
return NULL;
}
for (i = 0; i < n; i++) {
in[i] = extract_double(obj, i);
}
if (func->fdata == (void*)1) {
mayer_realfft(n, in);
} else {
mayer_realifft(n, in);
}
return (newVal(BSQ, 1, n, 1, DV_DOUBLE, in));
}