1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-14 12:22:00 -05:00
sdrangel/wdsp/shift.cpp

137 lines
3.1 KiB
C++
Raw Normal View History

2024-06-16 05:31:13 -04:00
/* shift.c
This file is part of a program that implements a Software-Defined Radio.
Copyright (C) 2013 Warren Pratt, NR0V
Copyright (C) 2024 Edouard Griffiths, F4EXB Adapted to SDRangel
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
The author can be reached by email at
warren@wpratt.com
*/
#include "comm.hpp"
#include "shift.hpp"
namespace WDSP {
2024-07-23 22:52:00 -04:00
void SHIFT::calc()
2024-06-16 05:31:13 -04:00
{
delta = TWOPI * shift / rate;
cos_delta = cos (delta);
sin_delta = sin (delta);
2024-06-16 05:31:13 -04:00
}
2024-07-23 22:52:00 -04:00
SHIFT::SHIFT (
int _run,
int _size,
float* _in,
float* _out,
int _rate,
double _fshift
) :
run(_run),
size(_size),
in(_in),
out(_out),
rate((double) _rate),
2024-08-03 05:05:12 -04:00
shift(_fshift),
phase(0.0)
2024-06-16 05:31:13 -04:00
{
2024-07-23 22:52:00 -04:00
calc();
2024-06-16 05:31:13 -04:00
}
void SHIFT::flush()
2024-06-16 05:31:13 -04:00
{
phase = 0.0;
2024-06-16 05:31:13 -04:00
}
void SHIFT::execute()
2024-06-16 05:31:13 -04:00
{
if (run)
2024-06-16 05:31:13 -04:00
{
2024-08-03 05:05:12 -04:00
double I1;
double Q1;
double t1;
double t2;
double cos_phase = cos (phase);
double sin_phase = sin (phase);
2024-08-03 05:05:12 -04:00
for (int i = 0; i < size; i++)
2024-06-16 05:31:13 -04:00
{
I1 = in[2 * i + 0];
Q1 = in[2 * i + 1];
2024-08-03 05:05:12 -04:00
out[2 * i + 0] = (float) (I1 * cos_phase - Q1 * sin_phase);
out[2 * i + 1] = (float) (I1 * sin_phase + Q1 * cos_phase);
2024-06-16 05:31:13 -04:00
t1 = cos_phase;
t2 = sin_phase;
cos_phase = t1 * cos_delta - t2 * sin_delta;
sin_phase = t1 * sin_delta + t2 * cos_delta;
phase += delta;
if (phase >= TWOPI)
phase -= TWOPI;
if (phase < 0.0 )
phase += TWOPI;
2024-06-16 05:31:13 -04:00
}
}
else if (in != out)
{
std::copy( in, in + size * 2, out);
}
2024-06-16 05:31:13 -04:00
}
void SHIFT::setBuffers(float* _in, float* _out)
2024-06-16 05:31:13 -04:00
{
in = _in;
out = _out;
2024-06-16 05:31:13 -04:00
}
void SHIFT::setSamplerate(int _rate)
2024-06-16 05:31:13 -04:00
{
rate = _rate;
phase = 0.0;
2024-07-23 22:52:00 -04:00
calc();
2024-06-16 05:31:13 -04:00
}
void SHIFT::setSize(int _size)
2024-06-16 05:31:13 -04:00
{
size = _size;
flush();
2024-06-16 05:31:13 -04:00
}
/********************************************************************************************************
* *
* Non static *
2024-06-16 05:31:13 -04:00
* *
********************************************************************************************************/
void SHIFT::SetRun (int _run)
2024-06-16 05:31:13 -04:00
{
run = _run;
2024-06-16 05:31:13 -04:00
}
2024-07-23 22:52:00 -04:00
void SHIFT::SetFreq(double _fshift)
2024-06-16 05:31:13 -04:00
{
2024-07-23 22:52:00 -04:00
shift = _fshift;
calc();
2024-06-16 05:31:13 -04:00
}
} // namespace WDSP