| 
									
										
										
										
											2023-11-19 06:43:20 +01:00
										 |  |  | ///////////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | // Copyright (C) 2012 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany     //
 | 
					
						
							|  |  |  | // written by Christian Daniel                                                       //
 | 
					
						
							|  |  |  | // Copyright (C) 2014-2015 John Greb <hexameron@spam.no>                             //
 | 
					
						
							|  |  |  | // Copyright (C) 2015, 2017-2018, 2020, 2022-2023 Edouard Griffiths, F4EXB <f4exb06@gmail.com> //
 | 
					
						
							|  |  |  | // Copyright (C) 2020 Kacper Michajłow <kasper93@gmail.com>                          //
 | 
					
						
							|  |  |  | //                                                                                   //
 | 
					
						
							|  |  |  | // 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                      //
 | 
					
						
							|  |  |  | // (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 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/>.              //
 | 
					
						
							|  |  |  | ///////////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							| 
									
										
										
										
											2014-06-27 17:36:13 +01:00
										 |  |  | // ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | //	fftfilt.cxx  --  Fast convolution Overlap-Add filter
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Filter implemented using overlap-add FFT convolution method
 | 
					
						
							|  |  |  | // h(t) characterized by Windowed-Sinc impulse response
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Reference:
 | 
					
						
							|  |  |  | //	 "The Scientist and Engineer's Guide to Digital Signal Processing"
 | 
					
						
							|  |  |  | //	 by Dr. Steven W. Smith, http://www.dspguide.com
 | 
					
						
							|  |  |  | //	 Chapters 16, 18 and 21
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Copyright (C) 2006-2008 Dave Freese, W1HKJ
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This file is part of fldigi.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Fldigi 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 3 of the License, or
 | 
					
						
							|  |  |  | // (at your option) any later version.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Fldigi 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 fldigi.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							| 
									
										
										
										
											2022-12-12 07:50:35 +01:00
										 |  |  | //
 | 
					
						
							|  |  |  | // Augmented with more filter types
 | 
					
						
							|  |  |  | // Copyright (C) 2015-2022 Edouard Griffiths, F4EXB
 | 
					
						
							| 
									
										
										
										
											2014-06-27 17:36:13 +01:00
										 |  |  | // ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <memory.h>
 | 
					
						
							| 
									
										
										
										
											2018-05-22 00:10:56 +02:00
										 |  |  | #include <algorithm>
 | 
					
						
							| 
									
										
										
										
											2014-06-27 17:36:13 +01:00
										 |  |  | #include <iostream>
 | 
					
						
							|  |  |  | #include <fstream>
 | 
					
						
							|  |  |  | #include <cstdlib>
 | 
					
						
							|  |  |  | #include <cmath>
 | 
					
						
							|  |  |  | #include <typeinfo>
 | 
					
						
							| 
									
										
										
										
											2022-12-10 19:02:38 +01:00
										 |  |  | #include <array>
 | 
					
						
							| 
									
										
										
										
											2014-06-27 17:36:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <sys/types.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <dsp/misc.h>
 | 
					
						
							|  |  |  | #include <dsp/fftfilt.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //------------------------------------------------------------------------------
 | 
					
						
							|  |  |  | // initialize the filter
 | 
					
						
							|  |  |  | // create forward and reverse FFTs
 | 
					
						
							|  |  |  | //------------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Only need a single instance of g_fft, used for both forward and reverse
 | 
					
						
							|  |  |  | void fftfilt::init_filter() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	flen2	= flen >> 1; | 
					
						
							|  |  |  | 	fft	= new g_fft<float>(flen); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	filter		= new cmplx[flen]; | 
					
						
							| 
									
										
										
										
											2017-03-16 10:11:03 +01:00
										 |  |  |     filterOpp   = new cmplx[flen]; | 
					
						
							| 
									
										
										
										
											2014-12-25 21:24:03 +00:00
										 |  |  | 	data		= new cmplx[flen]; | 
					
						
							|  |  |  | 	output		= new cmplx[flen2]; | 
					
						
							| 
									
										
										
										
											2014-06-27 17:36:13 +01:00
										 |  |  | 	ovlbuf		= new cmplx[flen2]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-15 00:03:34 +01:00
										 |  |  | 	std::fill(filter, filter + flen, cmplx{0, 0}); | 
					
						
							|  |  |  |     std::fill(filterOpp, filterOpp + flen, cmplx{0, 0}); | 
					
						
							|  |  |  | 	std::fill(data, data + flen , cmplx{0, 0}); | 
					
						
							|  |  |  | 	std::fill(output, output + flen2, cmplx{0, 0}); | 
					
						
							|  |  |  | 	std::fill(ovlbuf, ovlbuf + flen2, cmplx{0, 0}); | 
					
						
							| 
									
										
										
										
											2014-06-27 17:36:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	inptr = 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //------------------------------------------------------------------------------
 | 
					
						
							|  |  |  | // fft filter
 | 
					
						
							|  |  |  | // f1 < f2 ==> band pass filter
 | 
					
						
							|  |  |  | // f1 > f2 ==> band reject filter
 | 
					
						
							|  |  |  | // f1 == 0 ==> low pass filter
 | 
					
						
							|  |  |  | // f2 == 0 ==> high pass filter
 | 
					
						
							|  |  |  | //------------------------------------------------------------------------------
 | 
					
						
							| 
									
										
										
										
											2023-11-05 10:33:27 +01:00
										 |  |  | fftfilt::fftfilt(int len) : | 
					
						
							|  |  |  |     m_noiseReduction(len) | 
					
						
							| 
									
										
										
										
											2020-11-05 18:55:39 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	flen	= len; | 
					
						
							|  |  |  | 	pass    = 0; | 
					
						
							|  |  |  | 	window  = 0; | 
					
						
							| 
									
										
										
										
											2023-11-05 10:33:27 +01:00
										 |  |  |     m_dnr   = false; | 
					
						
							| 
									
										
										
										
											2020-11-05 18:55:39 +01:00
										 |  |  | 	init_filter(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-05 10:33:27 +01:00
										 |  |  | fftfilt::fftfilt(float f1, float f2, int len) : | 
					
						
							|  |  |  |     m_noiseReduction(len) | 
					
						
							| 
									
										
										
										
											2014-06-27 17:36:13 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	flen	= len; | 
					
						
							| 
									
										
										
										
											2018-02-22 03:04:42 +01:00
										 |  |  | 	pass    = 0; | 
					
						
							|  |  |  | 	window  = 0; | 
					
						
							| 
									
										
										
										
											2023-11-05 10:33:27 +01:00
										 |  |  |     m_dnr   = false; | 
					
						
							| 
									
										
										
										
											2014-06-27 17:36:13 +01:00
										 |  |  | 	init_filter(); | 
					
						
							|  |  |  | 	create_filter(f1, f2); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-05 10:33:27 +01:00
										 |  |  | fftfilt::fftfilt(float f2, int len) : | 
					
						
							|  |  |  |     m_noiseReduction(len) | 
					
						
							| 
									
										
										
										
											2015-06-21 12:46:47 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	flen	= len; | 
					
						
							| 
									
										
										
										
											2018-02-22 03:04:42 +01:00
										 |  |  |     pass    = 0; | 
					
						
							|  |  |  |     window  = 0; | 
					
						
							| 
									
										
										
										
											2023-11-05 10:33:27 +01:00
										 |  |  |     m_dnr   = false; | 
					
						
							| 
									
										
										
										
											2015-06-21 12:46:47 +02:00
										 |  |  | 	init_filter(); | 
					
						
							|  |  |  | 	create_dsb_filter(f2); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-27 17:36:13 +01:00
										 |  |  | fftfilt::~fftfilt() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (fft) delete fft; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (filter) delete [] filter; | 
					
						
							| 
									
										
										
										
											2017-03-16 10:11:03 +01:00
										 |  |  |     if (filterOpp) delete [] filterOpp; | 
					
						
							| 
									
										
										
										
											2014-12-25 21:24:03 +00:00
										 |  |  | 	if (data) delete [] data; | 
					
						
							| 
									
										
										
										
											2014-06-27 17:36:13 +01:00
										 |  |  | 	if (output) delete [] output; | 
					
						
							|  |  |  | 	if (ovlbuf) delete [] ovlbuf; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-18 01:30:32 +02:00
										 |  |  | void fftfilt::create_filter(float f1, float f2, FFTWindow::Function wf) | 
					
						
							| 
									
										
										
										
											2014-06-27 17:36:13 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-12-25 21:24:03 +00:00
										 |  |  | 	// initialize the filter to zero
 | 
					
						
							| 
									
										
										
										
											2020-11-15 00:03:34 +01:00
										 |  |  | 	std::fill(filter, filter + flen, cmplx{0, 0}); | 
					
						
							| 
									
										
										
										
											2014-06-27 17:36:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-25 21:24:03 +00:00
										 |  |  | 	// create the filter shape coefficients by fft
 | 
					
						
							|  |  |  | 	bool b_lowpass, b_highpass; | 
					
						
							| 
									
										
										
										
											2014-06-27 17:36:13 +01:00
										 |  |  | 	b_lowpass = (f2 != 0); | 
					
						
							|  |  |  | 	b_highpass = (f1 != 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (int i = 0; i < flen2; i++) { | 
					
						
							| 
									
										
										
										
											2014-12-25 21:24:03 +00:00
										 |  |  | 		filter[i] = 0; | 
					
						
							|  |  |  | 	// lowpass @ f2
 | 
					
						
							|  |  |  | 		if (b_lowpass) | 
					
						
							|  |  |  | 			filter[i] += fsinc(f2, i, flen2); | 
					
						
							|  |  |  | 	// highighpass @ f1
 | 
					
						
							|  |  |  | 		if (b_highpass) | 
					
						
							|  |  |  | 			filter[i] -= fsinc(f1, i, flen2); | 
					
						
							| 
									
										
										
										
											2014-06-27 17:36:13 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-12-25 21:24:03 +00:00
										 |  |  | 	// highpass is delta[flen2/2] - h(t)
 | 
					
						
							|  |  |  | 	if (b_highpass && f2 < f1) | 
					
						
							|  |  |  | 		filter[flen2 / 2] += 1; | 
					
						
							| 
									
										
										
										
											2014-06-27 17:36:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-18 01:30:32 +02:00
										 |  |  |     FFTWindow fwin; | 
					
						
							|  |  |  |     fwin.create(wf, flen2); | 
					
						
							|  |  |  |     fwin.apply(filter); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// for (int i = 0; i < flen2; i++)
 | 
					
						
							|  |  |  | 	// 	filter[i] *= _blackman(i, flen2);
 | 
					
						
							| 
									
										
										
										
											2014-06-27 17:36:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-22 00:10:56 +02:00
										 |  |  | 	fft->ComplexFFT(filter); // filter was expressed in the time domain (impulse response)
 | 
					
						
							| 
									
										
										
										
											2014-06-27 17:36:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-25 21:24:03 +00:00
										 |  |  | 	// normalize the output filter for unity gain
 | 
					
						
							| 
									
										
										
										
											2014-06-27 17:36:13 +01:00
										 |  |  | 	float scale = 0, mag; | 
					
						
							|  |  |  | 	for (int i = 0; i < flen2; i++) { | 
					
						
							|  |  |  | 		mag = abs(filter[i]); | 
					
						
							|  |  |  | 		if (mag > scale) scale = mag; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (scale != 0) { | 
					
						
							|  |  |  | 		for (int i = 0; i < flen; i++) | 
					
						
							|  |  |  | 			filter[i] /= scale; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-12 07:50:35 +01:00
										 |  |  | void fftfilt::create_filter(const std::vector<std::pair<float, float>>& limits, bool pass, FFTWindow::Function wf) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     std::vector<int> canvasNeg(flen2, pass ? 0 : 1); // initialize the negative frequencies filter canvas
 | 
					
						
							|  |  |  |     std::vector<int> canvasPos(flen2, pass ? 0 : 1); // initialize the positive frequencies filter canvas
 | 
					
						
							|  |  |  | 	std::fill(filter, filter + flen, cmplx{0, 0}); // initialize the positive filter to zero
 | 
					
						
							|  |  |  |     std::fill(filterOpp, filterOpp + flen, cmplx{0, 0}); // initialize the negative filter to zero
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (const auto& fs : limits) | 
					
						
							| 
									
										
										
										
											2022-12-10 19:02:38 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-12-12 07:50:35 +01:00
										 |  |  |         const float& f1 = fs.first + 0.5; | 
					
						
							|  |  |  |         const float& w = fs.second > 0.0 ? fs.second : 0.0; | 
					
						
							|  |  |  |         const float& f2 = f1 + w; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (int i = 0; i < flen; i++) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             if (pass) // pass
 | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 if ((i >= f1*flen) && (i <= f2*flen)) | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     if (i < flen2) { | 
					
						
							|  |  |  |                         canvasNeg[flen2-1-i] = 1; | 
					
						
							|  |  |  |                     } else { | 
					
						
							|  |  |  |                         canvasPos[i-flen2] = 1; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else // reject
 | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2022-12-13 07:00:50 +01:00
										 |  |  |                 if ((i >= f1*flen) && (i <= f2*flen)) | 
					
						
							|  |  |  |                 { | 
					
						
							| 
									
										
										
										
											2022-12-12 07:50:35 +01:00
										 |  |  |                     if (i < flen2) { | 
					
						
							|  |  |  |                         canvasNeg[flen2-1-i] = 0; | 
					
						
							|  |  |  |                     } else { | 
					
						
							|  |  |  |                         canvasPos[i-flen2] = 0; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-12-10 19:02:38 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-12-12 07:50:35 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-12-10 19:02:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-12 07:50:35 +01:00
										 |  |  |     std::vector<std::pair<int,int>> indexesNegList; | 
					
						
							|  |  |  |     std::vector<std::pair<int,int>> indexesPosList; | 
					
						
							|  |  |  |     int cn = 0; | 
					
						
							|  |  |  |     int cp = 0; | 
					
						
							| 
									
										
										
										
											2022-12-13 07:00:50 +01:00
										 |  |  |     int defaultSecond = pass ? 0 : flen2 - 1; | 
					
						
							| 
									
										
										
										
											2022-12-12 07:50:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (int i = 0; i < flen2; i++) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ((canvasNeg[i] == 1) && (cn == 0)) { | 
					
						
							| 
									
										
										
										
											2022-12-13 07:00:50 +01:00
										 |  |  |             indexesNegList.push_back(std::pair<int,int>{i, defaultSecond}); | 
					
						
							| 
									
										
										
										
											2022-12-10 19:02:38 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-12 07:50:35 +01:00
										 |  |  |         if ((canvasNeg[i] == 0) && (cn == 1)) { | 
					
						
							|  |  |  |             indexesNegList.back().second = i; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ((canvasPos[i] == 1) && (cp == 0)) { | 
					
						
							| 
									
										
										
										
											2022-12-13 07:00:50 +01:00
										 |  |  |             indexesPosList.push_back(std::pair<int,int>{i, defaultSecond}); | 
					
						
							| 
									
										
										
										
											2022-12-12 07:50:35 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ((canvasPos[i] == 0) && (cp == 1)) { | 
					
						
							|  |  |  |             indexesPosList.back().second = i; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         cn = canvasNeg[i]; | 
					
						
							|  |  |  |         cp = canvasPos[i]; | 
					
						
							| 
									
										
										
										
											2022-12-10 19:02:38 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-12 07:50:35 +01:00
										 |  |  |     for (const auto& indexes : indexesPosList) | 
					
						
							| 
									
										
										
										
											2022-12-10 19:02:38 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-12-12 07:50:35 +01:00
										 |  |  |         const float f1 = indexes.first / (float) flen; | 
					
						
							|  |  |  |         const float f2 = indexes.second / (float) flen; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (int i = 0; i < flen2; i++) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             if (f2 != 0) { | 
					
						
							|  |  |  |                 filter[i] += fsinc(f2, i, flen2); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (f1 != 0) { | 
					
						
							|  |  |  |                 filter[i] -= fsinc(f1, i, flen2); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-12-13 21:46:34 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (f2 == 0 && f1 != 0) { | 
					
						
							|  |  |  |             filter[flen2 / 2] += 1; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-12-10 19:02:38 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-12 07:50:35 +01:00
										 |  |  |     for (const auto& indexes : indexesNegList) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         const float f1 = indexes.first / (float) flen; | 
					
						
							|  |  |  |         const float f2 = indexes.second / (float) flen; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (int i = 0; i < flen2; i++) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             if (f2 != 0) { | 
					
						
							|  |  |  |                 filterOpp[i] += fsinc(f2, i, flen2); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (f1 != 0) { | 
					
						
							|  |  |  |                 filterOpp[i] -= fsinc(f1, i, flen2); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-12-13 21:46:34 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (f2 == 0 && f1 != 0) { | 
					
						
							|  |  |  |             filterOpp[flen2 / 2] += 1; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-12-12 07:50:35 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-12-10 19:02:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-12 07:50:35 +01:00
										 |  |  |     FFTWindow fwin; | 
					
						
							|  |  |  |     fwin.create(wf, flen2); | 
					
						
							|  |  |  |     fwin.apply(filter); | 
					
						
							|  |  |  |     fwin.apply(filterOpp); | 
					
						
							| 
									
										
										
										
											2022-12-10 19:02:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-12 07:50:35 +01:00
										 |  |  | 	fft->ComplexFFT(filter); // filter was expressed in the time domain (impulse response)
 | 
					
						
							|  |  |  |     fft->ComplexFFT(filterOpp); // filter was expressed in the time domain (impulse response)
 | 
					
						
							| 
									
										
										
										
											2022-12-10 19:02:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-12 07:50:35 +01:00
										 |  |  |     float scalen = 0, scalep = 0, magn, magp; // normalize the output filter for unity gain
 | 
					
						
							| 
									
										
										
										
											2022-12-10 19:02:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-12 07:50:35 +01:00
										 |  |  | 	for (int i = 0; i < flen2; i++) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  | 		magp = abs(filter[i]); | 
					
						
							| 
									
										
										
										
											2022-12-10 19:02:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-12 07:50:35 +01:00
										 |  |  |         if (magp > scalep) { | 
					
						
							|  |  |  |             scalep = magp; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-12-10 19:02:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-12 07:50:35 +01:00
										 |  |  |         magn = abs(filterOpp[i]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (magn > scalen) { | 
					
						
							|  |  |  |             scalen = magn; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (scalep != 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         std::for_each( | 
					
						
							|  |  |  |             filter, | 
					
						
							|  |  |  |             filter + flen, | 
					
						
							|  |  |  |             [scalep](fftfilt::cmplx& s) { s /= scalep; } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (scalen != 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         std::for_each( | 
					
						
							|  |  |  |             filterOpp, | 
					
						
							|  |  |  |             filterOpp + flen, | 
					
						
							|  |  |  |             [scalen](fftfilt::cmplx& s) { s /= scalen; } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-12-10 19:02:38 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-21 12:46:47 +02:00
										 |  |  | // Double the size of FFT used for equivalent SSB filter or assume FFT is half the size of the one used for SSB
 | 
					
						
							| 
									
										
										
										
											2022-05-18 01:30:32 +02:00
										 |  |  | void fftfilt::create_dsb_filter(float f2, FFTWindow::Function wf) | 
					
						
							| 
									
										
										
										
											2015-06-21 12:46:47 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	// initialize the filter to zero
 | 
					
						
							| 
									
										
										
										
											2020-11-15 00:03:34 +01:00
										 |  |  | 	std::fill(filter, filter + flen, cmplx{0, 0}); | 
					
						
							| 
									
										
										
										
											2015-06-21 12:46:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for (int i = 0; i < flen2; i++) { | 
					
						
							|  |  |  | 		filter[i] = fsinc(f2, i, flen2); | 
					
						
							| 
									
										
										
										
											2022-05-18 01:30:32 +02:00
										 |  |  | 		// filter[i] *= _blackman(i, flen2);
 | 
					
						
							| 
									
										
										
										
											2015-06-21 12:46:47 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-18 01:30:32 +02:00
										 |  |  |     FFTWindow fwin; | 
					
						
							|  |  |  |     fwin.create(wf, flen2); | 
					
						
							|  |  |  |     fwin.apply(filter); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-22 00:10:56 +02:00
										 |  |  | 	fft->ComplexFFT(filter); // filter was expressed in the time domain (impulse response)
 | 
					
						
							| 
									
										
										
										
											2015-06-21 12:46:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// normalize the output filter for unity gain
 | 
					
						
							|  |  |  | 	float scale = 0, mag; | 
					
						
							|  |  |  | 	for (int i = 0; i < flen2; i++) { | 
					
						
							|  |  |  | 		mag = abs(filter[i]); | 
					
						
							|  |  |  | 		if (mag > scale) scale = mag; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (scale != 0) { | 
					
						
							|  |  |  | 		for (int i = 0; i < flen; i++) | 
					
						
							|  |  |  | 			filter[i] /= scale; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-16 10:11:03 +01:00
										 |  |  | // Double the size of FFT used for equivalent SSB filter or assume FFT is half the size of the one used for SSB
 | 
					
						
							|  |  |  | // used with runAsym for in band / opposite band asymmetrical filtering. Can be used for vestigial sideband modulation.
 | 
					
						
							| 
									
										
										
										
											2022-05-18 01:30:32 +02:00
										 |  |  | void fftfilt::create_asym_filter(float fopp, float fin, FFTWindow::Function wf) | 
					
						
							| 
									
										
										
										
											2017-03-16 10:11:03 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     // in band
 | 
					
						
							|  |  |  |     // initialize the filter to zero
 | 
					
						
							| 
									
										
										
										
											2020-11-15 00:03:34 +01:00
										 |  |  |     std::fill(filter, filter + flen, cmplx{0, 0}); | 
					
						
							| 
									
										
										
										
											2017-03-16 10:11:03 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (int i = 0; i < flen2; i++) { | 
					
						
							|  |  |  |         filter[i] = fsinc(fin, i, flen2); | 
					
						
							| 
									
										
										
										
											2022-05-18 01:30:32 +02:00
										 |  |  |         // filter[i] *= _blackman(i, flen2);
 | 
					
						
							| 
									
										
										
										
											2017-03-16 10:11:03 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-18 01:30:32 +02:00
										 |  |  |     FFTWindow fwin; | 
					
						
							|  |  |  |     fwin.create(wf, flen2); | 
					
						
							|  |  |  |     fwin.apply(filter); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-22 00:10:56 +02:00
										 |  |  |     fft->ComplexFFT(filter); // filter was expressed in the time domain (impulse response)
 | 
					
						
							| 
									
										
										
										
											2017-03-16 10:11:03 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // normalize the output filter for unity gain
 | 
					
						
							|  |  |  |     float scale = 0, mag; | 
					
						
							|  |  |  |     for (int i = 0; i < flen2; i++) { | 
					
						
							|  |  |  |         mag = abs(filter[i]); | 
					
						
							|  |  |  |         if (mag > scale) scale = mag; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (scale != 0) { | 
					
						
							|  |  |  |         for (int i = 0; i < flen; i++) | 
					
						
							|  |  |  |             filter[i] /= scale; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // opposite band
 | 
					
						
							|  |  |  |     // initialize the filter to zero
 | 
					
						
							| 
									
										
										
										
											2020-11-15 00:03:34 +01:00
										 |  |  |     std::fill(filterOpp, filterOpp + flen, cmplx{0, 0}); | 
					
						
							| 
									
										
										
										
											2017-03-16 10:11:03 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (int i = 0; i < flen2; i++) { | 
					
						
							|  |  |  |         filterOpp[i] = fsinc(fopp, i, flen2); | 
					
						
							| 
									
										
										
										
											2022-05-18 01:30:32 +02:00
										 |  |  |         // filterOpp[i] *= _blackman(i, flen2);
 | 
					
						
							| 
									
										
										
										
											2017-03-16 10:11:03 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-18 01:30:32 +02:00
										 |  |  |     fwin.apply(filterOpp); | 
					
						
							| 
									
										
										
										
											2018-05-22 00:10:56 +02:00
										 |  |  |     fft->ComplexFFT(filterOpp); // filter was expressed in the time domain (impulse response)
 | 
					
						
							| 
									
										
										
										
											2017-03-16 10:11:03 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // normalize the output filter for unity gain
 | 
					
						
							|  |  |  |     scale = 0; | 
					
						
							|  |  |  |     for (int i = 0; i < flen2; i++) { | 
					
						
							|  |  |  |         mag = abs(filterOpp[i]); | 
					
						
							|  |  |  |         if (mag > scale) scale = mag; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (scale != 0) { | 
					
						
							|  |  |  |         for (int i = 0; i < flen; i++) | 
					
						
							|  |  |  |             filterOpp[i] /= scale; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-22 00:10:56 +02:00
										 |  |  | // This filter is constructed directly from frequency domain response. Run with runFilt.
 | 
					
						
							|  |  |  | void fftfilt::create_rrc_filter(float fb, float a) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     std::fill(filter, filter+flen, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (int i = 0; i < flen; i++) { | 
					
						
							|  |  |  |         filter[i] = frrc(fb, a, i, flen); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // normalize the output filter for unity gain
 | 
					
						
							|  |  |  |     float scale = 0, mag; | 
					
						
							|  |  |  |     for (int i = 0; i < flen; i++) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         mag = abs(filter[i]); | 
					
						
							|  |  |  |         if (mag > scale) { | 
					
						
							|  |  |  |             scale = mag; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (scale != 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         for (int i = 0; i < flen; i++) { | 
					
						
							|  |  |  |             filter[i] /= scale; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-21 12:46:47 +02:00
										 |  |  | // test bypass
 | 
					
						
							|  |  |  | int fftfilt::noFilt(const cmplx & in, cmplx **out) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	data[inptr++] = in; | 
					
						
							|  |  |  | 	if (inptr < flen2) | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	inptr = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	*out = data; | 
					
						
							|  |  |  | 	return flen2; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-27 17:36:13 +01:00
										 |  |  | // Filter with fast convolution (overlap-add algorithm).
 | 
					
						
							| 
									
										
										
										
											2014-12-22 20:23:55 +00:00
										 |  |  | int fftfilt::runFilt(const cmplx & in, cmplx **out) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-12-25 21:24:03 +00:00
										 |  |  | 	data[inptr++] = in; | 
					
						
							| 
									
										
										
										
											2014-12-22 20:23:55 +00:00
										 |  |  | 	if (inptr < flen2) | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	inptr = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-25 21:24:03 +00:00
										 |  |  | 	fft->ComplexFFT(data); | 
					
						
							| 
									
										
										
										
											2014-12-22 20:23:55 +00:00
										 |  |  | 	for (int i = 0; i < flen; i++) | 
					
						
							| 
									
										
										
										
											2014-12-25 21:24:03 +00:00
										 |  |  | 		data[i] *= filter[i]; | 
					
						
							| 
									
										
										
										
											2014-12-22 20:23:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-25 21:24:03 +00:00
										 |  |  | 	fft->InverseComplexFFT(data); | 
					
						
							| 
									
										
										
										
											2014-12-22 20:23:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for (int i = 0; i < flen2; i++) { | 
					
						
							| 
									
										
										
										
											2014-12-25 21:24:03 +00:00
										 |  |  | 		output[i] = ovlbuf[i] + data[i]; | 
					
						
							|  |  |  | 		ovlbuf[i] = data[flen2 + i]; | 
					
						
							| 
									
										
										
										
											2014-12-22 20:23:55 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-11-15 00:03:34 +01:00
										 |  |  | 	std::fill(data, data + flen , cmplx{0, 0}); | 
					
						
							| 
									
										
										
										
											2014-12-22 20:23:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	*out = output; | 
					
						
							|  |  |  | 	return flen2; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Second version for single sideband
 | 
					
						
							| 
									
										
										
										
											2017-03-15 15:37:51 +01:00
										 |  |  | int fftfilt::runSSB(const cmplx & in, cmplx **out, bool usb, bool getDC) | 
					
						
							| 
									
										
										
										
											2014-06-27 17:36:13 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-12-25 21:24:03 +00:00
										 |  |  | 	data[inptr++] = in; | 
					
						
							| 
									
										
										
										
											2014-06-27 17:36:13 +01:00
										 |  |  | 	if (inptr < flen2) | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	inptr = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-25 21:24:03 +00:00
										 |  |  | 	fft->ComplexFFT(data); | 
					
						
							| 
									
										
										
										
											2014-06-27 17:36:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-15 15:37:51 +01:00
										 |  |  | 	// get or reject DC component
 | 
					
						
							|  |  |  | 	data[0] = getDC ? data[0]*filter[0] : 0; | 
					
						
							| 
									
										
										
										
											2023-11-05 10:33:27 +01:00
										 |  |  |     m_noiseReduction.setScheme(m_dnrScheme); | 
					
						
							|  |  |  |     m_noiseReduction.init(); | 
					
						
							| 
									
										
										
										
											2017-03-15 15:37:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-13 09:06:43 +01:00
										 |  |  | 	// Discard frequencies for ssb
 | 
					
						
							| 
									
										
										
										
											2017-03-15 15:37:51 +01:00
										 |  |  | 	if (usb) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2023-11-05 10:33:27 +01:00
										 |  |  | 		for (int i = 1; i < flen2; i++) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2014-12-25 21:24:03 +00:00
										 |  |  | 			data[i] *= filter[i]; | 
					
						
							|  |  |  | 			data[flen2 + i] = 0; | 
					
						
							| 
									
										
										
										
											2023-11-05 10:33:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (m_dnr) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 m_noiseReduction.push(data[i], i); | 
					
						
							|  |  |  |                 m_noiseReduction.push(data[flen2 + i], flen2 + i); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2014-07-13 09:06:43 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-03-15 15:37:51 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-07-13 09:06:43 +01:00
										 |  |  | 	else | 
					
						
							| 
									
										
										
										
											2017-03-15 15:37:51 +01:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2023-11-05 10:33:27 +01:00
										 |  |  | 		for (int i = 1; i < flen2; i++) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2014-12-25 21:24:03 +00:00
										 |  |  | 			data[i] = 0; | 
					
						
							|  |  |  | 			data[flen2 + i] *= filter[flen2 + i]; | 
					
						
							| 
									
										
										
										
											2023-11-05 10:33:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (m_dnr) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 m_noiseReduction.push(data[i], i); | 
					
						
							|  |  |  |                 m_noiseReduction.push(data[flen2 + i], flen2 + i); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2014-07-13 09:06:43 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-03-15 15:37:51 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-07-13 09:06:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-05 10:33:27 +01:00
										 |  |  |     if (m_dnr) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         m_noiseReduction.m_aboveAvgFactor = m_dnrAboveAvgFactor; | 
					
						
							|  |  |  |         m_noiseReduction.m_sigmaFactor = m_dnrSigmaFactor; | 
					
						
							|  |  |  |         m_noiseReduction.m_nbPeaks = m_dnrNbPeaks; | 
					
						
							|  |  |  |         m_noiseReduction.calc(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (int i = 0; i < flen; i++) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             if (m_noiseReduction.cut(i)) { | 
					
						
							|  |  |  |                 data[i] = 0; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-13 09:06:43 +01:00
										 |  |  | 	// in-place FFT: freqdata overwritten with filtered timedata
 | 
					
						
							| 
									
										
										
										
											2014-12-25 21:24:03 +00:00
										 |  |  | 	fft->InverseComplexFFT(data); | 
					
						
							| 
									
										
										
										
											2014-06-27 17:36:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// overlap and add
 | 
					
						
							|  |  |  | 	for (int i = 0; i < flen2; i++) { | 
					
						
							| 
									
										
										
										
											2014-12-25 21:24:03 +00:00
										 |  |  | 		output[i] = ovlbuf[i] + data[i]; | 
					
						
							|  |  |  | 		ovlbuf[i] = data[i+flen2]; | 
					
						
							| 
									
										
										
										
											2014-06-27 17:36:13 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-11-15 00:03:34 +01:00
										 |  |  | 	std::fill(data, data + flen , cmplx{0, 0}); | 
					
						
							| 
									
										
										
										
											2014-06-27 17:36:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	*out = output; | 
					
						
							|  |  |  | 	return flen2; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-21 12:46:47 +02:00
										 |  |  | // Version for double sideband. You have to double the FFT size used for SSB.
 | 
					
						
							| 
									
										
										
										
											2018-05-13 17:27:24 +02:00
										 |  |  | int fftfilt::runDSB(const cmplx & in, cmplx **out, bool getDC) | 
					
						
							| 
									
										
										
										
											2015-06-21 12:46:47 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	data[inptr++] = in; | 
					
						
							|  |  |  | 	if (inptr < flen2) | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	inptr = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fft->ComplexFFT(data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (int i = 0; i < flen2; i++) { | 
					
						
							|  |  |  | 		data[i] *= filter[i]; | 
					
						
							|  |  |  | 		data[flen2 + i] *= filter[flen2 + i]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-13 17:27:24 +02:00
										 |  |  |     // get or reject DC component
 | 
					
						
							|  |  |  |     data[0] = getDC ? data[0] : 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-21 12:46:47 +02:00
										 |  |  | 	// in-place FFT: freqdata overwritten with filtered timedata
 | 
					
						
							|  |  |  | 	fft->InverseComplexFFT(data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// overlap and add
 | 
					
						
							|  |  |  | 	for (int i = 0; i < flen2; i++) { | 
					
						
							|  |  |  | 		output[i] = ovlbuf[i] + data[i]; | 
					
						
							|  |  |  | 		ovlbuf[i] = data[i+flen2]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-15 00:03:34 +01:00
										 |  |  | 	std::fill(data, data + flen , cmplx{0, 0}); | 
					
						
							| 
									
										
										
										
											2015-06-21 12:46:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	*out = output; | 
					
						
							|  |  |  | 	return flen2; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-16 10:11:03 +01:00
										 |  |  | // Version for asymmetrical sidebands. You have to double the FFT size used for SSB.
 | 
					
						
							|  |  |  | int fftfilt::runAsym(const cmplx & in, cmplx **out, bool usb) | 
					
						
							| 
									
										
										
										
											2017-03-16 02:45:51 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     data[inptr++] = in; | 
					
						
							|  |  |  |     if (inptr < flen2) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     inptr = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     fft->ComplexFFT(data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     data[0] *= filter[0]; // always keep DC
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (usb) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         for (int i = 1; i < flen2; i++) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             data[i] *= filter[i]; // usb
 | 
					
						
							| 
									
										
										
										
											2017-03-16 10:11:03 +01:00
										 |  |  |             data[flen2 + i] *= filterOpp[flen2 + i]; // lsb is the opposite
 | 
					
						
							| 
									
										
										
										
											2017-03-16 02:45:51 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         for (int i = 1; i < flen2; i++) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2017-03-16 10:11:03 +01:00
										 |  |  |             data[i] *= filterOpp[i]; // usb is the opposite
 | 
					
						
							| 
									
										
										
										
											2017-03-16 02:45:51 +01:00
										 |  |  |             data[flen2 + i] *= filter[flen2 + i]; // lsb
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // in-place FFT: freqdata overwritten with filtered timedata
 | 
					
						
							|  |  |  |     fft->InverseComplexFFT(data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // overlap and add
 | 
					
						
							|  |  |  |     for (int i = 0; i < flen2; i++) { | 
					
						
							|  |  |  |         output[i] = ovlbuf[i] + data[i]; | 
					
						
							|  |  |  |         ovlbuf[i] = data[i+flen2]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-15 00:03:34 +01:00
										 |  |  |     std::fill(data, data + flen , cmplx{0, 0}); | 
					
						
							| 
									
										
										
										
											2017-03-16 02:45:51 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     *out = output; | 
					
						
							|  |  |  |     return flen2; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-11 19:30:48 +00:00
										 |  |  | /* Sliding FFT from Fldigi */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct sfft::vrot_bins_pair { | 
					
						
							|  |  |  | 	cmplx vrot; | 
					
						
							|  |  |  | 	cmplx bins; | 
					
						
							|  |  |  | } ; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | sfft::sfft(int len) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	vrot_bins = new vrot_bins_pair[len]; | 
					
						
							|  |  |  | 	delay  = new cmplx[len]; | 
					
						
							|  |  |  | 	fftlen = len; | 
					
						
							|  |  |  | 	first = 0; | 
					
						
							|  |  |  | 	last = len - 1; | 
					
						
							|  |  |  | 	ptr = 0; | 
					
						
							|  |  |  | 	double phi = 0.0, tau = 2.0 * M_PI/ len; | 
					
						
							|  |  |  | 	k2 = 1.0; | 
					
						
							|  |  |  | 	for (int i = 0; i < len; i++) { | 
					
						
							|  |  |  | 		vrot_bins[i].vrot = cmplx( K1 * cos (phi), K1 * sin (phi) ); | 
					
						
							|  |  |  | 		phi += tau; | 
					
						
							|  |  |  | 		delay[i] = vrot_bins[i].bins = 0.0; | 
					
						
							|  |  |  | 		k2 *= K1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | sfft::~sfft() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	delete [] vrot_bins; | 
					
						
							|  |  |  | 	delete [] delay; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Sliding FFT, cmplx input, cmplx output
 | 
					
						
							|  |  |  | // FFT is computed for each value from first to last
 | 
					
						
							|  |  |  | // Values are not stable until more than "len" samples have been processed.
 | 
					
						
							| 
									
										
										
										
											2015-01-13 19:14:36 +00:00
										 |  |  | void sfft::run(const cmplx& input) | 
					
						
							| 
									
										
										
										
											2015-01-11 19:30:48 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	cmplx & de = delay[ptr]; | 
					
						
							|  |  |  | 	const cmplx z( input.real() - k2 * de.real(), input.imag() - k2 * de.imag()); | 
					
						
							|  |  |  | 	de = input; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-13 19:14:36 +00:00
										 |  |  | 	if (++ptr >= fftlen) | 
					
						
							|  |  |  | 		ptr = 0; | 
					
						
							| 
									
										
										
										
											2015-01-11 19:30:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-13 19:14:36 +00:00
										 |  |  | 	for (vrot_bins_pair *itr = vrot_bins + first, *end = vrot_bins + last; itr != end ; ++itr) | 
					
						
							|  |  |  | 		itr->bins = (itr->bins + z) * itr->vrot; | 
					
						
							| 
									
										
										
										
											2015-01-11 19:30:48 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-01-13 19:14:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Copies the frequencies to a pointer.
 | 
					
						
							|  |  |  | void sfft::fetch(float *result) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	for (vrot_bins_pair *itr = vrot_bins, *end = vrot_bins + last;  itr != end; ++itr, ++result) | 
					
						
							|  |  |  | 		*result = itr->bins.real() * itr->bins.real() | 
					
						
							|  |  |  |                         + itr->bins.imag() * itr->bins.imag(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 |