sdrangel/sdrbase/dsp/interpolator.h

163 wiersze
4.0 KiB
C
Czysty Zwykły widok Historia

#ifndef INCLUDE_INTERPOLATOR_H
#define INCLUDE_INTERPOLATOR_H
2015-07-05 15:08:06 +00:00
#ifdef USE_SIMD
#include <immintrin.h>
2015-07-05 15:08:06 +00:00
#endif
#include "dsp/dsptypes.h"
#include "util/export.h"
#include <stdio.h>
2016-05-09 09:02:27 +00:00
#ifndef __WINDOWS__
#include <unistd.h>
#endif
2015-08-29 23:26:51 +00:00
class SDRANGEL_API Interpolator {
public:
Interpolator();
~Interpolator();
void create(int phaseSteps, double sampleRate, double cutoff);
void free();
2014-11-25 08:31:44 +00:00
// Original code allowed for upsampling, but was never used that way
bool decimate(Real *distance, const Complex& next, Complex* result)
{
2014-11-25 08:31:44 +00:00
advanceFilter(next);
*distance -= 1.0;
2014-11-25 08:31:44 +00:00
if (*distance >= 1.0)
{
2014-11-25 08:31:44 +00:00
return false;
}
doInterpolate((int) floor(*distance * (Real)m_phaseSteps), result);
return true;
}
// interpolation works nearly the same way
bool interpolate(Real *distance, const Complex& next, Complex* result)
{
*distance -= 1.0;
if (*distance < 1.0) // use sample
{
advanceFilter(next);
doInterpolate((int) floor(*distance * (Real)m_phaseSteps), result);
return true; // need new input sample and increment distance
}
else // use zero
{
advanceFilter();
doInterpolate((int) floor(*distance * (Real)m_phaseSteps), result);
return false; // input sample was not used and do not increment distance
}
}
private:
float* m_taps;
float* m_alignedTaps;
float* m_taps2;
float* m_alignedTaps2;
std::vector<Complex> m_samples;
int m_ptr;
int m_phaseSteps;
int m_nTaps;
void createTaps(int nTaps, double sampleRate, double cutoff, std::vector<Real>* taps);
void advanceFilter(const Complex& next)
{
m_ptr--;
if(m_ptr < 0)
m_ptr = m_nTaps - 1;
m_samples[m_ptr] = next;
}
void advanceFilter()
{
m_ptr--;
if(m_ptr < 0)
m_ptr = m_nTaps - 1;
m_samples[m_ptr].real(0.0);
m_samples[m_ptr].imag(0.0);
}
void doInterpolate(int phase, Complex* result)
{
2014-11-25 15:24:21 +00:00
if (phase < 0)
phase = 0;
2015-07-07 20:28:50 +00:00
#if USE_SIMD
// beware of the ringbuffer
if(m_ptr == 0) {
// only one straight block
const float* src = (const float*)&m_samples[0];
const __m128* filter = (const __m128*)&m_alignedTaps[phase * m_nTaps * 2];
__m128 sum = _mm_setzero_ps();
int todo = m_nTaps / 2;
for(int i = 0; i < todo; i++) {
sum = _mm_add_ps(sum, _mm_mul_ps(_mm_loadu_ps(src), *filter));
src += 4;
filter += 1;
}
// add upper half to lower half and store
_mm_storel_pi((__m64*)result, _mm_add_ps(sum, _mm_shuffle_ps(sum, _mm_setzero_ps(), _MM_SHUFFLE(1, 0, 3, 2))));
} else {
// two blocks
const float* src = (const float*)&m_samples[m_ptr];
const __m128* filter = (const __m128*)&m_alignedTaps[phase * m_nTaps * 2];
__m128 sum = _mm_setzero_ps();
// first block
int block = m_nTaps - m_ptr;
int todo = block / 2;
if(block & 1)
todo++;
for(int i = 0; i < todo; i++) {
sum = _mm_add_ps(sum, _mm_mul_ps(_mm_loadu_ps(src), *filter));
src += 4;
filter += 1;
}
if(block & 1) {
// one sample beyond the end -> switch coefficient table
filter = (const __m128*)&m_alignedTaps2[phase * m_nTaps * 2 + todo * 4 - 4];
}
// second block
src = (const float*)&m_samples[0];
block = m_ptr;
todo = block / 2;
for(int i = 0; i < todo; i++) {
sum = _mm_add_ps(sum, _mm_mul_ps(_mm_loadu_ps(src), *filter));
src += 4;
filter += 1;
}
if(block & 1) {
// one sample remaining
sum = _mm_add_ps(sum, _mm_mul_ps(_mm_loadl_pi(_mm_setzero_ps(), (const __m64*)src), filter[0]));
}
// add upper half to lower half and store
_mm_storel_pi((__m64*)result, _mm_add_ps(sum, _mm_shuffle_ps(sum, _mm_setzero_ps(), _MM_SHUFFLE(1, 0, 3, 2))));
}
#else
int sample = m_ptr;
const Real* coeff = &m_alignedTaps[phase * m_nTaps * 2];
Real rAcc = 0;
Real iAcc = 0;
for(int i = 0; i < m_nTaps; i++) {
rAcc += *coeff * m_samples[sample].real();
iAcc += *coeff * m_samples[sample].imag();
sample = (sample + 1) % m_nTaps;
coeff += 2;
}
*result = Complex(rAcc, iAcc);
#endif
}
};
#endif // INCLUDE_INTERPOLATOR_H