| 
									
										
										
										
											2018-07-01 02:16:59 +02:00
										 |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | // Copyright (C) 2018 F4EXB                                                      //
 | 
					
						
							|  |  |  | // written by Edouard Griffiths                                                  //
 | 
					
						
							|  |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // This program is free software; you can redistribute it and/or modify          //
 | 
					
						
							|  |  |  | // it under the terms of the GNU General Public License as published by          //
 | 
					
						
							|  |  |  | // the Free Software Foundation as version 3 of the License, or                  //
 | 
					
						
							|  |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful,               //
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of                //
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                  //
 | 
					
						
							|  |  |  | // GNU General Public License V3 for more details.                               //
 | 
					
						
							|  |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU General Public License             //
 | 
					
						
							|  |  |  | // along with this program. If not, see <http://www.gnu.org/licenses/>.          //
 | 
					
						
							|  |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef SDRBASE_UTIL_FIXEDAVERAGE2D_H_
 | 
					
						
							|  |  |  | #define SDRBASE_UTIL_FIXEDAVERAGE2D_H_
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <algorithm>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<typename T> | 
					
						
							|  |  |  | class FixedAverage2D | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2018-10-12 08:47:14 +02:00
										 |  |  |     FixedAverage2D() : m_sum(0), m_maxSize(0), m_width(0), m_size(0), m_maxIndex(0) {} | 
					
						
							| 
									
										
										
										
											2018-07-01 02:16:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ~FixedAverage2D() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (m_sum) { | 
					
						
							|  |  |  |             delete[] m_sum; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void resize(unsigned int width, unsigned int size) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-10-12 08:47:14 +02:00
										 |  |  |         if (width > m_maxSize) | 
					
						
							| 
									
										
										
										
											2018-07-01 02:16:59 +02:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-10-12 08:47:14 +02:00
										 |  |  |             m_maxSize = width; | 
					
						
							| 
									
										
										
										
											2018-07-01 02:16:59 +02:00
										 |  |  |             if (m_sum) { | 
					
						
							|  |  |  |                 delete[] m_sum; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-10-12 08:47:14 +02:00
										 |  |  |             m_sum = new T[m_maxSize]; | 
					
						
							| 
									
										
										
										
											2018-07-01 02:16:59 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         m_width = width; | 
					
						
							|  |  |  |         m_size = size; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         std::fill(m_sum, m_sum+m_width, 0); | 
					
						
							| 
									
										
										
										
											2018-10-12 08:47:14 +02:00
										 |  |  |         m_maxIndex = 0; | 
					
						
							| 
									
										
										
										
											2018-07-01 02:16:59 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool storeAndGetAvg(T& avg, T v, unsigned int index) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (m_size <= 1) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             avg = v; | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         m_sum[index] += v; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-12 08:47:14 +02:00
										 |  |  |         if (m_maxIndex == m_size - 1) | 
					
						
							| 
									
										
										
										
											2018-07-01 02:16:59 +02:00
										 |  |  |         { | 
					
						
							|  |  |  |             avg = m_sum[index]/m_size; | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool storeAndGetSum(T& sum, T v, unsigned int index) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (m_size <= 1) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             sum = v; | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         m_sum[index] += v; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-12 08:47:14 +02:00
										 |  |  |         if (m_maxIndex < m_size - 1) | 
					
						
							| 
									
										
										
										
											2018-07-01 02:16:59 +02:00
										 |  |  |         { | 
					
						
							|  |  |  |             sum = m_sum[index]; | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool nextAverage() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-07-01 04:10:36 +02:00
										 |  |  |         if (m_size <= 1) { | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-12 08:47:14 +02:00
										 |  |  |         if (m_maxIndex == m_size - 1) | 
					
						
							| 
									
										
										
										
											2018-07-01 02:16:59 +02:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-10-12 08:47:14 +02:00
										 |  |  |             m_maxIndex = 0; | 
					
						
							| 
									
										
										
										
											2018-07-01 02:16:59 +02:00
										 |  |  |             std::fill(m_sum, m_sum+m_width, 0); | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-10-12 08:47:14 +02:00
										 |  |  |             m_maxIndex++; | 
					
						
							| 
									
										
										
										
											2018-07-01 02:16:59 +02:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     T *m_sum; | 
					
						
							| 
									
										
										
										
											2018-10-12 08:47:14 +02:00
										 |  |  |     unsigned int m_maxSize; | 
					
						
							| 
									
										
										
										
											2018-07-01 02:16:59 +02:00
										 |  |  |     unsigned int m_width; | 
					
						
							|  |  |  |     unsigned int m_size; | 
					
						
							| 
									
										
										
										
											2018-10-12 08:47:14 +02:00
										 |  |  |     unsigned int m_maxIndex; | 
					
						
							| 
									
										
										
										
											2018-07-01 02:16:59 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif /* SDRBASE_UTIL_FIXEDAVERAGE2D_H_ */
 |