sdrangel/sdrbase/dsp/phasediscri.h

117 wiersze
3.7 KiB
C++

///////////////////////////////////////////////////////////////////////////////////
// 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/>. //
///////////////////////////////////////////////////////////////////////////////////
#ifndef INCLUDE_DSP_PHASEDISCRI_H_
#define INCLUDE_DSP_PHASEDISCRI_H_
#include "dsp/dsptypes.h"
class PhaseDiscriminators
{
public:
/**
* Reset stored values
*/
void reset()
{
m_m1Sample = 0;
m_m2Sample = 0;
}
/**
* Scaling factor so that resulting excursion maps to [-1,+1]
*/
void setFMScaling(Real fmScaling)
{
m_fmScaling = fmScaling;
}
/**
* Standard discriminator using atan2. On modern processors this is as efficient as the non atan2 one.
* This is better for high fidelity.
*/
Real phaseDiscriminator(const Complex& sample)
{
Complex d(std::conj(m_m1Sample) * sample);
m_m1Sample = sample;
return (std::atan2(d.imag(), d.real()) / M_PI) * m_fmScaling;
}
/**
* Alternative without atan at the expense of a slight distorsion on very wideband signals
* http://www.embedded.com/design/configurable-systems/4212086/DSP-Tricks--Frequency-demodulation-algorithms-
* in addition it needs scaling by instantaneous magnitude squared and volume (0..10) adjustment factor
*/
Real phaseDiscriminator2(const Complex& sample)
{
Real ip = sample.real() - m_m2Sample.real();
Real qp = sample.imag() - m_m2Sample.imag();
Real h1 = m_m1Sample.real() * qp;
Real h2 = m_m1Sample.imag() * ip;
m_m2Sample = m_m1Sample;
m_m1Sample = sample;
//return ((h1 - h2) / M_PI_2) * m_fmScaling;
return (h1 - h2) * m_fmScaling;
}
/**
* Second alternative
*/
Real phaseDiscriminator3(const Complex& sample, long double& magsq)
{
Real fltI = sample.real();
Real fltQ = sample.imag();
double fltNorm;
Real fltNormI;
Real fltNormQ;
Real fltVal;
magsq = fltI*fltI + fltQ*fltQ;
fltNorm = std::sqrt(magsq);
fltNormI= fltI/fltNorm;
fltNormQ= fltQ/fltNorm;
fltVal = m_fltPreviousI*(fltNormQ - m_fltPreviousQ2);
fltVal -= m_fltPreviousQ*(fltNormI - m_fltPreviousI2);
fltVal += 2.0f;
fltVal /= 2.0f; // normally it is /4
m_fltPreviousQ2 = m_fltPreviousQ;
m_fltPreviousI2 = m_fltPreviousI;
m_fltPreviousQ = fltNormQ;
m_fltPreviousI = fltNormI;
return fltVal * m_fmScaling;
}
private:
Complex m_m1Sample;
Complex m_m2Sample;
Real m_fmScaling;
Real m_fltPreviousI;
Real m_fltPreviousQ;
Real m_fltPreviousI2;
Real m_fltPreviousQ2;
};
#endif /* INCLUDE_DSP_PHASEDISCRI_H_ */