2016-12-18 20:41:31 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Copyright (C) 2016 Edouard Griffiths, F4EXB //
|
|
|
|
// //
|
|
|
|
// 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 as version 3 of the License, or //
|
2019-04-11 08:32:15 -04:00
|
|
|
// (at your option) any later version. //
|
2016-12-18 20:41:31 -05:00
|
|
|
// //
|
|
|
|
// 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 V3 for more details. //
|
|
|
|
// //
|
|
|
|
// You should have received a copy of the GNU General Public License //
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include <QtGlobal>
|
|
|
|
#include <stdio.h>
|
2020-11-03 07:52:12 -05:00
|
|
|
#include <cmath>
|
2016-12-18 20:41:31 -05:00
|
|
|
#include "dsp/ncof.h"
|
|
|
|
|
2018-02-15 17:58:01 -05:00
|
|
|
Real NCOF::m_table[NCOF::TableSize+1];
|
2016-12-18 20:41:31 -05:00
|
|
|
bool NCOF::m_tableInitialized = false;
|
2018-02-15 17:58:01 -05:00
|
|
|
float NCOF::m_tableSizeLimit = (float) NCOF::TableSize;
|
2016-12-18 20:41:31 -05:00
|
|
|
|
|
|
|
void NCOF::initTable()
|
|
|
|
{
|
2018-02-15 17:58:01 -05:00
|
|
|
if(m_tableInitialized) {
|
2016-12-18 20:41:31 -05:00
|
|
|
return;
|
2018-02-15 17:58:01 -05:00
|
|
|
}
|
2016-12-18 20:41:31 -05:00
|
|
|
|
2018-02-15 17:58:01 -05:00
|
|
|
for(int i = 0; i <= TableSize; i++) {
|
2016-12-18 20:41:31 -05:00
|
|
|
m_table[i] = cos((2.0 * M_PI * i) / TableSize);
|
2018-02-15 17:58:01 -05:00
|
|
|
}
|
2016-12-18 20:41:31 -05:00
|
|
|
|
|
|
|
m_tableInitialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
NCOF::NCOF()
|
|
|
|
{
|
|
|
|
initTable();
|
|
|
|
m_phase = 0.0f;
|
2018-02-21 21:04:42 -05:00
|
|
|
m_phaseIncrement = 0.0f;
|
2016-12-18 20:41:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void NCOF::setFreq(Real freq, Real sampleRate)
|
|
|
|
{
|
2022-02-20 20:18:44 -05:00
|
|
|
m_phaseIncrement = sampleRate == 0 ? 0 : (freq * TableSize) / sampleRate;
|
|
|
|
qDebug("NCOF::setFreq: freq: %f sr: %f m_phaseIncrement: %f", freq, sampleRate, m_phaseIncrement);
|
2016-12-18 20:41:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
float NCOF::next()
|
|
|
|
{
|
|
|
|
int phase = nextPhase();
|
|
|
|
return m_table[phase];
|
|
|
|
}
|
|
|
|
|
|
|
|
Complex NCOF::nextIQ()
|
|
|
|
{
|
|
|
|
int phase = nextPhase();
|
|
|
|
return Complex(m_table[phase], -m_table[(phase + TableSize / 4) % TableSize]);
|
|
|
|
}
|
|
|
|
|
2018-01-31 16:34:55 -05:00
|
|
|
Complex NCOF::nextIQ(float imbalance)
|
|
|
|
{
|
|
|
|
int phase = nextPhase();
|
|
|
|
int phaseQ = imbalance < 0.0 ? phase + (int) (imbalance*TableSize) : phase;
|
|
|
|
int phaseI = imbalance < 0.0 ? phase : phase + (int) (imbalance*TableSize);
|
|
|
|
return Complex(m_table[phaseI % TableSize], -m_table[(phaseQ + TableSize / 4) % TableSize]);
|
|
|
|
}
|
|
|
|
|
2016-12-18 20:41:31 -05:00
|
|
|
Complex NCOF::nextQI()
|
|
|
|
{
|
|
|
|
int phase = nextPhase();
|
|
|
|
return Complex(-m_table[(phase + TableSize / 4) % TableSize], m_table[phase]);
|
|
|
|
}
|
|
|
|
|
|
|
|
float NCOF::get()
|
|
|
|
{
|
|
|
|
return m_table[(int) m_phase];
|
|
|
|
}
|
|
|
|
|
|
|
|
Complex NCOF::getIQ()
|
|
|
|
{
|
|
|
|
return Complex(m_table[(int) m_phase], -m_table[((int) m_phase + TableSize / 4) % TableSize]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NCOF::getIQ(Complex& c)
|
|
|
|
{
|
|
|
|
c.real(m_table[(int) m_phase]);
|
|
|
|
c.imag(-m_table[((int) m_phase + TableSize / 4) % TableSize]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Complex NCOF::getQI()
|
|
|
|
{
|
|
|
|
return Complex(-m_table[((int) m_phase + TableSize / 4) % TableSize], m_table[(int) m_phase]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NCOF::getQI(Complex& c)
|
|
|
|
{
|
|
|
|
c.imag(m_table[(int) m_phase]);
|
|
|
|
c.real(-m_table[((int) m_phase + TableSize / 4) % TableSize]);
|
|
|
|
}
|