From 334688b785f2ffbe9d53a7f8f37cb05610a9380e Mon Sep 17 00:00:00 2001 From: KubaPro010 Date: Mon, 9 Jun 2025 20:13:43 +0200 Subject: [PATCH] optimize or just clean up --- dsp/bs412.c | 2 +- dsp/filters.c | 2 +- dsp/filters.h | 1 + dsp/fm_modulator.c | 16 ---------------- dsp/fm_modulator.h | 10 +--------- dsp/gain_control.c | 16 ++++++++-------- dsp/gain_control.h | 1 + 7 files changed, 13 insertions(+), 35 deletions(-) diff --git a/dsp/bs412.c b/dsp/bs412.c index a29d243..d8f8403 100644 --- a/dsp/bs412.c +++ b/dsp/bs412.c @@ -34,7 +34,7 @@ float measure_mpx(MPXPowerMeasurement* mpx, float deviation) { #ifdef BS412_RMS float avg_deviation = sqrtf(mpx->sample * inv_counter); // RMs #else - float avg_deviation = mpx->sample * inv_counter; // RMs + float avg_deviation = mpx->sample * inv_counter; #endif float modulation_power = deviation_to_dbr(avg_deviation); diff --git a/dsp/filters.c b/dsp/filters.c index 1670de6..b033815 100644 --- a/dsp/filters.c +++ b/dsp/filters.c @@ -4,7 +4,7 @@ void init_preemphasis(ResistorCapacitor *filter, float tau, float sample_rate, f float dt = 1.0f / sample_rate; filter->alpha = tau / (tau + dt); - float omega = 2.0f * M_PI * ref_freq / sample_rate; + float omega = M_2PI * ref_freq / sample_rate; float cos_omega = cosf(omega); float numerator = sqrtf(1.0f + filter->alpha * filter->alpha - 2.0f * filter->alpha * cos_omega); diff --git a/dsp/filters.h b/dsp/filters.h index 0f39fa9..9ca4340 100644 --- a/dsp/filters.h +++ b/dsp/filters.h @@ -2,6 +2,7 @@ #include #include "../lib/optimization.h" +#include "../lib/constants.h" typedef struct { diff --git a/dsp/fm_modulator.c b/dsp/fm_modulator.c index d430e76..5fbb3a7 100644 --- a/dsp/fm_modulator.c +++ b/dsp/fm_modulator.c @@ -12,20 +12,4 @@ float modulate_fm(FMModulator *fm, float sample) { fm->osc_phase += (M_2PI * inst_freq) / fm->sample_rate; fm->osc_phase -= (fm->osc_phase >= M_2PI) ? M_2PI : 0.0f; return sinf(fm->osc_phase); -} - -void init_refrenced_fm_modulator(RefrencedFMModulator* fm, Oscillator* osc, float deviation) { - fm->deviation = deviation; - fm->osc = osc; -} - -float refrenced_modulate_fm(RefrencedFMModulator* fm, float sample, float phase_multiplier) { - float inst_freq = sample * fm->deviation; - float phase = (fm->osc->phase * phase_multiplier) + ((M_2PI * inst_freq) / fm->osc->sample_rate); - - if (phase >= M_2PI) { - phase -= M_2PI; - } - - return sinf(phase); } \ No newline at end of file diff --git a/dsp/fm_modulator.h b/dsp/fm_modulator.h index 9dc314b..e3f2f30 100644 --- a/dsp/fm_modulator.h +++ b/dsp/fm_modulator.h @@ -11,12 +11,4 @@ typedef struct } FMModulator; void init_fm_modulator(FMModulator *fm, float frequency, float deviation, float sample_rate); -float modulate_fm(FMModulator *fm, float sample); - -typedef struct -{ - float deviation; - Oscillator* osc; -} RefrencedFMModulator; -void init_refrenced_fm_modulator(RefrencedFMModulator *fm, Oscillator *osc, float deviation); -float refrenced_modulate_fm(RefrencedFMModulator *fm, float sample, float phase_multiplier); +float modulate_fm(FMModulator *fm, float sample); \ No newline at end of file diff --git a/dsp/gain_control.c b/dsp/gain_control.c index 6863b3f..7780e4c 100644 --- a/dsp/gain_control.c +++ b/dsp/gain_control.c @@ -15,22 +15,22 @@ void initAGC(AGC* agc, int sampleRate, float targetLevel, float minGain, float m agc->currentLevel = 0.0f; agc->rms_buffer = 0.0f; + agc->rmsAlpha = expf(-1.0f / (sampleRate * 0.04f)); } float process_agc(AGC* agc, float sidechain) { float x2 = sidechain * sidechain; - float rmsAlpha = expf(-1.0f / (agc->sampleRate * 0.04)); - agc->rms_buffer = rmsAlpha * agc->rms_buffer + (1.0f - rmsAlpha) * x2; - float instantLevel = sqrtf(agc->rms_buffer); + agc->rms_buffer = agc->rmsAlpha * agc->rms_buffer + (1.0f - agc->rmsAlpha) * x2; + const float instantLevel = sqrtf(agc->rms_buffer); + + const float levelAlpha = (instantLevel > agc->currentLevel) ? agc->attackCoef : agc->releaseCoef; + agc->currentLevel = levelAlpha * agc->currentLevel + (1.0f - levelAlpha) * instantLevel; - float alpha = (instantLevel > agc->currentLevel) ? agc->attackCoef : agc->releaseCoef; - agc->currentLevel = alpha * agc->currentLevel + (1.0f - alpha) * instantLevel; - - float desiredGain = agc->targetLevel / fmaxf(agc->currentLevel, 1e-10f); + float desiredGain = agc->targetLevel / (agc->currentLevel + 1e-10f); desiredGain = fminf(fmaxf(desiredGain, agc->minGain), agc->maxGain); - float gainAlpha = (desiredGain > agc->currentGain) ? agc->attackCoef : agc->releaseCoef; + const float gainAlpha = (desiredGain > agc->currentGain) ? agc->attackCoef : agc->releaseCoef; agc->currentGain = gainAlpha * agc->currentGain + (1.0f - gainAlpha) * desiredGain; return agc->currentGain; diff --git a/dsp/gain_control.h b/dsp/gain_control.h index 45f215e..d6a7b0c 100644 --- a/dsp/gain_control.h +++ b/dsp/gain_control.h @@ -16,6 +16,7 @@ typedef struct { float releaseCoef; float rms_buffer; + float rmsAlpha; } AGC; void initAGC(AGC* agc, int sampleRate, float targetLevel, float minGain, float maxGain, float attackTime, float releaseTime);