mathnet / mathnet-filtering

Math.NET Filtering (formerly Neodym)
http://filtering.mathdotnet.com
Other
238 stars 82 forks source link

Low Pass filter: different results in versions 0.5.0 and 0.6.0 #15

Open h28669 opened 5 years ago

h28669 commented 5 years ago

I have upgraded our project from Math.Net.Filtering 0.5.0 to 0.6.0 Now I get completely different results with LowPass filter.

Example var frequency = 1d / 360d; var samplingFrequency = 0.1; // Nyquist: sampling rate must be at least double the frequency var amplitude = 1; var seriesLength = 10; var input = Generate.Sinusoidal(seriesLength, samplingFrequency, frequency, amplitude); var filter = new LowPassFilter();

Result with 0.5.0 {0, 0.032, 0.203, 0.4, 0.584, 0.751, 0.895, 1.012, 1.098, 1.151}; Result with 0.6.0 {0, -0.003, -0.004, -0.005, -0.008, -0.007, -0.01, -0.009, -0.009, -0.012}

Which one is correct? Do I need to change something when setting up filter?

Within my wrapper class "LowPassFilter" the filter is created as follows: The values used are: filter.CutOffFrequency = 0.04; filter.OrderOfFilter = 2;

protected override OnlineFilter CreateFilter() { return OnlineFilter.CreateLowpass(ImpulseResponse.Finite, SamplingFrequency, CutOffFrequency.Value, OrderOfFilter); }

Thanks for help Gianni

SOCMarcel commented 4 years ago

Hello,

as far as I unterstand the source code (https://github.com/mathnet/mathnet-filtering/blob/master/src/Filtering/OnlineFilter.cs, https://github.com/mathnet/mathnet-filtering/blob/master/src/Filtering/FIR/FirCoefficients.cs) there seems to be an issue with CreateLowpass(ImpulseResponse mode, double sampleRate, double cutoffRate, int order):

In case of passed mode ImpulseResponse.Finite the following code is executed: double[] c = FirCoefficients.LowPass(sampleRate, cutoffRate, order >> 1); return new OnlineFirFilter(c); Yet as you can see in the history (probably corresponding to version change from 0.5 to 0.6) the signature of FirCoefficients.LowPass(...) changed from public static double[] LowPass(double samplingRate, double cutoff, int halforder = 0) to public static double[] LowPass(double samplingRate, double cutoff, double dcGain = 1.0, int halforder = 0)

So from my point of view the order parameter from CreateLowpass(...) gets now passed as dcGain to FirCoefficients.LowPass(...) instead of halforder which results in incorrect coefficients.

Kind regards. Marcel