-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspectrum_analyser.cmajor
132 lines (115 loc) · 4.29 KB
/
spectrum_analyser.cmajor
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
namespace spectrum_analyser
{
let DFT_BUFFER_SIZE = 2048;
let MAX_MEMORY_FRAMES = 15;
struct Spectrum
{
float[DFT_BUFFER_SIZE / 2] magnitudes;
float[DFT_BUFFER_SIZE / 2] peakMagnitudes;
}
graph Analyser [[main]]
{
input stream float<2> audioIn;
input dft.windowType;
input dft.peakMemory;
output stream float<2> audioOut;
output dft.spectrum;
output dft.frequencies;
node dft = DFT;
// audio routing
connection
{
audioIn -> audioOut, dft.stereoWave;
}
}
processor DFT
{
input stream float<2> stereoWave;
input event int windowType [[ name: "Window Type", min: 0, max: 3, init: 1, text: "Rectangular|Hann|Bartlett|Hamming", automatable: true ]];
input value int peakMemory [[ name: "Peak Memory", min: 1, max: MAX_MEMORY_FRAMES, step: 1, init: 5, automatable: true]];
output event Spectrum spectrum;
output event float[fftSize] frequencies;
float[windowSize] window;
event windowType(int type)
{
if (type == 0)
{
// Rectangular window
for (wrap<windowSize> i)
window[i] = 1.0f;
}
else if (type == 1)
{
// Hann window
for (wrap<windowSize> i)
window[i] = float(0.5f * (1.0f - cos(2.0f * pi * i / (windowSize - 1))));
}
else if (type == 2)
{
// Bartlett window
for (wrap<windowSize> i)
window[i] = 1.0f - 2.0f * abs(i - windowSize / 2) / windowSize;
}
else
{
// Hamming window
for (wrap<windowSize> i)
window[i] = float(0.54f - 0.46f * cos(2.0f * pi * i / (windowSize - 1)));
}
}
float normalizationFactor = 1.0f / windowSize;
let fftSize = DFT_BUFFER_SIZE / 2;
let windowSize = DFT_BUFFER_SIZE;
let fftsPerSecond = 20;
let framesBetweenFtts = (processor.frequency / fftsPerSecond) - windowSize;
void main()
{
float[fftSize] dftFrequencies;
for (wrap<fftSize> binIdx)
{
dftFrequencies[binIdx] = float(binIdx * processor.frequency / windowSize);
}
frequencies <- dftFrequencies;
wrap<MAX_MEMORY_FRAMES> memoryIdx = 0;
float[DFT_BUFFER_SIZE, MAX_MEMORY_FRAMES] magnitudeMemory;
float[DFT_BUFFER_SIZE] waveBuffer;
float[DFT_BUFFER_SIZE] complexSpectrum;
Spectrum magSpectrum;
loop
{
++memoryIdx;
for (wrap<windowSize> counter)
{
waveBuffer[counter] = (stereoWave[0] + stereoWave[1]) / 2.0f * window[counter];
advance();
}
// Calculate the spectrum
std::frequency::realOnlyForwardFFT(waveBuffer, complexSpectrum);
for (wrap<fftSize> binIdx)
{
const float real = complexSpectrum[binIdx];
const float imag = complexSpectrum.at(fftSize + binIdx);
magSpectrum.magnitudes[binIdx] = sqrt(real * real + imag * imag) * normalizationFactor * 1000.0f;
magnitudeMemory[binIdx, memoryIdx] = magSpectrum.magnitudes[binIdx];
}
// Calculate peak magnitude spectrum
for (wrap<fftSize> binIdx)
{
float peak = 0.0f;
wrap<MAX_MEMORY_FRAMES> frameIdx = memoryIdx;
for (int frameCount = 0; frameCount < peakMemory; ++frameCount)
{
--frameIdx;
if (magnitudeMemory[binIdx, frameIdx] > peak)
peak = magnitudeMemory[binIdx, frameIdx];
}
magSpectrum.peakMagnitudes[binIdx] = peak;
}
spectrum <- magSpectrum;
if const (framesBetweenFtts > 0)
loop (framesBetweenFtts)
advance();
}
}
}
}