-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathDFT.h
More file actions
218 lines (182 loc) · 4.89 KB
/
DFT.h
File metadata and controls
218 lines (182 loc) · 4.89 KB
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
#ifndef _H_DFT_
#define _H_DFT_
#include <string.h>
#include <math.h>
#include "FFT.h"
// It is okay to not inherit from FFTbase (FFT.h)
class DFTbrute :public FFTbase{
private :
int n_fft;
int n_hfft;
int n_channels;
double* buf;
// Common factor in Talyor Formula
double** factor_re;
double** factor_im;
double** ifactor_re;
double** ifactor_im;
const double DFT_pi = 3.14159265358979323846;
public :
inline DFTbrute(int n_fft, int n_channels);
inline ~DFTbrute();
inline void DFT(double*);
inline void DFT(double**);
inline void DFT(double **, int target_channels);
inline void iDFT(double*);
inline void iDFT(double**);
// Not FFT, auxilary function to interface with STFT
inline void FFT(double*);
inline void FFT(double**);
inline void FFT(double**, int target_channels);
inline void iFFT(double*);
inline void iFFT(double**);
};
DFTbrute::DFTbrute(int n_fft_, int n_channels_) {
this->n_fft = n_fft_;
this->n_hfft = n_fft_ / 2 + 1;
this->n_channels = n_channels_;
buf = new double[n_fft_ + 2];
factor_re = new double* [n_fft];
for (int i = 0; i < n_fft; i++) {
factor_re[i] = new double[n_fft];
for (int j = 0; j < n_fft; j++)
factor_re[i][j] = cos(-2.0 * DFT_pi * i * j / n_fft);
}
factor_im = new double* [n_fft];
for (int i = 0; i < n_fft; i++) {
factor_im[i] = new double[n_fft];
for (int j = 0; j < n_fft; j++)
factor_im[i][j] = sin(-2.0 * DFT_pi * i * j / n_fft);
}
ifactor_re = new double* [n_fft];
for (int i = 0; i < n_fft; i++) {
ifactor_re[i] = new double[n_fft];
for (int j = 0; j < n_fft; j++)
ifactor_re[i][j] = cos(2.0 * DFT_pi * i * j / n_fft);
}
ifactor_im = new double* [n_fft];
for (int i = 0; i < n_fft; i++) {
ifactor_im[i] = new double[n_fft];
for (int j = 0; j < n_fft; j++)
ifactor_im[i][j] = sin(2.0 * DFT_pi * i * j / n_fft);
}
}
DFTbrute::~DFTbrute() {
delete[] buf;
for (int i = 0; i < n_fft; i++) {
delete[] factor_re[i];
delete[] factor_im[i];
delete[] ifactor_re[i];
delete[] ifactor_im[i];
}
delete[] factor_re;
delete[] factor_im;
delete[] ifactor_re;
delete[] ifactor_im;
}
void DFTbrute::DFT(double** x) {
for (int i = 0; i < n_channels; i++) {
DFT(x[i]);
}
}
void DFTbrute::iDFT(double** x) {
for (int i = 0; i < n_channels; i++) {
iDFT(x[i]);
}
}
void DFTbrute::DFT(double** x, int target_channels) {
for (int i = 0; i < target_channels; i++) {
DFT(x[i]);
}
}
/*
x : input signal shape of [n_hfft*2] -> complex DFT output
e.g.
x = [s_1 s_2 s_3 .... s_n_fft, 0.0, 0.0]
-> x = [real_1, imag_1, real_2, imag_2, ... , real_n_hfft, imag_n_hfft]
*/
void DFTbrute::DFT(double* x) {
memcpy(buf, x, sizeof(double) * n_fft);
int re, im;
for (int k = 0; k < n_hfft; k++) {
re = 2*k;
im = 2*k + 1;
x[re] = 0;
x[im] = 0;
for (int n = 0; n < n_fft; n++) {
x[re] += buf[n] * factor_re[k][n];
x[im] += buf[n] * factor_im[k][n];
}
}
}
/*
x : complex DFT input[n_fft+2] -> output signal shape of [n_fft]
e.g.
x = [real_1, imag_1, real_2, imag_2, ... , real_n_hfft, imag_n_hfft]
->x = [s_1 s_2 s_3 .... s_n_fft, .0.0, 0.0]
n_fft = 8 example
0 : 3.18345404225869 + 0.00000000000000i
1 : 0.312254329339716 + 0.664395308387280i
2 : 0.407888857519389 - 0.398019401837019i
3 : 0.501028376018184 + 0.0657693633001526i
4 : -1.07520788103573 + 0.00000000000000i
5 : 0.501028376018184 - 0.0657693633001526i
6 : 0.407888857519389 + 0.398019401837019i
7 : 0.312254329339716 - 0.664395308387280i
*/
void DFTbrute::iDFT(double* x){
memcpy(buf, x, sizeof(double) * (n_fft+2));
double factor = 2* DFT_pi / n_fft;
double e_re, e_im;
int re, im;
int i_l, i_r;
int k;
int k2;
for (int n = 0; n < n_fft; n++) {
x[n] = 0.0;
// k = 0
k = 0;
re = 2 * k;
im = 2 * k + 1;
x[n] += buf[re]*ifactor_re[k][n];
x[n] += buf[re]* ifactor_im[k][n];
// 1 <= k < n_hfft -1
for (int k = 1; k < n_hfft - 1; k++) {
re = 2 * k;
im = 2 * k + 1;
x[n] += buf[re] * ifactor_re[k][n] - buf[im] * ifactor_im[k][n];
x[n] += buf[im] * ifactor_re[k][n] + buf[re] * ifactor_im[k][n];
}
// k = n_hfft -1
k = n_hfft - 1;
re = 2 * k;
im = 2 * k + 1;
x[n] += buf[re]*ifactor_re[k][n];
x[n] += buf[re]*ifactor_im[k][n];
// n_hfft <= k < n_fft
for (int k = n_hfft; k < n_fft; k++) {
k2 = k - n_hfft;
re = 2 * (n_hfft - k2 - 2);
im = 2 * (n_hfft - k2 - 2)+1;
x[n] += buf[re]*ifactor_re[k][n] + buf[im] * ifactor_im[k][n];
x[n] += -buf[im] * ifactor_re[k][n] + buf[re] * ifactor_im[k][n];
}
x[n] /= n_fft;
}
}
void DFTbrute::FFT(double* x) {
DFT(x);
}
void DFTbrute::FFT(double** x) {
DFT(x);
}
void DFTbrute::FFT(double** x,int target_channel) {
DFT(x,target_channel);
}
void DFTbrute::iFFT(double* x) {
iDFT(x);
}
void DFTbrute::iFFT(double** x) {
iDFT(x);
}
#endif