Skip to content

Commit

Permalink
#91 store sample delta instead of calculating it because this changes…
Browse files Browse the repository at this point in the history
… when the sample reaches the end of the sampling period, thereby changing the value that is removed from the MeanCalculator for the delta, resulting in inflated results.
  • Loading branch information
drmason789 committed Apr 30, 2022
1 parent 72d275b commit 98d2b0a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
7 changes: 3 additions & 4 deletions Trixter.XDream.API/Filters/MeanValueFilterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public MeanValueFilterBase(int periodMilliseconds)
public void Add(double value, DateTimeOffset timestamp)
{
double t = GetSampleTime(timestamp);
Sample newSample = new Sample(t, value);
Sample newSample = new Sample(t, value, value-this.samples.Latest?.Value);
this.samples.Add(newSample);
this.Add(newSample);
this.Trim(t);
Expand All @@ -85,13 +85,12 @@ public void Add(double value, DateTimeOffset timestamp)
this.ClearCachedValues();
}

public void AddDelta(int delta, DateTimeOffset timestamp)
public void AddDelta(double delta, DateTimeOffset timestamp)
{
if (this.samples.Count == 0)
throw new InvalidOperationException("Adding a delta requires an existing sample value.");

double x = this.samples.Latest.Value + delta;
this.Add(x, timestamp);
this.Add(this.samples.Latest.Value + delta, timestamp);
}

/// <summary>
Expand Down
13 changes: 7 additions & 6 deletions Trixter.XDream.API/Filters/Sample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ internal class Sample
protected Sample previous, next;

public double Value;
public double? Delta => this.Value - this.previous?.Value;
public double? Delta;

public double T;

public double WeightedValue => this.Value * this.dT;
Expand All @@ -23,16 +24,15 @@ public Sample Previous
{
get => this.previous;
set
{

{
if (value != null)
{
if (value.Next != null)
throw new InvalidOperationException("Next sample already set.");
else
value.next = this;
}
this.previous = value;
this.previous = value;
}
}
public Sample Next
Expand All @@ -52,9 +52,10 @@ public Sample Next
}


public Sample(double t, double v)
public Sample(double t, double v, double? dv)
{
this.Value = v;
this.Value = v;
this.Delta = dv;

this.T = t;
}
Expand Down

0 comments on commit 98d2b0a

Please sign in to comment.