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

start modularization

This commit is contained in:
2025-06-21 15:06:27 +02:00
parent 33ffcd9682
commit 16f0a53c05
18 changed files with 79 additions and 43 deletions

View File

@@ -1,46 +0,0 @@
#include "bs412.h"
#define LOG2_19000 log2f(19000.0f)
inline float dbr_to_deviation(float dbr) {
return 19000.0f * powf(2.0f, dbr * 0.332193f);
}
inline float deviation_to_dbr(float deviation) {
if(deviation == 0.0f) return -100.0f;
return 10.0f * (log2f(deviation) - LOG2_19000) * 0.30103f;
}
void init_modulation_power_measure(MPXPowerMeasurement* mpx, int sample_rate) {
mpx->sample_counter = 0;
mpx->sample = 0;
mpx->sample_rate = sample_rate;
#ifdef BS412_DEBUG
debug_printf("Initialized MPX power measurement with sample rate: %d\n", sample_rate);
#endif
}
float measure_mpx(MPXPowerMeasurement* mpx, float deviation) {
mpx->sample += deviation * deviation; // rmS
mpx->sample_counter++;
float inv_counter = 1.0f / mpx->sample_counter;
float avg_deviation = sqrtf(mpx->sample * inv_counter); // RMs
float modulation_power = deviation_to_dbr(avg_deviation);
#ifdef BS412_DEBUG
if(mpx->sample_counter % mpx->sample_rate == 0) {
debug_printf("MPX power: %f dBr\n", modulation_power);
}
#endif
if (mpx->sample_counter >= mpx->sample_rate * 60) {
#ifdef BS412_DEBUG
debug_printf("Resetting MPX power measurement\n");
#endif
mpx->sample = avg_deviation * avg_deviation;
mpx->sample_counter = 1;
}
return modulation_power;
}

View File

@@ -1,23 +0,0 @@
#pragma once
#ifdef DEBUG
#define BS412_DEBUG
#endif
#include <math.h>
#ifdef BS412_DEBUG
#include "../lib/debug.h"
#endif
typedef struct
{
int sample_counter;
int sample_rate;
double sample;
} MPXPowerMeasurement;
float dbr_to_deviation(float dbr);
float deviation_to_dbr(float deviation);
void init_modulation_power_measure(MPXPowerMeasurement *mpx, int sample_rate);
float measure_mpx(MPXPowerMeasurement *mpx, float deviation);

View File

@@ -1,16 +0,0 @@
#include "filters.h"
void init_preemphasis(ResistorCapacitor *filter, float tau, float sample_rate, float ref_freq) {
float dt = 1.0f / sample_rate;
filter->alpha = tau / (tau + dt);
float omega = M_2PI * ref_freq / sample_rate;
filter->gain = 1.0f / sqrtf(1.0f + filter->alpha * filter->alpha - 2.0f * filter->alpha * cosf(omega));
filter->prev_sample = 0.0f;
}
inline float apply_preemphasis(ResistorCapacitor *filter, float sample) {
float out = (sample - filter->alpha * filter->prev_sample) * filter->gain;
filter->prev_sample = sample;
return out;
}

View File

@@ -1,15 +0,0 @@
#pragma once
#include <math.h>
#include "../lib/optimization.h"
#include "../lib/constants.h"
typedef struct
{
float alpha;
float prev_sample;
float gain;
} ResistorCapacitor;
void init_preemphasis(ResistorCapacitor *filter, float tau, float sample_rate, float ref_freq);
float apply_preemphasis(ResistorCapacitor *filter, float sample);

View File

@@ -1,15 +0,0 @@
#include "fm_modulator.h"
void init_fm_modulator(FMModulator *fm, float frequency, float deviation, float sample_rate) {
fm->frequency = frequency;
fm->deviation = deviation;
fm->sample_rate = sample_rate;
fm->osc_phase = 0.0f;
}
float modulate_fm(FMModulator *fm, float sample) {
float inst_freq = fm->frequency+(sample*fm->deviation);
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);
}

View File

@@ -1,14 +0,0 @@
#pragma once
#include "../lib/oscillator.h"
typedef struct
{
float frequency;
float deviation;
float osc_phase;
float sample_rate;
} FMModulator;
void init_fm_modulator(FMModulator *fm, float frequency, float deviation, float sample_rate);
float modulate_fm(FMModulator *fm, float sample);

