-
Notifications
You must be signed in to change notification settings - Fork 8
/
fourier.h
223 lines (177 loc) · 4.65 KB
/
fourier.h
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
// fourier.h
#ifndef FOURIER
#include "wave.h"
namespace soundmath
{
template <typename T, size_t N> class Fourier
{
public:
void (*processor)(const T* in, T* out);
// in, middle, out need to be arrays of size (N * laps * 2)
Fourier(void (*processor)(const T*, T*), ShyFFT<T, N, RotationPhasor>* fft, Wave<T>* window, size_t laps, T* in, T* middle, T* out)
: processor(processor), in(in), middle(middle), out(out), fft(fft), window(window), laps(laps), stride(N / laps)
{
writepoints = new int[laps * 2];
readpoints = new int[laps * 2];
memset(writepoints, 0, sizeof(int) * laps * 2);
memset(readpoints, 0, sizeof(int) * laps * 2);
for (int i = 0; i < 2 * (int)laps; i++) // initialize half of writepoints
writepoints[i] = -i * (int)stride;
reading = new bool[laps * 2];
writing = new bool[laps * 2];
memset(reading, false, sizeof(bool) * laps * 2);
memset(writing, true, sizeof(bool) * laps * 2);
}
~Fourier()
{
delete [] writepoints;
delete [] readpoints;
delete [] reading;
delete [] writing;
}
// writes a single sample (with windowing) into the in array
void write(T x)
{
for (size_t i = 0; i < laps * 2; i++)
{
if (writing[i])
{
if (writepoints[i] >= 0)
{
T amp = (*window)((T)writepoints[i] / N);
in[writepoints[i] + N * i] = amp * x;
}
writepoints[i]++;
if (writepoints[i] == N)
{
writing[i] = false;
reading[i] = true;
readpoints[i] = 0;
forward(i); // FTs ith in to ith middle buffer
process(i); // user-defined; ought to move info from ith middle to out buffer
backward(i); // IFTs ith out to ith in buffer
current = i;
}
}
}
}
inline void forward(const size_t i)
{
fft->Direct((in + i * N), (middle + i * N)); // analysis
// arm_rfft_fast_f32(fft, in + i * N, middle + i * N, 0);
}
inline void backward(const size_t i)
{
fft->Inverse((out + i * N), (in + i * N)); // synthesis
// arm_rfft_fast_f32(fft, out + i * N, in + i * N, 1);
}
// executes user-defined callback
inline void process(const size_t i)
{
processor((middle + i * N), (out + i * N));
}
// read a single reconstructed sample
T read()
{
T accum = 0;
for (size_t i = 0; i < laps * 2; i++)
{
if (reading[i])
{
T amp = (*window)((T)readpoints[i] / N);
accum += amp * in[readpoints[i] + N * i];
readpoints[i]++;
if (readpoints[i] == N)
{
writing[i] = true;
reading[i] = false;
writepoints[i] = 0;
}
}
}
accum /= N * laps / 2.0;
return accum;
}
private:
T *in, *middle, *out;
public:
ShyFFT<T, N, RotationPhasor>* fft;
Wave<T>* window;
size_t laps;
size_t stride;
int* writepoints;
int* readpoints;
bool* reading;
bool* writing;
int current = 0;
};
template <typename T, size_t N> class Analyzer
{
public:
int (*processor)(const T* in);
// in, middle, out need to be arrays of size (N * laps * 2)
Analyzer(int (*processor)(const T*), ShyFFT<T, N, RotationPhasor>* fft, size_t laps, T* in, T* middle)
: processor(processor), in(in), middle(middle), fft(fft), laps(laps), stride(N / laps)
{
writepoints = new int[laps];
memset(writepoints, 0, sizeof(int) * laps);
for (int i = 0; i < (int)laps; i++) // initialize half of writepoints
writepoints[i] = -i * (int)stride;
writing = new bool[laps];
memset(writing, true, sizeof(bool) * laps);
}
~Analyzer()
{
delete [] writepoints;
delete [] writing;
}
// writes a single sample (with windowing) into the in array
void write(T x)
{
for (size_t i = 0; i < laps; i++)
{
if (writing[i])
{
if (writepoints[i] >= 0)
{
// T window = halfhann((T)writepoints[i] / N);
T window = hann((T)writepoints[i] / N);
in[writepoints[i] + N * i] = window * x;
}
writepoints[i]++;
if (writepoints[i] == N)
{
writing[i] = false;
forward(i); // FTs ith in to ith middle buffer
process(i); // user-defined; ought to move info from ith middle to out buffer
current = i;
size_t next = (i + 1) % (laps);
writing[next] = true;
writepoints[next] = 0;
}
}
}
}
inline void forward(const size_t i)
{
fft->Direct((in + i * N), (middle + i * N)); // analysis
// arm_rfft_fast_f32(fft, in + i * N, middle + i * N, 0);
}
// executes user-defined callback
inline void process(const size_t i)
{
processor((middle + i * N));
}
private:
T *in, *middle;
public:
ShyFFT<T, N, RotationPhasor>* fft;
size_t laps;
size_t stride;
int* writepoints;
bool* writing;
int current = 0;
};
}
#define FOURIER
#endif