-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpectralRecorder.cpp
More file actions
224 lines (195 loc) · 6.61 KB
/
SpectralRecorder.cpp
File metadata and controls
224 lines (195 loc) · 6.61 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
219
220
221
222
223
224
#include <SFML/Audio.hpp>
#include <complex>
#include <valarray>
#include <iostream>
#include "fft.hpp"
#include "SpectralRecorder.hpp"
#include "notes.hpp"
double Plume_window(float alpha, size_t N, size_t n)
{
return 0.5*(1.0 - cos((2.0 * PI * pow((double)n / (double)(N - 1), 1.0 / alpha))));
}
double Gaussian(double sigma, double x)
{
return 1.0 / (sigma*sqrt(2.0 * PI)) * (exp(-1.0 / 2.0 * pow(x / sigma, 2.0)));
}
bool compare_second(const std::pair<size_t, float> p1, const std::pair<size_t, float> p2)
{
return p1.second > p2.second;
}
bool compare_first(const std::pair<size_t, float> p1, const std::pair<size_t, float> p2)
{
return p1.first > p2.first;
}
SpectralRecorder::SpectralRecorder(size_t octave_min, size_t octave_max, double alpha) : sf::SoundRecorder()
{
_octave_min = octave_min;
_octave_max = octave_max;
double freq_min = notes[octave_min]["C "];
double freq_max = 3000.0; // notes[octave_max]["B "];
//_delta_freq = pow(2.0, octave_min + 1);
double sample_time = (1.0 / freq_min) * 3;
this->setProcessingInterval(sf::Time(sf::microseconds((sf::Int16)(sample_time * 1e6))));
_alpha = alpha;
//_buffer_size = (size_t)((freq_max) * (1.0 / delta_freq));
_buffer_size = (size_t)(4.0*freq_max*40*sample_time);
_delta_freq = 4.0*freq_max / _buffer_size;
std::cout << _delta_freq << std::endl;
_buffer.resize(_buffer_size);
_buffer_i = 0;
}
bool SpectralRecorder::onProcessSamples(const sf::Int16* samples, std::size_t sampleCount)
{
for (size_t sample_i = 0; sample_i < sampleCount; ++sample_i)
{
_buffer[_buffer_i] = Complex((double)(samples[sample_i]) / pow(2., 16), 0.0);
_buffer_i= (_buffer_i + 1) % _buffer_size;
}
return true;
}
size_t SpectralRecorder::getPSDSize()
{
return _buffer_size/2;
}
CArray SpectralRecorder::getFT()
{
CArray FT; FT.resize(_buffer_size);
for (size_t sample_i = 0; sample_i < _buffer_i; ++sample_i)
{
FT[sample_i] = _buffer[sample_i] * Plume_window(_alpha, _buffer_size, _buffer_i - sample_i);
}
for (size_t sample_i = _buffer_i; sample_i < _buffer_size; ++sample_i)
{
FT[sample_i] = _buffer[sample_i] * Plume_window(_alpha, _buffer_size, _buffer_i + _buffer_size - sample_i);
}
fft(FT);
return FT;
}
double* SpectralRecorder::getPSD()
{
CArray FT = getFT();
double* PSD = new double[_buffer_size/2];;
double max_norm = 0;
for (size_t signal_i = 0; signal_i < _buffer_size/2; ++signal_i)
{
double norm_i = norm(FT[signal_i]);
if (norm_i > max_norm) { max_norm = norm_i; }
PSD[signal_i] = norm_i;
}
for (size_t signal_i = 0; signal_i < _buffer_size / 2; ++signal_i)
{
PSD[signal_i] /= max_norm;
}
return PSD;
}
double* SpectralRecorder::getCumulativePSD(size_t n) {
double* CumulativePSD = getPSD();
for (size_t signal_i = 0; signal_i < _buffer_size / 2; ++signal_i)
{
for (size_t k = 2; k <= n; ++k)
{
CumulativePSD[signal_i] *= 10*CumulativePSD[std::min(_buffer_size/2, k*signal_i)];
}
}
return CumulativePSD;
}
std::vector<std::pair<size_t, double>> SpectralRecorder::getPeaks(double threshold, double _freq_diff)
{
double* PSD = getPSD();
//double* PSD = getCumulativePSD(2);
/*================ PEAK EXTRACTION ================*/
std::vector<std::pair<size_t, double>> list_peaks;
for (size_t freq_i = 1; freq_i < _buffer_size/2 - 1; ++freq_i)
{
if ((PSD[freq_i] >= PSD[freq_i + 1] && PSD[freq_i] >= PSD[freq_i - 1]) && (PSD[freq_i] > threshold))
{
list_peaks.push_back(std::pair<size_t, float>(freq_i, PSD[freq_i]));
}
}
/*================= PEAK SELECTION ================*/
std::vector<std::pair<size_t, double>> list_best_peaks;
for (size_t peak_i = 0; peak_i < list_peaks.size(); ++peak_i)
{
bool keep_this_peak = true;
for (size_t best_peak_i = 0; best_peak_i < list_best_peaks.size(); ++best_peak_i)
{
if (abs((int)(list_best_peaks[best_peak_i].first - list_peaks[peak_i].first)) < pow(2.0, _octave_min + 1) * _delta_freq)
{
if (list_best_peaks[best_peak_i].second <= list_peaks[peak_i].second)
list_best_peaks.erase(list_best_peaks.begin() + best_peak_i);
else
keep_this_peak = false;
break;
}
}
if (keep_this_peak)
list_best_peaks.push_back(list_peaks[peak_i]);
}
delete[] PSD;
return list_best_peaks;
}
double* SpectralRecorder::getDeviationDistribution(size_t lag_max) {
size_t sig = 2;
std::vector<std::pair<size_t, double>> list_peaks = getPeaks(0.05, 80);
for (size_t i = 0; i < 1; ++i) list_peaks.push_back(std::pair<size_t, double>(0, 0)); // Addition of a peak at the zero frequency
sort(list_peaks.begin(), list_peaks.end(), compare_first);
double* deviation_distribution = new double[getPSDSize()];
for (size_t i = 0; i < getPSDSize(); ++i) { deviation_distribution[i] = 0; }
for (size_t peak_i = 0; peak_i < list_peaks.size(); ++peak_i )
{
for (size_t peak_j = std::min(peak_i + 1, list_peaks.size()); peak_j < std::min(peak_i + lag_max, list_peaks.size()); ++peak_j)
{
size_t deviation = (list_peaks[peak_i].first - list_peaks[peak_j].first);
//================ SMOOTHING ================//
for (int delta = -2 * (int)sig; delta < 2 * (int)sig; ++delta)
{
if (deviation + delta > 0 && deviation + delta < getPSDSize())
deviation_distribution[deviation + delta] += 2*sig * Gaussian(sig, delta);
}
//===========================================//
}
}
return deviation_distribution;
}
double SpectralRecorder::getBestFreq()
{
double* deviation_distribution = getDeviationDistribution(3);
std::vector<std::pair<size_t, double>> list_peaks;
for (size_t freq_i = 1; freq_i < _buffer_size / 2 - 1; ++freq_i)
{
if ((deviation_distribution[freq_i] >= deviation_distribution[freq_i + 1] && deviation_distribution[freq_i] >= deviation_distribution[freq_i - 1]) && (deviation_distribution[freq_i] > 0))
{
list_peaks.push_back(std::pair<size_t, float>(freq_i, deviation_distribution[freq_i]));
}
}
delete[] deviation_distribution;
sort(list_peaks.begin(), list_peaks.end(), compare_second);
if (list_peaks.size() > 0)
return list_peaks[0].first * _delta_freq;
else
return 0;
}
std::string SpectralRecorder::getBestNote()
{
double freq = getBestFreq();
std::string note_name("NONE");
if (freq == 0) { return note_name; }
std::map<std::string, double>::iterator old_note = notes[0].begin();
for (size_t octave = 0; octave < 9; ++octave)
{
for (std::map<std::string, double>::iterator note = notes[octave].begin(); note != notes[octave].end(); ++note)
{
if (freq <= note->second && freq > old_note->second)
{
if (note->second - freq < freq - old_note->second)
note_name = note->first;
else
note_name = old_note->first;
break;
}
old_note = note;
}
if (note_name != std::string("NONE")) { break; }
}
return note_name;
}