| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | #ifndef INCLUDE_MOVINGAVERAGE_H
 | 
					
						
							|  |  |  | #define INCLUDE_MOVINGAVERAGE_H
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-26 02:01:29 +02:00
										 |  |  | #include <stdint.h>
 | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | #include <vector>
 | 
					
						
							| 
									
										
										
										
											2017-01-15 11:26:11 +01:00
										 |  |  | #include <algorithm>
 | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | #include "dsp/dsptypes.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-28 00:58:26 +01:00
										 |  |  | template<typename Type> class MovingAverage { | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2017-05-11 18:39:00 +02:00
										 |  |  | 	MovingAverage(int historySize, Type initial) | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2017-05-11 18:39:00 +02:00
										 |  |  | 	    resize(historySize, initial); | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-20 09:28:57 +02:00
										 |  |  | 	void resize(int historySize, Type initial) | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		m_history.resize(historySize); | 
					
						
							| 
									
										
										
										
											2017-01-15 11:26:11 +01:00
										 |  |  | 		std::fill(m_history.begin(), m_history.end(), initial); | 
					
						
							| 
									
										
										
										
											2016-08-26 02:01:29 +02:00
										 |  |  | 		m_sum = (Type) m_history.size() * initial; | 
					
						
							|  |  |  | 		m_index = 0; | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-20 09:28:57 +02:00
										 |  |  | 	void feed(Type value) | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2016-08-26 02:01:29 +02:00
										 |  |  |         Type& oldest = m_history[m_index]; | 
					
						
							|  |  |  |         m_sum += value - oldest; | 
					
						
							|  |  |  |         oldest = value; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-15 11:26:11 +01:00
										 |  |  |         if (m_index < m_history.size()) { | 
					
						
							|  |  |  |             m_index++; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             m_index = 0; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-20 09:28:57 +02:00
										 |  |  | 	void fill(Type value) | 
					
						
							| 
									
										
										
										
											2015-05-12 12:12:13 +02:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2017-01-15 11:26:11 +01:00
										 |  |  |         std::fill(m_history.begin(), m_history.end(), value); | 
					
						
							| 
									
										
										
										
											2016-08-26 02:01:29 +02:00
										 |  |  | 		m_sum = (Type) m_history.size() * value; | 
					
						
							| 
									
										
										
										
											2015-05-12 12:12:13 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-20 09:28:57 +02:00
										 |  |  | 	Type average() const | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2016-08-26 02:01:29 +02:00
										 |  |  | 		return m_sum / (Type) m_history.size(); | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-13 11:56:14 +02:00
										 |  |  | 	Type sum() const | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return m_sum; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | protected: | 
					
						
							| 
									
										
										
										
											2015-06-20 09:28:57 +02:00
										 |  |  | 	std::vector<Type> m_history; | 
					
						
							|  |  |  | 	Type m_sum; | 
					
						
							| 
									
										
										
										
											2016-08-26 02:01:29 +02:00
										 |  |  | 	uint32_t m_index; | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif // INCLUDE_MOVINGAVERAGE_H
 |