View File

@@ -1,37 +0,0 @@
#include "gain_control.h"
void initAGC(AGC* agc, int sampleRate, float targetLevel, float minGain, float maxGain, float attackTime, float releaseTime) {
agc->sampleRate = sampleRate;
agc->targetLevel = targetLevel;
agc->minGain = minGain;
agc->maxGain = maxGain;
agc->attackTime = attackTime;
agc->releaseTime = releaseTime;
agc->attackCoef = expf(-1.0f / (sampleRate * attackTime));
agc->releaseCoef = expf(-1.0f / (sampleRate * releaseTime));
agc->currentGain = 1.0f;
agc->currentLevel = 0.0f;
agc->rms_buffer = 0.0f;
agc->rmsAlpha = expf(-1.0f / (sampleRate * 0.02f));
}
float process_agc(AGC* agc, float sidechain) {
float x2 = sidechain * sidechain;
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 desiredGain = agc->targetLevel / (agc->currentLevel + 1e-10f);
desiredGain = fminf(fmaxf(desiredGain, agc->minGain), agc->maxGain);
const float gainAlpha = (desiredGain > agc->currentGain) ? agc->attackCoef : agc->releaseCoef;
agc->currentGain = gainAlpha * agc->currentGain + (1.0f - gainAlpha) * desiredGain;
return agc->currentGain;
}

View File

@@ -1,23 +0,0 @@
#pragma once
#include <math.h>
typedef struct {
float targetLevel;
float maxGain;
float minGain;
float attackTime;
float releaseTime;
float currentGain;
float currentLevel;
int sampleRate;
float attackCoef;
float releaseCoef;
float rms_buffer;
float rmsAlpha;
} AGC;
void initAGC(AGC* agc, int sampleRate, float targetLevel, float minGain, float maxGain, float attackTime, float releaseTime);
float process_agc(AGC* agc, float sidechain);

40
dsp/oscillator.c Normal file
View File

@@ -0,0 +1,40 @@
#include "oscillator.h"
void init_oscillator(Oscillator *osc, float frequency, float sample_rate) {
osc->phase = 0.0f;
osc->phase_increment = (M_2PI * frequency) / sample_rate;
osc->sample_rate = sample_rate;
}
inline void change_oscillator_frequency(Oscillator *osc, float frequency) {
osc->phase_increment = (M_2PI * frequency) / osc->sample_rate;
}
float get_oscillator_sin_sample(Oscillator *osc) {
float sample = sinf(osc->phase);
advance_oscillator(osc);
return sample;
}
float get_oscillator_cos_sample(Oscillator *osc) {
float sample = cosf(osc->phase);
advance_oscillator(osc);
return sample;
}
float get_oscillator_sin_multiplier_ni(Oscillator *osc, float multiplier) {
float new_phase = osc->phase * multiplier;
new_phase -= (new_phase >= M_2PI) ? M_2PI : 0.0f;
return sinf(new_phase);
}
float get_oscillator_cos_multiplier_ni(Oscillator *osc, float multiplier) {
float new_phase = osc->phase * multiplier;
new_phase -= (new_phase >= M_2PI) ? M_2PI : 0.0f;
return cosf(new_phase);
}
inline void advance_oscillator(Oscillator *osc) {
osc->phase += osc->phase_increment;
if (osc->phase >= M_2PI) osc->phase -= M_2PI;
}

18
dsp/oscillator.h Normal file
View File

@@ -0,0 +1,18 @@
#pragma once
#include "../lib/constants.h"
#include <math.h>
typedef struct {
float phase;
float phase_increment;
float sample_rate;
} Oscillator;
void init_oscillator(Oscillator *osc, float frequency, float sample_rate);
void change_oscillator_frequency(Oscillator *osc, float frequency);
float get_oscillator_sin_sample(Oscillator *osc);
float get_oscillator_cos_sample(Oscillator *osc);
float get_oscillator_sin_multiplier_ni(Oscillator *osc, float multiplier);
float get_oscillator_cos_multiplier_ni(Oscillator *osc, float multiplier);
void advance_oscillator(Oscillator *osc);