AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
VectorRollingStatistics.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 using UnityEngine;
4 
5 namespace HoloToolkit.Unity
6 {
11  {
16 
21 
26 
30  public Vector3 Average;
31 
35  public float ActualSampleCount;
36 
40  private int currentSampleIndex;
41 
45  private Vector3[] samples;
46 
50  private Vector3 cumulativeFrame;
51 
55  private Vector3 cumulativeFrameSquared;
56 
60  private int cumulativeFrameSamples;
61 
66  private int maxSamples;
67 
72  public void Init(int sampleCount)
73  {
74  maxSamples = sampleCount;
75  samples = new Vector3[sampleCount];
76  Reset();
77  }
78 
82  public void Reset()
83  {
84  currentSampleIndex = 0;
85  ActualSampleCount = 0;
86  cumulativeFrame = Vector3.zero;
87  cumulativeFrameSquared = Vector3.zero;
88  cumulativeFrameSamples = 0;
89  CurrentStandardDeviation = 0.0f;
90  StandardDeviationDeltaAfterLatestSample = 0.0f;
91  StandardDeviationsAwayOfLatestSample = 0.0f;
92  Average = Vector3.zero;
93  if (samples != null)
94  {
95  for (int index = 0; index < samples.Length; index++)
96  {
97  samples[index] = Vector3.zero;
98  }
99  }
100  }
101 
106  public void AddSample(Vector3 value)
107  {
108  if (maxSamples == 0)
109  {
110  return;
111  }
112 
113  // remove the old sample from our accumulation
114  Vector3 oldSample = samples[currentSampleIndex];
115  cumulativeFrame -= oldSample;
116  cumulativeFrameSquared -= (oldSample.Mul(oldSample));
117 
118  // Add the new sample to the accumulation
119  samples[currentSampleIndex] = value;
120  cumulativeFrame += value;
121  cumulativeFrameSquared += value.Mul(value);
122 
123  // Keep track of how many samples we have
124  cumulativeFrameSamples++;
125  ActualSampleCount = Mathf.Min(maxSamples, cumulativeFrameSamples);
126 
127  // see how many standard deviations the current sample is from the previous average
128  Vector3 deltaFromAverage = (Average - value);
129  float oldStandardDeviation = CurrentStandardDeviation;
130  StandardDeviationsAwayOfLatestSample = oldStandardDeviation == 0 ? 0 : (deltaFromAverage / oldStandardDeviation).magnitude;
131 
132  // And calculate new averages and standard deviations
133  // (note that calculating a standard deviation of a Vector3 might not
134  // be done properly, but the logic is working for the gaze stabilization scenario)
135  Average = cumulativeFrame / ActualSampleCount;
136  float newStandardDev = Mathf.Sqrt((cumulativeFrameSquared / ActualSampleCount - Average.Mul(Average)).magnitude);
137  StandardDeviationDeltaAfterLatestSample = oldStandardDeviation - newStandardDev;
138  CurrentStandardDeviation = newStandardDev;
139 
140  // update the next list position
141  currentSampleIndex = (currentSampleIndex + 1) % maxSamples;
142  }
143  }
144 }
float CurrentStandardDeviation
Current standard deviation of the positions of the vectors
float StandardDeviationsAwayOfLatestSample
How many standard deviations the latest sample was away.
float StandardDeviationDeltaAfterLatestSample
Difference to standardDeviation when the latest sample was added.
Vector Statistics used in gaze stabilization.
float ActualSampleCount
The number of samples in the current set (may be 0 - maxSamples)
void Init(int sampleCount)
Initialize the rolling stats.
void AddSample(Vector3 value)
Adds a new sample to the sample list and updates the stats.