Skip to content

Commit 5e19374

Browse files
committed
Provide Window Function Name
1 parent bc770e2 commit 5e19374

File tree

5 files changed

+52
-15
lines changed

5 files changed

+52
-15
lines changed

src/AudioTools/AudioLibs/AudioFFT.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,13 +329,13 @@ class AudioFFTBase : public AudioStream {
329329

330330
/// We try to fill the buffer at once
331331
int availableForWrite() override {
332-
return cfg.channels * bytesPerSample() * cfg.length;
332+
return cfg.length * cfg.channels * bytesPerSample();
333333
}
334334

335335
/// Data available for reverse fft
336336
int available() override {
337-
int step_size = cfg.stride > 0 ? cfg.stride : cfg.length;
338-
return step_size * cfg.channels * bytesPerSample();
337+
assert(cfg.stride != 0);
338+
return cfg.stride * cfg.channels * bytesPerSample();
339339
}
340340

341341
/// The number of bins used by the FFT which are relevant for the result
@@ -456,7 +456,10 @@ class AudioFFTBase : public AudioStream {
456456
/// sets the value of a bin
457457
bool setBin(int idx, float real, float img) {
458458
has_rfft_data = true;
459-
return p_driver->setBin(idx, real, img);
459+
if (idx < 0 || idx >= size()) return false;
460+
bool rc = p_driver->setBin(idx, real, img);
461+
bool rc1 = p_driver->setBin(cfg.length - idx, real, img);
462+
return rc && rc1;
460463
}
461464
/// sets the value of a bin
462465
bool setBin(int pos, FFTBin &bin) {

src/AudioTools/AudioLibs/AudioRealFFT.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class FFTDriverRealFFT : public FFTDriver {
6767
float getValue(int idx) override { return v_x[idx];}
6868

6969
bool setBin(int pos, float real, float img) override {
70-
if (pos>=len) return false;
70+
if (pos < 0 || pos >= len) return false;
7171
v_x[pos] = real;
7272
v_f[pos] = img;
7373
return true;

src/AudioTools/AudioLibs/FFT/FFTWindows.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class WindowFunction {
4040
/// Provides the number of samples (fft length)
4141
inline int samples() { return i_samples; }
4242

43+
virtual const char* name() = 0;
44+
4345
protected:
4446
float samples_minus_1 = 0.0f;
4547
int i_samples = 0;
@@ -68,6 +70,12 @@ class BufferedWindow : public WindowFunction {
6870
public:
6971
BufferedWindow(WindowFunction* wf) { p_wf = wf; }
7072

73+
const char* name() {
74+
static char buffer[80] = "Buffered ";
75+
strncpy(buffer+9, p_wf->name(),69);
76+
return buffer;
77+
}
78+
7179
virtual void begin(int samples) override {
7280
// process only if there is a change
7381
WindowFunction::begin(samples);
@@ -102,7 +110,8 @@ class Rectange : public WindowFunction {
102110
float factor_internal(int idx) {
103111
if (idx < 0 || idx >= i_samples) return 0;
104112
return 1.0f; }
105-
};
113+
const char* name() { return "Rectange"; }
114+
};
106115

107116
/**
108117
* @brief Hamming FFT Window function
@@ -115,6 +124,7 @@ class Hamming : public WindowFunction {
115124
float factor_internal(int idx) {
116125
return 0.54f - (0.46f * cos(twoPi * ratio(idx)));
117126
}
127+
const char* name() { return "Hamming"; }
118128
};
119129

120130
/**
@@ -125,6 +135,8 @@ class Hamming : public WindowFunction {
125135
class Hann : public WindowFunction {
126136
public:
127137
Hann() = default;
138+
const char* name() { return "Hann"; }
139+
128140
float factor_internal(int idx) {
129141
return 0.54f * (1.0f - cos(twoPi * ratio(idx)));
130142
}
@@ -138,6 +150,7 @@ class Hann : public WindowFunction {
138150
class Triangle : public WindowFunction {
139151
public:
140152
Triangle() = default;
153+
const char* name() { return "Triangle"; }
141154
float factor_internal(int idx) {
142155
return 1.0f - ((2.0f * fabs((idx - 1) -
143156
(static_cast<float>(i_samples - 1) / 2.0f))) /
@@ -154,6 +167,7 @@ class Triangle : public WindowFunction {
154167
class Nuttall : public WindowFunction {
155168
public:
156169
Nuttall() = default;
170+
const char* name() { return "Nuttall"; }
157171
float factor_internal(int idx) override {
158172
float r = ratio(idx);
159173
return 0.355768f - (0.487396f * (cos(twoPi * r))) +
@@ -170,6 +184,7 @@ class Nuttall : public WindowFunction {
170184
class Blackman : public WindowFunction {
171185
public:
172186
Blackman() = default;
187+
const char* name() { return "Blackman"; }
173188
float factor_internal(int idx)override {
174189
float r = ratio(idx);
175190
return 0.42323f - (0.49755f * (cos(twoPi * r))) +
@@ -185,6 +200,7 @@ class Blackman : public WindowFunction {
185200
class BlackmanNuttall : public WindowFunction {
186201
public:
187202
BlackmanNuttall() = default;
203+
const char* name() { return "BlackmanNuttall"; }
188204
float factor_internal(int idx) override{
189205
float r = ratio(idx);
190206
return 0.3635819f - (0.4891775f * (cos(twoPi * r))) +
@@ -200,6 +216,7 @@ class BlackmanNuttall : public WindowFunction {
200216
class BlackmanHarris : public WindowFunction {
201217
public:
202218
BlackmanHarris() = default;
219+
const char* name() { return "BlackmanHarris"; }
203220
float factor_internal(int idx) override{
204221
float r = ratio(idx);
205222
return 0.35875f - (0.48829f * (cos(twoPi * r))) +
@@ -215,6 +232,7 @@ class BlackmanHarris : public WindowFunction {
215232
class FlatTop : public WindowFunction {
216233
public:
217234
FlatTop() = default;
235+
const char* name() { return "FlatTop"; }
218236
float factor_internal(int idx) override{
219237
float r = ratio(idx);
220238
return 0.2810639f - (0.5208972f * cos(twoPi * r)) +
@@ -230,6 +248,7 @@ class FlatTop : public WindowFunction {
230248
class Welch : public WindowFunction {
231249
public:
232250
Welch() = default;
251+
const char* name() { return "Welch"; }
233252
float factor_internal(int idx) override{
234253
float tmp = (((idx - 1) - samples_minus_1 / 2.0f) / (samples_minus_1 / 2.0f));
235254
return 1.0f - (tmp*tmp);

src/AudioTools/CoreAudio/AudioEffects/FFTEffects.h

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,49 +41,56 @@ class FFTEffect : public AudioOutput {
4141
}
4242

4343
bool begin(FFTEffectConfig info) {
44+
copier.setLogName("ifft");
4445
setAudioInfo(info);
4546
fft_cfg.length = info.length;
46-
fft_cfg.stride = info.stride;
47+
fft_cfg.stride = info.stride > 0 ? info.stride : info.length;
4748
fft_cfg.window_function = info.window_function;
4849
return begin();
4950
}
5051

5152
bool begin() override {
53+
TRACED();
5254
// copy result to output
5355
copier.begin(*p_out, fft);
5456

5557
// setup fft
5658
fft_cfg.copyFrom(audioInfo());
57-
fft_cfg.length = 1024;
58-
fft_cfg.stride = 512;
59-
fft_cfg.window_function = &buffered;
6059
fft_cfg.callback = effect_callback;
60+
LOGI("length: %d", fft_cfg.length);
61+
LOGI("stride: %d", fft_cfg.stride);
62+
LOGI("window_function: %s", (fft_cfg.window_function!=nullptr) ? fft_cfg.window_function->name() : "-");
6163
return fft.begin(fft_cfg);
6264
}
6365

6466
size_t write(const uint8_t *data, size_t len) override {
67+
TRACED();
6568
return fft.write(data, len);
6669
}
6770

6871
protected:
6972
Print *p_out = nullptr;
7073
AudioRealFFT fft;
71-
AudioFFTConfig fft_cfg{fft.defaultConfig()};
74+
AudioFFTConfig fft_cfg{fft.defaultConfig(RXTX_MODE)};
7275
Hann hann;
7376
BufferedWindow buffered{&hann};
7477
StreamCopy copier;
7578

7679
virtual void effect(AudioFFTBase &fft) = 0;
7780

7881
static void effect_callback(AudioFFTBase &fft) {
82+
TRACED();
7983
FFTEffect *ref = (FFTEffect *)fft.config().ref;
8084
// execute effect
8185
ref->effect(fft);
8286
// write ifft to output
8387
ref->processOutput();
8488
}
8589

86-
void processOutput() { while (copier.copy()); }
90+
void processOutput() {
91+
TRACED();
92+
while (copier.copy());
93+
}
8794
};
8895

8996
/**
@@ -103,9 +110,10 @@ class FFTRobotize : public FFTEffect {
103110
protected:
104111
/// Robotise the output
105112
void effect(AudioFFTBase &fft) {
113+
TRACED();
106114
FFTBin bin;
107115
for (int n = 0; n < fft.size(); n++) {
108-
float amplitude = fft.magnitudeFast(n);
116+
float amplitude = fft.magnitude(n);
109117
// update new bin value
110118
bin.real = amplitude;
111119
bin.img = 0;
@@ -131,9 +139,10 @@ class FFTWhisper : public FFTEffect {
131139
protected:
132140
/// Robotise the output
133141
void effect(AudioFFTBase &fft) {
142+
TRACED();
134143
FFTBin bin;
135144
for (int n = 0; n < fft.size(); n++) {
136-
float amplitude = fft.magnitudeFast(n);
145+
float amplitude = fft.magnitude(n);
137146
float phase = rand() / (float)RAND_MAX * 2.f * M_PI;
138147

139148
// update new bin value
@@ -174,6 +183,7 @@ class FFTPitchShift : public FFTEffect {
174183

175184
FFTPitchShiftConfig defaultConfig() {
176185
FFTPitchShiftConfig result;
186+
result.shift = shift;
177187
return result;
178188
}
179189

@@ -190,13 +200,16 @@ class FFTPitchShift : public FFTEffect {
190200

191201
/// Pitch Shift
192202
void effect(AudioFFTBase &fft) {
203+
TRACED();
193204
FFTBin bin;
194205
int max = fft.size();
195206

196207
if (shift < 0) {
197208
// copy bins: left shift
198209
for (int n = -shift; n < max; n++) {
199210
int to_bin = n + shift;
211+
assert(to_bin >= 0);
212+
assert(to_bin < max);
200213
fft.getBin(n, bin);
201214
fft.setBin(to_bin, bin);
202215
}
@@ -209,6 +222,8 @@ class FFTPitchShift : public FFTEffect {
209222
// copy bins: right shift
210223
for (int n = max - shift; n <= 0; n--) {
211224
int to_bin = n + shift;
225+
assert(to_bin >= 0);
226+
assert(to_bin < max);
212227
fft.getBin(n, bin);
213228
fft.setBin(to_bin, bin);
214229
}

tests-cmake/ifft/ifft.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ int bin_idx = 0;
1313
void fftFillData(AudioFFTBase &fft) {
1414
fft.clearBins();
1515
FFTBin bin{1.0f,1.0f};
16-
fft.setBin(bin_idx, bin);
16+
assert(fft.setBin(bin_idx, bin));
1717

1818
// restart from first bin
1919
if (++bin_idx>=fft.size()) bin_idx = 0;

0 commit comments

Comments
 (0)