Created
March 17, 2017 10:10
-
-
Save mikasjp/a6b616799e8ddec258eeabe9d00678df to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace InertialNavigationSystem | |
{ | |
public class FIRFilter: IFilter | |
{ | |
private List<Sample> Memory { get; set; } | |
private List<double> Coefficients { get; set; } | |
public FIRFilter(List<double> FilterCoefficients) | |
{ | |
double sum = 0; | |
foreach(double Coefficient in FilterCoefficients) | |
sum += Coefficient; | |
if (sum != 1) | |
throw new Exception("Sum of coefficients must be 1."); | |
Coefficients = FilterCoefficients; | |
Memory = new List<Sample>(Coefficients.Count); | |
} | |
public Sample AddSample(Sample sample) | |
{ | |
if (Memory.Count == Memory.Capacity) | |
Memory.Remove(Memory.Last()); | |
Memory.Insert(0, sample); | |
Sample resultSample = new Sample(sample.Time, 0); | |
for(int i=0; i<Memory.Count; i++) | |
resultSample.Value += Memory[i].Value * Coefficients[i]; | |
return resultSample; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment