1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-18 14:21:49 -05:00
sdrangel/sdrbase/dsp/phaselock.h

182 lines
5.9 KiB
C
Raw Normal View History

///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2015 F4EXB //
// written by Edouard Griffiths //
// //
// 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 <vector>
#include "dsp/dsptypes.h"
#include "export.h"
/** Phase-locked loop mainly for broadcadt FM stereo pilot. */
2018-03-03 14:23:38 -05:00
class SDRBASE_API PhaseLock
{
public:
/** Expected pilot frequency (used for PPS events). */
static const int pilot_frequency = 19000;
/** Timestamp event produced once every 19000 pilot periods. */
struct PpsEvent
{
quint64 pps_index;
quint64 sample_index;
double block_position;
};
/**
* Construct phase-locked loop.
*
* freq :: 19 kHz center frequency relative to sample freq
* (0.5 is Nyquist)
* bandwidth :: bandwidth relative to sample frequency
* minsignal :: minimum pilot amplitude
*/
PhaseLock(Real freq, Real bandwidth, Real minsignal);
virtual ~PhaseLock()
{}
/**
* Change phase locked loop parameters
*
* freq :: 19 kHz center frequency relative to sample freq
* (0.5 is Nyquist)
* bandwidth :: bandwidth relative to sample frequency
* minsignal :: minimum pilot amplitude
*/
void configure(Real freq, Real bandwidth, Real minsignal);
/**
* Process samples and extract 19 kHz pilot tone.
* Generate phase-locked 38 kHz tone with unit amplitude.
* Bufferized version with input and output vectors
*/
void process(const std::vector<Real>& samples_in, std::vector<Real>& samples_out);
/**
2015-12-07 16:31:44 -05:00
* Process samples and track a pilot tone. Generate samples for single or multiple phase-locked
* signals. Implement the processPhase virtual method to produce the output samples.
* In flow version. Ex: Use 19 kHz stereo pilot tone to generate 38 kHz (stereo) and 57 kHz
* pilots (see RDSPhaseLock class below).
2015-12-07 16:31:44 -05:00
* This is the in flow version
*/
2015-12-07 16:31:44 -05:00
void process(const Real& sample_in, Real *samples_out);
2018-05-12 00:01:54 -04:00
void process(const Real& real_in, const Real& imag_in, Real *samples_out);
/** Return true if the phase-locked loop is locked. */
bool locked() const
{
return m_lock_cnt >= m_lock_delay;
}
/** Return detected amplitude of pilot signal. */
Real get_pilot_level() const
{
return 2 * m_pilot_level;
}
protected:
Real m_phase;
Real m_psin;
Real m_pcos;
/**
* Callback method to produce multiple outputs from the current phase value in m_phase
* and/or the sin and cos values in m_psin and m_pcos
*/
virtual void processPhase(Real *samples_out) const { (void) samples_out; }
private:
Real m_minfreq, m_maxfreq;
Real m_phasor_b0, m_phasor_a1, m_phasor_a2;
Real m_phasor_i1, m_phasor_i2, m_phasor_q1, m_phasor_q2;
Real m_loopfilter_b0, m_loopfilter_b1;
Real m_loopfilter_x1;
Real m_freq;
Real m_minsignal;
Real m_pilot_level;
int m_lock_delay;
int m_lock_cnt;
int m_pilot_periods;
quint64 m_pps_cnt;
quint64 m_sample_cnt;
std::vector<PpsEvent> m_pps_events;
2018-05-12 00:01:54 -04:00
void process_phasor(Real& phasor_i, Real& phasor_q);
};
class SimplePhaseLock : public PhaseLock
{
public:
SimplePhaseLock(Real freq, Real bandwidth, Real minsignal) :
PhaseLock(freq, bandwidth, minsignal)
{}
virtual ~SimplePhaseLock()
{}
protected:
virtual void processPhase(Real *samples_out) const
{
samples_out[0] = m_psin; // f Pilot
samples_out[1] = m_pcos; // f Pilot
}
};
class StereoPhaseLock : public PhaseLock
{
public:
StereoPhaseLock(Real freq, Real bandwidth, Real minsignal) :
PhaseLock(freq, bandwidth, minsignal)
{}
virtual ~StereoPhaseLock()
{}
protected:
2015-12-07 16:31:44 -05:00
virtual void processPhase(Real *samples_out) const
{
samples_out[0] = m_psin; // f Pilot
// Generate double-frequency output.
// sin(2*x) = 2 * sin(x) * cos(x)
2015-12-25 18:19:41 -05:00
samples_out[1] = 2.0 * m_psin * m_pcos; // 2f Pilot sin
// cos(2*x) = 2 * cos(x) * cos(x) - 1
samples_out[2] = (2.0 * m_pcos * m_pcos) - 1.0; // 2f Pilot cos
}
};
class RDSPhaseLock : public PhaseLock
{
public:
RDSPhaseLock(Real freq, Real bandwidth, Real minsignal) :
PhaseLock(freq, bandwidth, minsignal)
{}
virtual ~RDSPhaseLock()
{}
protected:
2015-12-07 16:31:44 -05:00
virtual void processPhase(Real *samples_out) const
{
2015-12-10 08:10:48 -05:00
samples_out[0] = m_psin; // Pilot signal (f)
// Generate double-frequency output.
// sin(2*x) = 2 * sin(x) * cos(x)
2015-12-10 08:10:48 -05:00
samples_out[1] = 2.0 * m_psin * m_pcos; // Pilot signal (2f)
2015-12-25 18:19:41 -05:00
// cos(2*x) = 2 * cos(x) * cos(x) - 1
samples_out[2] = (2.0 * m_pcos * m_pcos) - 1.0; // 2f Pilot cos
samples_out[3] = m_phase; // Pilot phase
}
};