0
1
mirror of https://github.com/radio95-rnt/fm95.git synced 2026-02-27 03:23:54 +01:00

make the bs412 actually work (use rms)

This commit is contained in:
2025-04-18 22:02:08 +02:00
parent de3763dc83
commit e730f13a6d
3 changed files with 21 additions and 13 deletions

View File

@@ -1,11 +1,11 @@
#include "bs412.h" #include "bs412.h"
float dbr_to_deviation(float dbr) { float dbr_to_deviation(float dbr) {
return 19000.0f * powf(10.0f, dbr / 20.0f); return (19000.0f * 0.70710678f) * powf(10.0f, dbr / 20.0f);
} }
float deviation_to_dbr(float deviation) { float deviation_to_dbr(float deviation) {
return 20 * log10f((deviation + 1e-6f) / 19000.0f); return 20 * log10f((deviation + 1e-6f) / (19000.0f * 0.70710678f));
} }
void init_modulation_power_measure(MPXPowerMeasurement* mpx, int sample_rate) { void init_modulation_power_measure(MPXPowerMeasurement* mpx, int sample_rate) {
@@ -15,16 +15,19 @@ void init_modulation_power_measure(MPXPowerMeasurement* mpx, int sample_rate) {
} }
float measure_mpx(MPXPowerMeasurement* mpx, float deviation) { float measure_mpx(MPXPowerMeasurement* mpx, float deviation) {
mpx->sample += deviation; mpx->sample += deviation * deviation;
mpx->i++;
float avg_deviation = (float)mpx->sample / mpx->i; float avg_deviation = sqrtf(mpx->sample / mpx->i);
float modulation_power = deviation_to_dbr(avg_deviation); float modulation_power = deviation_to_dbr(avg_deviation);
mpx->i++; if (mpx->i == 1 || (mpx->i % mpx->sample_rate) == 0) {
}
if (mpx->i >= mpx->sample_rate*60) { if (mpx->i >= mpx->sample_rate * 60) {
mpx->sample = avg_deviation; mpx->sample = 0;
mpx->i = 2; mpx->i = 0;
} }
return modulation_power;
} return modulation_power;
}

View File

@@ -1,11 +1,12 @@
#pragma once #pragma once
#include "debug.h"
#include <math.h> #include <math.h>
typedef struct typedef struct
{ {
int i; int i;
int sample_rate; int sample_rate;
float sample; double sample;
} MPXPowerMeasurement; } MPXPowerMeasurement;
float dbr_to_deviation(float dbr); float dbr_to_deviation(float dbr);

4
lib/debug.h Normal file
View File

@@ -0,0 +1,4 @@
#pragma once
#include <stdio.h>
#define debug_printf(fmt, ...) \
printf("[%s:%d in %s] " fmt, __FILE__, __LINE__, __func__, ##__VA_ARGS__)