diff --git a/dsp/bs412.c b/dsp/bs412.c index 1dd103e..a29d243 100644 --- a/dsp/bs412.c +++ b/dsp/bs412.c @@ -1,5 +1,7 @@ #include "bs412.h" +#define BS412_RMS + #define LOG2_19000 log2f(19000.0f) inline float dbr_to_deviation(float dbr) { @@ -21,11 +23,19 @@ void init_modulation_power_measure(MPXPowerMeasurement* mpx, int sample_rate) { } float measure_mpx(MPXPowerMeasurement* mpx, float deviation) { +#ifdef BS412_RMS mpx->sample += deviation * deviation; // rmS +#else + mpx->sample += fabsf(deviation); +#endif mpx->sample_counter++; float inv_counter = 1.0f / mpx->sample_counter; +#ifdef BS412_RMS float avg_deviation = sqrtf(mpx->sample * inv_counter); // RMs +#else + float avg_deviation = mpx->sample * inv_counter; // RMs +#endif float modulation_power = deviation_to_dbr(avg_deviation); #ifdef BS412_DEBUG @@ -38,7 +48,7 @@ float measure_mpx(MPXPowerMeasurement* mpx, float deviation) { #ifdef BS412_DEBUG debug_printf("Resetting MPX power measurement\n"); #endif - mpx->sample = avg_deviation * avg_deviation; + mpx->sample = deviation * deviation; mpx->sample_counter = 1; } diff --git a/src/fm95.c b/src/fm95.c index e8c37d2..a2a5aa1 100644 --- a/src/fm95.c +++ b/src/fm95.c @@ -3,7 +3,7 @@ #include #include -#define LPF_ORDER 18 +#define LPF_ORDER 17 #define buffer_maxlength 12288 #define buffer_tlength_fragsize 12288 @@ -518,11 +518,18 @@ int main(int argc, char **argv) { float mpower = measure_mpx(&power, (audio+mpx) * mpx_deviation); // Standard requires that the output is measured specifically if (mpower > mpx_power) { float excess_power = mpower - mpx_power; - excess_power = deviation_to_dbr(dbr_to_deviation(excess_power) - dbr_to_deviation(mpx_only)); // make sure mpx is not included in the power to attenuate, because we'd be attuating the mpx signal for audio - - float target_gain = dbr_to_deviation(-excess_power)/mpx_deviation; - bs412_audio_gain = 0.9f * bs412_audio_gain + 0.1f * target_gain; - audio *= bs412_audio_gain; + excess_power = deviation_to_dbr(dbr_to_deviation(excess_power) - dbr_to_deviation(mpx_only)); + + if (excess_power > 0.0f && excess_power < 10.0f) { + float target_gain = dbr_to_deviation(-excess_power) / mpx_deviation; + + target_gain = fmaxf(target_gain, 0.1f); + target_gain = fminf(target_gain, 1.0f); + + bs412_audio_gain = 0.9f * bs412_audio_gain + 0.1f * target_gain; + } + } else { + bs412_audio_gain = fminf(1.0f, bs412_audio_gain + 0.001f); } iirfilt_rrrf_execute(mpx_lpf, audio, &audio); // Should have no effect, as audio should be 0-15, and 23-53, this is a filter for 53, assuming the filter is good, this is precaution and recomendation