mirror of
				https://github.com/DerTyp7/defrain-shooter-unity.git
				synced 2025-10-30 21:17:09 +01:00 
			
		
		
		
	CHANGED TO MIRROR
This commit is contained in:
		
							
								
								
									
										37
									
								
								Assets/Mirror/Runtime/ExponentialMovingAverage.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								Assets/Mirror/Runtime/ExponentialMovingAverage.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,37 @@ | ||||
| namespace Mirror | ||||
| { | ||||
|     // implementation of N-day EMA | ||||
|     // it calculates an exponential moving average roughly equivalent to the last n observations | ||||
|     // https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average | ||||
|     public class ExponentialMovingAverage | ||||
|     { | ||||
|         readonly float alpha; | ||||
|         bool initialized; | ||||
|  | ||||
|         public double Value { get; private set; } | ||||
|         public double Var { get; private set; } | ||||
|  | ||||
|         public ExponentialMovingAverage(int n) | ||||
|         { | ||||
|             // standard N-day EMA alpha calculation | ||||
|             alpha = 2.0f / (n + 1); | ||||
|         } | ||||
|  | ||||
|         public void Add(double newValue) | ||||
|         { | ||||
|             // simple algorithm for EMA described here: | ||||
|             // https://en.wikipedia.org/wiki/Moving_average#Exponentially_weighted_moving_variance_and_standard_deviation | ||||
|             if (initialized) | ||||
|             { | ||||
|                 double delta = newValue - Value; | ||||
|                 Value += alpha * delta; | ||||
|                 Var = (1 - alpha) * (Var + alpha * delta * delta); | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 Value = newValue; | ||||
|                 initialized = true; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 DerTyp187
					DerTyp187