Skip to content

Instantly share code, notes, and snippets.

@mikasjp
Created March 17, 2017 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikasjp/a6b616799e8ddec258eeabe9d00678df to your computer and use it in GitHub Desktop.
Save mikasjp/a6b616799e8ddec258eeabe9d00678df to your computer and use it in GitHub Desktop.
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