mirror of
https://github.com/radio95-rnt/fm95.git
synced 2026-07-30 16:29:15 +02:00
agc?
This commit is contained in:
+3
-3
@@ -3,9 +3,9 @@
|
|||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int sample_rate;
|
int sample_rate;
|
||||||
double sample;
|
double sample;
|
||||||
} MPXPowerMeasurement;
|
} MPXPowerMeasurement;
|
||||||
|
|
||||||
float dbr_to_deviation(float dbr);
|
float dbr_to_deviation(float dbr);
|
||||||
|
|||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#define debug_printf(fmt, ...) \
|
#define debug_printf(fmt, ...) \
|
||||||
printf("[%s:%d in %s] " fmt, __FILE__, __LINE__, __func__, ##__VA_ARGS__)
|
printf("[%s:%d in %s] " fmt, __FILE__, __LINE__, __func__, ##__VA_ARGS__)
|
||||||
|
|||||||
@@ -7,12 +7,6 @@
|
|||||||
#include "optimization.h"
|
#include "optimization.h"
|
||||||
#include "oscillator.h"
|
#include "oscillator.h"
|
||||||
|
|
||||||
#if USE_NEON
|
|
||||||
#define LPF_ORDER 20 // neon has to have divisable by 4
|
|
||||||
#else
|
|
||||||
#define LPF_ORDER 10
|
|
||||||
#endif
|
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
float alpha;
|
float alpha;
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
#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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
#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);
|
||||||
+5
-5
@@ -23,15 +23,15 @@ float get_oscillator_cos_sample(Oscillator *osc) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
float get_oscillator_sin_multiplier_ni(Oscillator *osc, float multiplier) {
|
float get_oscillator_sin_multiplier_ni(Oscillator *osc, float multiplier) {
|
||||||
float new_phase = osc->phase * multiplier;
|
float new_phase = osc->phase * multiplier;
|
||||||
new_phase -= (new_phase >= M_2PI) ? M_2PI : 0.0f;
|
new_phase -= (new_phase >= M_2PI) ? M_2PI : 0.0f;
|
||||||
return sinf(new_phase);
|
return sinf(new_phase);
|
||||||
}
|
}
|
||||||
|
|
||||||
float get_oscillator_cos_multiplier_ni(Oscillator *osc, float multiplier) {
|
float get_oscillator_cos_multiplier_ni(Oscillator *osc, float multiplier) {
|
||||||
float new_phase = osc->phase * multiplier;
|
float new_phase = osc->phase * multiplier;
|
||||||
new_phase -= (new_phase >= M_2PI) ? M_2PI : 0.0f;
|
new_phase -= (new_phase >= M_2PI) ? M_2PI : 0.0f;
|
||||||
return cosf(new_phase);
|
return cosf(new_phase);
|
||||||
}
|
}
|
||||||
|
|
||||||
void advance_oscillator(Oscillator *osc) {
|
void advance_oscillator(Oscillator *osc) {
|
||||||
|
|||||||
+30
-30
@@ -133,41 +133,41 @@ void calculate_dcf77_bits(time_t now, int *bits) {
|
|||||||
bits[20] = 1;
|
bits[20] = 1;
|
||||||
|
|
||||||
int minutes = t->tm_min;
|
int minutes = t->tm_min;
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
bits[21 + i] = (minutes % 10 >> i) & 1;
|
bits[21 + i] = (minutes % 10 >> i) & 1;
|
||||||
}
|
}
|
||||||
for (int i = 0; i < 3; i++) {
|
for (int i = 0; i < 3; i++) {
|
||||||
bits[25 + i] = (minutes / 10 >> i) & 1;
|
bits[25 + i] = (minutes / 10 >> i) & 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int minute_parity = 0;
|
int minute_parity = 0;
|
||||||
for (int i = 21; i <= 27; i++) {
|
for (int i = 21; i <= 27; i++) {
|
||||||
minute_parity ^= bits[i];
|
minute_parity ^= bits[i];
|
||||||
}
|
}
|
||||||
bits[28] = minute_parity;
|
bits[28] = minute_parity;
|
||||||
|
|
||||||
int hours = t->tm_hour;
|
int hours = t->tm_hour;
|
||||||
if(cest) hours += 1;
|
if(cest) hours += 1;
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
bits[29 + i] = (hours % 10 >> i) & 1;
|
bits[29 + i] = (hours % 10 >> i) & 1;
|
||||||
}
|
}
|
||||||
for (int i = 0; i < 2; i++) {
|
for (int i = 0; i < 2; i++) {
|
||||||
bits[33 + i] = (hours / 10 >> i) & 1;
|
bits[33 + i] = (hours / 10 >> i) & 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int hour_parity = 0;
|
int hour_parity = 0;
|
||||||
for (int i = 29; i <= 34; i++) {
|
for (int i = 29; i <= 34; i++) {
|
||||||
hour_parity ^= bits[i];
|
hour_parity ^= bits[i];
|
||||||
}
|
}
|
||||||
bits[35] = hour_parity;
|
bits[35] = hour_parity;
|
||||||
|
|
||||||
int day = t->tm_mday;
|
int day = t->tm_mday;
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
bits[36 + i] = (day % 10 >> i) & 1;
|
bits[36 + i] = (day % 10 >> i) & 1;
|
||||||
}
|
}
|
||||||
for (int i = 0; i < 2; i++) {
|
for (int i = 0; i < 2; i++) {
|
||||||
bits[40 + i] = (day / 10 >> i) & 1;
|
bits[40 + i] = (day / 10 >> i) & 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int dow = t->tm_wday == 0 ? 7 : t->tm_wday;
|
int dow = t->tm_wday == 0 ? 7 : t->tm_wday;
|
||||||
bits[42] = dow & 0x01;
|
bits[42] = dow & 0x01;
|
||||||
@@ -176,17 +176,17 @@ void calculate_dcf77_bits(time_t now, int *bits) {
|
|||||||
|
|
||||||
int month = t->tm_mon + 1;
|
int month = t->tm_mon + 1;
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
bits[45 + i] = (month % 10 >> i) & 1;
|
bits[45 + i] = (month % 10 >> i) & 1;
|
||||||
}
|
}
|
||||||
bits[49] = (month / 10) & 0x01;
|
bits[49] = (month / 10) & 0x01;
|
||||||
|
|
||||||
int year = t->tm_year % 100;
|
int year = t->tm_year % 100;
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
bits[50 + i] = (year % 10 >> i) & 1;
|
bits[50 + i] = (year % 10 >> i) & 1;
|
||||||
}
|
}
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
bits[54 + i] = (year / 10 >> i) & 1;
|
bits[54 + i] = (year / 10 >> i) & 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int year_parity = 0;
|
int year_parity = 0;
|
||||||
for (int i = 36; i <= 57; i++) {
|
for (int i = 36; i <= 57; i++) {
|
||||||
|
|||||||
@@ -21,6 +21,7 @@
|
|||||||
#include "../lib/fm_modulator.h"
|
#include "../lib/fm_modulator.h"
|
||||||
#include "../lib/optimization.h"
|
#include "../lib/optimization.h"
|
||||||
#include "../lib/bs412.h"
|
#include "../lib/bs412.h"
|
||||||
|
#include "../lib/gain_control.h"
|
||||||
|
|
||||||
#define DEFAULT_SAMPLE_RATE 192000
|
#define DEFAULT_SAMPLE_RATE 192000
|
||||||
|
|
||||||
@@ -453,6 +454,10 @@ int main(int argc, char **argv) {
|
|||||||
MPXPowerMeasurement mpx_only_power;
|
MPXPowerMeasurement mpx_only_power;
|
||||||
init_modulation_power_measure(&mpx_only_power, sample_rate);
|
init_modulation_power_measure(&mpx_only_power, sample_rate);
|
||||||
|
|
||||||
|
AGC agc;
|
||||||
|
void initAGC(AGC* agc, int sampleRate, float targetLevel, float minGain, float maxGain, float attackTime, float releaseTime);
|
||||||
|
initAGC(&agc, sample_rate, 0.707f, 0.25f, 2.5f, 0.01f, 0.4f);
|
||||||
|
|
||||||
signal(SIGINT, stop);
|
signal(SIGINT, stop);
|
||||||
signal(SIGTERM, stop);
|
signal(SIGTERM, stop);
|
||||||
|
|
||||||
@@ -512,6 +517,7 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
float ready_l = apply_preemphasis(&preemp_l, l_in);
|
float ready_l = apply_preemphasis(&preemp_l, l_in);
|
||||||
float ready_r = apply_preemphasis(&preemp_r, r_in);
|
float ready_r = apply_preemphasis(&preemp_r, r_in);
|
||||||
|
ready_l = process_agc_stereo(&agc, ready_l, ready_r, &ready_r);
|
||||||
ready_l = hard_clip(ready_l*audio_volume, clipper_threshold);
|
ready_l = hard_clip(ready_l*audio_volume, clipper_threshold);
|
||||||
ready_r = hard_clip(ready_r*audio_volume, clipper_threshold);
|
ready_r = hard_clip(ready_r*audio_volume, clipper_threshold);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user