summaryrefslogtreecommitdiffstats
path: root/src/Fourier.cpp
blob: 7d7b2f1c44d9f4030b6605661344835f9e5d2a30 (plain)
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
230
231
232
233
/*
-----------------------------------------------------------------------
Copyright: 2010-2014, iMinds-Vision Lab, University of Antwerp
                2014, CWI, Amsterdam

Contact: astra@uantwerpen.be
Website: http://sf.net/projects/astra-toolbox

This file is part of the ASTRA Toolbox.


The ASTRA Toolbox is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

The ASTRA Toolbox is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>.

-----------------------------------------------------------------------
$Id$
*/

#include "astra/Fourier.h"

namespace astra {


void discreteFourierTransform1D(unsigned int iLength,
                                const float32* pfRealIn,
                                const float32* pfImaginaryIn,
                                float32* pfRealOut,
                                float32* pfImaginaryOut,
                                unsigned int iStrideIn,
                                unsigned int iStrideOut,
                                bool inverse)
{
	for (unsigned int w = 0; w < iLength; w++)
	{
		pfRealOut[iStrideOut*w] = pfImaginaryOut[iStrideOut*w] = 0;
		for (unsigned int y = 0; y < iLength; y++)
		{
			float32 a = 2 * PI * w * y / float32(iLength);
			if (!inverse)
				a = -a;
			float32 ca = cos(a);
			float32 sa = sin(a);
			pfRealOut[iStrideOut*w] += pfRealIn[iStrideIn*y] * ca - pfImaginaryIn[iStrideIn*y] * sa;
			pfImaginaryOut[iStrideOut*w] += pfRealIn[iStrideIn*y] * sa + pfImaginaryIn[iStrideIn*y] * ca;   
		}
	}

	if (inverse) {
		for (unsigned int x = 0; x < iLength; ++x) {
			pfRealOut[iStrideOut*x] /= iLength;
			pfImaginaryOut[iStrideOut*x] /= iLength;
		}
	}
}

void discreteFourierTransform2D(unsigned int iHeight, unsigned int iWidth,
                                const float32* pfRealIn,
                                const float32* pfImaginaryIn,
                                float32* pfRealOut,
                                float32* pfImaginaryOut,
                                bool inverse)
{
	float32* reTemp = new float32[iWidth * iHeight];
	float32* imTemp = new float32[iWidth * iHeight];

	//calculate the fourier transform of the columns
	for (unsigned int x = 0; x < iWidth; x++)
	{
		discreteFourierTransform1D(iHeight, pfRealIn+x, pfImaginaryIn+x,
		                           reTemp+x, imTemp+x,
		                           iWidth, iWidth, inverse);
	}

	//calculate the fourier transform of the rows
	for(unsigned int y = 0; y < iHeight; y++)
	{
		discreteFourierTransform1D(iWidth,
		                           reTemp+y*iWidth,
		                           imTemp+y*iWidth,
		                           pfRealOut+y*iWidth,
		                           pfImaginaryOut+y*iWidth,
		                           1, 1, inverse);
	}

	delete[] reTemp;
	delete[] imTemp;
}

/** permute the entries from pfDataIn into pfDataOut to prepare for an
 *  in-place FFT. pfDataIn may be equal to pfDataOut.
 */
static void bitReverse(unsigned int iLength,
                       const float32* pfDataIn, float32* pfDataOut,
                       unsigned int iStrideShiftIn,
                       unsigned int iStrideShiftOut)
{
	if (pfDataIn == pfDataOut) {
		assert(iStrideShiftIn == iStrideShiftOut);
		float32 t;
		unsigned int j = 0;
		for(unsigned int i = 0; i < iLength - 1; i++) {
			if (i < j) {
				t = pfDataOut[i<<iStrideShiftOut];
				pfDataOut[i<<iStrideShiftOut] = pfDataOut[j<<iStrideShiftOut];
				pfDataOut[j<<iStrideShiftOut] = t;
			}
			unsigned int k = iLength / 2;
			while (k <= j) {
				j -= k;
				k /= 2;
			}
			j += k;
		}
	} else {
		unsigned int j = 0;
		for(unsigned int i = 0; i < iLength - 1; i++) {
			pfDataOut[i<<iStrideShiftOut] = pfDataIn[j<<iStrideShiftIn];
			unsigned int k = iLength / 2;
			while (k <= j) {
				j -= k;
				k /= 2;
			}
			j += k;
		}
		pfDataOut[(iLength-1)<<iStrideShiftOut] = pfDataIn[(iLength-1)<<iStrideShiftOut];
	}
}

static unsigned int log2(unsigned int n)
{
	unsigned int l = 0;
	while (n > 1) {
		n /= 2;
		++l;
	}
	return l;
}

/** perform 1D FFT. iLength, iStrideIn, iStrideOut must be powers of two. */
void fastTwoPowerFourierTransform1D(unsigned int iLength,
                                    const float32* pfRealIn,
                                    const float32* pfImaginaryIn,
                                    float32* pfRealOut,
                                    float32* pfImaginaryOut,
                                    unsigned int iStrideIn,
                                    unsigned int iStrideOut,
                                    bool inverse)
{
	unsigned int iStrideShiftIn = log2(iStrideIn);
	unsigned int iStrideShiftOut = log2(iStrideOut);
	unsigned int iLogLength = log2(iLength);

	bitReverse(iLength, pfRealIn, pfRealOut, iStrideShiftIn, iStrideShiftOut);
	bitReverse(iLength, pfImaginaryIn, pfImaginaryOut, iStrideShiftIn, iStrideShiftOut);

	float32 ca = -1.0;
	float32 sa = 0.0;
	unsigned int l1 = 1, l2 = 1;
	for(unsigned int l=0; l < iLogLength; ++l)
	{
		l1 = l2;
		l2 *= 2;
		float32 u1 = 1.0;
		float32 u2 = 0.0;
		for(unsigned int j = 0; j < l1; j++)
		{
			for(unsigned int i = j; i < iLength; i += l2)
			{
				unsigned int i1 = i + l1;
				float32 t1 = u1 * pfRealOut[i1<<iStrideShiftOut] - u2 * pfImaginaryOut[i1<<iStrideShiftOut];
				float32 t2 = u1 * pfImaginaryOut[i1<<iStrideShiftOut] + u2 * pfRealOut[i1<<iStrideShiftOut];
				pfRealOut[i1<<iStrideShiftOut] = pfRealOut[i<<iStrideShiftOut] - t1;
				pfImaginaryOut[i1<<iStrideShiftOut] = pfImaginaryOut[i<<iStrideShiftOut] - t2;
				pfRealOut[i<<iStrideShiftOut] += t1;
				pfImaginaryOut[i<<iStrideShiftOut] += t2;
			}
			float32 z =  u1 * ca - u2 * sa;
			u2 = u1 * sa + u2 * ca;
			u1 = z;
		}
		sa = sqrt((1.0 - ca) / 2.0);
		if (!inverse) 
			sa = -sa;
		ca = sqrt((1.0 + ca) / 2.0);
	}

	if (inverse) {
		for (unsigned int i = 0; i < iLength; ++i) {
			pfRealOut[i<<iStrideShiftOut] /= iLength;
			pfImaginaryOut[i<<iStrideShiftOut] /= iLength;
		}
	}
}

void fastTwoPowerFourierTransform2D(unsigned int iHeight,
                                    unsigned int iWidth,
                                    const float32* pfRealIn,
                                    const float32* pfImaginaryIn,
                                    float32* pfRealOut,
                                    float32* pfImaginaryOut,
                                    bool inverse)
{
	//calculate the fourier transform of the columns
	for (unsigned int x = 0; x < iWidth; x++)
	{
		fastTwoPowerFourierTransform1D(iHeight, pfRealIn+x, pfImaginaryIn+x,
		                               pfRealOut+x, pfImaginaryOut+x,
		                               iWidth, iWidth, inverse);
	}

	//calculate the fourier transform of the rows
	for (unsigned int y = 0; y < iHeight; y++)
	{
		fastTwoPowerFourierTransform1D(iWidth,
		                               pfRealOut+y*iWidth,
		                               pfImaginaryOut+y*iWidth,
		                               pfRealOut+y*iWidth,
		                               pfImaginaryOut+y*iWidth,
		                               1, 1, inverse);
	}
}

}