Advertisement
prog

FirFilter

Jul 28th, 2011
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.27 KB | None | 0 0
  1.     public class FirFilter : IFilter
  2.     {
  3.         private double[] _coefficients;
  4.         private double[] _queue;
  5.         private int _index;
  6.  
  7.         public FirFilter()
  8.         {
  9.             _coefficients = new[]
  10.                                 {
  11.                                     0, 0.0000717, 0.0001444, 0.0002168, 0.0002873, 0.0003538, 0.0004134, 0.0004627,
  12.                                     0.0004977, 0.0005138, 0.0005067, 0.0004718, 0.0004056, 0.0003052, 0.0001698, 0,
  13.                                     -0.0002009, -0.000427, -0.0006702, -0.0009192, -0.0011609, -0.0013801, -0.0015604,
  14.                                     -0.001685, -0.001738, -0.0017049, -0.0015742, -0.0013381, -0.000994, -0.0005449, 0,
  15.                                     0.0006246, 0.0013064, 0.0020166, 0.0027211, 0.0033818, 0.0039577, 0.0044075, 0.004691,
  16.                                     0.0047721, 0.0046204, 0.0042141, 0.0035416, 0.0026033, 0.0014134, 0, -0.0015943,
  17.                                     -0.003313, -0.0050866, -0.0068351, -0.0084696, -0.0098961, -0.0110186, -0.0117432,
  18.                                     -0.0119819, -0.0116567, -0.0107036, -0.0090761, -0.0067478, -0.0037156, 0, 0.0043531,
  19.                                     0.0092736, 0.0146682, 0.0204226, 0.0264051, 0.0324702, 0.0384635, 0.0442269, 0.0496037,
  20.                                     0.0544445, 0.0586122, 0.061987, 0.0644708, 0.065991, 0.0665028, 0.065991, 0.0644708,
  21.                                     0.061987, 0.0586122, 0.0544445, 0.0496037, 0.0442269, 0.0384635, 0.0324702, 0.0264051,
  22.                                     0.0204226, 0.0146682, 0.0092736, 0.0043531, 0, -0.0037156, -0.0067478, -0.0090761,
  23.                                     -0.0107036, -0.0116567, -0.0119819, -0.0117432, -0.0110186, -0.0098961, -0.0084696,
  24.                                     -0.0068351, -0.0050866, -0.003313, -0.0015943, 0, 0.0014134, 0.0026033, 0.0035416,
  25.                                     0.0042141, 0.0046204, 0.0047721, 0.004691, 0.0044075, 0.0039577, 0.0033818, 0.0027211,
  26.                                     0.0020166, 0.0013064, 0.0006246, 0, -0.0005449, -0.000994, -0.0013381, -0.0015742,
  27.                                     -0.0017049, -0.001738, -0.001685, -0.0015604, -0.0013801, -0.0011609, -0.0009192,
  28.                                     -0.0006702, -0.000427, -0.0002009, 0, 0.0001698, 0.0003052, 0.0004056, 0.0004718,
  29.                                     0.0005067, 0.0005138, 0.0004977, 0.0004627, 0.0004134, 0.0003538, 0.0002873, 0.0002168,
  30.                                     0.0001444, 0.0000717, 0
  31.                                 };
  32.              _queue = new double[_coefficients.Length];
  33.         }
  34.  
  35.         public unsafe double Process(double sample)
  36.         {
  37.             int n = _coefficients.Length;
  38.             double result = 0.0;
  39.             fixed (double* coefficients = _coefficients, queue = _queue)
  40.             {
  41.                 if (--_index < 0)
  42.                     _index = n - 1;
  43.                 queue[_index] = sample;
  44.                 for (int i = 0; i < n; i++)
  45.                 {
  46.                     result += queue[_index] * coefficients[i];
  47.                     if (++_index >= n)
  48.                         _index = 0;
  49.                 }
  50.             }
  51.             return result;
  52.         }
  53.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement