diff options
author | Eric <ewild@sysmocom.de> | 2023-04-26 22:12:06 +0200 |
---|---|---|
committer | Eric <ewild@sysmocom.de> | 2023-07-07 19:16:40 +0200 |
commit | 6a3e4b32f05a843309d59cb73690885b26549bd7 (patch) | |
tree | 74cf683818130a612cb4929b5577613eff50cccb | |
parent | c3e515a28b2f3c7f7ef27a1ab38ef198da961f1c (diff) |
ms: flexible template for value_type buffer sum
Manual attempts to get the number of complex and single samples right
turned out to be a bit error prone at times...
Change-Id: I3c9953073555e3a7f70b78b0946dfdf949175a82
-rw-r--r-- | Transceiver52M/ms/ms.h | 16 | ||||
-rw-r--r-- | Transceiver52M/ms/ms_rx_lower.cpp | 12 |
2 files changed, 18 insertions, 10 deletions
diff --git a/Transceiver52M/ms/ms.h b/Transceiver52M/ms/ms.h index 8ca9b02..ad2d056 100644 --- a/Transceiver52M/ms/ms.h +++ b/Transceiver52M/ms/ms.h @@ -84,16 +84,19 @@ void convert_and_scale_i(float *dst, const float *src, unsigned int src_len, ST template <typename T> struct is_complex : std::false_type { using baset = T; + static const unsigned int len_mul = 1; }; template <typename T> struct is_complex<std::complex<T>> : std::true_type { using baset = typename std::complex<T>::value_type; + static const unsigned int len_mul = 2; }; template <typename T> struct is_complex<Complex<T>> : std::true_type { using baset = typename Complex<T>::value_type; + static const unsigned int len_mul = 2; }; } // namespace cvt_internal @@ -106,6 +109,19 @@ void convert_and_scale(DST_T *dst, const SRC_T *src, unsigned int src_len, ST sc return cvt_internal::convert_and_scale_i((vd *)dst, (vs *)src, src_len, scale); } +template <typename array_t> +float normed_abs_sum(array_t *src, int len) +{ + using vd = typename cvt_internal::is_complex<array_t>::baset; + auto len_mul = cvt_internal::is_complex<array_t>::len_mul; + auto ptr = reinterpret_cast<const vd *>(src); + float sum = 0; + for (unsigned int i = 0; i < len * len_mul; i++) + sum += std::abs(ptr[i]); + sum /= len * len_mul; + return sum; +} + struct one_burst { one_burst() { diff --git a/Transceiver52M/ms/ms_rx_lower.cpp b/Transceiver52M/ms/ms_rx_lower.cpp index 26ee131..1872c6c 100644 --- a/Transceiver52M/ms/ms_rx_lower.cpp +++ b/Transceiver52M/ms/ms_rx_lower.cpp @@ -101,11 +101,7 @@ void ms_trx::maybe_update_gain(one_burst &brst) const unsigned int rx_max_cutoff = (rxFullScale * 2) / 3; static int gain_check = 0; static float runmean = 0; - float sum = 0; - for (auto i : brst.burst) - sum += abs(i.real()) + abs(i.imag()); - sum /= ONE_TS_BURST_LEN * 2; - + float sum = normed_abs_sum(&brst.burst[0], ONE_TS_BURST_LEN); runmean = gain_check ? (runmean * (gain_check + 2) - 1 + sum) / (gain_check + 2) : sum; if (gain_check == avgburst_num - 1) { @@ -231,12 +227,8 @@ SCH_STATE ms_trx::search_for_sch(dev_buf_t *rcd) sch_pos = 0; rcv_done = true; auto sch_search_fun = [this] { - auto ptr = reinterpret_cast<const int16_t *>(first_sch_buf); const auto target_val = rxFullScale / 8; - float sum = 0; - for (unsigned int i = 0; i < SCH_LEN_SPS * 2; i++) - sum += std::abs(ptr[i]); - sum /= SCH_LEN_SPS * 2; + float sum = normed_abs_sum(first_sch_buf, SCH_LEN_SPS); //FIXME: arbitrary value, gain cutoff if (sum > target_val || rxgain >= 60) // enough ? |