-
Notifications
You must be signed in to change notification settings - Fork 0
/
FFTFilter.cs
executable file
·98 lines (89 loc) · 3.03 KB
/
FFTFilter.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
using System;
using System.Numerics;
namespace ffteq
{
class FFTFilter : Effect
{
private bool lowPass;
private double hzFrom;
private double hzTo;
/// <summary>
/// Given total size of FFT and sample rate, converts hz into index of
/// the corresponding FFT bin.
/// </summary>
private int HzToBinNum(double hz, int sampleRate, int binsTotal)
{
return (int) (hz / sampleRate * binsTotal);
}
private Complex[] DoubleToComplex(double[] xs)
{
Complex[] result = new Complex[xs.Length];
for (int i = 0; i < xs.Length; i++)
{
result[i] = new Complex(xs[i], 0);
}
return result;
}
private double[] ComplexToDouble(Complex[] xs)
{
double[] result = new Double[xs.Length];
for (int i = 0; i < xs.Length; i++)
{
result[i] = xs[i].Real;
}
return result;
}
public FFTFilter(bool lowPass, double hzFrom, double hzTo)
{
this.lowPass = lowPass;
this.hzFrom = hzFrom;
this.hzTo = hzTo;
}
public override Signal Process(Signal inSignal)
{
int n = inSignal.SampleNum;
int sampleRate = inSignal.SampleRate;
int binFrom = HzToBinNum(hzFrom, sampleRate, n);
int binTo = HzToBinNum(hzTo, sampleRate, n);
Complex[] inSamples = DoubleToComplex(inSignal.Data);
Complex[] bins = FFT.DoFFT(inSamples);
if (lowPass)
{
// Create linear slope
int slopeLen = binTo - binFrom;
for (int i = binFrom; i < Math.Min(binTo, n / 2); i++)
{
double coef = 1.0 - ((double) (i - binFrom)) / slopeLen;
bins[i] *= coef;
bins[n - 1 - i] *= coef;
}
// Fully filter out
for (int i = binTo; i < n / 2; i++)
{
bins[i] = new Complex(0, 0);
bins[n - 1 - i] = new Complex(0, 0);
}
}
else // High pass
{
// Fully filter out
for (int i = 0; i < Math.Min(binFrom, n / 2); i++)
{
bins[i] = new Complex(0, 0);
bins[n - 1 - i] = new Complex(0, 0);
}
// Create linear slope
int slopeLen = binTo - binFrom;
for (int i = binFrom; i < Math.Min(binTo, n / 2); i++)
{
double coef = ((double) (i - binFrom)) / slopeLen;
bins[i] *= coef;
bins[n - 1 - i] *= coef;
}
}
double[] outSamples = ComplexToDouble(FFT.DoIFFT(bins));
Signal outSignal = new Signal(outSamples, sampleRate);
return outSignal;
}
}
}