-
Notifications
You must be signed in to change notification settings - Fork 10
/
spectrogram.cpp
187 lines (157 loc) · 5.44 KB
/
spectrogram.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
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
#include "spectrogram.h"
#include <assert.h>
#include <iostream>
#include "fftcuda.h"
Spectrogram::Spectrogram(unsigned int _sampleRate,
unsigned int _sampleLength,
unsigned int _samplesPerLine,
unsigned int _numLines) {
sampleRate = _sampleRate;
sampleLength = _sampleLength;
samplesPerLine = _samplesPerLine;
numLines = _numLines;
ringBufferSize = (numLines - 1) * samplesPerLine + sampleLength;
sampleCounter = 0;
waveRingBuffer = new float[ringBufferSize];
std::fill_n(waveRingBuffer, ringBufferSize, 0.0f);
ringBufferInd = 0;
fftSize = 4096;
headTime = 0.0f;
deltaTime = 0.0f;
deltaTime = ((double)samplesPerLine)/((double)sampleRate);
frequencyList.clear();
for (unsigned int indFreq = 0; indFreq < fftSize; indFreq++) {
float freq = ((float)(indFreq)) * ((float)sampleRate) /((float)fftSize);
frequencyList.push_back(freq);
}
}
Spectrogram::~Spectrogram() {
delete [] waveRingBuffer;
waveRingBuffer = 0;
}
unsigned int
Spectrogram::processData(float *buffer,
unsigned int bufferLength) {
unsigned int newLines = 0;
float waveEnvMin = 0, waveEnvMax = 0;
for (unsigned int bufferInd = 0; bufferInd < bufferLength; bufferInd++) {
float value = buffer[bufferInd];
if (value > waveEnvMax) {
waveEnvMax = value;
}
if (value < waveEnvMin) {
waveEnvMin = value;
}
waveRingBuffer[ringBufferInd] = value;
ringBufferInd = (ringBufferInd + 1) % ringBufferSize;
sampleCounter++;
if (sampleCounter == fftSize) {
sampleCounter -= samplesPerLine;
newLines++;
// Fill the fftData array with most recent sample data from the ring buffer:
float *fftAbs = new float[fftSize];
#ifdef CUDA_FFT
float *fftData = new float[fftSize];
#else
std::complex<float> *fftData = new std::complex<float>[fftSize];
#endif
unsigned int startIndex = (ringBufferInd - fftSize + ringBufferSize) % ringBufferSize;
for (unsigned int indBuffer = 0; indBuffer < fftSize; indBuffer++) {
unsigned int sampleIndex = (startIndex + indBuffer) % ringBufferSize;
fftData[indBuffer] = waveRingBuffer[sampleIndex];
//std::cout << sampleIndex << " " << indBuffer << " " << fftData[indBuffer] << std::endl;
}
#ifdef CUDA_FFT
PerformCUDAFFT(fftData, fftData, fftSize);
#else
FFTCompute(fftData, fftSize);
#endif
// Compute the absolute value of each complex Fouerier coefficient and assemble
// them into a array:
for (unsigned int indBuffer = 0; indBuffer < fftSize; indBuffer++) {
fftAbs[indBuffer] = std::abs(fftData[indBuffer]) / ((float)fftSize);
}
// Store the new line in the spectrogram:
addLine(fftAbs, fftSize, waveEnvMin, waveEnvMax);
delete [] fftData;
delete [] fftAbs;
}
}
return newLines;
}
void
Spectrogram::removeFoot(unsigned int numLines) {
for (unsigned int indLine = 0; indLine < numLines; indLine++) {
assert(!spectrogramData.empty());
assert(!timeList.empty());
spectrogramData.pop_front();
timeList.pop_front();
waveEnvelopeMin.pop_front();;
waveEnvelopeMax.pop_front();;
footTime += deltaTime;
}
}
void
Spectrogram::addLine(float *fourierData,
unsigned int dataLength,
float envMin,
float envMax) {
std::vector<float> fourierDataVec;
if (spectrogramData.size() >= numLines) {
removeFoot(spectrogramData.size() - numLines + 1);
}
fourierDataVec.assign(fourierData, fourierData + dataLength);
spectrogramData.push_back(fourierDataVec);
waveEnvelopeMax.push_back(envMax);
waveEnvelopeMin.push_back(envMin);
headTime += deltaTime;
timeList.push_back(headTime);
}
void
Spectrogram::FFTCompute(std::complex<float> *data,
unsigned int dataLength) {
for (unsigned int pos= 0; pos < dataLength; pos++) {
unsigned int mask = dataLength;
unsigned int mirrormask = 1;
unsigned int target = 0;
while (mask != 1) {
mask >>= 1;
if (pos & mirrormask)
target |= mask;
mirrormask <<= 1;
}
if (target > pos) {
std::complex<float> tmp = data[pos];
data[pos] = data[target];
data[target] = tmp;
}
}
for (unsigned int step = 1; step < dataLength; step <<= 1) {
const unsigned int jump = step << 1;
const float delta = M_PI / float(step);
const float sine = sin(delta * 0.5);
const std::complex<float> mult (-2.*sine*sine, sin(delta));
std::complex<float> factor(1.0, 0.0);
for (unsigned int group = 0; group < step; ++group) {
for (unsigned int pair = group; pair < dataLength; pair += jump) {
const unsigned int match = pair + step;
const std::complex<float> prod(factor * data[match]);
data[match] = data[pair] - prod;
data[pair] += prod;
}
factor = mult * factor + factor;
}
}
}
double
Spectrogram::getDeltaTime() {
return deltaTime;
}
double
Spectrogram::getHeadTime() {
return headTime;
}
double
Spectrogram::getFootTime() {
return footTime;
}