2016-10-20 12:04:15 -04: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 //
|
|
|
|
// //
|
|
|
|
// 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 "ammod.h"
|
|
|
|
|
|
|
|
#include <QTime>
|
|
|
|
#include <QDebug>
|
2016-10-20 13:34:30 -04:00
|
|
|
#include <QMutexLocker>
|
2016-10-20 12:04:15 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <complex.h>
|
|
|
|
#include <dsp/upchannelizer.h>
|
|
|
|
#include "dsp/dspengine.h"
|
|
|
|
#include "dsp/pidcontroller.h"
|
|
|
|
|
|
|
|
MESSAGE_CLASS_DEFINITION(AMMod::MsgConfigureAMMod, Message)
|
|
|
|
|
|
|
|
AMMod::AMMod() :
|
|
|
|
m_settingsMutex(QMutex::Recursive)
|
|
|
|
{
|
|
|
|
setObjectName("AMMod");
|
|
|
|
|
|
|
|
m_config.m_outputSampleRate = 48000;
|
|
|
|
m_config.m_inputFrequencyOffset = 0;
|
|
|
|
m_config.m_rfBandwidth = 12500;
|
|
|
|
m_config.m_afBandwidth = 3000;
|
2016-10-20 13:34:30 -04:00
|
|
|
m_config.m_modFactor = 20;
|
2016-10-20 12:04:15 -04:00
|
|
|
m_config.m_audioSampleRate = DSPEngine::instance()->getAudioSampleRate();
|
|
|
|
|
|
|
|
apply();
|
|
|
|
|
|
|
|
m_audioBuffer.resize(1<<14);
|
|
|
|
m_audioBufferFill = 0;
|
|
|
|
|
|
|
|
m_movingAverage.resize(16, 0);
|
|
|
|
m_volumeAGC.resize(4096, 0.003, 0);
|
|
|
|
m_magsq = 0.0;
|
2016-10-20 13:34:30 -04:00
|
|
|
|
|
|
|
m_toneNco.setFreq(1000.0, m_config.m_audioSampleRate);
|
2016-10-20 12:04:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
AMMod::~AMMod()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-10-23 19:27:23 -04:00
|
|
|
void AMMod::configure(MessageQueue* messageQueue, Real rfBandwidth, Real afBandwidth, float modFactor, bool audioMute)
|
2016-10-20 12:04:15 -04:00
|
|
|
{
|
2016-10-23 19:27:23 -04:00
|
|
|
Message* cmd = MsgConfigureAMMod::create(rfBandwidth, afBandwidth, modFactor, audioMute);
|
2016-10-20 12:04:15 -04:00
|
|
|
messageQueue->push(cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AMMod::pull(Sample& sample)
|
|
|
|
{
|
2016-10-20 13:34:30 -04:00
|
|
|
Complex ci;
|
|
|
|
|
2016-10-30 15:57:27 -04:00
|
|
|
m_settingsMutex.lock();
|
2016-10-27 22:34:06 -04:00
|
|
|
|
|
|
|
if (m_interpolatorDistance > 1.0f) // decimate
|
|
|
|
{
|
2016-10-27 21:18:24 -04:00
|
|
|
Real t = m_toneNco.next();
|
|
|
|
m_modSample.real(((t+1.0f) * m_running.m_modFactor * 16384.0f)); // modulate and scale zero frequency carrier
|
|
|
|
m_modSample.imag(0.0f);
|
2016-10-27 22:34:06 -04:00
|
|
|
|
|
|
|
while (!m_interpolator.decimate(&m_interpolatorDistanceRemain, m_modSample, &ci))
|
|
|
|
{
|
|
|
|
Real t = m_toneNco.next();
|
|
|
|
m_modSample.real(((t+1.0f) * m_running.m_modFactor * 16384.0f)); // modulate and scale zero frequency carrier
|
|
|
|
m_modSample.imag(0.0f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (m_interpolator.interpolate(&m_interpolatorDistanceRemain, m_modSample, &ci))
|
|
|
|
{
|
|
|
|
Real t = m_toneNco.next();
|
|
|
|
m_modSample.real(((t+1.0f) * m_running.m_modFactor * 16384.0f)); // modulate and scale zero frequency carrier
|
|
|
|
m_modSample.imag(0.0f);
|
|
|
|
}
|
|
|
|
}
|
2016-10-20 13:34:30 -04:00
|
|
|
|
2016-10-27 21:18:24 -04:00
|
|
|
m_interpolatorDistanceRemain += m_interpolatorDistance;
|
|
|
|
|
2016-10-23 20:24:36 -04:00
|
|
|
ci *= m_carrierNco.nextIQ(); // shift to carrier frequency
|
2016-10-20 13:34:30 -04:00
|
|
|
|
2016-10-30 15:57:27 -04:00
|
|
|
m_settingsMutex.unlock();
|
|
|
|
|
2016-10-23 20:24:36 -04:00
|
|
|
Real magsq = ci.real() * ci.real() + ci.imag() * ci.imag();
|
2016-10-20 13:34:30 -04:00
|
|
|
magsq /= (1<<30);
|
|
|
|
m_movingAverage.feed(magsq);
|
|
|
|
m_magsq = m_movingAverage.average();
|
|
|
|
|
|
|
|
sample.m_real = (FixReal) ci.real();
|
|
|
|
sample.m_imag = (FixReal) ci.imag();
|
2016-10-20 12:04:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void AMMod::start()
|
|
|
|
{
|
|
|
|
qDebug() << "AMMod::start: m_outputSampleRate: " << m_config.m_outputSampleRate
|
|
|
|
<< " m_inputFrequencyOffset: " << m_config.m_inputFrequencyOffset;
|
|
|
|
|
|
|
|
m_audioFifo.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AMMod::stop()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AMMod::handleMessage(const Message& cmd)
|
|
|
|
{
|
|
|
|
qDebug() << "AMMod::handleMessage";
|
|
|
|
|
|
|
|
if (UpChannelizer::MsgChannelizerNotification::match(cmd))
|
|
|
|
{
|
|
|
|
UpChannelizer::MsgChannelizerNotification& notif = (UpChannelizer::MsgChannelizerNotification&) cmd;
|
|
|
|
|
|
|
|
m_config.m_outputSampleRate = notif.getSampleRate();
|
|
|
|
m_config.m_inputFrequencyOffset = notif.getFrequencyOffset();
|
|
|
|
|
|
|
|
apply();
|
|
|
|
|
|
|
|
qDebug() << "AMMod::handleMessage: MsgChannelizerNotification:"
|
|
|
|
<< " m_outputSampleRate: " << m_config.m_outputSampleRate
|
|
|
|
<< " m_inputFrequencyOffset: " << m_config.m_inputFrequencyOffset;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (MsgConfigureAMMod::match(cmd))
|
|
|
|
{
|
|
|
|
MsgConfigureAMMod& cfg = (MsgConfigureAMMod&) cmd;
|
|
|
|
|
|
|
|
m_config.m_rfBandwidth = cfg.getRFBandwidth();
|
|
|
|
m_config.m_afBandwidth = cfg.getAFBandwidth();
|
2016-10-20 13:34:30 -04:00
|
|
|
m_config.m_modFactor = cfg.getModFactor();
|
2016-10-20 12:04:15 -04:00
|
|
|
m_config.m_audioMute = cfg.getAudioMute();
|
|
|
|
|
|
|
|
apply();
|
|
|
|
|
|
|
|
qDebug() << "AMMod::handleMessage: MsgConfigureAMMod:"
|
|
|
|
<< " m_rfBandwidth: " << m_config.m_rfBandwidth
|
|
|
|
<< " m_afBandwidth: " << m_config.m_afBandwidth
|
2016-10-20 13:34:30 -04:00
|
|
|
<< " m_modFactor: " << m_config.m_modFactor
|
2016-10-20 12:04:15 -04:00
|
|
|
<< " m_audioMute: " << m_config.m_audioMute;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AMMod::apply()
|
|
|
|
{
|
|
|
|
|
2016-10-23 20:24:36 -04:00
|
|
|
if ((m_config.m_inputFrequencyOffset != m_running.m_inputFrequencyOffset) ||
|
|
|
|
(m_config.m_outputSampleRate != m_running.m_outputSampleRate))
|
2016-10-20 12:04:15 -04:00
|
|
|
{
|
2016-10-24 20:34:29 -04:00
|
|
|
m_settingsMutex.lock();
|
2016-10-23 20:24:36 -04:00
|
|
|
m_carrierNco.setFreq(m_config.m_inputFrequencyOffset, m_config.m_outputSampleRate);
|
2016-10-24 20:34:29 -04:00
|
|
|
m_settingsMutex.unlock();
|
2016-10-20 12:04:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if((m_config.m_outputSampleRate != m_running.m_outputSampleRate) ||
|
|
|
|
(m_config.m_rfBandwidth != m_running.m_rfBandwidth))
|
|
|
|
{
|
|
|
|
m_settingsMutex.lock();
|
|
|
|
m_interpolatorDistanceRemain = 0;
|
2016-10-27 13:55:52 -04:00
|
|
|
m_interpolatorConsumed = false;
|
|
|
|
m_interpolatorDistance = (Real) m_config.m_audioSampleRate / (Real) m_config.m_outputSampleRate;
|
2016-10-31 22:37:01 -04:00
|
|
|
m_interpolator.create(48, m_config.m_outputSampleRate, m_config.m_rfBandwidth / 2.2, 3.0);
|
2016-10-20 12:04:15 -04:00
|
|
|
m_settingsMutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
if((m_config.m_afBandwidth != m_running.m_afBandwidth) ||
|
|
|
|
(m_config.m_audioSampleRate != m_running.m_audioSampleRate))
|
|
|
|
{
|
|
|
|
m_settingsMutex.lock();
|
|
|
|
m_lowpass.create(21, m_config.m_audioSampleRate, m_config.m_afBandwidth);
|
|
|
|
m_settingsMutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_running.m_outputSampleRate = m_config.m_outputSampleRate;
|
|
|
|
m_running.m_inputFrequencyOffset = m_config.m_inputFrequencyOffset;
|
|
|
|
m_running.m_rfBandwidth = m_config.m_rfBandwidth;
|
|
|
|
m_running.m_afBandwidth = m_config.m_afBandwidth;
|
2016-10-20 13:34:30 -04:00
|
|
|
m_running.m_modFactor = m_config.m_modFactor;
|
2016-10-20 12:04:15 -04:00
|
|
|
m_running.m_audioSampleRate = m_config.m_audioSampleRate;
|
|
|
|
m_running.m_audioMute = m_config.m_audioMute;
|
|
|
|
}
|
|
|
|
|