-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathFFTOutSignal.cs
129 lines (107 loc) · 3.53 KB
/
FFTOutSignal.cs
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
using System;
using System.Runtime.InteropServices;
using FftSharp;
using NAudio.Utils;
namespace VL.Audio
{
public enum FFTWindowFunction
{
None,
Hamming,
Hann,
BlackmannHarris
}
public class FFTOutSignal : SinkSignal
{
protected CircularBuffer FRingBuffer = new CircularBuffer(512);
public FFTOutSignal(AudioSignal input)
{
InputSignal.Value = input;
FRingBuffer.BufferFilled = CalcFFT;
}
public int Size
{
get
{
return FRingBuffer.Size;
}
set
{
FRingBuffer.Size = value;
}
}
public float DBRange
{
get => FdBRange;
set
{
FdBRange = value;
FMindB = (float)Decibels.DecibelsToLinear(-FdBRange);
}
}
float FMindB = (float)Decibels.DecibelsToLinear(-120);
public float Smoothing { get; set; }
WindowFunction windowFunc;
public WindowFunction WindowFunc
{
get
{
return windowFunc;
}
set
{
if (windowFunc != value)
{
windowFunc = value;
FWindow = AudioUtils.CreateWindowDouble(FRingBuffer.Size, WindowFunc);
}
}
}
double[] FFFTBuffer = new double[1];
Complex[] FFFTComplexBuffer = new Complex[1];
public float[] FFTOut = new float[2];
public double[] FFTOutRealVal = new double[2];
public double[] FFTOutImagVal = new double[2];
double[] FWindow = new double[1];
private float FdBRange;
protected override void FillBuffer(float[] buffer, int offset, int count)
{
if (InputSignal.Value != null)
{
InputSignal.Read(buffer, offset, count);
//calc fft
var fftSize = FRingBuffer.Size;
if (FFFTBuffer.Length != fftSize)
{
FFFTBuffer = new double[fftSize];
FFFTComplexBuffer = new Complex[fftSize];
FFTOut = new float[fftSize/2];
FFTOutRealVal = new double[fftSize / 2];
FFTOutImagVal = new double[fftSize / 2];
FWindow = AudioUtils.CreateWindowDouble(fftSize, WindowFunc);
}
//write to buffer
FRingBuffer.Write(buffer, offset, count);
}
}
void CalcFFT(float[] ringbufferData)
{
var fftSize = FRingBuffer.Size;
FRingBuffer.ReadDoubleWindowed(FFFTBuffer, FWindow, 0, fftSize);
var complex = new Span<Complex>(FFFTComplexBuffer);
Transform.MakeComplex(complex, new Span<double>(FFFTBuffer));
// Calculate the FFT as an array of complex numbers
Transform.FFT(complex);
var halfSize = fftSize/2;
FFTOut[0] = 0;
for (int n = 1; n < halfSize; n++)
{
var lastValue = FFTOut[n];
var newValue = (float)Decibels.LinearToDecibels(Math.Max(complex[n].MagnitudeSquared, FMindB)) / FdBRange + 1;
FFTOut[n] = newValue * (1 - Smoothing) + lastValue * Smoothing;
FFTOutRealVal[n] = complex[n].Real;
FFTOutImagVal[n] = complex[n].Imaginary;
}
}
}
}