forked from Kitware/VTK
/
vtkImageFourierFilter.cxx
231 lines (208 loc) · 6.34 KB
/
vtkImageFourierFilter.cxx
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*=========================================================================
Program: Visualization Toolkit
Module: vtkImageFourierFilter.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkImageFourierFilter.h"
#include "vtkMath.h"
#include <math.h>
/*=========================================================================
Vectors of complex numbers.
=========================================================================*/
//----------------------------------------------------------------------------
// This function calculates one step of a FFT.
// It is specialized for a factor of 2.
// It is engineered for no decimation.
// (forward: fb = 1, backward: fb = -1)
void vtkImageFourierFilter::ExecuteFftStep2(vtkImageComplex *p_in,
vtkImageComplex *p_out,
int N, int bsize, int fb)
{
int i1, i2;
vtkImageComplex *p1, *p2, *p3;
vtkImageComplex q, fact1, fact, temp;
/* Copy the links with no factors. */
p1 = p_in;
p3 = p_out;
for(i1 = 0; i1 < N / (bsize * 2); ++i1) // loop 0->1
{
p2 = p1;
for(i2 = 0; i2 < bsize; ++i2) // loop 0->2
{
*p3 = *p2; // out[0] = in[0]; out[1] = in[1];
++p2;
++p3;
}
p2 = p1;
for(i2 = 0; i2 < bsize; ++i2)
{
*p3 = *p2; // out[2] = in[0]; out[3] = in[1];
++p2;
++p3;
}
p1 = p1 + bsize;
}
/* Add the links with factors. */
fact1.Real = 1.0;
fact1.Imag = 0.0;
q.Real = 0.0;
q.Imag = -(2.0 * vtkMath::Pi()) * fb / (bsize * 2.0);
vtkImageComplexExponential(q, q);
p3 = p_out;
for(i1 = 0; i1 < N / (bsize * 2); ++i1)
{
fact = fact1;
p2 = p1;
for(i2 = 0; i2 < bsize; ++i2)
{
vtkImageComplexMultiply(fact, *p2, temp);
vtkImageComplexAdd(temp, *p3, *p3);
vtkImageComplexMultiply(q, fact, fact);
++p2; // out[0] += in[2]; out[1] += -i*in[3];
++p3;
}
p2 = p1;
for(i2 = 0; i2 < bsize; ++i2)
{
vtkImageComplexMultiply(fact, *p2, temp);
vtkImageComplexAdd(temp, *p3, *p3);
vtkImageComplexMultiply(q, fact, fact);
++p2;
++p3;
}
p1 = p1 + bsize;
}
}
//----------------------------------------------------------------------------
// This function calculates one step of a FFT (using any factor).
// It is engineered for no decimation.
// N: length of arrays
// bsize: Size of FFT so far (should be scaled by n after this step)
// n: size of this steps butterfly.
// fb: forward: fb = 1, backward: fb = -1
void vtkImageFourierFilter::ExecuteFftStepN(vtkImageComplex *p_in,
vtkImageComplex *p_out,
int N, int bsize, int n, int fb)
{
int i0, i1, i2, i3;
vtkImageComplex *p1, *p2, *p3;
vtkImageComplex q, fact, temp;
p3 = p_out;
for(i0 = 0; i0 < N; ++i0)
{
p3->Real = 0.0;
p3->Imag = 0.0;
++p3;
}
p1 = p_in;
for(i0 = 0; i0 < n; ++i0)
{
q.Real = 0.0;
q.Imag = -(2.0 * vtkMath::Pi()) * i0 * fb / (bsize*1.0*n);
vtkImageComplexExponential(q, q);
p3 = p_out;
for(i1 = 0; i1 < N / (bsize * n); ++i1)
{
fact.Real = 1.0;
fact.Imag = 0.0;
for(i3 = 0; i3 < n; ++i3)
{
p2 = p1;
for(i2 = 0; i2 < bsize; ++i2)
{
vtkImageComplexMultiply(fact, *p2, temp);
vtkImageComplexAdd(temp, *p3, *p3);
vtkImageComplexMultiply(q, fact, fact);
++p2;
++p3;
}
}
p1 = p1 + bsize;
}
}
}
//----------------------------------------------------------------------------
// This function calculates the whole fft (or rfft) of an array.
// The contents of the input array are changed.
// It is engineered for no decimation so input and output cannot be equal.
// (fb = 1) => fft, (fb = -1) => rfft;
void vtkImageFourierFilter::ExecuteFftForwardBackward(vtkImageComplex *in,
vtkImageComplex *out,
int N, int fb)
{
vtkImageComplex *p1, *p2, *p3;
int block_size = 1;
int rest_size = N;
int n = 2;
int idx;
// If this is a reverse transform (scale accordingly).
if(fb == -1)
{
p1 = in;
for(idx = 0; idx < N; ++idx)
{
p1->Real = p1->Real / N;
p1->Imag = p1->Imag / N;
++p1;
}
}
p1 = in;
p2 = out;
while(block_size < N && n <= N)
{
if((rest_size % n) == 0)
{
// n is a prime factor, perform one "butterfly" stage of the fft.
if(n == 2)
{
this->ExecuteFftStep2(p1, p2, N, block_size, fb);
}
else
{
this->ExecuteFftStepN(p1, p2, N, block_size, n, fb);
}
block_size = block_size * n;
rest_size = rest_size / n;
// switch input and output.
p3 = p1;
p1 = p2;
p2 = p3;
}
else
{
// n is not a prime factor. increment n to see if n+1 is.
++n;
}
}
// If the results ended up in the input, copy to output.
if(p1 != out)
{
for(n = 0; n < N; ++n)
{
*out++ = *p1++;
}
}
}
//----------------------------------------------------------------------------
// This function calculates the whole fft of an array.
// The contents of the input array are changed.
// (It is engineered for no decimation)
void vtkImageFourierFilter::ExecuteFft(vtkImageComplex *in,
vtkImageComplex *out, int N)
{
this->ExecuteFftForwardBackward(in, out, N, 1);
}
//----------------------------------------------------------------------------
// This function calculates the whole fft of an array.
// The contents of the input array are changed.
// (It is engineered for no decimation)
void vtkImageFourierFilter::ExecuteRfft(vtkImageComplex *in,
vtkImageComplex *out, int N)
{
this->ExecuteFftForwardBackward(in, out, N, -1);
}