common: Add pre and de emphasis, if the radio does not support it

Connect to the oscillator of the transmitter - good girl!
You need to enable pre emphasis in this case.

Connect to the discriminator of the receiver - good boy!
You need to enable de emphasis in this case.
pull/1/head
Andreas Eversberg 7 years ago
parent 5062628e52
commit 4356c93afa
  1. 1
      .gitignore
  2. 4
      src/anetz/anetz.c
  3. 2
      src/anetz/anetz.h
  4. 2
      src/anetz/main.c
  5. 4
      src/bnetz/bnetz.c
  6. 2
      src/bnetz/bnetz.h
  7. 2
      src/bnetz/main.c
  8. 1
      src/common/Makefile.am
  9. 102
      src/common/emphasis.c
  10. 17
      src/common/emphasis.h
  11. 2
      src/common/main.h
  12. 20
      src/common/main_common.c
  13. 12
      src/common/sender.c
  14. 6
      src/common/sender.h
  15. 5
      src/nmt/dsp.c
  16. 2
      src/nmt/main.c
  17. 4
      src/nmt/nmt.c
  18. 2
      src/nmt/nmt.h
  19. 10
      src/test/Makefile.am
  20. 75
      src/test/test_emphasis.c

1
.gitignore vendored

@ -24,3 +24,4 @@ src/anetz/anetz
src/bnetz/bnetz
src/nmt/nmt
test/test_compander
test/test_emphasis

