-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathOnlineFilter.cs
More file actions
226 lines (202 loc) · 8.31 KB
/
OnlineFilter.cs
File metadata and controls
226 lines (202 loc) · 8.31 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
225
226
// <copyright file="OnlineFilter.cs" company="Math.NET">
// Math.NET Filtering, part of the Math.NET Project
// http://filtering.mathdotnet.com
// http://github.com/mathnet/mathnet-filtering
//
// Copyright (c) 2009-2014 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using System;
using MathNet.Filtering.FIR;
using MathNet.Filtering.IIR;
using MathNet.Filtering.Median;
namespace MathNet.Filtering
{
/// <summary>
/// Online filters allow processing incoming samples immediately and hence
/// provide a nearly real-time response with a fixed delay.
/// </summary>
public abstract class OnlineFilter : IOnlineFilter
{
/// <summary>
/// Create a filter to remove high frequencies in online processing scenarios.
/// </summary>
public static OnlineFilter CreateLowpass(ImpulseResponse mode, double sampleRate, double cutoffRate, int order)
{
if (mode == ImpulseResponse.Finite)
{
double[] c = FirCoefficients.LowPass(sampleRate, cutoffRate, 1, order >> 1);
return new OnlineFirFilter(c);
}
if (mode == ImpulseResponse.Infinite)
{
// TODO: investigate (bandwidth)
double[] c = IirCoefficients.LowPass(sampleRate, cutoffRate, cutoffRate);
return new OnlineIirFilter(c);
}
throw new ArgumentException("mode");
}
/// <summary>
/// Create a filter to remove high frequencies in online processing scenarios.
/// </summary>
public static OnlineFilter CreateLowpass(ImpulseResponse mode, double sampleRate, double cutoffRate)
{
return CreateLowpass(
mode,
sampleRate,
cutoffRate,
mode == ImpulseResponse.Finite ? 64 : 4); // order
}
/// <summary>
/// Create a filter to remove low frequencies in online processing scenarios.
/// </summary>
public static OnlineFilter CreateHighpass(ImpulseResponse mode, double sampleRate, double cutoffRate, int order)
{
if (mode == ImpulseResponse.Finite)
{
double[] c = FirCoefficients.HighPass(sampleRate, cutoffRate, order >> 1);
return new OnlineFirFilter(c);
}
if (mode == ImpulseResponse.Infinite)
{
// TODO: investigate (bandwidth)
double[] c = IirCoefficients.HighPass(sampleRate, cutoffRate, cutoffRate);
return new OnlineIirFilter(c);
}
throw new ArgumentException("mode");
}
/// <summary>
/// Create a filter to remove low frequencies in online processing scenarios.
/// </summary>
public static OnlineFilter CreateHighpass(ImpulseResponse mode, double sampleRate, double cutoffRate)
{
return CreateHighpass(
mode,
sampleRate,
cutoffRate,
mode == ImpulseResponse.Finite ? 64 : 4); // order
}
/// <summary>
/// Create a filter to remove low and high frequencies in online processing scenarios.
/// </summary>
public static OnlineFilter CreateBandpass(ImpulseResponse mode, double sampleRate, double cutoffLowRate, double cutoffHighRate, int order)
{
if (mode == ImpulseResponse.Finite)
{
double[] c = FirCoefficients.BandPass(sampleRate, cutoffLowRate, cutoffHighRate, order >> 1);
return new OnlineFirFilter(c);
}
if (mode == ImpulseResponse.Infinite)
{
double[] c = IirCoefficients.BandPass(sampleRate, cutoffLowRate, cutoffHighRate);
return new OnlineIirFilter(c);
}
throw new ArgumentException("mode");
}
/// <summary>
/// Create a filter to remove low and high frequencies in online processing scenarios.
/// </summary>
public static OnlineFilter CreateBandpass(ImpulseResponse mode, double sampleRate, double cutoffLowRate, double cutoffHighRate)
{
return CreateBandpass(
mode,
sampleRate,
cutoffLowRate,
cutoffHighRate,
mode == ImpulseResponse.Finite ? 64 : 4); // order
}
/// <summary>
/// Create a filter to remove middle (all but low and high) frequencies in online processing scenarios.
/// </summary>
public static OnlineFilter CreateBandstop(ImpulseResponse mode, double sampleRate, double cutoffLowRate, double cutoffHighRate, int order)
{
if (mode == ImpulseResponse.Finite)
{
double[] c = FirCoefficients.BandStop(sampleRate, cutoffLowRate, cutoffHighRate, order >> 1);
return new OnlineFirFilter(c);
}
if (mode == ImpulseResponse.Infinite)
{
double[] c = IirCoefficients.BandStop(sampleRate, cutoffLowRate, cutoffHighRate);
return new OnlineIirFilter(c);
}
throw new ArgumentException("mode");
}
/// <summary>
/// Create a filter to remove middle (all but low and high) frequencies in online processing scenarios.
/// </summary>
public static OnlineFilter CreateBandstop(ImpulseResponse mode, double sampleRate, double cutoffLowRate, double cutoffHighRate)
{
return CreateBandstop(
mode,
sampleRate,
cutoffLowRate,
cutoffHighRate,
mode == ImpulseResponse.Finite ? 64 : 4); // order
}
/// <summary>
/// Create a filter to remove noise in online processing scenarios.
/// </summary>
/// <param name="order">
/// Window Size, should be odd. A larger number results in a smoother
/// response but also in a longer delay.
/// </param>
/// <remarks>The de-noise filter is implemented as an unweighted median filter.</remarks>
public static OnlineFilter CreateDenoise(int order)
{
return new OnlineMedianFilter(order);
}
/// <summary>
/// Create a filter to remove noise in online processing scenarios.
/// </summary>
/// <remarks>The de-noise filter is implemented as an unweighted median filter.</remarks>
public static OnlineFilter CreateDenoise()
{
return CreateDenoise(7);
}
/// <summary>
/// Process a single sample.
/// </summary>
public abstract double ProcessSample(double sample);
/// <summary>
/// Reset internal state (not coefficients!).
/// </summary>
public abstract void Reset();
/// <summary>
/// Process a sequence of sample.
/// </summary>
public virtual double[] ProcessSamples(double[] samples)
{
if (null == samples)
{
return null;
}
double[] ret = new double[samples.Length];
for (int i = 0; i < samples.Length; i++)
{
ret[i] = ProcessSample(samples[i]);
}
return ret;
}
}
}