Skip to content

Commit

Permalink
Add chirp corrector class
Browse files Browse the repository at this point in the history
  • Loading branch information
BatchDrake committed May 5, 2023
1 parent 36b751a commit f39e084
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 0 deletions.
2 changes: 2 additions & 0 deletions AmateurDSN.pro
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ isEmpty(SIGDIGGER_PREFIX) {
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
ChirpCorrector.cpp \
ExternalTool.cpp \
ExternalToolFactory.cpp \
ForwarderWidget.cpp \
Expand Down Expand Up @@ -60,6 +61,7 @@ FORMS += \
SNRTool.ui

HEADERS += \
ChirpCorrector.h \
ExternalTool.h \
ExternalToolFactory.h \
ForwarderWidget.h \
Expand Down
143 changes: 143 additions & 0 deletions ChirpCorrector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
//
// ChirpCorrector.cpp: Chirp corrector
// Copyright (C) 2023 Gonzalo José Carracedo Carballal
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program. If not, see
// <http://www.gnu.org/licenses/>
//
#include "ChirpCorrector.h"
#include <SuWidgetsHelpers.h>

using namespace SigDigger;

SUBOOL
onChirpCorrectorBaseBandData(
void *privdata,
suscan_analyzer_t *,
SUCOMPLEX *samples,
SUSCOUNT length)
{
ChirpCorrector *corrector = reinterpret_cast<ChirpCorrector *>(privdata);

corrector->process(samples, length);

return SU_TRUE;
}

ChirpCorrector::ChirpCorrector()
{
su_ncqo_init(&m_ncqo, 0);
}

void
ChirpCorrector::refreshCorrector()
{
if (m_doReset) {
m_currOmega = 0;
m_doReset = false;
}

if (m_doNewRate) {
SUDOUBLE chirpRatePerSample;
SUDOUBLE sampRate = SCAST(SUDOUBLE, m_analyzer->getSampleRate());
m_chirpRate = m_desiredRate;

chirpRatePerSample = m_chirpRate / sampRate;
m_deltaOmega = -SCAST(
SUDOUBLE,
SU_NORM2ANG_FREQ(SU_ABS2NORM_FREQ(sampRate, chirpRatePerSample)));

m_doNewRate = false;
}
}

void
ChirpCorrector::process(SUCOMPLEX *samples, SUSCOUNT length)
{
if (m_enabled) {
SUSCOUNT i;
SUDOUBLE currOmega, deltaOmega;

refreshCorrector();

currOmega = m_currOmega;
deltaOmega = m_deltaOmega;

for (i = 0; i < length; ++i) {
currOmega += deltaOmega;
if (currOmega > M_PI)
currOmega -= 2 * M_PI;
else if (currOmega < -M_PI)
currOmega += 2 * M_PI;

su_ncqo_set_angfreq(&m_ncqo, SU_ASFLOAT(currOmega));
samples[i] *= su_ncqo_read(&m_ncqo);
}

m_currOmega = currOmega;
}
}

void
ChirpCorrector::ensureCorrector()
{
if (m_analyzer != nullptr) {
if (m_enabled && !m_installed) {
m_doNewRate = true;
m_doReset = true;
m_analyzer->registerBaseBandFilter(
onChirpCorrectorBaseBandData,
this,
AMATEUR_DSN_CHIRP_CORRECTOR_PRIO);
m_installed = true;
}
}
}

void
ChirpCorrector::setAnalyzer(Suscan::Analyzer *analyzer)
{
if (analyzer != m_analyzer)
m_installed = false;

m_analyzer = analyzer;

ensureCorrector();
}

void
ChirpCorrector::setEnabled(bool enabled)
{
m_enabled = enabled;
ensureCorrector();
}

void
ChirpCorrector::setChirpRate(SUDOUBLE rate)
{
m_desiredRate = rate;
m_doNewRate = true;

}

void
ChirpCorrector::reset()
{
m_doReset = true;
}

ChirpCorrector::~ChirpCorrector()
{
// We do not need this for now
}
72 changes: 72 additions & 0 deletions ChirpCorrector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
// ChirpCorrector.h: Chirp corrector
// Copyright (C) 2023 Gonzalo José Carracedo Carballal
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program. If not, see
// <http://www.gnu.org/licenses/>
//
#ifndef CHIRPCORRECTOR_H
#define CHIRPCORRECTOR_H

#include <Suscan/Library.h>
#include <Suscan/Analyzer.h>

#define AMATEUR_DSN_CHIRP_CORRECTOR_PRIO -0x1000

SUBOOL onChirpCorrectorBaseBandData(
void *privdata,
suscan_analyzer_t *,
SUCOMPLEX *samples,
SUSCOUNT length);

namespace SigDigger {
class ChirpCorrector
{
Suscan::Analyzer *m_analyzer = nullptr;
SUDOUBLE m_chirpRate = 0;
SUDOUBLE m_deltaOmega = 0;
SUDOUBLE m_currOmega = 0;
SUSCOUNT m_sampCount = 0;

bool m_doNewRate = false;
bool m_doReset = false;

SUDOUBLE m_desiredRate = 0;
bool m_enabled = false;
bool m_installed = false;
su_ncqo_t m_ncqo;

void ensureCorrector();
void refreshCorrector();
void process(SUCOMPLEX *samples, SUSCOUNT length);

friend SUBOOL
::onChirpCorrectorBaseBandData(
void *privdata,
suscan_analyzer_t *,
SUCOMPLEX *samples,
SUSCOUNT length);

public:
ChirpCorrector();
~ChirpCorrector();

void setAnalyzer(Suscan::Analyzer *);
void setEnabled(bool);
void setChirpRate(SUDOUBLE rate);
void reset();
};
}

#endif // CHIRPCORRECTOR_H

0 comments on commit f39e084

Please sign in to comment.