satdump/src-core/common/dsp/utils/freq_shift.cpp

51 lines
1.4 KiB
C++
Raw Permalink Normal View History

2021-06-09 00:33:52 +02:00
#include "freq_shift.h"
#include <volk/volk.h>
#include <cmath>
2021-06-09 18:30:25 +02:00
#ifndef M_PI
#define M_PI 3.14159265358979323846 /* pi */
#endif
2021-06-09 00:33:52 +02:00
namespace dsp
{
2023-02-21 16:44:44 +01:00
FreqShiftBlock::FreqShiftBlock(std::shared_ptr<dsp::stream<complex_t>> input, double samplerate, double shift) : Block(input)
2021-06-09 00:33:52 +02:00
{
2023-02-21 16:44:44 +01:00
set_freq(samplerate, shift);
2021-06-09 00:33:52 +02:00
}
void FreqShiftBlock::work()
{
int nsamples = input_stream->read();
if (nsamples <= 0)
2021-08-13 10:55:45 +02:00
{
input_stream->flush();
2021-06-09 00:33:52 +02:00
return;
2021-08-13 10:55:45 +02:00
}
2021-10-22 15:26:17 +02:00
2023-02-21 16:44:44 +01:00
mtx.lock();
2024-04-01 14:22:49 +02:00
#if VOLK_VERSION >= 030100
volk_32fc_s32fc_x2_rotator2_32fc((lv_32fc_t *)output_stream->writeBuf, (lv_32fc_t *)input_stream->readBuf, (lv_32fc_t *)&phase_delta, (lv_32fc_t *)&phase, nsamples);
#else
2021-10-22 15:26:17 +02:00
volk_32fc_s32fc_x2_rotator_32fc((lv_32fc_t *)output_stream->writeBuf, (lv_32fc_t *)input_stream->readBuf, phase_delta, (lv_32fc_t *)&phase, nsamples);
2024-04-01 14:22:49 +02:00
#endif
2023-02-21 16:44:44 +01:00
mtx.unlock();
2021-10-22 15:26:17 +02:00
2021-06-09 00:33:52 +02:00
input_stream->flush();
output_stream->swap(nsamples);
}
2022-03-29 15:49:18 +02:00
2023-02-21 16:44:44 +01:00
void FreqShiftBlock::set_freq(double samplerate, double shift)
{
mtx.lock();
phase = complex_t(1, 0);
phase_delta = complex_t(cos(hz_to_rad(shift, samplerate)), sin(hz_to_rad(shift, samplerate)));
mtx.unlock();
}
void FreqShiftBlock::set_freq_raw(double freq)
2022-03-29 15:49:18 +02:00
{
2023-02-21 16:44:44 +01:00
mtx.lock();
2022-03-29 15:49:18 +02:00
phase_delta = complex_t(cosf(freq), sinf(freq));
2023-02-21 16:44:44 +01:00
mtx.unlock();
2022-03-29 15:49:18 +02:00
}
2021-06-09 00:33:52 +02:00
}