0
1
mirror of https://github.com/radio95-rnt/fm95.git synced 2026-02-26 11:22:00 +01:00
This commit is contained in:
2025-04-27 13:11:42 +02:00
parent ca82168784
commit c4d5c39ed5
17 changed files with 82 additions and 149 deletions

View File

@@ -1,30 +0,0 @@
#include "bs412.h"
float dbr_to_deviation(float dbr) {
return 19000.0f * powf(10.0f, dbr / 10.0f);
}
float deviation_to_dbr(float deviation) {
return 10 * log10f((deviation + 1e-6f) / 19000.0f);
}
void init_modulation_power_measure(MPXPowerMeasurement* mpx, int sample_rate) {
mpx->i = 1;
mpx->sample = 0;
mpx->sample_rate = sample_rate;
}
float measure_mpx(MPXPowerMeasurement* mpx, float deviation) {
mpx->sample += deviation * deviation; // rmS
mpx->i++;
float avg_deviation = sqrtf(mpx->sample / mpx->i); // RMs
float modulation_power = deviation_to_dbr(avg_deviation);
if (mpx->i >= mpx->sample_rate * 60) {
mpx->sample = 0;
mpx->i = 0;
}
return modulation_power;
}

View File

@@ -1,15 +0,0 @@
#pragma once
#include <math.h>
typedef struct
{
int i;
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,7 +0,0 @@
#pragma once
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#ifndef M_2PI
#define M_2PI (M_PI * 2.0)
#endif

View File

@@ -1,16 +0,0 @@
#include "filters.h"
void init_preemphasis(ResistorCapacitor *filter, float tau, float sample_rate) {
filter->prev_sample = 0.0f;
filter->alpha = expf(-1 / (tau*sample_rate));
filter->gain = 1.0f / sqrtf(1.0f - filter->alpha);
}
float apply_preemphasis(ResistorCapacitor *filter, float sample) {
float out = (sample - filter->alpha * filter->prev_sample) * filter->gain;
filter->prev_sample = sample;
return out;
}
float hard_clip(float sample, float threshold) {
return fmaxf(-threshold, fminf(threshold, sample));
}

View File

@@ -1,20 +0,0 @@
#pragma once
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "constants.h"
#include "optimization.h"
#include "oscillator.h"
typedef struct
{
float alpha;
float prev_sample;
float gain;
} ResistorCapacitor;
void init_preemphasis(ResistorCapacitor *filter, float tau, float sample_rate);
float apply_preemphasis(ResistorCapacitor *filter, float sample);
float hard_clip(float sample, float threshold);

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 "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,40 +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;
}
float process_agc_stereo(AGC* agc, float left, float right, float *right_out) {
float sample = (left+right)/2;
float x2 = sample * sample;
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);
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);
desiredGain = fminf(fmaxf(desiredGain, agc->minGain), agc->maxGain);
float gainAlpha = (desiredGain > agc->currentGain) ? agc->attackCoef : agc->releaseCoef;
agc->currentGain = gainAlpha * agc->currentGain + (1.0f - gainAlpha) * desiredGain;
*right_out = right * agc->currentGain;
return left * agc->currentGain;
}

View File

@@ -1,22 +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;
} AGC;
void initAGC(AGC* agc, int sampleRate, float targetLevel, float minGain, float maxGain, float attackTime, float releaseTime);
float process_agc_stereo(AGC* agc, float left, float right, float *right_out);

View File

@@ -1,42 +0,0 @@
#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;
}
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);
}
void advance_oscillator(Oscillator *osc) {
osc->phase += osc->phase_increment;
if (osc->phase >= M_2PI) {
osc->phase -= M_2PI;
}
}

View File

@@ -1,18 +0,0 @@
#pragma once
#include "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);