From 86f27fc4d609e54d3026f3c197f261619287d06d Mon Sep 17 00:00:00 2001 From: f4exb Date: Sat, 27 Jul 2024 23:29:15 +0200 Subject: [PATCH] WDSP: NBP: use vectors instead of C arrays and disable copy constructor --- wdsp/nbp.cpp | 54 ++++++++++++++++++++++------------------------- wdsp/nbp.hpp | 34 ++++++++++++++++------------- wdsp/resample.cpp | 21 +++--------------- wdsp/resample.hpp | 10 +++++---- 4 files changed, 53 insertions(+), 66 deletions(-) diff --git a/wdsp/nbp.cpp b/wdsp/nbp.cpp index bf75535ce..37bc29bc5 100644 --- a/wdsp/nbp.cpp +++ b/wdsp/nbp.cpp @@ -44,20 +44,11 @@ NOTCHDB::NOTCHDB(int _master_run, int _maxnotches) master_run = _master_run; maxnotches = _maxnotches; nn = 0; - fcenter = new double[maxnotches]; // (float *) malloc0 (maxnotches * sizeof (float)); - fwidth = new double[maxnotches]; // (float *) malloc0 (maxnotches * sizeof (float)); - nlow = new double[maxnotches]; // (float *) malloc0 (maxnotches * sizeof (float)); - nhigh = new double[maxnotches]; // (float *) malloc0 (maxnotches * sizeof (float)); - active = new int[maxnotches]; // (int *) malloc0 (maxnotches * sizeof (int )); -} - -NOTCHDB::~NOTCHDB() -{ - delete[] (active); - delete[] (nhigh); - delete[] (nlow); - delete[] (fwidth); - delete[] (fcenter); + fcenter.resize(maxnotches); // (float *) malloc0 (maxnotches * sizeof (float)); + fwidth.resize(maxnotches); // (float *) malloc0 (maxnotches * sizeof (float)); + nlow.resize(maxnotches); // (float *) malloc0 (maxnotches * sizeof (float)); + nhigh.resize(maxnotches); // (float *) malloc0 (maxnotches * sizeof (float)); + active.resize(maxnotches); // (int *) malloc0 (maxnotches * sizeof (int )); } int NOTCHDB::addNotch(int notch, double _fcenter, double _fwidth, int _active) @@ -198,17 +189,17 @@ double NBP::min_notch_width() int NBP::make_nbp ( int nn, - int* active, - double* center, - double* width, - double* nlow, - double* nhigh, + std::vector& active, + std::vector& center, + std::vector& width, + std::vector& nlow, + std::vector& nhigh, double minwidth, int autoincr, double flow, double fhigh, - double* bplow, - double* bphigh, + std::vector& bplow, + std::vector& bphigh, int* havnotch ) { @@ -328,8 +319,15 @@ void NBP::calc_lightweight() bplow[i] -= offset; bphigh[i] -= offset; } - impulse = fir_mbandpass (nc, numpb, bplow, bphigh, - rate, gain / (float)(2 * size), wintype); + impulse = fir_mbandpass ( + nc, + numpb, + bplow.data(), + bphigh.data(), + rate, + gain / (float)(2 * size), + wintype + ); FIRCORE::setImpulse_fircore (fircore, impulse, 1); // print_impulse ("nbp.txt", size + 1, impulse, 1, 0); delete[](impulse); @@ -375,8 +373,8 @@ void NBP::calc_impulse () impulse = fir_mbandpass ( nc, numpb, - bplow, - bphigh, + bplow.data(), + bphigh.data(), rate, gain / (float)(2 * size), wintype @@ -431,8 +429,8 @@ NBP::NBP( maxpb(_maxpb), notchdb(_notchdb) { - bplow = new double[maxpb]; // (float *) malloc0 (maxpb * sizeof (float)); - bphigh = new double[maxpb]; // (float *) malloc0 (maxpb * sizeof (float)); + bplow.resize(maxpb); // (float *) malloc0 (maxpb * sizeof (float)); + bphigh.resize(maxpb); // (float *) malloc0 (maxpb * sizeof (float)); calc_impulse (); fircore = FIRCORE::create_fircore (size, in, out, nc, mp, impulse); // print_impulse ("nbp.txt", size + 1, impulse, 1, 0); @@ -442,8 +440,6 @@ NBP::NBP( NBP::~NBP() { FIRCORE::destroy_fircore (fircore); - delete[] (bphigh); - delete[] (bplow); } void NBP::flush() diff --git a/wdsp/nbp.hpp b/wdsp/nbp.hpp index 23d0a4b5b..97b52c5b3 100644 --- a/wdsp/nbp.hpp +++ b/wdsp/nbp.hpp @@ -28,6 +28,8 @@ warren@wpratt.com #ifndef wdsp_nbp_h #define wdsp_nbp_h +#include + #include "export.h" namespace WDSP { @@ -41,15 +43,16 @@ public: double tunefreq; double shift; int nn; - int* active; - double* fcenter; - double* fwidth; - double* nlow; - double* nhigh; + std::vector active; + std::vector fcenter; + std::vector fwidth; + std::vector nlow; + std::vector nhigh; int maxnotches; NOTCHDB(int master_run, int maxnotches); - ~NOTCHDB(); + NOTCHDB(const NOTCHDB&) = delete; + ~NOTCHDB() = default; int addNotch (int notch, double fcenter, double fwidth, int active); int getNotch (int notch, double* fcenter, double* fwidth, int* active); @@ -79,8 +82,8 @@ public: float* impulse; // filter impulse response int maxpb; // maximum number of passbands NOTCHDB* notchdb; // ptr to addr of notch-database data structure - double* bplow; // array of passband lows - double* bphigh; // array of passband highs + std::vector bplow; // array of passband lows + std::vector bphigh; // array of passband highs int numpb; // number of passbands FIRCORE *fircore; int havnotch; @@ -104,6 +107,7 @@ public: int maxpb, NOTCHDB* notchdb ); + NBP(const NBP&) = delete; ~NBP(); void flush(); @@ -127,17 +131,17 @@ private: double min_notch_width (); static int make_nbp ( int nn, - int* active, - double* center, - double* width, - double* nlow, - double* nhigh, + std::vector& active, + std::vector& center, + std::vector& width, + std::vector& nlow, + std::vector& nhigh, double minwidth, int autoincr, double flow, double fhigh, - double* bplow, - double* bphigh, + std::vector& bplow, + std::vector& bphigh, int* havnotch ); }; diff --git a/wdsp/resample.cpp b/wdsp/resample.cpp index b06ed959f..e4f24309c 100644 --- a/wdsp/resample.cpp +++ b/wdsp/resample.cpp @@ -84,7 +84,7 @@ void RESAMPLE::calc() ncoef = (ncoef / L + 1) * L; cpp = ncoef / L; - h = new double[ncoef]; // (float *)malloc0(ncoef * sizeof(float)); + h.resize(ncoef); // (float *)malloc0(ncoef * sizeof(float)); impulse = FIR::fir_bandpass(ncoef, fc_norm_low, fc_norm_high, 1.0, 1, 0, gain * (double)L); i = 0; @@ -95,19 +95,13 @@ void RESAMPLE::calc() } ringsize = cpp; - ring = new double[ringsize]; // (float *)malloc0(ringsize * sizeof(complex)); + ring.resize(ringsize); // (float *)malloc0(ringsize * sizeof(complex)); idx_in = ringsize - 1; phnum = 0; delete[] (impulse); } -void RESAMPLE::decalc() -{ - delete[] ring; - delete[] h; -} - RESAMPLE::RESAMPLE ( int _run, int _size, @@ -133,15 +127,10 @@ RESAMPLE::RESAMPLE ( calc(); } -RESAMPLE::~RESAMPLE() -{ - decalc(); -} - void RESAMPLE::flush() { - std::fill(ring, ring + 2 * ringsize, 0); + std::fill(ring.begin(), ring.end(), 0); idx_in = ringsize - 1; phnum = 0; } @@ -210,14 +199,12 @@ void RESAMPLE::setSize(int _size) void RESAMPLE::setInRate(int _rate) { - decalc(); in_rate = _rate; calc(); } void RESAMPLE::setOutRate(int _rate) { - decalc(); out_rate = _rate; calc(); } @@ -226,7 +213,6 @@ void RESAMPLE::setFCLow(double _fc_low) { if (fc_low != _fc_low) { - decalc(); fc_low = _fc_low; calc(); } @@ -236,7 +222,6 @@ void RESAMPLE::setBandwidth(double _fc_low, double _fc_high) { if (fc_low != _fc_low || _fc_high != fcin) { - decalc(); fc_low = _fc_low; fcin = _fc_high; calc(); diff --git a/wdsp/resample.hpp b/wdsp/resample.hpp index 8f4d7f95c..d442039fe 100644 --- a/wdsp/resample.hpp +++ b/wdsp/resample.hpp @@ -34,6 +34,8 @@ warren@wpratt.com #ifndef wdsp_resample_h #define wdsp_resample_h +#include + #include "export.h" namespace WDSP { @@ -56,9 +58,9 @@ public: int ncoef; // number of coefficients int L; // interpolation factor int M; // decimation factor - double* h; // coefficients + std::vector h; // coefficients int ringsize; // number of complex pairs the ring buffer holds - double* ring; // ring buffer + std::vector ring; // ring buffer int cpp; // coefficients of the phase int phnum; // phase number @@ -73,7 +75,8 @@ public: int ncoef, double gain ); - ~RESAMPLE(); + RESAMPLE(const RESAMPLE&) = delete; + ~RESAMPLE() = default; void flush(); int execute(); @@ -90,7 +93,6 @@ public: private: void calc(); - void decalc(); }; } // namespace WDSP