| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  | /*  compress.c
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This file is part of a program that implements a Software-Defined Radio. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Copyright (C) 2011, 2013, 2017 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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This software is based upon the algorithm described by Peter Martinez, G3PLX, | 
					
						
							|  |  |  | in the January 2010 issue of RadCom magazine. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "comm.hpp"
 | 
					
						
							|  |  |  | #include "compress.hpp"
 | 
					
						
							|  |  |  | #include "TXA.hpp"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace WDSP { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-07 21:14:09 +02:00
										 |  |  | COMPRESSOR::COMPRESSOR( | 
					
						
							|  |  |  |     int _run, | 
					
						
							|  |  |  |     int _buffsize, | 
					
						
							|  |  |  |     float* _inbuff, | 
					
						
							|  |  |  |     float* _outbuff, | 
					
						
							|  |  |  |     double _gain | 
					
						
							|  |  |  | ) : | 
					
						
							|  |  |  |     run(_run), | 
					
						
							|  |  |  |     buffsize(_buffsize), | 
					
						
							|  |  |  |     inbuff(_inbuff), | 
					
						
							|  |  |  |     outbuff(_outbuff), | 
					
						
							|  |  |  |     gain(_gain) | 
					
						
							|  |  |  | {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void COMPRESSOR::flush() | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-08-07 21:14:09 +02:00
										 |  |  |     // Nothing to do
 | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-07 21:14:09 +02:00
										 |  |  | void COMPRESSOR::execute() | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-08-07 21:14:09 +02:00
										 |  |  |     double mag; | 
					
						
							|  |  |  |     if (run) | 
					
						
							|  |  |  |         for (int i = 0; i < buffsize; i++) | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2024-08-07 21:14:09 +02:00
										 |  |  |             mag = sqrt(inbuff[2 * i + 0] * inbuff[2 * i + 0] + inbuff[2 * i + 1] * inbuff[2 * i + 1]); | 
					
						
							|  |  |  |             if (gain * mag > 1.0) | 
					
						
							|  |  |  |                 outbuff[2 * i + 0] = (float) (inbuff[2 * i + 0] / mag); | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  |             else | 
					
						
							| 
									
										
										
										
											2024-08-07 21:14:09 +02:00
										 |  |  |                 outbuff[2 * i + 0] = (float) (inbuff[2 * i + 0] * gain); | 
					
						
							|  |  |  |             outbuff[2 * i + 1] = 0.0; | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-08-07 21:14:09 +02:00
										 |  |  |     else if (inbuff != outbuff) | 
					
						
							|  |  |  |         std::copy(inbuff, inbuff + buffsize * 2, outbuff); | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-07 21:14:09 +02:00
										 |  |  | void COMPRESSOR::setBuffers(float* _in, float* _out) | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-08-07 21:14:09 +02:00
										 |  |  |     inbuff = _in; | 
					
						
							|  |  |  |     outbuff = _out; | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-07 21:14:09 +02:00
										 |  |  | void COMPRESSOR::setSamplerate(int) | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-08-07 21:14:09 +02:00
										 |  |  |     // Nothing to do
 | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-07 21:14:09 +02:00
										 |  |  | void COMPRESSOR::setSize(int _size) | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-08-07 21:14:09 +02:00
										 |  |  |     buffsize = _size; | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /********************************************************************************************************
 | 
					
						
							|  |  |  | *                                                                                                       * | 
					
						
							|  |  |  | *                                           TXA Properties                                              * | 
					
						
							|  |  |  | *                                                                                                       * | 
					
						
							|  |  |  | ********************************************************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-07 21:14:09 +02:00
										 |  |  | void COMPRESSOR::setGain(float _gain) | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-08-07 21:14:09 +02:00
										 |  |  |     gain = pow (10.0, _gain / 20.0); | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } // namespace WDSP
 |