SDRPlusPlus/core/src/dsp/resampling.h

218 lines
7.7 KiB
C
Raw Normal View History

2020-06-22 16:45:57 +02:00
#pragma once
2020-11-02 03:57:44 +01:00
#include <dsp/block.h>
#include <dsp/window.h>
2020-11-02 17:48:17 +01:00
#include <numeric>
2020-11-12 00:53:38 +01:00
#include <string.h>
2021-04-16 19:53:47 +02:00
#include <dsp/math.h>
2020-06-22 16:45:57 +02:00
namespace dsp {
template <class T>
2020-11-02 03:57:44 +01:00
class PolyphaseResampler : public generic_block<PolyphaseResampler<T>> {
2020-06-22 16:45:57 +02:00
public:
2020-11-02 03:57:44 +01:00
PolyphaseResampler() {}
2020-06-22 16:45:57 +02:00
2020-11-02 03:57:44 +01:00
PolyphaseResampler(stream<T>* in, dsp::filter_window::generic_window* window, float inSampleRate, float outSampleRate) { init(in, window, inSampleRate, outSampleRate); }
2020-06-22 16:45:57 +02:00
2020-11-02 03:57:44 +01:00
~PolyphaseResampler() {
2021-07-12 05:03:51 +02:00
if (!generic_block<PolyphaseResampler<T>>::_block_init) { return; }
2020-11-02 03:57:44 +01:00
generic_block<PolyphaseResampler<T>>::stop();
volk_free(buffer);
volk_free(taps);
2020-12-26 21:33:33 +01:00
freeTapPhases();
2021-07-12 05:03:51 +02:00
generic_block<PolyphaseResampler<T>>::_block_init = false;
2020-06-22 16:45:57 +02:00
}
2020-11-02 03:57:44 +01:00
void init(stream<T>* in, dsp::filter_window::generic_window* window, float inSampleRate, float outSampleRate) {
_in = in;
_window = window;
_inSampleRate = inSampleRate;
_outSampleRate = outSampleRate;
2020-06-22 16:45:57 +02:00
2020-11-02 21:13:28 +01:00
int _gcd = std::gcd((int)_inSampleRate, (int)_outSampleRate);
_interp = _outSampleRate / _gcd;
_decim = _inSampleRate / _gcd;
2020-11-02 03:57:44 +01:00
tapCount = _window->getTapCount();
taps = (float*)volk_malloc(tapCount * sizeof(float), volk_get_alignment());
2020-11-04 00:42:39 +01:00
_window->createTaps(taps, tapCount, _interp);
2020-06-22 16:45:57 +02:00
2020-12-26 21:33:33 +01:00
buildTapPhases();
2020-11-02 03:57:44 +01:00
buffer = (T*)volk_malloc(STREAM_BUFFER_SIZE * sizeof(T) * 2, volk_get_alignment());
2020-11-04 00:42:39 +01:00
memset(buffer, 0, STREAM_BUFFER_SIZE * sizeof(T) * 2);
2021-07-13 20:34:31 +02:00
counter = 0;
offset = 0;
2020-11-02 03:57:44 +01:00
generic_block<PolyphaseResampler<T>>::registerInput(_in);
generic_block<PolyphaseResampler<T>>::registerOutput(&out);
2021-07-12 05:03:51 +02:00
generic_block<PolyphaseResampler<T>>::_block_init = true;
2020-06-22 16:45:57 +02:00
}
2020-11-02 03:57:44 +01:00
void setInput(stream<T>* in) {
2021-07-12 05:03:51 +02:00
assert(generic_block<PolyphaseResampler<T>>::_block_init);
2020-11-02 03:57:44 +01:00
std::lock_guard<std::mutex> lck(generic_block<PolyphaseResampler<T>>::ctrlMtx);
generic_block<PolyphaseResampler<T>>::tempStop();
2020-11-02 17:48:17 +01:00
generic_block<PolyphaseResampler<T>>::unregisterInput(_in);
2020-11-02 03:57:44 +01:00
_in = in;
2021-07-13 20:34:31 +02:00
counter = 0;
offset = 0;
2020-11-02 17:48:17 +01:00
generic_block<PolyphaseResampler<T>>::registerInput(_in);
2020-11-02 03:57:44 +01:00
generic_block<PolyphaseResampler<T>>::tempStart();
2020-06-22 16:45:57 +02:00
}
2020-11-02 03:57:44 +01:00
void setInSampleRate(float inSampleRate) {
2021-07-12 05:03:51 +02:00
assert(generic_block<PolyphaseResampler<T>>::_block_init);
2020-11-02 03:57:44 +01:00
std::lock_guard<std::mutex> lck(generic_block<PolyphaseResampler<T>>::ctrlMtx);
generic_block<PolyphaseResampler<T>>::tempStop();
_inSampleRate = inSampleRate;
int _gcd = std::gcd((int)_inSampleRate, (int)_outSampleRate);
_interp = _outSampleRate / _gcd;
_decim = _inSampleRate / _gcd;
2020-12-26 21:33:33 +01:00
buildTapPhases();
2021-07-13 20:34:31 +02:00
counter = 0;
offset = 0;
2020-11-02 03:57:44 +01:00
generic_block<PolyphaseResampler<T>>::tempStart();
2020-06-22 16:45:57 +02:00
}
2020-11-02 03:57:44 +01:00
void setOutSampleRate(float outSampleRate) {
2021-07-12 05:03:51 +02:00
assert(generic_block<PolyphaseResampler<T>>::_block_init);
2020-11-02 03:57:44 +01:00
std::lock_guard<std::mutex> lck(generic_block<PolyphaseResampler<T>>::ctrlMtx);
generic_block<PolyphaseResampler<T>>::tempStop();
_outSampleRate = outSampleRate;
int _gcd = std::gcd((int)_inSampleRate, (int)_outSampleRate);
_interp = _outSampleRate / _gcd;
_decim = _inSampleRate / _gcd;
2020-12-26 21:33:33 +01:00
buildTapPhases();
2021-07-13 20:34:31 +02:00
counter = 0;
offset = 0;
2020-11-02 03:57:44 +01:00
generic_block<PolyphaseResampler<T>>::tempStart();
2020-06-22 16:45:57 +02:00
}
2020-11-02 03:57:44 +01:00
int getInterpolation() {
2021-07-12 05:03:51 +02:00
assert(generic_block<PolyphaseResampler<T>>::_block_init);
2020-11-02 03:57:44 +01:00
return _interp;
2020-06-22 16:45:57 +02:00
}
2020-11-02 03:57:44 +01:00
int getDecimation() {
2021-07-12 05:03:51 +02:00
assert(generic_block<PolyphaseResampler<T>>::_block_init);
2020-11-02 03:57:44 +01:00
return _decim;
2020-06-22 16:45:57 +02:00
}
2020-11-02 03:57:44 +01:00
void updateWindow(dsp::filter_window::generic_window* window) {
2021-07-12 05:03:51 +02:00
assert(generic_block<PolyphaseResampler<T>>::_block_init);
2020-11-02 21:13:28 +01:00
std::lock_guard<std::mutex> lck(generic_block<PolyphaseResampler<T>>::ctrlMtx);
2021-04-18 01:15:57 +02:00
generic_block<PolyphaseResampler<T>>::tempStop();
2020-11-02 03:57:44 +01:00
_window = window;
volk_free(taps);
tapCount = window->getTapCount();
taps = (float*)volk_malloc(tapCount * sizeof(float), volk_get_alignment());
2020-11-04 00:42:39 +01:00
window->createTaps(taps, tapCount, _interp);
2020-12-26 21:33:33 +01:00
buildTapPhases();
2021-07-13 20:34:31 +02:00
counter = 0;
offset = 0;
2021-04-18 01:15:57 +02:00
generic_block<PolyphaseResampler<T>>::tempStart();
2020-06-22 16:45:57 +02:00
}
2020-11-02 03:57:44 +01:00
int calcOutSize(int in) {
2021-07-12 05:03:51 +02:00
assert(generic_block<PolyphaseResampler<T>>::_block_init);
2020-11-02 03:57:44 +01:00
return (in * _interp) / _decim;
2020-06-22 16:45:57 +02:00
}
2020-11-03 19:22:53 +01:00
virtual int run() override {
2021-03-29 21:53:43 +02:00
int count = _in->read();
2020-11-02 16:16:21 +01:00
if (count < 0) {
return -1;
}
2020-07-19 15:59:44 +02:00
2021-04-18 01:15:57 +02:00
memcpy(&buffer[tapsPerPhase], _in->readBuf, count * sizeof(T));
_in->flush();
// Write to output
int outIndex = 0;
2021-07-13 18:47:34 +02:00
int inOffset = offset;
int _counter = counter;
2021-04-18 01:15:57 +02:00
if constexpr (std::is_same_v<T, float>) {
2021-07-13 18:47:34 +02:00
while (inOffset < count) {
volk_32f_x2_dot_prod_32f(&out.writeBuf[outIndex++], &buffer[inOffset], tapPhases[_counter], tapsPerPhase);
_counter += _decim;
2021-07-13 20:15:42 +02:00
inOffset += (_counter / _interp);
_counter = (_counter % _interp);
2021-04-18 00:47:23 +02:00
}
2021-04-18 01:15:57 +02:00
}
if constexpr (std::is_same_v<T, complex_t> || std::is_same_v<T, stereo_t>) {
2021-07-13 18:47:34 +02:00
while (inOffset < count) {
volk_32fc_32f_dot_prod_32fc((lv_32fc_t*)&out.writeBuf[outIndex++], (lv_32fc_t*)&buffer[inOffset], tapPhases[_counter], tapsPerPhase);
_counter += _decim;
2021-07-13 20:15:42 +02:00
inOffset += (_counter / _interp);
_counter = (_counter % _interp);
2020-07-19 15:59:44 +02:00
}
2021-04-18 00:47:23 +02:00
}
2021-07-13 20:15:42 +02:00
2021-07-13 18:47:34 +02:00
if (!out.swap(outIndex)) { return -1; }
offset = inOffset - count;
counter = _counter;
2021-04-18 01:15:57 +02:00
memmove(buffer, &buffer[count], tapsPerPhase * sizeof(T));
2021-04-17 22:37:50 +02:00
2020-11-02 03:57:44 +01:00
return count;
}
2020-08-20 18:29:23 +02:00
2020-11-02 03:57:44 +01:00
stream<T> out;
2020-08-20 18:29:23 +02:00
2020-11-02 03:57:44 +01:00
private:
void buildTapPhases() {
if (!taps) {
2020-12-26 21:33:33 +01:00
return;
}
if (!tapPhases.empty()) {
2020-12-26 21:33:33 +01:00
freeTapPhases();
}
int phases = _interp;
tapsPerPhase = (tapCount + phases - 1) / phases; //Integer division ceiling
2020-12-26 21:33:33 +01:00
bufStart = &buffer[tapsPerPhase];
for (int i = 0; i < phases; i++) {
2020-12-26 21:33:33 +01:00
tapPhases.push_back((float*)volk_malloc(tapsPerPhase * sizeof(float), volk_get_alignment()));
}
int currentTap = 0;
for (int tap = 0; tap < tapsPerPhase; tap++) {
2020-12-26 21:33:33 +01:00
for (int phase = 0; phase < phases; phase++) {
if (currentTap < tapCount) {
tapPhases[(_interp - 1) - phase][tap] = taps[currentTap++];
2020-12-26 21:33:33 +01:00
}
else {
tapPhases[(_interp - 1) - phase][tap] = 0;
2020-12-26 21:33:33 +01:00
}
}
}
}
void freeTapPhases() {
for (auto& tapPhase : tapPhases) {
2020-12-26 21:33:33 +01:00
volk_free(tapPhase);
}
tapPhases.clear();
}
2020-11-02 03:57:44 +01:00
stream<T>* _in;
2020-08-20 18:29:23 +02:00
2020-11-02 03:57:44 +01:00
dsp::filter_window::generic_window* _window;
2020-09-18 00:23:03 +02:00
2020-11-02 03:57:44 +01:00
T* bufStart;
T* buffer;
int tapCount;
int _interp, _decim;
float _inSampleRate, _outSampleRate;
float* taps;
2020-09-18 00:23:03 +02:00
2021-07-13 18:47:34 +02:00
int counter = 0;
int offset = 0;
2020-12-26 21:33:33 +01:00
int tapsPerPhase;
std::vector<float*> tapPhases;
2020-11-02 03:57:44 +01:00
};
}