-
Notifications
You must be signed in to change notification settings - Fork 7
/
fft.cpp
68 lines (52 loc) · 1.32 KB
/
fft.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
#if defined(__MK20DX256__) || defined(__MK20DX128__)
#include "fft.h"
#include <Logging.h>
#include <Audio.h>
using namespace pixl;
namespace pixl {
FFTInput::FFTInput(AudioAnalyzeFFT1024* fft, int start, int range)
: fft_(fft), amp_(0.0), freq_(0.0),
start_(start), range_(range) {}
void FFTInput::update() {
if (fft_->available()) {
float max_delta = 0.0;
int max_value = 0;
int max_index = 0;
for (int i = start_; i < range_; i++) {
int value = fft_->output[i];
float delta = (float)value / (float)max_[i];
// Decay max
max_[i] = max_[i] * 0.993;
// Track max
if (value > max_[i]) {
max_[i] = value;
}
// Find max amplitude
if (value > max_value) {
max_value = value;
}
// Track the freq that changed the most
if (delta > max_delta) {
max_delta = delta;
max_index = i;
}
}
Log.Debug("max index: %d\n", max_index);
Log.Debug("max value: %d\n", max_value);
freq_ = (float)max_index / (range_ - 1);
freq_ = min(1.0, freq_);
amp_ = (float)max_value / 6000.0;
amp_ = min(1.0, amp_);
}
}
float FFTInput::getInput(int index) {
if (index == 0) {
return amp_;
} else if (index == 1) {
return freq_;
} else {
return 0.0;
}
}
} // end namespace pixl
#endif