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-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() {
|
|
|
|
generic_block<PolyphaseResampler<T>>::stop();
|
|
|
|
volk_free(buffer);
|
|
|
|
volk_free(taps);
|
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());
|
|
|
|
_window->createTaps(taps, tapCount);
|
|
|
|
for (int i = 0; i < tapCount / 2; i++) {
|
|
|
|
float tap = taps[tapCount - i - 1];
|
2020-11-02 21:13:28 +01:00
|
|
|
taps[tapCount - i - 1] = taps[i] * (float)_interp;
|
|
|
|
taps[i] = tap * (float)_interp;
|
2020-06-22 16:45:57 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 03:57:44 +01:00
|
|
|
buffer = (T*)volk_malloc(STREAM_BUFFER_SIZE * sizeof(T) * 2, volk_get_alignment());
|
|
|
|
bufStart = &buffer[tapCount];
|
|
|
|
generic_block<PolyphaseResampler<T>>::registerInput(_in);
|
|
|
|
generic_block<PolyphaseResampler<T>>::registerOutput(&out);
|
2020-06-22 16:45:57 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 03:57:44 +01:00
|
|
|
void setInput(stream<T>* in) {
|
|
|
|
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;
|
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) {
|
|
|
|
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;
|
|
|
|
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) {
|
|
|
|
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;
|
|
|
|
generic_block<PolyphaseResampler<T>>::tempStart();
|
2020-06-22 16:45:57 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 03:57:44 +01:00
|
|
|
int getInterpolation() {
|
|
|
|
return _interp;
|
2020-06-22 16:45:57 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 03:57:44 +01:00
|
|
|
int getDecimation() {
|
|
|
|
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) {
|
2020-11-02 21:13:28 +01:00
|
|
|
std::lock_guard<std::mutex> lck(generic_block<PolyphaseResampler<T>>::ctrlMtx);
|
|
|
|
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());
|
|
|
|
window->createTaps(taps, tapCount);
|
|
|
|
for (int i = 0; i < tapCount / 2; i++) {
|
|
|
|
float tap = taps[tapCount - i - 1];
|
2020-11-02 21:13:28 +01:00
|
|
|
taps[tapCount - i - 1] = taps[i] * (float)_interp;
|
|
|
|
taps[i] = tap * (float)_interp;
|
2020-06-22 16:45:57 +02:00
|
|
|
}
|
2020-11-02 21:13:28 +01: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) {
|
|
|
|
return (in * _interp) / _decim;
|
2020-06-22 16:45:57 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 03:57:44 +01:00
|
|
|
int run() {
|
|
|
|
count = _in->read();
|
2020-11-02 16:16:21 +01:00
|
|
|
if (count < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2020-07-19 15:59:44 +02:00
|
|
|
|
2020-11-02 03:57:44 +01:00
|
|
|
int outCount = calcOutSize(count);
|
2020-07-19 15:59:44 +02:00
|
|
|
|
2020-11-02 03:57:44 +01:00
|
|
|
memcpy(bufStart, _in->data, count * sizeof(T));
|
|
|
|
_in->flush();
|
2020-07-19 15:59:44 +02:00
|
|
|
|
2020-11-02 03:57:44 +01:00
|
|
|
// Write to output
|
2020-11-02 16:16:21 +01:00
|
|
|
if (out.aquire() < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2020-07-19 15:59:44 +02:00
|
|
|
int outIndex = 0;
|
2020-11-02 21:13:28 +01:00
|
|
|
// if constexpr (std::is_same_v<T, float>) {
|
|
|
|
// // for (int i = 0; outIndex < outCount; i += _decim) {
|
|
|
|
// // out.data[outIndex] = 0;
|
|
|
|
// // for (int j = i % _interp; j < tapCount; j += _interp) {
|
|
|
|
// // out.data[outIndex] += buffer[((i - j) / _interp) + tapCount] * taps[j];
|
|
|
|
// // }
|
|
|
|
// // outIndex++;
|
|
|
|
// // }
|
|
|
|
// for (int i = 0; i < outCount; i++) {
|
|
|
|
// out.data[i] = 1.0f;
|
|
|
|
// }
|
|
|
|
// }
|
2020-11-02 03:57:44 +01:00
|
|
|
if constexpr (std::is_same_v<T, complex_t>) {
|
2020-11-02 21:13:28 +01:00
|
|
|
static_assert(std::is_same_v<T, complex_t>);
|
|
|
|
spdlog::warn("{0}", outCount);
|
2020-11-02 03:57:44 +01:00
|
|
|
for (int i = 0; outIndex < outCount; i += _decim) {
|
|
|
|
out.data[outIndex].i = 0;
|
|
|
|
out.data[outIndex].q = 0;
|
|
|
|
for (int j = i % _interp; j < tapCount; j += _interp) {
|
|
|
|
out.data[outIndex].i += buffer[((i - j) / _interp) + tapCount].i * taps[j];
|
|
|
|
out.data[outIndex].q += buffer[((i - j) / _interp) + tapCount].q * taps[j];
|
2020-07-19 15:59:44 +02:00
|
|
|
}
|
2020-11-02 03:57:44 +01:00
|
|
|
outIndex++;
|
2020-07-19 15:59:44 +02:00
|
|
|
}
|
|
|
|
}
|
2020-11-02 21:13:28 +01:00
|
|
|
out.write(outCount);
|
2020-08-20 18:29:23 +02:00
|
|
|
|
2020-11-02 03:57:44 +01:00
|
|
|
memmove(buffer, &buffer[count], tapCount * sizeof(T));
|
2020-08-20 18:29:23 +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:
|
|
|
|
int count;
|
|
|
|
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
|
|
|
|
2020-11-02 03:57:44 +01:00
|
|
|
};
|
|
|
|
}
|