mathnet / mathnet-filtering

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

LP-FirCoefficients do not sum up to 1 #8

Closed lebalz closed 5 years ago

lebalz commented 7 years ago

The FirCoefficients for a lowpass FIR filter (and probably for the other filter-types aswell) have a bug and do not sum up to 1. Therefor the resulting onlinefilter will process incorrect samples. The following example demonstrates the bug:

var coeffs = FirCoefficients.LowPass(200, 50, 2);
var s = coeffs.Sum(); // s = 1.136619772367581 instead of 1.0
var filt = new OnlineFirFilter(coeffs);
var res = filt.ProcessSamples(new double[] { 100, 100, 100, 100, 100, 100});
/*res:
    [0]: 1.9490215455257391E-15
    [1]: 31.830988618379074
    [2]: 81.830988618379081
    [3]: 113.66197723675815
    [4]: 113.66197723675815
    [5]: 113.66197723675813
*/

when adding the following 2 lines of code after line $78$ in MathNet.Filtering.FIR.FirCoefficients.LowPass it should be fixed:

double s = c.Sum();
for (int i = 0; i < c.Length; i++) { c[i] /= s; }
josesoyo commented 6 years ago

Yesterday I add the lines you specified + the reference to Linq and it works, since I see that there is few activity and you proposed the solution, why don't you create a pull request? I am quite confident it will be accepted

cdrnet commented 5 years ago

Finally pulled in the change - thanks!