-
Notifications
You must be signed in to change notification settings - Fork 4
/
fft_1d.cpp
185 lines (143 loc) · 4.96 KB
/
fft_1d.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
// Software: Fast Fourier Transform 1D (for real and complex signals)
// 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(NlogN)
// Space complexity: O(N).
#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;
// type = 1: The original signal is real
// type = 2: The original signal is complex
int type;
// Number of time samples
int N;
// Normalization constant c = 1/sqrt(N)
double c;
// 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 == 0) {
std::cerr << "Not enough input parameters!" << std::endl;
return;
}
if (nInputs > 2) {
std::cerr << "Maximum 2 input parameters only!" << std::endl;
return;
}
if (nOutputs != 2) {
std::cerr << "The number of output parameters must be 2 (the real and imaginary parts)!" << std::endl;
return;
}
type = nInputs;
if ((mxGetM(input_pointers[0]) > (size_t)(1)) && (mxGetN(input_pointers[0]) > (size_t)(1))) {
std::cerr << "The original signal must be a vector!" << std::endl;
return;
}
if (mxGetM(input_pointers[0]) > (size_t)(1)) {
N = mxGetM(input_pointers[0]);
} else {
N = mxGetN(input_pointers[0]);
}
// Normalization constant
c = 1.0 / sqrt(N);
// The original signal s and its Fourier transform F
double *Re_s;
double *Im_s;
double *Re_F;
double *Im_F;
// Memory allocation
Re_s = mxGetPr(input_pointers[0]);
output_pointers[0] = mxCreateDoubleMatrix(N, 1, mxREAL);
Re_F = mxGetPr(output_pointers[0]);
output_pointers[1] = mxCreateDoubleMatrix(N, 1, mxREAL);
Im_F = mxGetPr(output_pointers[1]);
// +-----------------------------+
// | The original signal is real |
// +-----------------------------+
if (nInputs == 1) {
FFT_real(Re_s, Re_F, Im_F, N, 1);
for (int frequency = 0; frequency < N; ++frequency) {
Re_F[frequency] *= c;
Im_F[frequency] *= c;
}
return;
}
// +--------------------------------+
// | The original signal is complex |
// +--------------------------------+
if (((mxGetM(input_pointers[0]) != mxGetM(input_pointers[1])) || (mxGetN(input_pointers[0]) != mxGetN(input_pointers[1])))) {
std::cerr << "The size of the real part and the complex part are not equal!" << std::endl;
return;
}
Im_s = mxGetPr(input_pointers[1]);
FFT_complex(Re_s, Im_s, Re_F, Im_F, N, 1);
for (int frequency = 0; frequency < N; ++frequency) {
Re_F[frequency] *= c;
Im_F[frequency] *= c;
}
}