/// <summary> /// Runs a single sample through a Filter with initialState /// </summary> /// <param name="value"> The input value</param> /// <param name="initialState">The initial state of the filter </param> /// <param name="finalState"> The final State of the Filter</param> /// <returns> the value of the filtered signal</returns> public double Filt(double value, FilterState initialState, out FilterState finalState) { double[] s = initialState.StateValue; if (s.Length < (m_A.Length + m_B.Length - 2)) { s = new double[(m_A.Length + m_B.Length - 2) - s.Length]; Array.Fill(s, 0.0D); s = initialState.StateValue.Concat(s).ToArray(); } double fx = value * m_B[0] + m_B.Select((z, i) => (i > 0? z * s[i - 1] : 0.0D)).Sum(); fx += m_A.Select((z, i) => (i > 0 ? z * -s[i + m_B.Length - 2] : 0.0D)).Sum(); fx = fx / m_A[0]; finalState = new FilterState() { StateValue = new double[] { value } .Concat(s.Take(m_B.Length - 2).ToArray()) .Concat(new double[] { fx }) .Concat(s.Skip(m_B.Length - 1).Take(m_A.Length - 2).ToArray()) .ToArray() }; return(fx * m_gain); }
/// <summary> /// Runs an evenly sampled signal through the Filter /// </summary> /// <param name="signal"> f(t) for the signal </param> /// <returns></returns> public double[] Filt(double[] signal) { int n = signal.Count(); double[] output = new double[n]; FilterState state = new FilterState(); for (int i = 0; i < n; i++) { output[i] = Filt(signal[i], state, out state); } return(output); }