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;
}
// // console output for Windows: available in DEBUG or in case of ENABLE_DIGITAL_LAB
#if (defined(WIN32) && (defined(ENABLE_DIGITAL_LAB) || defined(_DEBUG)))
//Deactivated code to allocate an explicit Console on Windows.
//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()) {
freopen("CONOUT$", "w", stdout);
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) {
// http://dspguru.com/dsp/tricks/magnitude-estimator
/* magnitude ~= alpha * max(|I|, |Q|) + beta * min(|I|, |Q|) */
double abs_inphase = fabs(inphase);
double abs_quadrature = fabs(quadrature);
if (abs_inphase > abs_quadrature) {
return alpha * abs_inphase + beta * abs_quadrature;
} else {
return alpha * abs_quadrature + beta * abs_inphase;
}
double DemodulatorThread::abMagnitude(float inphase, float quadrature) {
// cast to double, so we keep precision despite the **2 op later.
double dinphase = (double)inphase;
double dquadrature = (double)quadrature;
//sqrt() has been an insanely fast intrinsic for years, use it !
return sqrt(dinphase * dinphase + dquadrature * dquadrature);
}
float DemodulatorThread::linearToDb(float linear) {
// http://dspguru.com/dsp/tricks/magnitude-estimator
double DemodulatorThread::linearToDb(double linear) {
#define SMALL 1e-20
if (linear <= SMALL) {
linear = float(SMALL);
linear = double(SMALL);
}
return 20.0 * log10(linear);
}
@ -133,31 +132,32 @@ void DemodulatorThread::run() {
cModem->demodulate(cModemKit, &modemData, ati);
float currentSignalLevel = 0;
float sampleTime = float(inp->data.size()) / float(inp->sampleRate);
double currentSignalLevel = 0;
double sampleTime = double(inp->data.size()) / double(inp->sampleRate);
if (audioOutputQueue != nullptr && ati && ati->data.size()) {
float accum = 0;
double accum = 0;
if (cModem->useSignalOutput()) {
if (cModem->useSignalOutput()) {
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 {
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();
if (currentSignalLevel > sc) {
sc = currentSignalLevel;
}
@ -166,6 +166,7 @@ void DemodulatorThread::run() {
sf = currentSignalLevel;
}
if (sl+1.0f > sc) {
sc = sl+1.0f;
}

View File

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