HoloLens 2 Gait Training
The purpose of this project is to create gait training application.
Loading...
Searching...
No Matches
BaseNoiseClass.cs
Go to the documentation of this file.
1using Microsoft.MixedReality.Toolkit.UI;
2using System;
3using System.Collections.Generic;
4using System.Globalization;
5using System.Linq;
6using TMPro;
7using UnityEngine;
8
9public abstract class BaseNoiseClass : MonoBehaviour
10{
11
12 #region Private Varibles
13
15 private GameObject m_NoiseDataPanel = null;
16
17 #endregion
18
19 #region Protected Variables
20
22 protected const float m_Mean = 0.0f;
23
25 protected const float m_NoiseSTD = 1.0f;
26
28 protected float m_MeanPeriod = 1.0f;
29
31 protected float m_SDPeriod = 2.0f;
32
34 protected float m_Multiplier = 0.0f;
35
37 protected int m_SampleSize = 5000;
38
41
43 protected bool m_NoiseAppliedFlag = false;
44
46 protected const float m_DefaultISOWalkSpeed = -1.0f;
47
50
52 protected List<float> m_NoiseValueList = null;
53
55 protected List<float> m_StandardNoiseDistribution = null;
56
58 protected TextMeshPro m_MeanPeriodLabel;
59 protected TextMeshPro m_SDPeriodLabel;
60 protected TextMeshPro m_SampleSizeLabel;
61 protected TextMeshPro m_PreferredSpeedLabel;
62 protected TextMeshPro m_CurrentPattern;
63 protected TextMeshPro m_Title;
64
65 protected GameObject m_ApplyButton;
66 protected GameObject m_DistributionButton;
67
68 #endregion
69
70 #region Public Varibles
71
73 public List<float> NoiseValueList { get => m_NoiseValueList; }
74
76 public bool NoiseAppliedFlag { get => m_NoiseAppliedFlag; }
77
79 public TextMeshPro CurrentPattern { get => m_CurrentPattern; }
80
83
85 public List<float> NoiseDistribution { get => m_StandardNoiseDistribution; }
86
87 #endregion
88
93 protected virtual void Awake()
94 {
95 m_NoiseDataPanel = GameObject.FindGameObjectWithTag("NoiseDataPanel");
97 m_NoiseValueList = new List<float>();
98 m_StandardNoiseDistribution = new List<float>();
101 }
102
107 {
108 if( m_NoiseDataPanel != null )
109 {
110 m_MeanPeriodLabel = m_NoiseDataPanel.transform.Find("MeanPeriod").gameObject.GetComponent<TextMeshPro>();
111 m_SDPeriodLabel = m_NoiseDataPanel.transform.Find("SD_Period").gameObject.GetComponent<TextMeshPro>();
112 m_SampleSizeLabel = m_NoiseDataPanel.transform.Find("SampleSize").gameObject.GetComponent<TextMeshPro>();
113 m_PreferredSpeedLabel = m_NoiseDataPanel.transform.Find("Preferred_Speed").gameObject.GetComponent<TextMeshPro>();
114 m_CurrentPattern = m_NoiseDataPanel.transform.Find("NoiseLbl").gameObject.GetComponent<TextMeshPro>();
115 m_Title = m_NoiseDataPanel.transform.Find("Title").gameObject.GetComponent<TextMeshPro>(); ;
116
117 m_ApplyButton = m_NoiseDataPanel.transform.Find("ApplyPattern").gameObject;
118 m_DistributionButton = m_NoiseDataPanel.transform.Find("NewDistribution").gameObject;
119 }
120 else
121 {
122 Debug.Log("Cannot Find NoiseDataPanel Object");
123 }
124 }
125
131 protected double GetStandardDeviation(ref List<float> basePinkNoiseList)
132 {
133 double ret = 0;
134 int count = basePinkNoiseList.Count();
135
136 if (count > 1)
137 {
138 //Compute the Average
139 double avg = basePinkNoiseList.Average();
140
141 //Perform the Sum of (value-avg)^2
142 double sum = basePinkNoiseList.Sum(d => (d - avg) * (d - avg));
143
144 //Put it all together
145 ret = Math.Sqrt(sum / (count - 1));
146 }
147
148 return ret;
149 }
150
156 protected void ConvertToZScore(ref List<float> basePinkNoiseList)
157 {
158 double mean = basePinkNoiseList.Average();
159 double sd = GetStandardDeviation(ref basePinkNoiseList);
160
161 for (int i = 0; i < basePinkNoiseList.Count; i++)
162 {
163 float val = (float)((basePinkNoiseList[i] - mean) / sd);
165 }
166 }
167
171 protected abstract void CalculateNoise();
172
176 protected abstract void CalculateBaseNoise();
177
182 public abstract void ApplyPattern();
183
188 public virtual void GenerateNewDistribution()
189 {
191 }
192
197 public virtual void CanclePattern()
198 {
199 m_NoiseAppliedFlag = false;
200 m_Title.text = "Pattern reset to default speed ";
201 m_ApplyButton.GetComponent<Interactable>().IsToggled = false;
202 m_DistributionButton.GetComponent<Interactable>().IsToggled = false;
203
204 GameObject avatar = GameObject.FindGameObjectWithTag("Avatar");
205
206 if( avatar != null )
207 {
208 avatar.GetComponent<AvatarAnimationState>().ResetAnimationLengthList();
209 }
210
211 if( m_NoiseValueList.Count > 0 )
212 {
213 m_NoiseValueList.Clear();
214 }
215 }
216
222 protected void SetReadyMessage( bool flag, string lbl )
223 {
224 if( flag == true )
225 {
226 m_Title.text = lbl + " Noise Ready";
227 m_NoiseAppliedFlag = true;
228 }
229 else
230 {
231 m_Title.text = lbl + " Noise is NOT Ready";
232 m_NoiseAppliedFlag = false;
233 }
234 }
235
241 protected double ExtractDecimalFromUI( string textFromUI )
242 {
243 string numberPart = textFromUI.Split().Last();
244 double number = 0.0;
245 bool isValid = double.TryParse( numberPart, System.Globalization.NumberStyles.Any, CultureInfo.InvariantCulture, out number );
246
247 if( isValid == true )
248 {
249 return number;
250 }
251
252 return 0.0;
253 }
254
258 protected virtual void SetUITextVisibility()
259 {
260 m_MeanPeriodLabel.gameObject.SetActive( true );
261 m_SDPeriodLabel.gameObject.SetActive( true );
262 m_SampleSizeLabel.gameObject.SetActive( true );
263 m_PreferredSpeedLabel.gameObject.SetActive( false );
264 }
265
270 protected virtual void PopulateVariablesWithDataFromUI()
271 {
275 }
276}
This class is used to control the avartar animation state.
virtual void PopulateVariablesWithDataFromUI()
Populate data variables used to alter noise. The data are gained through UI lables which are set by t...
TextMeshPro m_PreferredSpeedLabel
abstract void CalculateNoise()
Calculate the colored noise
const float m_NoiseSTD
Hold the standard distribution.
TextMeshPro m_CurrentPattern
float m_MeanPeriod
Hold the Mean Period Value.
void ConvertToZScore(ref List< float > basePinkNoiseList)
This converts Z values to Z Score values. May get off a small amount due to round error.
List< float > m_StandardNoiseDistribution
A List to hold Normal(Gaussian) distribution.
TextMeshPro m_SDPeriodLabel
GameObject m_ApplyButton
int m_SampleSize
Defines how many samples we want.
TextMeshPro m_MeanPeriodLabel
We get the value from these text labels.
float m_SDPeriod
Hold the standard distribution period.
float PreferredWalkingSpeed
Property to get user's preferred walking speed (Read-Only)
virtual void GenerateNewDistribution()
Generate a new normal(Gaussian) distribution Mapped to NoiseDataPanel NewDistribution button.
List< float > m_NoiseValueList
This list stores the calculated colored noise values.
TextMeshPro m_Title
abstract void ApplyPattern()
Calculate the noise according to the user input. Mapped to NoiseDataPanel ApplyPattern button.
double GetStandardDeviation(ref List< float > basePinkNoiseList)
This function calculate the sample Standard Deviation value and return it.
bool NoiseAppliedFlag
Property to get noise applied flag (Read-Only)
bool m_NoiseAppliedFlag
This flag will indicate we applied or cancel the noise to animations.
List< float > NoiseDistribution
Property to get standard normal(Gaussian) distribution (Read-Only)
float m_Multiplier
Used this as a multiplier to calculate pink noise.
TextMeshPro m_SampleSizeLabel
double ExtractDecimalFromUI(string textFromUI)
Get the number part from the UI textfields.
void InitializeNoiseDataPanelObjects()
Cache the references of NoiseDataPanel gameobjects.
GameObject m_NoiseDataPanel
Reference to NoiseDataPanel UI.
float m_PreferredWalkingSpeed
Hold the user's preferred walking speed.
const float m_Mean
Hold the Mean value.
TextMeshPro CurrentPattern
Property to get the current noise in use (Read-Only)
void SetReadyMessage(bool flag, string lbl)
Indicate noise is successgully applied or not.
virtual void Awake()
When invoke the scripts performas all the initializations including, Lists, scripts,...
virtual void CanclePattern()
Cancel the current noise pattern and reset it with default speed. Mapped to NoiseDataPanel CancelPatt...
abstract void CalculateBaseNoise()
Calculate the base colored noise
GaussianDistribution m_GaussianDistribution
Reference to GaussianDistribution script.
List< float > NoiseValueList
Property to get colored noise distribution (Read-Only)
GameObject m_DistributionButton
const float m_DefaultISOWalkSpeed
Default ISO preferred walking speed.
virtual void SetUITextVisibility()
Change the visibility of the text fields according to the noise patterns.
Takes two uniformly distributed deviates within the unit circle, and transforms them into two indepen...