-
Notifications
You must be signed in to change notification settings - Fork 4
/
fft_2d.cpp
201 lines (161 loc) · 5.52 KB
/
fft_2d.cpp
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
// Software: Fast Fourier Transform 2D (for real signals only)
// Author: Hy Truong Son
// Position: PhD Student
// Institution: Department of Computer Science, The University of Chicago
// Email: [email protected], [email protected]
// Website: http://people.inf.elte.hu/hytruongson/
// Copyright 2016 (c) Hy Truong Son. All rights reserved.
// Time complexity: O(N^2logN)
// Space complexity: O(N^2)
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <set>
#include <iterator>
#include <algorithm>
#include <ctime>
#include "mex.h"
using namespace std;
void vector2matrix(double *input, int nRows, int nCols, double **output) {
for (int i = 0; i < nRows; ++i) {
for (int j = 0; j < nCols; ++j) {
output[i][j] = input[j * nRows + i];
}
}
}
void matrix2vector(double **input, int nRows, int nCols, double *output) {
for (int i = 0; i < nRows; ++i) {
for (int j = 0; j < nCols; ++j) {
output[j * nRows + i] = input[i][j];
}
}
}
// Fast Fourier Transform 1D for real signals
void FFT_real(double *Signal, double *Re_F, double *Im_F, int N, int t) {
if (N == 1) {
Re_F[0] = Signal[0];
Im_F[0] = 0.0;
return;
}
int half = N / 2;
FFT_real(Signal, Re_F, Im_F, half, 2 * t);
FFT_real(Signal + t, Re_F + half, Im_F + half, half, 2 * t);
for (int k = 0; k < half; ++k) {
double r1 = Re_F[k];
double i1 = Im_F[k];
double r2 = Re_F[k + half];
double i2 = Im_F[k + half];
double alpha = - 2.0 * M_PI * (double)(k) / (double)(N);
double r3 = cos(alpha);
double i3 = sin(alpha);
double r4 = r2 * r3 - i2 * i3;
double i4 = r3 * i2 + r2 * i3;
Re_F[k] = r1 + r4;
Im_F[k] = i1 + i4;
Re_F[k + half] = r1 - r4;
Im_F[k + half] = i1 - i4;
}
}
// Fast Fourier Transform 1D for complex signals
void FFT_complex(double *Re_Signal, double *Im_Signal, double *Re_F, double *Im_F, int N, int t) {
if (N == 1) {
Re_F[0] = Re_Signal[0];
Im_F[0] = Im_Signal[0];
return;
}
int half = N / 2;
FFT_complex(Re_Signal, Im_Signal, Re_F, Im_F, half, 2 * t);
FFT_complex(Re_Signal + t, Im_Signal + t, Re_F + half, Im_F + half, half, 2 * t);
for (int k = 0; k < half; ++k) {
double r1 = Re_F[k];
double i1 = Im_F[k];
double r2 = Re_F[k + half];
double i2 = Im_F[k + half];
double alpha = - 2.0 * M_PI * (double)(k) / (double)(N);
double r3 = cos(alpha);
double i3 = sin(alpha);
double r4 = r2 * r3 - i2 * i3;
double i4 = r3 * i2 + r2 * i3;
Re_F[k] = r1 + r4;
Im_F[k] = i1 + i4;
Re_F[k + half] = r1 - r4;
Im_F[k + half] = i1 - i4;
}
}
void mexFunction(int nOutputs, mxArray *output_pointers[], int nInputs, const mxArray *input_pointers[]) {
if (nInputs != 1) {
std::cerr << "The number of input parameters must be exactly 1 (only for the real signal)!" << std::endl;
return;
}
if (nOutputs != 2) {
std::cerr << "The number of output parameters must be exactly 2 (real and imaginary parts)!" << std::endl;
return;
}
// M: number of rows
// N: number of columns
int M = mxGetM(input_pointers[0]);
int N = mxGetN(input_pointers[0]);
// Normalization constant
double c = 1.0 / sqrt(M * N);
// Memory allocation
double **s = new double* [M]; // The original signal
double **Re_F = new double* [M]; // Its Fourier transform
double **Im_F = new double* [M]; // Its Fourier transform
double **Re_P = new double* [M]; // Temparary matrix
double **Im_P = new double* [M]; // Temparary matrix
for (int row = 0; row < M; ++row) {
s[row] = new double [N];
Re_F[row] = new double [N];
Im_F[row] = new double [N];
Re_P[row] = new double [N];
Im_P[row] = new double [N];
}
// Initialization
vector2matrix(mxGetPr(input_pointers[0]), M, N, s);
// Fast Fourier Transform 2D - Precomputation
/*
double *A = new double [N];
double *B = new double [N];
double *C = new double [N];
*/
for (int u = 0; u < M; ++u) {
FFT_real(s[u], Re_P[u], Im_P[u], N, 1);
// Alternative
/*
for (int v = 0; v < N; ++v) {
A[v] = s[u][v];
}
FFT_real(A, B, C, N);
for (int y = 0; y < N; ++y) {
Re_P[u][y] = B[y];
Im_P[u][y] = C[y];
}
*/
}
// Fast Fourier Transform 2D - Computation
double *Re_p = new double [M];
double *Im_p = new double [M];
double *Re_f = new double [M];
double *Im_f = new double [M];
for (int y = 0; y < N; ++y) {
for (int u = 0; u < M; ++u) {
Re_p[u] = Re_P[u][y];
Im_p[u] = Im_P[u][y];
}
FFT_complex(Re_p, Im_p, Re_f, Im_f, M, 1);
for (int x = 0; x < M; ++x) {
Re_F[x][y] = Re_f[x] * c;
Im_F[x][y] = Im_f[x] * c;
}
}
// Return outputs
output_pointers[0] = mxCreateDoubleMatrix(M, N, mxREAL);
matrix2vector(Re_F, M, N, mxGetPr(output_pointers[0]));
output_pointers[1] = mxCreateDoubleMatrix(M, N, mxREAL);
matrix2vector(Im_F, M, N, mxGetPr(output_pointers[1]));
}