0
1
mirror of https://github.com/radio95-rnt/fm95.git synced 2026-02-26 19:23:51 +01:00

optimize or just clean up

This commit is contained in:
2025-06-09 20:13:43 +02:00
parent cad6080c07
commit 334688b785
7 changed files with 13 additions and 35 deletions

View File

@@ -34,7 +34,7 @@ float measure_mpx(MPXPowerMeasurement* mpx, float deviation) {
#ifdef BS412_RMS #ifdef BS412_RMS
float avg_deviation = sqrtf(mpx->sample * inv_counter); // RMs float avg_deviation = sqrtf(mpx->sample * inv_counter); // RMs
#else #else
float avg_deviation = mpx->sample * inv_counter; // RMs float avg_deviation = mpx->sample * inv_counter;
#endif #endif
float modulation_power = deviation_to_dbr(avg_deviation); float modulation_power = deviation_to_dbr(avg_deviation);

View File

@@ -4,7 +4,7 @@ void init_preemphasis(ResistorCapacitor *filter, float tau, float sample_rate, f
float dt = 1.0f / sample_rate; float dt = 1.0f / sample_rate;
filter->alpha = tau / (tau + dt); 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 cos_omega = cosf(omega);
float numerator = sqrtf(1.0f + filter->alpha * filter->alpha - 2.0f * filter->alpha * cos_omega); float numerator = sqrtf(1.0f + filter->alpha * filter->alpha - 2.0f * filter->alpha * cos_omega);

View File

@@ -2,6 +2,7 @@
#include <math.h> #include <math.h>
#include "../lib/optimization.h" #include "../lib/optimization.h"
#include "../lib/constants.h"
typedef struct typedef struct
{ {

View File

@@ -12,20 +12,4 @@ float modulate_fm(FMModulator *fm, float sample) {
fm->osc_phase += (M_2PI * inst_freq) / fm->sample_rate; fm->osc_phase += (M_2PI * inst_freq) / fm->sample_rate;
fm->osc_phase -= (fm->osc_phase >= M_2PI) ? M_2PI : 0.0f; fm->osc_phase -= (fm->osc_phase >= M_2PI) ? M_2PI : 0.0f;
return sinf(fm->osc_phase); 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);
} }

View File

@@ -11,12 +11,4 @@ typedef struct
} FMModulator; } FMModulator;
void init_fm_modulator(FMModulator *fm, float frequency, float deviation, float sample_rate); void init_fm_modulator(FMModulator *fm, float frequency, float deviation, float sample_rate);
float modulate_fm(FMModulator *fm, float sample); 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);

View File

@@ -15,22 +15,22 @@ void initAGC(AGC* agc, int sampleRate, float targetLevel, float minGain, float m
agc->currentLevel = 0.0f; agc->currentLevel = 0.0f;
agc->rms_buffer = 0.0f; agc->rms_buffer = 0.0f;
agc->rmsAlpha = expf(-1.0f / (sampleRate * 0.04f));
} }
float process_agc(AGC* agc, float sidechain) { float process_agc(AGC* agc, float sidechain) {
float x2 = sidechain * sidechain; float x2 = sidechain * sidechain;
float rmsAlpha = expf(-1.0f / (agc->sampleRate * 0.04)); agc->rms_buffer = agc->rmsAlpha * agc->rms_buffer + (1.0f - agc->rmsAlpha) * x2;
agc->rms_buffer = rmsAlpha * agc->rms_buffer + (1.0f - rmsAlpha) * x2; const float instantLevel = sqrtf(agc->rms_buffer);
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; float desiredGain = agc->targetLevel / (agc->currentLevel + 1e-10f);
agc->currentLevel = alpha * agc->currentLevel + (1.0f - alpha) * instantLevel;
float desiredGain = agc->targetLevel / fmaxf(agc->currentLevel, 1e-10f);
desiredGain = fminf(fmaxf(desiredGain, agc->minGain), agc->maxGain); 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; agc->currentGain = gainAlpha * agc->currentGain + (1.0f - gainAlpha) * desiredGain;
return agc->currentGain; return agc->currentGain;

View File

@@ -16,6 +16,7 @@ typedef struct {
float releaseCoef; float releaseCoef;
float rms_buffer; float rms_buffer;
float rmsAlpha;
} AGC; } AGC;
void initAGC(AGC* agc, int sampleRate, float targetLevel, float minGain, float maxGain, float attackTime, float releaseTime); void initAGC(AGC* agc, int sampleRate, float targetLevel, float minGain, float maxGain, float attackTime, float releaseTime);