2017-04-25 281 views
1

所以我一直在尝试在C#中使用Mathnet过滤库实现低通滤波器。我有一个问题,因为我不知道如何使用该方法为过滤器创建系数。有人能告诉我如何指定截止频率(它必须以每个单位的采样数)? 例如,如果我希望截止频率为400Hz,那么每个单位的采样数是多少? 谢谢。Mathnet过滤截止频率

public Filter(ISampleProvider _source, long Length, int _cutoff) 
    { 
     source = _source; 
     cutoffFrequency = _cutoff; 

     float[] s = new float[Length]; 
     _source.Read(s, 0, (int)Length); 
     Normalize(s); 

     var coefficients = MathNet.Filtering.FIR.FirCoefficients.LowPass(_source.WaveFormat.SampleRate, (double)400/ ((double)source.WaveFormat.SampleRate/(double)Length), 2); 
     MathNet.Filtering.FIR.OnlineFirFilter filter = new MathNet.Filtering.FIR.OnlineFirFilter(coefficients); 
     double[] output = Array.ConvertAll(s, x => (double)x); 

     double[] output2 = filter.ProcessSamples(output); 

     output1 = new float[output2.Length]; 
     for (int i = 0; i < output2.Length; i++) 
     { 
      output1[i] = Convert.ToSingle(output2[i]); 
     } 

    } 

我试着通过频率分辨率我的信号,但这样的信号似乎并没有做任何改动来划分我想要的frequncy。

回答

0

我最近一直在试用这个库。下面是建立一个FIR滤波器与一窗口函数,以改善它(对于2 MSPS输入和125千赫的截止频率)的一个简单的例子:

  double samplingRate = 2000000; 
      double cutoffFreq = 125000; 
      int filterWidth = 130; 

      var mathNetCoeffs = MathNet.Filtering.FIR.FirCoefficients.LowPass(samplingRate, cutoffFreq, filterWidth/2); 
      MathNet.Filtering.Windowing.BlackmanWindow blackmanWindow = new MathNet.Filtering.Windowing.BlackmanWindow(); 
      blackmanWindow.Width = mathNetCoeffs.Length; 
      var windowArr = blackmanWindow.CopyToArray(); 
      for (int i = 0; i < mathNetCoeffs.Length; i++) mathNetCoeffs[i] *= windowArr[i]; 
      MathNet.Filtering.FIR.OnlineFirFilter mathNetFilter = new MathNet.Filtering.FIR.OnlineFirFilter(mathNetCoeffs); 

窗口函数是用于使一个工作滤波器非常重要的。 海明是另一个流行的选择,但我在这里使用布莱克曼。然后通过调用ProcessSample或ProcessSamples使用过滤器:

double mathNetFiltered = mathNetFilter.ProcessSample(value);

还要注意的是,实际的过滤器宽度将filterWidth + 1,你要实际的过滤器宽度为奇数(良好的过滤对称性),所以设置filterWidth设置为偶数值。