-
Notifications
You must be signed in to change notification settings - Fork 4
/
ifft_1d.cpp
138 lines (109 loc) · 3.8 KB
/
ifft_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
// Software: Inverse 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 (with the different harmonic coefficient) for complex signals
// It becomes the Inverse Fast Fourier Transform 1D
void iFFT(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;
iFFT(Re_Signal, Im_Signal, Re_F, Im_F, half, 2 * t);
iFFT(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 != 2) {
std::cerr << "The number of input parameters must be 2 (real and imaginary parts)!" << std::endl;
return;
}
if (nOutputs == 0) {
std::cerr << "Not enough output parameters!" << std::endl;
return;
}
if (nOutputs > 2) {
std::cerr << "Maximum 2 output parameters!" << std::endl;
return;
}
type = nOutputs;
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]) != mxGetM(input_pointers[1])) || (mxGetN(input_pointers[0]) != mxGetN(input_pointers[1]))) {
std::cerr << "The size of two parameters must be the same!" << 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_Signal;
double *Im_Signal;
double *Re_F;
double *Im_F;
// Memory allocation
Re_F = mxGetPr(input_pointers[0]);
Im_F = mxGetPr(input_pointers[1]);
output_pointers[0] = mxCreateDoubleMatrix(N, 1, mxREAL);
Re_Signal = mxGetPr(output_pointers[0]);
if (nOutputs == 2) {
output_pointers[1] = mxCreateDoubleMatrix(N, 1, mxREAL);
Im_Signal = mxGetPr(output_pointers[1]);
} else {
Im_Signal = new double [N];
}
// The order of parameters change compared to FFT
iFFT(Re_F, Im_F, Re_Signal, Im_Signal, N, 1);
for (int frequency = 0; frequency < N; ++frequency) {
Re_Signal[frequency] *= c;
Im_Signal[frequency] *= c;
}
}