| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  | /*  emnr.h
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This file is part of a program that implements a Software-Defined Radio. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Copyright (C) 2015 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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef wdsp_emnr_h
 | 
					
						
							|  |  |  | #define wdsp_emnr_h
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-29 08:57:02 +02:00
										 |  |  | #include <array>
 | 
					
						
							|  |  |  | #include <vector>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  | #include "fftw3.h"
 | 
					
						
							|  |  |  | #include "export.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace WDSP { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class WDSP_API EMNR | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     int run; | 
					
						
							|  |  |  |     int position; | 
					
						
							|  |  |  |     int bsize; | 
					
						
							| 
									
										
										
										
											2024-06-25 03:50:48 +02:00
										 |  |  |     float* in; | 
					
						
							|  |  |  |     float* out; | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  |     int fsize; | 
					
						
							|  |  |  |     int ovrlp; | 
					
						
							|  |  |  |     int incr; | 
					
						
							| 
									
										
										
										
											2024-07-29 08:57:02 +02:00
										 |  |  |     std::vector<float> window; | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  |     int iasize; | 
					
						
							| 
									
										
										
										
											2024-07-29 08:57:02 +02:00
										 |  |  |     std::vector<float> inaccum; | 
					
						
							|  |  |  |     std::vector<float> forfftin; | 
					
						
							|  |  |  |     std::vector<float> forfftout; | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  |     int msize; | 
					
						
							| 
									
										
										
										
											2024-07-29 08:57:02 +02:00
										 |  |  |     std::vector<double> mask; | 
					
						
							|  |  |  |     std::vector<float> revfftin; | 
					
						
							|  |  |  |     std::vector<float> revfftout; | 
					
						
							|  |  |  |     std::vector<std::vector<float>> save; | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  |     int oasize; | 
					
						
							| 
									
										
										
										
											2024-07-29 08:57:02 +02:00
										 |  |  |     std::vector<float> outaccum; | 
					
						
							| 
									
										
										
										
											2024-07-18 02:08:05 +02:00
										 |  |  |     double rate; | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  |     int wintype; | 
					
						
							| 
									
										
										
										
											2024-07-18 02:08:05 +02:00
										 |  |  |     double ogain; | 
					
						
							|  |  |  |     double gain; | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  |     int nsamps; | 
					
						
							|  |  |  |     int iainidx; | 
					
						
							|  |  |  |     int iaoutidx; | 
					
						
							|  |  |  |     int init_oainidx; | 
					
						
							|  |  |  |     int oainidx; | 
					
						
							|  |  |  |     int oaoutidx; | 
					
						
							|  |  |  |     int saveidx; | 
					
						
							| 
									
										
										
										
											2024-06-25 03:50:48 +02:00
										 |  |  |     fftwf_plan Rfor; | 
					
						
							|  |  |  |     fftwf_plan Rrev; | 
					
						
							| 
									
										
										
										
											2024-07-29 20:12:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-29 08:57:02 +02:00
										 |  |  |     struct G | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-07-29 08:57:02 +02:00
										 |  |  |         int incr; | 
					
						
							|  |  |  |         double rate; | 
					
						
							|  |  |  |         int msize; | 
					
						
							| 
									
										
										
										
											2024-07-29 20:12:44 +02:00
										 |  |  |         std::vector<double>& mask; | 
					
						
							|  |  |  |         const std::vector<float>& y; | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  |         int gain_method; | 
					
						
							|  |  |  |         int npe_method; | 
					
						
							|  |  |  |         int ae_run; | 
					
						
							| 
									
										
										
										
											2024-07-29 20:12:44 +02:00
										 |  |  |         std::vector<double> lambda_y; | 
					
						
							|  |  |  |         std::vector<double> lambda_d; | 
					
						
							|  |  |  |         std::vector<double> prev_mask; | 
					
						
							|  |  |  |         std::vector<double> prev_gamma; | 
					
						
							| 
									
										
										
										
											2024-07-06 22:40:25 +02:00
										 |  |  |         double gf1p5; | 
					
						
							|  |  |  |         double alpha; | 
					
						
							|  |  |  |         double eps_floor; | 
					
						
							|  |  |  |         double gamma_max; | 
					
						
							|  |  |  |         double q; | 
					
						
							|  |  |  |         double gmax; | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  |         //
 | 
					
						
							| 
									
										
										
										
											2024-07-29 20:12:44 +02:00
										 |  |  |         std::array<double, 241*241> GG; | 
					
						
							|  |  |  |         std::array<double, 241*241> GGS; | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  |         FILE* fileb; | 
					
						
							| 
									
										
										
										
											2024-07-29 20:12:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-29 08:57:02 +02:00
										 |  |  |         G( | 
					
						
							|  |  |  |             int incr, | 
					
						
							|  |  |  |             double rate, | 
					
						
							|  |  |  |             int msize, | 
					
						
							| 
									
										
										
										
											2024-07-29 20:12:44 +02:00
										 |  |  |             std::vector<double>& mask, | 
					
						
							|  |  |  |             const std::vector<float>& y | 
					
						
							| 
									
										
										
										
											2024-07-29 08:57:02 +02:00
										 |  |  |         ); | 
					
						
							|  |  |  |         G(const G&) = delete; | 
					
						
							|  |  |  |         G& operator=(const G& other) = delete; | 
					
						
							| 
									
										
										
										
											2024-07-29 20:12:44 +02:00
										 |  |  |         ~G() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         void calc_gamma0(); | 
					
						
							|  |  |  |         void calc_gamma1(); | 
					
						
							|  |  |  |         void calc_gamma2(); | 
					
						
							|  |  |  |         void calc_lambda_y(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private: | 
					
						
							|  |  |  |         static double getKey(const std::array<double, 241*241>& type, double gamma, double xi); | 
					
						
							|  |  |  |         static double e1xb (double x); | 
					
						
							|  |  |  |         static double bessI0 (double x); | 
					
						
							|  |  |  |         static double bessI1 (double x); | 
					
						
							| 
									
										
										
										
											2024-08-02 08:01:46 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  |     G *g; | 
					
						
							| 
									
										
										
										
											2024-07-29 20:12:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-29 08:57:02 +02:00
										 |  |  |     struct NP | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         int incr; | 
					
						
							| 
									
										
										
										
											2024-07-06 22:40:25 +02:00
										 |  |  |         double rate; | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  |         int msize; | 
					
						
							| 
									
										
										
										
											2024-07-29 20:12:44 +02:00
										 |  |  |         std::vector<double>& lambda_y; | 
					
						
							|  |  |  |         std::vector<double>& lambda_d; | 
					
						
							| 
									
										
										
										
											2024-07-06 22:40:25 +02:00
										 |  |  |         double alphaCsmooth; | 
					
						
							|  |  |  |         double alphaMax; | 
					
						
							| 
									
										
										
										
											2024-07-29 08:57:02 +02:00
										 |  |  |         double alphaCmin; | 
					
						
							| 
									
										
										
										
											2024-07-06 22:40:25 +02:00
										 |  |  |         double alphaMin_max_value; | 
					
						
							|  |  |  |         double snrq; | 
					
						
							|  |  |  |         double betamax; | 
					
						
							|  |  |  |         double invQeqMax; | 
					
						
							|  |  |  |         double av; | 
					
						
							|  |  |  |         double Dtime; | 
					
						
							| 
									
										
										
										
											2024-07-29 08:57:02 +02:00
										 |  |  |         int U; | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  |         int V; | 
					
						
							|  |  |  |         int D; | 
					
						
							| 
									
										
										
										
											2024-07-29 20:12:44 +02:00
										 |  |  |         std::vector<double> p; | 
					
						
							|  |  |  |         std::vector<double> alphaOptHat; | 
					
						
							| 
									
										
										
										
											2024-07-29 08:57:02 +02:00
										 |  |  |         double alphaC; | 
					
						
							| 
									
										
										
										
											2024-07-29 20:12:44 +02:00
										 |  |  |         std::vector<double> alphaHat; | 
					
						
							|  |  |  |         std::vector<double> sigma2N; | 
					
						
							|  |  |  |         std::vector<double> pbar; | 
					
						
							|  |  |  |         std::vector<double> p2bar; | 
					
						
							|  |  |  |         std::vector<double> Qeq; | 
					
						
							| 
									
										
										
										
											2024-07-06 22:40:25 +02:00
										 |  |  |         double MofD; | 
					
						
							|  |  |  |         double MofV; | 
					
						
							| 
									
										
										
										
											2024-07-29 08:57:02 +02:00
										 |  |  |         std::array<double, 4> invQbar_points; | 
					
						
							|  |  |  |         std::array<double, 4> nsmax; | 
					
						
							| 
									
										
										
										
											2024-07-29 20:12:44 +02:00
										 |  |  |         std::vector<double> bmin; | 
					
						
							|  |  |  |         std::vector<double> bmin_sub; | 
					
						
							|  |  |  |         std::vector<int> k_mod; | 
					
						
							|  |  |  |         std::vector<double> actmin; | 
					
						
							|  |  |  |         std::vector<double> actmin_sub; | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  |         int subwc; | 
					
						
							| 
									
										
										
										
											2024-07-29 20:12:44 +02:00
										 |  |  |         std::vector<int> lmin_flag; | 
					
						
							|  |  |  |         std::vector<double> pmin_u; | 
					
						
							|  |  |  |         std::vector<std::vector<double>> actminbuff; | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  |         int amb_idx; | 
					
						
							| 
									
										
										
										
											2024-07-29 20:12:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-29 08:57:02 +02:00
										 |  |  |         NP( | 
					
						
							|  |  |  |             int incr, | 
					
						
							|  |  |  |             double rate, | 
					
						
							|  |  |  |             int msize, | 
					
						
							| 
									
										
										
										
											2024-07-29 20:12:44 +02:00
										 |  |  |             std::vector<double>& lambda_y, | 
					
						
							|  |  |  |             std::vector<double>& lambda_d | 
					
						
							| 
									
										
										
										
											2024-07-29 08:57:02 +02:00
										 |  |  |         ); | 
					
						
							|  |  |  |         NP(const NP&) = delete; | 
					
						
							|  |  |  |         NP& operator=(const NP& other) = delete; | 
					
						
							| 
									
										
										
										
											2024-07-29 20:12:44 +02:00
										 |  |  |         ~NP() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-29 08:57:02 +02:00
										 |  |  |         void LambdaD(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private: | 
					
						
							|  |  |  |         static const std::array<double, 18> DVals; | 
					
						
							|  |  |  |         static const std::array<double, 18> MVals; | 
					
						
							|  |  |  |         static void interpM ( | 
					
						
							|  |  |  |             double* res, | 
					
						
							|  |  |  |             double x, | 
					
						
							|  |  |  |             int nvals, | 
					
						
							|  |  |  |             const std::array<double, 18>& xvals, | 
					
						
							|  |  |  |             const std::array<double, 18>& yvals | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2024-08-02 08:01:46 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  |     NP *np; | 
					
						
							| 
									
										
										
										
											2024-07-29 20:12:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-29 08:57:02 +02:00
										 |  |  |     struct NPS | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         int incr; | 
					
						
							| 
									
										
										
										
											2024-07-06 22:40:25 +02:00
										 |  |  |         double rate; | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  |         int msize; | 
					
						
							| 
									
										
										
										
											2024-07-29 20:12:44 +02:00
										 |  |  |         const std::vector<double>& lambda_y; | 
					
						
							|  |  |  |         std::vector<double>& lambda_d; | 
					
						
							| 
									
										
										
										
											2024-07-06 22:40:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         double alpha_pow; | 
					
						
							|  |  |  |         double alpha_Pbar; | 
					
						
							|  |  |  |         double epsH1; | 
					
						
							|  |  |  |         double epsH1r; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-29 20:12:44 +02:00
										 |  |  |         std::vector<double> sigma2N; | 
					
						
							|  |  |  |         std::vector<double> PH1y; | 
					
						
							|  |  |  |         std::vector<double> Pbar; | 
					
						
							|  |  |  |         std::vector<double> EN2y; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-29 08:57:02 +02:00
										 |  |  |         NPS( | 
					
						
							|  |  |  |             int incr, | 
					
						
							|  |  |  |             double rate, | 
					
						
							|  |  |  |             int msize, | 
					
						
							| 
									
										
										
										
											2024-07-29 20:12:44 +02:00
										 |  |  |             const std::vector<double>& lambda_y, | 
					
						
							|  |  |  |             std::vector<double>& lambda_d, | 
					
						
							| 
									
										
										
										
											2024-07-29 08:57:02 +02:00
										 |  |  |             double alpha_pow, | 
					
						
							|  |  |  |             double alpha_Pbar, | 
					
						
							|  |  |  |             double epsH1 | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |         NPS(const NPS&) = delete; | 
					
						
							|  |  |  |         NPS& operator=(const NPS& other) = delete; | 
					
						
							| 
									
										
										
										
											2024-07-29 20:12:44 +02:00
										 |  |  |         ~NPS() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-29 08:57:02 +02:00
										 |  |  |         void LambdaDs(); | 
					
						
							| 
									
										
										
										
											2024-08-02 08:01:46 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  |     NPS *nps; | 
					
						
							| 
									
										
										
										
											2024-07-29 08:57:02 +02:00
										 |  |  |     struct AE | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         int msize; | 
					
						
							| 
									
										
										
										
											2024-07-29 20:12:44 +02:00
										 |  |  |         const std::vector<double>& lambda_y; | 
					
						
							| 
									
										
										
										
											2024-07-06 22:40:25 +02:00
										 |  |  |         double zetaThresh; | 
					
						
							|  |  |  |         double psi; | 
					
						
							| 
									
										
										
										
											2024-07-29 20:12:44 +02:00
										 |  |  |         std::vector<double> nmask; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-29 08:57:02 +02:00
										 |  |  |         AE( | 
					
						
							|  |  |  |             int msize, | 
					
						
							| 
									
										
										
										
											2024-07-29 20:12:44 +02:00
										 |  |  |             const std::vector<double>& lambda_y, | 
					
						
							| 
									
										
										
										
											2024-07-29 08:57:02 +02:00
										 |  |  |             double zetaThresh, | 
					
						
							|  |  |  |             double psi | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |         AE(const AE&) = delete; | 
					
						
							|  |  |  |         AE& operator=(const AE& other) = delete; | 
					
						
							| 
									
										
										
										
											2024-07-29 20:12:44 +02:00
										 |  |  |         ~AE() = default; | 
					
						
							| 
									
										
										
										
											2024-08-02 08:01:46 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  |     AE *ae; | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-29 08:57:02 +02:00
										 |  |  |     EMNR( | 
					
						
							| 
									
										
										
										
											2024-07-06 22:40:25 +02:00
										 |  |  |         int run, | 
					
						
							|  |  |  |         int position, | 
					
						
							|  |  |  |         int size, | 
					
						
							|  |  |  |         float* in, | 
					
						
							|  |  |  |         float* out, | 
					
						
							|  |  |  |         int fsize, | 
					
						
							|  |  |  |         int ovrlp, | 
					
						
							|  |  |  |         int rate, | 
					
						
							|  |  |  |         int wintype, | 
					
						
							| 
									
										
										
										
											2024-07-18 02:08:05 +02:00
										 |  |  |         double gain, | 
					
						
							| 
									
										
										
										
											2024-07-06 22:40:25 +02:00
										 |  |  |         int gain_method, | 
					
						
							|  |  |  |         int npe_method, | 
					
						
							|  |  |  |         int ae_run | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2024-07-29 08:57:02 +02:00
										 |  |  |     EMNR(const EMNR&) = delete; | 
					
						
							|  |  |  |     EMNR& operator=(const EMNR& other) = delete; | 
					
						
							|  |  |  |     ~EMNR(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void flush(); | 
					
						
							|  |  |  |     void execute(int pos); | 
					
						
							|  |  |  |     void setBuffers(float* in, float* out); | 
					
						
							|  |  |  |     void setSamplerate(int rate); | 
					
						
							|  |  |  |     void setSize(int size); | 
					
						
							|  |  |  |     // Public Properties
 | 
					
						
							|  |  |  |     void setGainMethod(int method); | 
					
						
							|  |  |  |     void setNpeMethod(int method); | 
					
						
							|  |  |  |     void setAeRun(int run); | 
					
						
							|  |  |  |     void setAeZetaThresh(double zetathresh); | 
					
						
							|  |  |  |     void setAePsi(double psi); | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2024-07-29 08:57:02 +02:00
										 |  |  |     void calc_window(); | 
					
						
							|  |  |  |     void calc(); | 
					
						
							|  |  |  |     void decalc(); | 
					
						
							|  |  |  |     void aepf(); | 
					
						
							|  |  |  |     void calc_gain(); | 
					
						
							| 
									
										
										
										
											2024-06-16 11:31:13 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } // namespace WDSP
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif
 |