Generate compador table only once when the application starts

This commit is contained in:
Andreas Eversberg 2023-03-18 19:49:56 +01:00
parent bdecacc99e
commit 28a297f97d
8 changed files with 33 additions and 12 deletions

View File

@ -162,6 +162,8 @@ void dsp_init(void)
}
dsp_sync_check[0x712] = 0x00; /* no bit error */
dsp_sync_check[0x0ed] = 0x80; /* no bit error */
compandor_init();
}
static void dsp_init_ramp(amps_t *amps)
@ -194,7 +196,7 @@ int dsp_init_sender(amps_t *amps, int tolerant)
int half;
/* attack (3ms) and recovery time (13.5ms) according to amps specs */
init_compandor(&amps->cstate, 8000, 3.0, 13.5);
setup_compandor(&amps->cstate, 8000, 3.0, 13.5);
PDEBUG_CHAN(DDSP, DEBUG_DEBUG, "Init DSP for transceiver.\n");

View File

@ -76,6 +76,7 @@ const char *cnetz_dsp_mode_name(enum dsp_mode mode)
void dsp_init(void)
{
compandor_init();
}
static void dsp_init_ramp(cnetz_t *cnetz)
@ -160,7 +161,7 @@ int dsp_init_sender(cnetz_t *cnetz, int measure_speed, double clock_speed[2], en
/* init compandor, according to C-Netz specs, attack and recovery time
* shall not exceed according to ITU G.162 */
init_compandor(&cnetz->cstate, 8000, 5.0, 22.5);
setup_compandor(&cnetz->cstate, 8000, 5.0, 22.5);
/* use duration of one bit to ramp level of last frame to current frame */
cnetz->offset_range = ceil(cnetz->fsk_bitduration);

View File

@ -19,6 +19,7 @@
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "../libsample/sample.h"
@ -39,6 +40,7 @@
#define ENVELOPE_MAX 9.990
static double sqrt_tab[10000];
static int compandor_initalized = 0;
/*
* Init compandor according to ITU-T G.162 specification
@ -46,10 +48,24 @@ static double sqrt_tab[10000];
* Hopefully this is correct
*
*/
void init_compandor(compandor_t *state, double samplerate, double attack_ms, double recovery_ms)
void compandor_init(void)
{
int i;
// FIXME: make global, not at instance
for (i = 0; i < 10000; i++)
sqrt_tab[i] = sqrt(i * 0.001);
compandor_initalized = 1;
}
void setup_compandor(compandor_t *state, double samplerate, double attack_ms, double recovery_ms)
{
if (!compandor_initalized) {
fprintf(stderr, "Compandor nicht initialized.\n");
abort();
}
memset(state, 0, sizeof(*state));
state->c.peak = 1.0;
@ -60,10 +76,6 @@ void init_compandor(compandor_t *state, double samplerate, double attack_ms, dou
state->c.step_down = pow(COMPRESS_RECOVERY_FACTOR, 1000.0 / recovery_ms / samplerate);
state->e.step_up = pow(EXPAND_ATTACK_FACTOR, 1000.0 / attack_ms / samplerate);
state->e.step_down = pow(EXPAND_RECOVERY_FACTOR, 1000.0 / recovery_ms / samplerate);
// FIXME: make global, not at instance
for (i = 0; i < 10000; i++)
sqrt_tab[i] = sqrt(i * 0.001);
}
void compress_audio(compandor_t *state, sample_t *samples, int num)

View File

@ -14,7 +14,8 @@ typedef struct compandor {
} e;
} compandor_t;
void init_compandor(compandor_t *state, double samplerate, double attack_ms, double recovery_ms);
void compandor_init(void);
void setup_compandor(compandor_t *state, double samplerate, double attack_ms, double recovery_ms);
void compress_audio(compandor_t *state, sample_t *samples, int num);
void expand_audio(compandor_t *state, sample_t *samples, int num);

View File

@ -24,6 +24,7 @@
#include <pthread.h>
#include <sys/ioctl.h>
#include <math.h>
#include <sys/param.h>
#include "../libsample/sample.h"
#include "../libdebug/debug.h"
#include "../libdisplay/display.h"
@ -246,7 +247,7 @@ static void print_measurements(int on)
memset(line_color + width - MAX_UNIT_LEN, 4, MAX_UNIT_LEN); /* blue */
else
memset(line_color + width - MAX_UNIT_LEN, 3, MAX_UNIT_LEN); /* yellow */
strncpy(line + width - MAX_UNIT_LEN + 1, text, (strlen(text) < MAX_UNIT_LEN) ? strlen(text) : MAX_UNIT_LEN);
memcpy(line + width - MAX_UNIT_LEN + 1, text, MAX(strlen(text), MAX_UNIT_LEN));
display_line(on, width);
}
}

View File

@ -97,6 +97,8 @@ void dsp_init(void)
/* dialtone sine */
dsp_sine_dialtone[i] = s * TX_PEAK_DIALTONE;
}
compandor_init();
}
static int fsk_send_bit(void *inst);
@ -109,7 +111,7 @@ int dsp_init_sender(nmt_t *nmt, double deviation_factor)
int i;
/* attack (3ms) and recovery time (13.5ms) according to NMT specs */
init_compandor(&nmt->cstate, 8000, 3.0, 13.5);
setup_compandor(&nmt->cstate, 8000, 3.0, 13.5);
PDEBUG_CHAN(DDSP, DEBUG_DEBUG, "Init DSP for Transceiver.\n");

View File

@ -64,6 +64,7 @@
/* global init for FSK */
void dsp_init(void)
{
compandor_init();
}
static int fsk_send_bit(void *inst);
@ -75,7 +76,7 @@ static void super_receive_bit(void *inst, int bit, double quality, double level)
int dsp_init_sender(r2000_t *r2000)
{
/* attack (3ms) and recovery time (13.5ms) according to NMT specs */
init_compandor(&r2000->cstate, 8000, 3.0, 13.5);
setup_compandor(&r2000->cstate, 8000, 3.0, 13.5);
PDEBUG_CHAN(DDSP, DEBUG_DEBUG, "Init DSP for Transceiver.\n");

View File

@ -72,7 +72,8 @@ int main(void)
sample_t samples[SAMPLERATE * 2];
int f;
init_compandor(&cstate, SAMPLERATE, ATTACK_MS, RECOVERY_MS);
compandor_init();
setup_compandor(&cstate, SAMPLERATE, ATTACK_MS, RECOVERY_MS);
for (f = 0; f < 3; f++) {
/* -16 and -4 dB */