From 47525ef79654da00c72cfa7b431d50edd7848d09 Mon Sep 17 00:00:00 2001 From: Joris van Rantwijk Date: Tue, 31 Dec 2013 15:19:14 +0100 Subject: [PATCH] Very slight speed improvement in FIR filters. --- Filter.cc | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/Filter.cc b/Filter.cc index 3920a49..930a410 100644 --- a/Filter.cc +++ b/Filter.cc @@ -109,22 +109,27 @@ void LowPassFilterFirIQ::process(const IQSampleVector& samples_in, if (n == 0) return; + // NOTE: We use m_coeff the wrong way around because it is slightly + // faster to scan forward through the array. The result is still correct + // because the coefficients are symmetric. + // The first few samples need data from m_state. unsigned int i = 0; for (; i < n && i < order; i++) { - IQSample y(0); - for (unsigned int j = 0; j <= i; j++) - y += samples_in[i-j] * m_coeff[j]; - for (unsigned int j = i + 1; j <= order; j++) - y += m_state[order + i - j] * m_coeff[j]; + IQSample y = 0; + for (unsigned int j = 0; j < order - i; j++) + y += m_state[i+j] * m_coeff[j]; + for (unsigned int j = order - i; j <= order; j++) + y += samples_in[i-order+j] * m_coeff[j]; samples_out[i] = y; } // Remaining samples only need data from samples_in. for (; i < n; i++) { - IQSample y(0); + IQSample y = 0; + IQSampleVector::const_iterator inp = samples_in.begin() + i - order; for (unsigned int j = 0; j <= order; j++) - y += samples_in[i-j] * m_coeff[j]; + y += inp[j] * m_coeff[j]; samples_out[i] = y; } @@ -182,17 +187,17 @@ void DownsampleFilter::process(const SampleVector& samples_in, unsigned int i = 0; for (; p < n && p < order; p += pstep, i++) { Sample y = 0; - for (unsigned int j = 0; j <= p; j++) + for (unsigned int j = 1; j <= p; j++) y += samples_in[p-j] * m_coeff[j]; for (unsigned int j = p + 1; j <= order; j++) - y += m_state[order + p - j] * m_coeff[j]; + y += m_state[order+p-j] * m_coeff[j]; samples_out[i] = y; } // Remaining samples only need data from samples_in. for (; p < n; p += pstep, i++) { Sample y = 0; - for (unsigned int j = 0; j <= order; j++) + for (unsigned int j = 1; j <= order; j++) y += samples_in[p-j] * m_coeff[j]; samples_out[i] = y; }