can compress?

This commit is contained in:
2025-12-30 22:18:03 +01:00
parent b0d7d2fdbe
commit fb5c8f46ef
2 changed files with 20 additions and 3 deletions
+19 -4
View File
@@ -19,6 +19,8 @@ void init_bs412(BS412Compressor* mpx, uint32_t mpx_deviation, float target_power
mpx->target = target_power; mpx->target = target_power;
mpx->gain = 0.0f; mpx->gain = 0.0f;
mpx->max = max; mpx->max = max;
mpx->can_compress = 0;
mpx->second_counter = 0;
#ifdef BS412_DEBUG #ifdef BS412_DEBUG
debug_printf("Initialized MPX power measurement with sample rate: %d\n", sample_rate); debug_printf("Initialized MPX power measurement with sample rate: %d\n", sample_rate);
#endif #endif
@@ -36,7 +38,7 @@ static inline float soft_clip_tanh(float sample, float threshold) {
float bs412_compress(BS412Compressor* mpx, float sample) { float bs412_compress(BS412Compressor* mpx, float sample) {
mpx->avg_power += mpx->alpha * ((sample * sample * mpx->mpx_deviation * mpx->mpx_deviation) - mpx->avg_power); mpx->avg_power += mpx->alpha * ((sample * sample * mpx->mpx_deviation * mpx->mpx_deviation) - mpx->avg_power);
float avg_deviation = sqrtf(mpx->avg_power) / sqrtf(2); float avg_deviation = sqrtf(mpx->avg_power) * sqrtf(2);
float modulation_power = deviation_to_dbr(avg_deviation); float modulation_power = deviation_to_dbr(avg_deviation);
if(mpx->target <= -100.0f) { if(mpx->target <= -100.0f) {
@@ -50,12 +52,25 @@ float bs412_compress(BS412Compressor* mpx, float sample) {
return sample; return sample;
} }
#ifdef BS412_DEBUG
if(mpx->sample_counter > mpx->sample_rate) { if(mpx->sample_counter > mpx->sample_rate) {
#ifdef BS412_DEBUG
debug_printf("MPX power: %.2f dBr with gain %.2fx (%.2f dBr)\n", modulation_power, mpx->gain, deviation_to_dbr(avg_deviation * mpx->gain)); debug_printf("MPX power: %.2f dBr with gain %.2fx (%.2f dBr)\n", modulation_power, mpx->gain, deviation_to_dbr(avg_deviation * mpx->gain));
mpx->sample_counter = 0;
}
#endif #endif
mpx->sample_counter = 0;
if(mpx->can_compress == 0) mpx->second_counter++;
}
if(mpx->can_compress == 0 && mpx->sample_counter > 60) {
#ifdef BS412_DEBUG
debug_printf("Can compress.\n");
#endif
mpx->can_compress = 1;
}
if(mpx->can_compress == 0) {
mpx->sample_counter++;
return sample;
}
float target_gain = powf(10.0f, (mpx->target - modulation_power) / 10.0f); float target_gain = powf(10.0f, (mpx->target - modulation_power) / 10.0f);
if (modulation_power > mpx->target) { if (modulation_power > mpx->target) {
+2
View File
@@ -23,6 +23,8 @@ typedef struct
float gain; float gain;
double avg_power; double avg_power;
double alpha; double alpha;
uint8_t can_compress : 1;
uint8_t second_counter;
} BS412Compressor; } BS412Compressor;
float dbr_to_deviation(float dbr); float dbr_to_deviation(float dbr);