From 28cfad98ff48bb5cc7a09f3821f5853f07b6312f Mon Sep 17 00:00:00 2001 From: f4exb Date: Sat, 10 Aug 2024 02:06:19 +0200 Subject: [PATCH] WDSP: impulse responses refactoring (3) --- wdsp/fcurve.cpp | 2 +- wdsp/fcurve.hpp | 2 +- wdsp/fir.cpp | 28 ++++++++++++---------------- wdsp/fir.hpp | 6 +++--- 4 files changed, 17 insertions(+), 21 deletions(-) diff --git a/wdsp/fcurve.cpp b/wdsp/fcurve.cpp index 9e0c74bb6..d70773caf 100644 --- a/wdsp/fcurve.cpp +++ b/wdsp/fcurve.cpp @@ -149,7 +149,7 @@ void FCurve::fc_impulse (std::vector& impulse, int nc, float f0, float f1 } // generate mask for Overlap-Save Filter -float* FCurve::fc_mults (std::vector& mults, int size, float f0, float f1, float g0, float g1, int curve, float samplerate, float scale, int ctfmode, int wintype) +void FCurve::fc_mults (std::vector& mults, int size, float f0, float f1, float g0, float g1, int curve, float samplerate, float scale, int ctfmode, int wintype) { std::vector impulse(2 * (size + 1)); fc_impulse (impulse, size + 1, f0, f1, g0, g1, curve, samplerate, scale, ctfmode, wintype); diff --git a/wdsp/fcurve.hpp b/wdsp/fcurve.hpp index 4b33b1eb9..1d05bb339 100644 --- a/wdsp/fcurve.hpp +++ b/wdsp/fcurve.hpp @@ -38,7 +38,7 @@ class WDSP_API FCurve { public: static void fc_impulse (std::vector& impulse, int nc, float f0, float f1, float g0, float g1, int curve, float samplerate, float scale, int ctfmode, int wintype); - static float* fc_mults (std::vector& mults, int size, float f0, float f1, float g0, float g1, int curve, float samplerate, float scale, int ctfmode, int wintype); + static void fc_mults (std::vector& mults, int size, float f0, float f1, float g0, float g1, int curve, float samplerate, float scale, int ctfmode, int wintype); }; } // namespace WDSP diff --git a/wdsp/fir.cpp b/wdsp/fir.cpp index aba6ddaae..956aac8ba 100644 --- a/wdsp/fir.cpp +++ b/wdsp/fir.cpp @@ -53,11 +53,11 @@ void FIR::fftcv_mults (std::vector& mults, int NM, float* c_impulse) fftwf_destroy_plan (ptmp); } -float* FIR::get_fsamp_window(int N, int wintype) +void FIR::get_fsamp_window(std::vector& window, int N, int wintype) { double arg0; double arg1; - auto window = new float[N]; + window.resize(N); switch (wintype) { case 0: @@ -91,7 +91,6 @@ float* FIR::get_fsamp_window(int N, int wintype) for (int i = 0; i < N; i++) window[i] = 1.0; } - return window; } void FIR::fir_fsamp_odd (std::vector& c_impulse, int N, const float* A, int rtype, double scale, int wintype) @@ -122,7 +121,8 @@ void FIR::fir_fsamp_odd (std::vector& c_impulse, int N, const float* A, i } fftwf_execute (ptmp); fftwf_destroy_plan (ptmp); - float* window = get_fsamp_window(N, wintype); + std::vector window; + get_fsamp_window(window, N, wintype); switch (rtype) { case 0: @@ -139,7 +139,6 @@ void FIR::fir_fsamp_odd (std::vector& c_impulse, int N, const float* A, i default: break; } - delete[] window; } void FIR::fir_fsamp (std::vector& c_impulse, int N, const float* A, int rtype, double scale, int wintype) @@ -180,7 +179,8 @@ void FIR::fir_fsamp (std::vector& c_impulse, int N, const float* A, int r c_impulse[2 * n + 1] = 0.0; } } - float* window = get_fsamp_window (N, wintype); + std::vector window; + get_fsamp_window (window, N, wintype); switch (rtype) { case 0: @@ -197,7 +197,6 @@ void FIR::fir_fsamp (std::vector& c_impulse, int N, const float* A, int r default: break; } - delete[] window; } float* FIR::fir_bandpass (int N, double f_low, double f_high, double samplerate, int wintype, int rtype, double scale) @@ -277,7 +276,7 @@ float* FIR::fir_bandpass (int N, double f_low, double f_high, double samplerate, return c_impulse; } -float *FIR::fir_read (int N, const char *filename, int rtype, float scale) +void FIR::fir_read (std::vector& c_impulse, int N, const char *filename, int rtype, float scale) // N = number of real or complex coefficients (see rtype) // *filename = filename // rtype = 0: real coefficients @@ -289,12 +288,12 @@ float *FIR::fir_read (int N, const char *filename, int rtype, float scale) FILE *file; float I; float Q; - auto c_impulse = new float[N * 2]; - std::fill(c_impulse, c_impulse + N*2, 0); + c_impulse.resize(N * 2); + std::fill(c_impulse.begin(), c_impulse.end(), 0); file = fopen (filename, "r"); if (!file) { - return c_impulse; + return; } for (int i = 0; i < N; i++) @@ -325,7 +324,6 @@ float *FIR::fir_read (int N, const char *filename, int rtype, float scale) } } fclose (file); - return c_impulse; } void FIR::analytic (int N, float* in, float* out) @@ -430,7 +428,7 @@ void FIR::mp_imp (int N, std::vector& fir, std::vector& mpfir, int // impulse response of a zero frequency filter comprising a cascade of two resonators, // each followed by a detrending filter -float* FIR::zff_impulse(int nc, float scale) +void FIR::zff_impulse(std::vector& c_dresdet, int nc, float scale) { // nc = number of coefficients (power of two) int n_resdet = nc / 2 - 1; // size of single zero-frequency resonator with detrender @@ -444,7 +442,7 @@ float* FIR::zff_impulse(int nc, float scale) // allocate the float and complex versions and make the values std::vector dresdet(n_dresdet); auto div = (float) ((nc / 2 + 1) * (nc / 2 + 1)); // calculate divisor - auto c_dresdet = new float[nc * 2]; + c_dresdet.resize(nc * 2); for (int n = 0; n < n_dresdet; n++) // convolve to make the cascade { for (int k = 0; k < n_resdet; k++) @@ -454,8 +452,6 @@ float* FIR::zff_impulse(int nc, float scale) c_dresdet[2 * n + 0] = dresdet[n] * scale; c_dresdet[2 * n + 1] = 0.0; } - - return c_dresdet; } } // namespace WDSP diff --git a/wdsp/fir.hpp b/wdsp/fir.hpp index 2cf4a5f4f..39e3b63e6 100644 --- a/wdsp/fir.hpp +++ b/wdsp/fir.hpp @@ -44,9 +44,9 @@ public: private: static void analytic (int N, float* in, float* out); - static float* get_fsamp_window(int N, int wintype); - static float *fir_read (int N, const char *filename, int rtype, float scale); - static float* zff_impulse(int nc, float scale); + static void get_fsamp_window(std::vector& window, int N, int wintype); + static void fir_read (std::vector& impulse, int N, const char *filename, int rtype, float scale); + static void zff_impulse(std::vector& impulse, int nc, float scale); }; #endif