Add library to normalize speech level (Sendevolumenregler SVoR)

Note: This lib has no user yet.
This commit is contained in:
Andreas Eversberg 2017-11-18 12:54:12 +01:00
parent 021c695883
commit 06092e90ed
8 changed files with 252 additions and 0 deletions

2
.gitignore vendored
View File

@ -24,6 +24,7 @@ src/libdebug/libdebug.a
src/libmobile/libmobile.a
src/libdisplay/libdisplay.a
src/libimage/libimage.a
src/libsendevolumenregler/libsendevolumenregler.a
src/libcompandor/libcompandor.a
src/libgoertzel/libgoertzel.a
src/libjitter/libjitter.a
@ -57,6 +58,7 @@ src/r2000/radiocom2000
src/tv/osmotv
sim/cnetz_sim
src/test/test_filter
src/test/test_sendevolumenregler
src/test/test_compandor
src/test/test_emphasis
src/test/test_dms

View File

@ -79,6 +79,7 @@ AC_OUTPUT(
src/libmobile/Makefile
src/libdisplay/Makefile
src/libimage/Makefile
src/libsendevolumenregler/Makefile
src/libcompandor/Makefile
src/libgoertzel/Makefile
src/libjitter/Makefile

View File

@ -6,6 +6,7 @@ SUBDIRS = \
libdisplay \
libsample \
libimage \
libsendevolumenregler \
libcompandor \
libgoertzel \
libjitter \

View File

@ -0,0 +1,6 @@
AM_CPPFLAGS = -Wall -Wextra -g $(all_includes)
noinst_LIBRARIES = libsendevolumenregler.a
libsendevolumenregler_a_SOURCES = \
sendevolumenregler.c

View File

@ -0,0 +1,115 @@
/* Sendevolumenregler to be used by B-Netz
*
* (C) 2017 by Andreas Eversberg <jolly@eversberg.eu>
* All Rights Reserved
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* This function keeps the speech level for the transmitter constant.
*
* B-Netz specs say that levels are amplified to speech level. The maximum gain
* is 16 dB. The level may overshoot by 2.6 dB and must lowered to normal level
* within 20 ms. The level should raise 4.3 dB per second until target level is
* reached.
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include "../libsample/sample.h"
#include "sendevolumenregler.h"
#define db2level(db) pow(10, (double)db / 20.0)
/*
* Init function
*
* abwaerts_dbs = how many dB per second to lower the amplification, if input signal is above dBm0
* aufwaerts_dbs = how many dB per second to raise the amplification, if input signal is below dBm0
* maximum_db = limit of the output level above dBm0
* minimum_db = below this input level, the amplification is not raised, so it stays constant.
* dbm0_level = target level to be treated as 0 dB
*
* Hopefully this is correct
*
*/
void init_sendevolumenregler(sendevolumenregler_t *state, double samplerate, double abwaerts_dbs, double aufwaerts_dbs, double maximum_db, double minimum_db, double dbm0_level)
{
memset(state, 0, sizeof(*state));
state->peak = 1.0;
state->envelope = 1.0;
state->dbm0_level = dbm0_level;
state->step_down = pow(db2level(abwaerts_dbs), 1.0 / samplerate);
state->step_up = pow(db2level(aufwaerts_dbs), 1.0 / samplerate);
state->maximum_level = db2level(maximum_db);
state->minimum_level = db2level(minimum_db);
}
/*
* how to works:
*
* if value is above 'peak', raise 'peak' to value instantaniously
* if value is below 'peak', lower 'peak' by 'step_up' (increase level)
* if 'peak' is above 'envelope', raise 'envelope' by 'step_down' (decrease level)
* if 'peak' is below 'envelope', lower 'envelope' to 'peak' (increase level)
* 'envelope' will not fall below 'minimum_level' (maximum amplification)
* if 'peak' is 'maximum_level' above 'envelope', raise 'envelope' to 'maximum_level' below 'peak'
*/
void sendevolumenregler(sendevolumenregler_t *state, sample_t *samples, int num)
{
double value, peak, envelope, step_up, step_down, maximum_level, minimum_level, dbm0_level;
int i;
dbm0_level = state->dbm0_level;
step_up = state->step_up;
step_down = state->step_down;
maximum_level = state->maximum_level;
minimum_level = state->minimum_level;
peak = state->peak;
envelope = state->envelope;
for (i = 0; i < num; i++) {
/* normalize sample value to dbm0_level level */
value = *samples / dbm0_level;
/* 'peak' is the level that raises directly with the value
* level, but falls as specified by step_up. */
if (fabs(value) > peak)
peak = fabs(value);
else
peak /= step_up;
/* 'evelope' is the level that raises with the specified step_down
* to 'peak', but falls with 'peak'. */
if (peak > envelope)
envelope *= step_down;
else
envelope = peak;
/* no envelope below minimum level */
if (envelope < minimum_level)
envelope = minimum_level;
/* raise envelope, if 'peak' exceeds maximum level */
if (peak / envelope > maximum_level)
envelope = peak / maximum_level;
*samples++ = value / envelope * dbm0_level;
}
state->envelope = envelope;
state->peak = peak;
}

View File

@ -0,0 +1,14 @@
typedef struct sendevolumenregler {
double peak;
double envelope;
double step_up;
double step_down;
double minimum_level;
double maximum_level;
double dbm0_level;
} sendevolumenregler_t;
void init_sendevolumenregler(sendevolumenregler_t *state, double samplerate, double abwaerts_dbs, double aufwaerts_dbs, double maximum_db, double minimum_db, double dbm0_level);
void sendevolumenregler(sendevolumenregler_t *state, sample_t *samples, int num);

View File

@ -2,6 +2,7 @@ AM_CPPFLAGS = -Wall -g $(all_includes)
noinst_PROGRAMS = \
test_filter \
test_sendevolumenregler \
test_compandor \
test_emphasis \
test_dms \
@ -37,6 +38,13 @@ test_filter_LDADD += \
$(SOAPY_LIBS)
endif
test_sendevolumenregler_SOURCES = test_sendevolumenregler.c
test_sendevolumenregler_LDADD = \
$(COMMON_LA) \
$(top_builddir)/src/libsendevolumenregler/libsendevolumenregler.a \
-lm
test_compandor_SOURCES = dummy.x test_compandor.c
test_compandor_LDADD = \

View File

@ -0,0 +1,105 @@
#include <stdio.h>
#include <stdint.h>
#include <math.h>
#include <string.h>
#include "../libsample/sample.h"
#include "../libsendevolumenregler/sendevolumenregler.h"
#define level2db(level) (20 * log10(level))
#define db2level(db) pow(10, (double)db / 20.0)
#define SAMPLERATE 48000
#define ABWAERTS_DBS (2.6 / 0.020) /* 2.6 dB in 20 ms */
#define AUFWAERTS_DBS 4.3
#define MINIMUM_DB -16.0
#define MAXIMUM_DB 2.6
static double test_frequency[3] = { 1000.0, 4000.0, 300.0 };
static sample_t samples_plus4db[SAMPLERATE];
static sample_t samples_minus20db[SAMPLERATE];
static sample_t samples_0db[SAMPLERATE];
/* generate 2 samples: one with -4 dB, the other with -16 dB */
static void generate_test_sample(double test_frequency)
{
int i;
double value;
for (i = 0; i < SAMPLERATE; i++) {
value = cos(2.0 * M_PI * test_frequency / (double)SAMPLERATE * i);
samples_plus4db[i] = value * db2level(4);
samples_minus20db[i] = value * db2level(-20);
samples_0db[i] = value;
}
}
static void check_level(sample_t *samples, const char *desc, double target_db, int when)
{
int i;
double last = 0.0, envelope = 0.0;
int up = 0;
double factor;
for (i = 0; i < when; i++) {
//puts(debug_amplitude(samples[i]));
if (last < samples[i]) {
up = 1;
} else if (last > samples[i]) {
if (up) {
envelope = last;
}
up = 0;
}
#if 0
if ((i % (SAMPLERATE/(SAMPLERATE/100))) == 0)
printf("%s: envelope = %.4f (val=%.4f) (when=%d ms)\n", desc, level2db(envelope), envelope, i * 1000 / SAMPLERATE);
#endif
last = samples[i];
}
factor = envelope / db2level(target_db);
printf("%s: envelope after %d ms is %.4f db (factor %.4f)\n", desc, i * 1000 / SAMPLERATE, level2db(envelope), factor);
if (factor > 1.1 || factor < 0.9) {
printf("**** ERROR: we expected a factor close to 1.0\n");
}
}
int main(void)
{
sendevolumenregler_t state;
sample_t samples[SAMPLERATE * 2];
int f;
for (f = 0; f < 3; f++) {
printf("Testing frequency %.0f Hz:\n", test_frequency[f]);
generate_test_sample(test_frequency[f]);
/* increment low level */
memcpy(samples, samples_minus20db, SAMPLERATE * sizeof(sample_t));
init_sendevolumenregler(&state, SAMPLERATE, ABWAERTS_DBS, AUFWAERTS_DBS, MAXIMUM_DB, MINIMUM_DB, 1.0);
sendevolumenregler(&state, samples, SAMPLERATE);
check_level(samples, "aufwaerts (-20 dB)", AUFWAERTS_DBS - 20.0, 1000 * SAMPLERATE / 1000);
/* decrement high level */
memcpy(samples, samples_plus4db, SAMPLERATE * sizeof(sample_t));
init_sendevolumenregler(&state, SAMPLERATE, ABWAERTS_DBS, AUFWAERTS_DBS, MAXIMUM_DB, MINIMUM_DB, 1.0);
sendevolumenregler(&state, samples, SAMPLERATE);
check_level(samples, "abwaerts (+4 dB)", MAXIMUM_DB / 2.0, 10 * SAMPLERATE / 1000);
check_level(samples, "abwaerts (+4 dB)", 0.0, 20 * SAMPLERATE / 1000);
/* 0 DB */
memcpy(samples, samples_0db, SAMPLERATE * sizeof(sample_t));
init_sendevolumenregler(&state, SAMPLERATE, ABWAERTS_DBS, AUFWAERTS_DBS, MAXIMUM_DB, MINIMUM_DB, 1.0);
check_level(samples, "unaffected level", 0.0, 1000 * SAMPLERATE / 1000);
puts("");
}
return 0;
}