-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spectrum.cpp
128 lines (100 loc) · 3.57 KB
/
Spectrum.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
#include "Spectrum.h"
Spectrum::Spectrum(const size_t size) {
this->size = size;
bar_data_size = size/2;
bar_data = std::make_unique<float[]>(bar_data_size);
wave_window = std::make_unique<float[]>(size);
normalisation_window = std::make_unique<float[]>(bar_data_size);
UseFlatNormalisation();
UseHanningWindow();
fft_in = std::make_unique<double[]>(size);
fft_out = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * size);
plan = fftw_plan_dft_r2c_1d(size, fft_in.get(), fft_out, FFTW_ESTIMATE);
}
Spectrum::~Spectrum() {
fftw_destroy_plan(plan);
fftw_free(fft_out);
}
void Spectrum::UseHanningWindow() {
double delta = 2 * M_PI / (double)size;
for (size_t i = 0; i < size; i++) {
double phase = i * delta;
wave_window[i] = (float)(0.5 * (1.0 - cos(phase)));
}
}
void Spectrum::UseHammingWindow() {
double delta = 2 * M_PI / (double)size;
for (size_t i = 0; i < size; i++) {
double phase = i * delta;
wave_window[i] = (float)(0.54 - .46*cos(phase));
}
}
void Spectrum::UseBlackmanWindow() {
double delta = 2 * M_PI / (double)size;
for (size_t i = 0; i < size; i++) {
double phase = i * delta;
wave_window[i] = (float)(0.42 - .5*cos(phase) + .08*cos(2 * phase));
}
}
void Spectrum::UseFlatNormalisation() {
for (size_t i = 0; i < bar_data_size; i++) {
normalisation_window[i] = 1;
}
}
void Spectrum::UseLinearNormalisation(float start, float end) {
for (size_t i = 0; i < bar_data_size; i++) {
normalisation_window[i] = map(i, 0, bar_data_size - 1, start, end);
}
}
void Spectrum::Update(const float data[]) {
// apply window and scale to data and copy into input array
for (size_t i = 0; i < size; i++) {
fft_in[i] = data[i] * wave_window[i] * scale;
}
fftw_execute(plan);
// convert data from output to usable data
for (size_t i = 0; i < bar_data_size; i++) {
// Compute magnitude squared from real and imaginary components of FFT
float mag = fft_out[i][0]*fft_out[i][0] + fft_out[i][1]*fft_out[i][1];
// Normalise
mag = mag * normalisation_window[i];
// Clip magnitude to [0, 1]
mag = std::clamp(mag, 0.0f, 1.0f);
// Use weighted average to smooth the data
bar_data[i] = bar_data[i] * (1 - average_weight) + mag * average_weight;
}
}
float Spectrum::BarDataAt(float i) const {
if (i < 0 || i >= bar_data_size)
return 0;
float intpart;
float weight = modff(i, &intpart);
size_t start = intpart;
size_t end = std::min(start + 1, bar_data_size - 1);
return (1 - weight) * bar_data[start] + weight * bar_data[end];
}
void Spectrum::GetData(
float freq_start,
float freq_end,
float sample_rate,
float output[],
size_t output_size
) const {
const float freq_max = sample_rate/2;
const float input_size = map(std::abs(freq_start - freq_end), 0, freq_max, 0, bar_data_size - 1);
int neighbours = floor(input_size / output_size);
for (size_t i = 0; i < output_size; i++) {
const float freq = map(i, 0, output_size - 1, freq_start, freq_end);
const float base_i = map(freq, 0, freq_max, 0, bar_data_size - 1);
size_t count = 0;
float out_sum = 0;
for (int offset = -neighbours; offset <= neighbours; offset++) {
const float input_i = base_i + offset;
if (input_i < 0 || input_i >= bar_data_size)
continue;
out_sum += BarDataAt(input_i);
count++;
}
output[i] = out_sum / count;
}
}