@ -137,7 +137,7 @@ static void anetz_timeout(struct timer *timer);
static void anetz_go_idle(anetz_t *anetz);
/* Create transceiver instance and link to a list. */
int anetz_create(const char *sounddev, int samplerate, const char *write_wave, const char *read_wave, int kanal, int loopback, double loss_volume)
int anetz_create(const char *sounddev, int samplerate, int pre_emphasis, int de_emphasis, const char *write_wave, const char *read_wave, int kanal, int loopback, double loss_volume)
{
anetz_t *anetz;
int rc;
@ -156,7 +156,7 @@ int anetz_create(const char *sounddev, int samplerate, const char *write_wave, c
PDEBUG(DANETZ, DEBUG_DEBUG, "Creating 'A-Netz' instance for 'Kanal' = %d (sample rate %d).\n", kanal, samplerate);
/* init general part of transceiver */
rc = sender_create(&anetz->sender, sounddev, samplerate, write_wave, read_wave, kanal, loopback, loss_volume, -1);
rc = sender_create(&anetz->sender, sounddev, samplerate, pre_emphasis, de_emphasis, write_wave, read_wave, kanal, loopback, loss_volume, -1);
if (rc < 0) {
PDEBUG(DANETZ, DEBUG_ERROR, "Failed to init 'Sender' processing!\n");
goto error;

@ -41,7 +41,7 @@ typedef struct anetz {
double anetz_kanal2freq(int kanal, int unterband);
int anetz_init(void);
int anetz_create(const char *sounddev, int samplerate, const char *write_wave, const char *read_wave, int kanal, int loopback, double loss_volume);
int anetz_create(const char *sounddev, int samplerate, int pre_emphasis, int de_emphasis, const char *write_wave, const char *read_wave, int kanal, int loopback, double loss_volume);
void anetz_destroy(sender_t *sender);
void anetz_loss_indication(anetz_t *anetz);
void anetz_receive_tone(anetz_t *anetz, int bit);

@ -142,7 +142,7 @@ int main(int argc, char *argv[])
}
/* create transceiver instance */
rc = anetz_create(sounddev, samplerate, write_wave, read_wave, kanal, loopback, lossdetect / 100.0);
rc = anetz_create(sounddev, samplerate, do_pre_emphasis, do_de_emphasis, write_wave, read_wave, kanal, loopback, lossdetect / 100.0);
if (rc < 0) {
fprintf(stderr, "Failed to create \"Sender\" instance. Quitting!\n");
goto fail;

@ -242,7 +242,7 @@ static void bnetz_timeout(struct timer *timer);
static void bnetz_go_idle(bnetz_t *bnetz);
/* Create transceiver instance and link to a list. */
int bnetz_create(const char *sounddev, int samplerate, const char *write_wave, const char *read_wave, int kanal, int gfs, int loopback, double loss_factor, const char *pilot)
int bnetz_create(const char *sounddev, int samplerate, int pre_emphasis, int de_emphasis, const char *write_wave, const char *read_wave, int kanal, int gfs, int loopback, double loss_factor, const char *pilot)
{
bnetz_t *bnetz;
int use_pilot_tone = -1;
@ -300,7 +300,7 @@ error_pilot:
PDEBUG(DBNETZ, DEBUG_DEBUG, "Creating 'B-Netz' instance for 'Kanal' = %d 'Gruppenfreisignal' = %d (sample rate %d).\n", kanal, gfs, samplerate);
/* init general part of transceiver */
rc = sender_create(&bnetz->sender, sounddev, samplerate, write_wave, read_wave, kanal, loopback, loss_factor, use_pilot_tone);
rc = sender_create(&bnetz->sender, sounddev, samplerate, pre_emphasis, de_emphasis, write_wave, read_wave, kanal, loopback, loss_factor, use_pilot_tone);
if (rc < 0) {
PDEBUG(DBNETZ, DEBUG_ERROR, "Failed to init transceiver process!\n");
goto error;

@ -90,7 +90,7 @@ typedef struct bnetz {
double bnetz_kanal2freq(int kanal, int unterband);
int bnetz_init(void);
int bnetz_create(const char *sounddev, int samplerate, const char *write_wave, const char *read_wave, int kanal, int gfs, int loopback, double loss_volume, const char *pilot);
int bnetz_create(const char *sounddev, int samplerate, int pre_emphasis, int de_emphasis, const char *write_wave, const char *read_wave, int kanal, int gfs, int loopback, double loss_volume, const char *pilot);
void bnetz_destroy(sender_t *sender);
void bnetz_loss_indication(bnetz_t *bnetz);
void bnetz_receive_tone(bnetz_t *bnetz, int bit);

@ -154,7 +154,7 @@ int main(int argc, char *argv[])
}
/* create transceiver instance */
rc = bnetz_create(sounddev, samplerate, write_wave, read_wave, kanal, gfs, loopback, (double)lossdetect / 100.0, pilot);
rc = bnetz_create(sounddev, samplerate, do_pre_emphasis, do_de_emphasis, write_wave, read_wave, kanal, gfs, loopback, (double)lossdetect / 100.0, pilot);
if (rc < 0) {
fprintf(stderr, "Failed to create \"Sender\" instance. Quitting!\n");
goto fail;

@ -18,6 +18,7 @@ libcommon_a_SOURCES = \
../common/besetztton.c \
../common/mncc_sock.c \
../common/cause.c \
../common/emphasis.c \
../common/compander.c \
../common/sender.c \
../common/main_common.c

@ -0,0 +1,102 @@
/* Pre-Emphasis and De-Emphasis implementation
*
* (C) 2016 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/>.
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include "emphasis.h"
#include "debug.h"
int init_emphasis(emphasis_t *state, int samplerate)
{
double factor;
memset(state, 0, sizeof(*state));
if (samplerate < 24000) {
PDEBUG(DDSP, DEBUG_ERROR, "Sample rate must be at least 24000 Hz!\n");
return -1;
}
factor = 0.95;
state->p.factor = factor;
state->p.amp = samplerate / 48000.0 * 4.0; /* mysterious 48000 */
state->d.factor = factor;
state->d.amp = 1.0 / (samplerate / 48000.0 * 4.0); /* mysterious 48000 */
return 0;
}
void pre_emphasis(emphasis_t *state, int16_t *samples, int num)
{
int32_t sample;
double old_value, new_value, last_value, factor, amp;
int i;
last_value = state->p.last_value;
factor = state->p.factor;
amp = state->p.amp;
for (i = 0; i < num; i++) {
old_value = (double)(*samples) / 32768.0;
new_value = old_value - factor * last_value;
last_value = old_value;
sample = (int)(amp * new_value * 32768.0);
if (sample > 32767)
sample = 32767;
else if (sample < -32768)
sample = -32768;
*samples++ = sample;
}
state->p.last_value = last_value;
}
void de_emphasis(emphasis_t *state, int16_t *samples, int num)
{
int32_t sample;
double old_value, new_value, last_value, factor, amp;
int i;
last_value = state->d.last_value;
factor = state->d.factor;
amp = state->d.amp;
for (i = 0; i < num; i++) {
old_value = (double)(*samples) / 32768.0;
new_value = old_value + factor * last_value;
last_value = new_value;
sample = (int)(amp * new_value * 32768.0);
if (sample > 32767)
sample = 32767;
else if (sample < -32768)
sample = -32768;
*samples++ = sample;
}
state->d.last_value = last_value;
}

@ -0,0 +1,17 @@
typedef struct emphasis {
struct {
double last_value;
double factor;
double amp;
} p;
struct {
double last_value;
double factor;
double amp;
} d;
} emphasis_t;
int init_emphasis(emphasis_t *state, int samplerate);
void pre_emphasis(emphasis_t *state, int16_t *samples, int num);
void de_emphasis(emphasis_t *state, int16_t *samples, int num);

@ -8,6 +8,8 @@ extern int use_mncc_sock;
extern int send_patterns;
extern int loopback;
extern int rt_prio;
extern int do_pre_emphasis;
extern int do_de_emphasis;
extern const char *read_wave;
extern const char *write_wave;

@ -36,6 +36,8 @@ int use_mncc_sock = 0;
int send_patterns = 1;
int loopback = 0;
int rt_prio = 0;
int do_pre_emphasis = 0;
int do_de_emphasis = 0;
const char *read_wave = NULL;
const char *write_wave = NULL;
@ -66,6 +68,12 @@ void print_help_common(const char *arg0, const char *ext_usage)
printf(" Loopback test: 1 = internal | 2 = external | 3 = echo\n");
printf(" -r --realtime <prio>\n");
printf(" Set prio: 0 to diable, 99 for maximum (default = %d)\n", rt_prio);
printf(" -E --pre-emphasis\n");
printf(" Enable pre-emphasis, if you directly connect to the oscillator of the\n");
printf(" transmitter. (No pre-emphasis done by the transmitter.)\n");
printf(" -e --de-emphasis\n");
printf(" Enable de-emphasis, if you directly connect to the discriminator of\n");
printf(" the receiver. (No de-emphasis done by the receiver.)\n");
printf(" -W --write-wave <file>\n");
printf(" Write received audio to given wav audio file.\n");
printf(" -R --read-wave <file>\n");
@ -84,12 +92,14 @@ static struct option long_options_common[] = {
{"send-patterns", 0, 0, 'p'},
{"loopback", 1, 0, 'L'},
{"realtime", 1, 0, 'r'},
{"pre-emphasis", 0, 0, 'E'},
{"de-emphasis", 0, 0, 'e'},
{"write-wave", 1, 0, 'W'},
{"read-wave", 1, 0, 'R'},
{0, 0, 0, 0}
};
const char *optstring_common = "hD:k:d:s:c:l:mp:L:r:W:R:";
const char *optstring_common = "hD:k:d:s:c:l:mp:L:r:EeW:R:";
struct option *long_options;
char *optstring;
@ -159,6 +169,14 @@ void opt_switch_common(int c, char *arg0, int *skip_args)
rt_prio = atoi(optarg);
*skip_args += 2;
break;
case 'E':
do_pre_emphasis = 1;
*skip_args += 1;
break;
case 'e':
do_de_emphasis = 1;
*skip_args += 1;
break;
case 'W':
write_wave = strdup(optarg);
*skip_args += 2;

@ -32,13 +32,15 @@ sender_t *sender_head = NULL;
static sender_t **sender_tailp = &sender_head;
/* Init transceiver instance and link to list of transceivers. */
int sender_create(sender_t *sender, const char *sounddev, int samplerate, const char *write_wave, const char *read_wave, int kanal, int loopback, double loss_volume, int use_pilot_signal)
int sender_create(sender_t *sender, const char *sounddev, int samplerate, int pre_emphasis, int de_emphasis, const char *write_wave, const char *read_wave, int kanal, int loopback, double loss_volume, int use_pilot_signal)
{
int rc = 0;
PDEBUG(DSENDER, DEBUG_DEBUG, "Creating 'Sender' instance\n");
sender->samplerate = samplerate;
sender->pre_emphasis = pre_emphasis;
sender->de_emphasis = de_emphasis;
sender->kanal = kanal;
sender->loopback = loopback;
sender->loss_volume = loss_volume;
@ -80,6 +82,10 @@ int sender_create(sender_t *sender, const char *sounddev, int samplerate, const
}
}
rc = init_emphasis(&sender->estate, samplerate);
if (rc < 0)
goto error;
*sender_tailp = sender;
sender_tailp = &sender->next;
@ -151,6 +157,8 @@ void process_sender(sender_t *sender, int latspl)
jitter_load(&sender->audio, samples, count);
else
sender_send(sender, samples, count);
if (sender->pre_emphasis)
pre_emphasis(&sender->estate, samples, count);
switch (sender->use_pilot_signal) {
case 2:
/* tone if pilot signal is on */
@ -201,6 +209,8 @@ void process_sender(sender_t *sender, int latspl)
return;
}
if (count) {
if (sender->de_emphasis)
de_emphasis(&sender->estate, samples, count);
if (sender->wave_play.fp)
wave_read(&sender->wave_play, samples, count);
if (sender->loopback != 1) {

@ -3,6 +3,7 @@
#include "samplerate.h"
#include "jitter.h"
#include "loss.h"
#include "emphasis.h"
/* common structure of each transmitter */
typedef struct sender {
@ -18,6 +19,9 @@ typedef struct sender {
void *sound;
int samplerate;
samplerate_t srstate; /* sample rate conversion state */
int pre_emphasis; /* use pre_emhasis, done by sender */
int de_emphasis; /* use de_emhasis, done by sender */
emphasis_t estate; /* pre and de emphasis */
/* loopback test */
int loopback; /* 0 = off, 1 = internal, 2 = external */
@ -47,7 +51,7 @@ typedef struct sender {
/* list of all senders */
extern sender_t *sender_head;
int sender_create(sender_t *sender, const char *sounddev, int samplerate, const char *write_wave, const char *read_wave, int kanal, int loopback, double loss_volume, int use_pilot_signal);
int sender_create(sender_t *sender, const char *sounddev, int samplerate, int pre_emphasis, int de_emphasis, const char *write_wave, const char *read_wave, int kanal, int loopback, double loss_volume, int use_pilot_signal);
void sender_destroy(sender_t *sender);
void sender_send(sender_t *sender, int16_t *samples, int count);
void sender_receive(sender_t *sender, int16_t *samples, int count);

@ -33,7 +33,7 @@
#define PI M_PI
/* signalling */
#define TX_AUDIO_0dBm0 16384 /* works quite well */
#define TX_AUDIO_0dBm0 32767 /* works quite well */
#define TX_PEAK_FSK 16384.0 /* peak amplitude of signalling FSK */
#define TX_PEAK_SUPER 1638.0 /* peak amplitude of supervisory signal */
#define BIT_RATE 1200 /* baud rate */
@ -467,8 +467,7 @@ void sender_receive(sender_t *sender, int16_t *samples, int length)
spl = nmt->sender.rxbuf;
pos = nmt->sender.rxbuf_pos;
for (i = 0; i < count; i++) {
#warning hacking: remove after preemphasis implementation
spl[pos++] = down[i] / 2;
spl[pos++] = down[i];
if (pos == 160) {
call_tx_audio(nmt->sender.callref, spl, 160);
pos = 0;

@ -239,7 +239,7 @@ int main(int argc, char *argv[])
}
/* create transceiver instance */
rc = nmt_create(sounddev, samplerate, write_wave, read_wave, kanal, (loopback) ? CHAN_TYPE_TEST : chan_type, ms_power, nmt_digits2value(traffic_area, 2), area_no, compander, supervisory, loopback);
rc = nmt_create(sounddev, samplerate, do_pre_emphasis, do_de_emphasis, write_wave, read_wave, kanal, (loopback) ? CHAN_TYPE_TEST : chan_type, ms_power, nmt_digits2value(traffic_area, 2), area_no, compander, supervisory, loopback);
if (rc < 0) {
fprintf(stderr, "Failed to create transceiver instance. Quitting!\n");
goto fail;

@ -283,7 +283,7 @@ static void nmt_timeout(struct timer *timer);
static void nmt_go_idle(nmt_t *nmt);
/* Create transceiver instance and link to a list. */
int nmt_create(const char *sounddev, int samplerate, const char *write_wave, const char *read_wave, int channel, enum nmt_chan_type chan_type, uint8_t ms_power, uint8_t traffic_area, uint8_t area_no, int compander, int supervisory, int loopback)
int nmt_create(const char *sounddev, int samplerate, int pre_emphasis, int de_emphasis, const char *write_wave, const char *read_wave, int channel, enum nmt_chan_type chan_type, uint8_t ms_power, uint8_t traffic_area, uint8_t area_no, int compander, int supervisory, int loopback)
{
nmt_t *nmt;
int rc;
@ -324,7 +324,7 @@ int nmt_create(const char *sounddev, int samplerate, const char *write_wave, con
PDEBUG(DNMT, DEBUG_DEBUG, "Creating 'NMT' instance for channel = %d (sample rate %d).\n", channel, samplerate);
/* init general part of transceiver */
rc = sender_create(&nmt->sender, sounddev, samplerate, write_wave, read_wave, channel, loopback, 0, -1);
rc = sender_create(&nmt->sender, sounddev, samplerate, pre_emphasis, de_emphasis, write_wave, read_wave, channel, loopback, 0, -1);
if (rc < 0) {
PDEBUG(DNMT, DEBUG_ERROR, "Failed to init transceiver process!\n");
goto error;

@ -134,7 +134,7 @@ const char *chan_type_long_name(enum nmt_chan_type chan_type);
double nmt_channel2freq(int channel, int uplink);
void nmt_country_list(void);
uint8_t nmt_country_by_short_name(const char *short_name);
int nmt_create(const char *sounddev, int samplerate, const char *write_wave, const char *read_wave, int channel, enum nmt_chan_type chan_type, uint8_t ms_power, uint8_t traffic_area, uint8_t area_no, int compander, int supervisory, int loopback);
int nmt_create(const char *sounddev, int samplerate, int pre_emphasis, int de_emphasis, const char *write_wave, const char *read_wave, int channel, enum nmt_chan_type chan_type, uint8_t ms_power, uint8_t traffic_area, uint8_t area_no, int compander, int supervisory, int loopback);
void nmt_destroy(sender_t *sender);
void nmt_receive_frame(nmt_t *nmt, const char *bits, double quality, double level, double frames_elapsed);
const char *nmt_get_frame(nmt_t *nmt);

@ -1,7 +1,8 @@
AM_CPPFLAGS = -Wall -g $(all_includes)
noinst_PROGRAMS = \
test_compander
test_compander \
test_emphasis
test_compander_SOURCES = test_compander.c
@ -10,3 +11,10 @@ test_compander_LDADD = \
$(top_builddir)/src/common/libcommon.a \
-lm
test_emphasis_SOURCES = test_emphasis.c
test_emphasis_LDADD = \
$(COMMON_LA) \
$(top_builddir)/src/common/libcommon.a \
-lm

@ -0,0 +1,75 @@
#include <stdio.h>
#include <stdint.h>
#include <math.h>
#include <string.h>
#include "../common/emphasis.h"
#define level2db(level) (20 * log10(level))
#define db2level(db) pow(10, (double)db / 20.0)
#define SAMPLERATE 48000
#define DEVIATION 8000.0
static void check_level(int16_t *samples, const char *desc)
{
int i;
int last = 0, envelope = 0;
int up = 0;
int freq;
for (i = 0; i < SAMPLERATE; i++) {
if (last < samples[i]) {
up = 1;
} else if (last > samples[i]) {
if (up) {
envelope = last;
}
up = 0;
}
if ((i % (SAMPLERATE/40)) == 0) {
freq = 500 + 500 * (i / (SAMPLERATE / 8));
printf("%s: f = %d envelop = %.4f\n", desc, freq, level2db((double)envelope / DEVIATION));
}
last = samples[i];
}
}
static void gen_samples(int16_t *samples)
{
int i;
double value;
int freq;
for (i = 0; i < SAMPLERATE; i++) {
freq = 500 + 500 * (i / (SAMPLERATE / 8));
value = sin(2.0 * M_PI * (double)freq / (double)SAMPLERATE * (double)i);
samples[i] = value * DEVIATION;
}
}
int main(void)
{
emphasis_t estate;
int16_t samples[SAMPLERATE];
/* generate sweep 0..4khz */
gen_samples(samples);
init_emphasis(&estate, SAMPLERATE);
// check_level(samples, "unchanged");
pre_emphasis(&estate, samples, SAMPLERATE);
check_level(samples, "pre-emphasis");
/* generate sweep 0..4khz */
gen_samples(samples);
de_emphasis(&estate, samples, SAMPLERATE);
check_level(samples, "de-emphasis");
return 0;
}
Loading…
Cancel
Save