| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  | /*  meter.c
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This file is part of a program that implements a Software-Defined Radio. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Copyright (C) 2013 Warren Pratt, NR0V | 
					
						
							|  |  |  | Copyright (C) 2024 Edouard Griffiths, F4EXB Adapted to SDRangel | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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; either version 2 | 
					
						
							|  |  |  | of the License, or (at your option) any later version. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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 for more details. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  | along with this program; if not, write to the Free Software | 
					
						
							|  |  |  | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The author can be reached by email at | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | warren@wpratt.com | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "comm.hpp"
 | 
					
						
							|  |  |  | #include "meterlog10.hpp"
 | 
					
						
							|  |  |  | #include "meter.hpp"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace WDSP { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-24 15:32:21 +02:00
										 |  |  | void METER::calc() | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-07-24 15:32:21 +02:00
										 |  |  |     mult_average = exp(-1.0 / (rate * tau_average)); | 
					
						
							|  |  |  |     mult_peak = exp(-1.0 / (rate * tau_peak_decay)); | 
					
						
							|  |  |  |     flush(); | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-24 15:32:21 +02:00
										 |  |  | METER::METER( | 
					
						
							|  |  |  |     int _run, | 
					
						
							|  |  |  |     int* _prun, | 
					
						
							|  |  |  |     int _size, | 
					
						
							|  |  |  |     float* _buff, | 
					
						
							|  |  |  |     int _rate, | 
					
						
							|  |  |  |     double _tau_av, | 
					
						
							|  |  |  |     double _tau_decay, | 
					
						
							|  |  |  |     double* _result, | 
					
						
							|  |  |  |     int _enum_av, | 
					
						
							|  |  |  |     int _enum_pk, | 
					
						
							|  |  |  |     int _enum_gain, | 
					
						
							|  |  |  |     double* _pgain | 
					
						
							| 
									
										
										
										
											2024-08-03 11:05:12 +02:00
										 |  |  | ) : | 
					
						
							|  |  |  |     run(_run), | 
					
						
							|  |  |  |     prun(_prun), | 
					
						
							|  |  |  |     size(_size), | 
					
						
							|  |  |  |     buff(_buff), | 
					
						
							|  |  |  |     rate((double) _rate), | 
					
						
							|  |  |  |     tau_average(_tau_av), | 
					
						
							|  |  |  |     tau_peak_decay(_tau_decay), | 
					
						
							|  |  |  |     result(_result), | 
					
						
							|  |  |  |     enum_av(_enum_av), | 
					
						
							|  |  |  |     enum_pk(_enum_pk), | 
					
						
							|  |  |  |     enum_gain(_enum_gain), | 
					
						
							|  |  |  |     pgain(_pgain) | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-07-24 15:32:21 +02:00
										 |  |  |     calc(); | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-24 15:32:21 +02:00
										 |  |  | void METER::flush() | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-07-24 15:32:21 +02:00
										 |  |  |     avg  = 0.0; | 
					
						
							|  |  |  |     peak = 0.0; | 
					
						
							|  |  |  |     result[enum_av] = -100.0; | 
					
						
							|  |  |  |     result[enum_pk] = -100.0; | 
					
						
							| 
									
										
										
										
											2024-07-14 21:51:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-03 11:05:12 +02:00
										 |  |  |     if ((pgain != nullptr) && (enum_gain >= 0)) | 
					
						
							| 
									
										
										
										
											2024-07-24 15:32:21 +02:00
										 |  |  |         result[enum_gain] = 0.0; | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-24 15:32:21 +02:00
										 |  |  | void METER::execute() | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     int srun; | 
					
						
							| 
									
										
										
										
											2024-07-13 23:59:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-03 11:05:12 +02:00
										 |  |  |     if (prun != nullptr) | 
					
						
							|  |  |  |         srun = *prun; | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  |     else | 
					
						
							|  |  |  |         srun = 1; | 
					
						
							| 
									
										
										
										
											2024-07-13 23:59:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-24 15:32:21 +02:00
										 |  |  |     if (run && srun) | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         double smag; | 
					
						
							|  |  |  |         double np = 0.0; | 
					
						
							| 
									
										
										
										
											2024-07-13 23:59:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-03 11:05:12 +02:00
										 |  |  |         for (int i = 0; i < size; i++) | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2024-07-24 15:32:21 +02:00
										 |  |  |             double xr = buff[2 * i + 0]; | 
					
						
							|  |  |  |             double xi = buff[2 * i + 1]; | 
					
						
							| 
									
										
										
										
											2024-07-17 00:39:28 +02:00
										 |  |  |             smag = xr*xr + xi*xi; | 
					
						
							| 
									
										
										
										
											2024-07-24 15:32:21 +02:00
										 |  |  |             avg = avg * mult_average + (1.0 - mult_average) * smag; | 
					
						
							|  |  |  |             peak *= mult_peak; | 
					
						
							| 
									
										
										
										
											2024-07-14 21:51:22 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (smag > np) | 
					
						
							|  |  |  |                 np = smag; | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-07-13 23:59:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-24 15:32:21 +02:00
										 |  |  |         if (np > peak) | 
					
						
							|  |  |  |             peak = np; | 
					
						
							| 
									
										
										
										
											2024-07-13 23:59:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-28 11:36:45 +02:00
										 |  |  |         result[enum_av] = 10.0 * MemLog::mlog10 (avg  <= 0 ? 1.0e-20 : avg); | 
					
						
							|  |  |  |         result[enum_pk] = 10.0 * MemLog::mlog10 (peak <= 0 ? 1.0e-20 : peak); | 
					
						
							| 
									
										
										
										
											2024-07-13 23:59:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-03 11:05:12 +02:00
										 |  |  |         if ((pgain != nullptr) && (enum_gain >= 0)) | 
					
						
							| 
									
										
										
										
											2024-07-28 11:36:45 +02:00
										 |  |  |             result[enum_gain] = 20.0 * MemLog::mlog10 (*pgain <= 0 ? 1.0e-20 : *pgain); | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-07-24 15:32:21 +02:00
										 |  |  |         if (enum_av   >= 0) | 
					
						
							|  |  |  |             result[enum_av]   = -100.0; | 
					
						
							|  |  |  |         if (enum_pk   >= 0) | 
					
						
							|  |  |  |             result[enum_pk]   = -100.0; | 
					
						
							|  |  |  |         if (enum_gain >= 0) | 
					
						
							|  |  |  |             result[enum_gain] = 0.0; | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-24 15:32:21 +02:00
										 |  |  | void METER::setBuffers(float* in) | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-07-24 15:32:21 +02:00
										 |  |  |     buff = in; | 
					
						
							| 
									
										
										
										
											2024-07-28 11:36:45 +02:00
										 |  |  |     flush(); | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-24 15:32:21 +02:00
										 |  |  | void METER::setSamplerate(int _rate) | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-07-24 15:32:21 +02:00
										 |  |  |     rate = (double) _rate; | 
					
						
							|  |  |  |     calc(); | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-24 15:32:21 +02:00
										 |  |  | void METER::setSize(int _size) | 
					
						
							| 
									
										
										
										
											2024-06-24 10:20:14 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-07-24 15:32:21 +02:00
										 |  |  |     size = _size; | 
					
						
							|  |  |  |     flush(); | 
					
						
							| 
									
										
										
										
											2024-06-24 10:20:14 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /********************************************************************************************************
 | 
					
						
							|  |  |  | *                                                                                                       * | 
					
						
							| 
									
										
										
										
											2024-07-24 15:32:21 +02:00
										 |  |  | *                                           Public Properties                                           * | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  | *                                                                                                       * | 
					
						
							|  |  |  | ********************************************************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-03 11:05:12 +02:00
										 |  |  | double METER::getMeter(int mt) const | 
					
						
							| 
									
										
										
										
											2024-06-24 10:20:14 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-07-24 15:32:21 +02:00
										 |  |  |     return result[mt]; | 
					
						
							| 
									
										
										
										
											2024-06-24 10:20:14 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | } // namespace WDSP
 |