0
1
mirror of https://github.com/radio95-rnt/fm95.git synced 2026-02-27 03:23:54 +01:00

rename bs412 names and also add debug there, convert to tabs, remove 38 khz min for mpx dev and change bs412 gain reduction logic

This commit is contained in:
2025-04-30 20:00:00 +02:00
parent 92a358bfdb
commit 0f74e8e1b7
4 changed files with 33 additions and 21 deletions

View File

@@ -9,21 +9,30 @@ float deviation_to_dbr(float deviation) {
}
void init_modulation_power_measure(MPXPowerMeasurement* mpx, int sample_rate) {
mpx->i = 0;
mpx->sample_counter = 0;
mpx->sample = 0;
mpx->sample_rate = sample_rate;
}
float measure_mpx(MPXPowerMeasurement* mpx, float deviation) {
mpx->sample += deviation * deviation; // rmS
mpx->i++;
mpx->sample_counter++;
float avg_deviation = sqrtf(mpx->sample / mpx->i); // RMs
float avg_deviation = sqrtf(mpx->sample / mpx->sample_counter); // RMs
float modulation_power = deviation_to_dbr(avg_deviation);
#ifdef BS412_DEBUG
if(mpx->i % mpx->sample_rate == 0) {
debug_printf("MPX power: %f dBr\n", modulation_power);
}
#endif
if (mpx->i >= mpx->sample_rate * 60) {
#ifdef BS412_DEBUG
debug_printf("Resetting MPX power measurement\n");
#endif
mpx->sample = avg_deviation * avg_deviation;
mpx->i = 1;
mpx->sample_counter = 1;
}
return modulation_power;

View File

@@ -1,9 +1,10 @@
#pragma once
#include <math.h>
#include "../lib/debug.h"
typedef struct
{
int i;
int sample_counter;
int sample_rate;
double sample;
} MPXPowerMeasurement;

View File

@@ -237,10 +237,6 @@ int main(int argc, char **argv) {
break;
case 'd': // MPX deviation
mpx_deviation = strtof(optarg, NULL);
if (mpx_deviation < 38000) {
fprintf(stderr, "Warning: MPX deviation cannot be lower than 38000. Setting to 38000.\n");
mpx_deviation = 38000;
}
break;
case 'A': // Master vol
master_volume = strtof(optarg, NULL);
@@ -370,6 +366,8 @@ int main(int argc, char **argv) {
MPXPowerMeasurement mpx_only_power;
init_modulation_power_measure(&mpx_only_power, sample_rate);
float bs412_audio_gain = 1.0f;
AGC agc;
// fs target min max attack relese
initAGC(&agc, sample_rate, 0.625f, 0.0f, 1.25f, 0.025f, 0.25f);
@@ -463,13 +461,17 @@ int main(int argc, char **argv) {
if(sca_on) mpx += modulate_fm(&sca_mod, hard_clip(current_sca_in, sca_clipper_threshold))*SCA_VOLUME;
float mpx_only = measure_mpx(&mpx_only_power, mpx * mpx_deviation);
float mpower = measure_mpx(&power, (audio+mpx) * mpx_deviation);
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_only - mpx_power; // Make sure that MPX doesn't affect the audio
audio *= (dbr_to_deviation(-excess_power)/mpx_deviation); // This should be more dynamic, but too bad
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;
}
audio = hard_clip(audio, 1-mpx); // Prevent clipping, via clipping the audio signal with relation to the mpx signal
audio = hard_clip(audio, 1.0f-mpx); // Prevent clipping, via clipping the audio signal with relation to the mpx signal
output[i] = (audio+mpx)*master_volume;
if(rds_on || stereo) advance_oscillator(&osc);