Merge pull request #425 from vsonnier/small_cleanup_squelsh_and_misc

Small cleanup squelsh and misc
This commit is contained in:
Charles J. Cliffe 2016-08-13 19:41:53 -04:00 committed by GitHub
commit 19533e08de
3 changed files with 32 additions and 29 deletions

View File

@ -165,8 +165,11 @@ bool CubicSDR::OnInit() {
return false; return false;
} }
// // console output for Windows: available in DEBUG or in case of ENABLE_DIGITAL_LAB //Deactivated code to allocate an explicit Console on Windows.
#if (defined(WIN32) && (defined(ENABLE_DIGITAL_LAB) || defined(_DEBUG))) //This tends to hang the apllication on heavy demod (re)creation.
//To continue to debug with std::cout traces, simply run CubicSDR in a MINSYS2 compatble shell on Windows:
//ex: Cygwin shell, Git For Windows Bash shell....
#if (0)
if (AllocConsole()) { if (AllocConsole()) {
freopen("CONOUT$", "w", stdout); freopen("CONOUT$", "w", stdout);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);

View File

@ -39,23 +39,22 @@ void DemodulatorThread::onBindOutput(std::string name, ThreadQueueBase *threadQu
} }
} }
float DemodulatorThread::abMagnitude(double alpha, double beta, float inphase, float quadrature) { double DemodulatorThread::abMagnitude(float inphase, float quadrature) {
// http://dspguru.com/dsp/tricks/magnitude-estimator
/* magnitude ~= alpha * max(|I|, |Q|) + beta * min(|I|, |Q|) */ // cast to double, so we keep precision despite the **2 op later.
double abs_inphase = fabs(inphase); double dinphase = (double)inphase;
double abs_quadrature = fabs(quadrature); double dquadrature = (double)quadrature;
if (abs_inphase > abs_quadrature) {
return alpha * abs_inphase + beta * abs_quadrature; //sqrt() has been an insanely fast intrinsic for years, use it !
} else { return sqrt(dinphase * dinphase + dquadrature * dquadrature);
return alpha * abs_quadrature + beta * abs_inphase;
}
} }
float DemodulatorThread::linearToDb(float linear) { double DemodulatorThread::linearToDb(double linear) {
// http://dspguru.com/dsp/tricks/magnitude-estimator
#define SMALL 1e-20 #define SMALL 1e-20
if (linear <= SMALL) { if (linear <= SMALL) {
linear = float(SMALL); linear = double(SMALL);
} }
return 20.0 * log10(linear); return 20.0 * log10(linear);
} }
@ -133,31 +132,32 @@ void DemodulatorThread::run() {
cModem->demodulate(cModemKit, &modemData, ati); cModem->demodulate(cModemKit, &modemData, ati);
float currentSignalLevel = 0; double currentSignalLevel = 0;
float sampleTime = float(inp->data.size()) / float(inp->sampleRate); double sampleTime = double(inp->data.size()) / double(inp->sampleRate);
if (audioOutputQueue != nullptr && ati && ati->data.size()) { if (audioOutputQueue != nullptr && ati && ati->data.size()) {
float accum = 0; double accum = 0;
if (cModem->useSignalOutput()) {
if (cModem->useSignalOutput()) {
for (auto i : ati->data) { for (auto i : ati->data) {
accum += abMagnitude(0.948059448969, 0.392699081699, i, 0.0); accum += abMagnitude(i, 0.0);
} }
currentSignalLevel = linearToDb(accum / float(ati->data.size())); currentSignalLevel = linearToDb(accum / double(ati->data.size()));
} else { } else {
for (auto i : inp->data) { for (auto i : inp->data) {
accum += abMagnitude(0.948059448969, 0.392699081699, i.real, i.imag); accum += abMagnitude(i.real, i.imag);
} }
currentSignalLevel = linearToDb(accum / float(inp->data.size())); currentSignalLevel = linearToDb(accum / double(inp->data.size()));
} }
float sf = signalFloor.load(), sc = signalCeil.load(), sl = squelchLevel.load(); float sf = signalFloor.load(), sc = signalCeil.load(), sl = squelchLevel.load();
if (currentSignalLevel > sc) { if (currentSignalLevel > sc) {
sc = currentSignalLevel; sc = currentSignalLevel;
} }
@ -166,6 +166,7 @@ void DemodulatorThread::run() {
sf = currentSignalLevel; sf = currentSignalLevel;
} }
if (sl+1.0f > sc) { if (sl+1.0f > sc) {
sc = sl+1.0f; sc = sl+1.0f;
} }

View File

@ -34,15 +34,14 @@ public:
float getSignalFloor(); float getSignalFloor();
void setSquelchLevel(float signal_level_in); void setSquelchLevel(float signal_level_in);
float getSquelchLevel(); float getSquelchLevel();
bool getSquelchBreak(); bool getSquelchBreak();
static void releaseSquelchLock(DemodulatorInstance *inst); static void releaseSquelchLock(DemodulatorInstance *inst);
protected: protected:
float abMagnitude(double alpha, double beta, float inphase, float quadrature); double abMagnitude(float inphase, float quadrature);
float linearToDb(float linear); double linearToDb(double linear);
DemodulatorInstance *demodInstance = nullptr; DemodulatorInstance *demodInstance = nullptr;
ReBuffer<AudioThreadInput> outputBuffers; ReBuffer<AudioThreadInput> outputBuffers;