2017-04-25 53 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設置爲偶數值。