New common FSK implementation, replaces all individual implementations

This commit is contained in:
Andreas Eversberg
2017-08-05 10:41:23 +02:00
parent ffd3b848e1
commit 534411d660
21 changed files with 785 additions and 1117 deletions

View File

@@ -286,15 +286,11 @@ static void dms_encode_dt(nmt_t *nmt, uint8_t d, uint8_t s, uint8_t n, uint8_t *
printf("\n");
#endif
/* render wave form */
test_dms_frame(frame, 127); // used by test program
dms->frame_length = ffsk_render_frame(&nmt->ffsk, frame, 127, dms->frame_spl);
dms->frame_valid = 1;
dms->frame_pos = 0;
if (dms->frame_length > dms->frame_size) {
PDEBUG(DDMS, DEBUG_ERROR, "Frame exceeds buffer, please fix!\n");
abort();
}
/* store frame */
memcpy(dms->tx_frame, frame, 127);
dms->tx_frame_length = 127;
dms->tx_frame_pos = 0;
dms->tx_frame_valid = 1;
}
/* encode RR frame and schedule for next transmission */
@@ -334,29 +330,27 @@ static void dms_encode_rr(nmt_t *nmt, uint8_t d, uint8_t s, uint8_t n)
printf("\n");
#endif
/* render wave form */
test_dms_frame(frame, 77); // used by test program
dms->frame_length = ffsk_render_frame(&nmt->ffsk, frame, 77, dms->frame_spl);
dms->frame_valid = 1;
dms->frame_pos = 0;
if (dms->frame_length > dms->frame_size) {
PDEBUG(DDMS, DEBUG_ERROR, "Frame exceeds buffer, please fix!\n");
abort();
}
/* store frame */
memcpy(dms->tx_frame, frame, 77);
dms->tx_frame_length = 77;
dms->tx_frame_pos = 0;
dms->tx_frame_valid = 1;
}
/* check if we have to transmit a frame and render it
* also do nothing until a currently transmitted frame is completely
* transmitted.
*
* this function is public, so it can be used by test routine.
*/
static void trigger_frame_transmission(nmt_t *nmt)
void trigger_frame_transmission(nmt_t *nmt)
{
dms_t *dms = &nmt->dms;
struct dms_frame *dms_frame;
int i;
/* ongoing transmission, so we wait */
if (dms->frame_valid)
if (dms->tx_frame_valid)
return;
/* check for RR first, because high priority */
@@ -416,41 +410,21 @@ static void trigger_frame_transmission(nmt_t *nmt)
}
/* send data using FSK */
int fsk_dms_frame(nmt_t *nmt, sample_t *samples, int length)
int dms_send_bit(nmt_t *nmt)
{
dms_t *dms = &nmt->dms;
sample_t *spl;
int i;
int count, max;
next_frame:
/* check if no frame is currently transmitted */
if (dms->frame_length == 0) {
dms->frame_valid = 0;
if (!dms->tx_frame_valid)
return -1;
if (!dms->tx_frame_length || dms->tx_frame_pos == dms->tx_frame_length) {
dms->tx_frame_valid = 0;
trigger_frame_transmission(nmt);
if (!dms->frame_valid)
return length;
}
/* send audio from frame */
max = dms->frame_length;
count = max - dms->frame_pos;
//printf("length = %d count=%d\n", length, count);
if (count > length)
count = length;
spl = dms->frame_spl + dms->frame_pos;
for (i = 0; i < count; i++) {
*samples++ = *spl++;
}
dms->frame_pos += count;
/* check for end of frame and stop */
if (dms->frame_pos == max) {
dms->frame_length = 0;
/* we need more ? */
if (length)
goto next_frame;
if (!dms->tx_frame_valid)
return -1;
}
return length;
return dms->tx_frame[dms->tx_frame_pos++];
}
/*
@@ -869,7 +843,7 @@ void dms_reset(nmt_t *nmt)
dms->rx_in_sync = 0;
memset(&dms->state, 0, sizeof(dms->state));
dms->frame_valid = 0;
dms->tx_frame_valid = 0;
while (dms->state.frame_list)
dms_frame_delete(nmt, dms->state.frame_list);

View File

@@ -24,11 +24,10 @@ struct dms_state {
typedef struct dms {
/* DMS transmission */
int frame_valid; /* set, if there is a valid frame in sample buffer */
sample_t *frame_spl; /* 127 * fsk_bit_length */
int frame_size; /* total size of buffer */
int frame_pos; /* current sample position in frame_spl */
int frame_length; /* number of samples currently in frame_spl */
int tx_frame_valid; /* do we have or had a valid frame? */
char tx_frame[127]; /* carries bits of one frame to transmit */
int tx_frame_length;
int tx_frame_pos;
uint16_t rx_sync; /* shift register to detect sync */
double rx_sync_level[256]; /* level infos */
double rx_sync_quality[256]; /* quality infos */
@@ -52,7 +51,7 @@ typedef struct dms {
int dms_init_sender(nmt_t *nmt);
void dms_cleanup_sender(nmt_t *nmt);
int fsk_dms_frame(nmt_t *nmt, sample_t *samples, int length);
int dms_send_bit(nmt_t *nmt);
void fsk_receive_bit_dms(nmt_t *nmt, int bit, double quality, double level);
void dms_reset(nmt_t *nmt);
@@ -60,5 +59,5 @@ void dms_send(nmt_t *nmt, const uint8_t *data, int length, int eight_bits);
void dms_all_sent(nmt_t *nmt);
void dms_receive(nmt_t *nmt, const uint8_t *data, int length, int eight_bits);
void test_dms_frame(const char *frame, int length);
void trigger_frame_transmission(nmt_t *nmt);

View File

@@ -59,7 +59,10 @@
#define COMPANDOR_0DB 1.0 /* A level of 0dBm (1.0) shall be unaccected */
#define TX_PEAK_FSK (4200.0 / 1800.0 * 1000.0 / DBM0_DEVIATION)
#define TX_PEAK_SUPER (300.0 / 4015.0 * 1000.0 / DBM0_DEVIATION)
#define BIT_RATE 1200
#define BIT_RATE 1200.0
#define BIT_ADJUST 0.1 /* how much do we adjust bit clock on frequency change */
#define F0 1800.0
#define F1 1200.0
#define MAX_DISPLAY 1.4 /* something above dBm0 */
#define DIALTONE_HZ 425.0 /* dial tone frequency */
#define TX_PEAK_DIALTONE 0.5 /* dial tone peak FIXME */
@@ -81,7 +84,7 @@ static double super_freq[5] = {
static sample_t dsp_sine_super[65536];
static sample_t dsp_sine_dialtone[65536];
/* global init for FFSK */
/* global init for dsp */
void dsp_init(void)
{
int i;
@@ -95,17 +98,15 @@ void dsp_init(void)
/* dialtone sine */
dsp_sine_dialtone[i] = s * TX_PEAK_DIALTONE;
}
ffsk_global_init(TX_PEAK_FSK);
}
static int fsk_send_bit(void *inst);
static void fsk_receive_bit(void *inst, int bit, double quality, double level);
/* Init FSK of transceiver */
int dsp_init_sender(nmt_t *nmt, double deviation_factor)
{
sample_t *spl;
double samples_per_bit;
int i;
/* attack (3ms) and recovery time (13.5ms) according to NMT specs */
@@ -119,32 +120,12 @@ int dsp_init_sender(nmt_t *nmt, double deviation_factor)
PDEBUG(DDSP, DEBUG_DEBUG, "Using FSK level of %.3f (%.3f KHz deviation @ 1500 Hz)\n", TX_PEAK_FSK * deviation_factor, 3.5 * deviation_factor);
PDEBUG(DDSP, DEBUG_DEBUG, "Using Supervisory level of %.3f (%.3f KHz deviation @ 4015 Hz)\n", TX_PEAK_SUPER * deviation_factor, 0.3 * deviation_factor);
/* init ffsk */
if (ffsk_init(&nmt->ffsk, nmt, fsk_receive_bit, nmt->sender.kanal, nmt->sender.samplerate) < 0) {
PDEBUG_CHAN(DDSP, DEBUG_DEBUG, "FFSK init failed!\n");
/* init fsk */
if (fsk_init(&nmt->fsk, nmt, fsk_send_bit, fsk_receive_bit, nmt->sender.samplerate, BIT_RATE, F0, F1, TX_PEAK_FSK, 1, BIT_ADJUST) < 0) {
PDEBUG_CHAN(DDSP, DEBUG_DEBUG, "FSK init failed!\n");
return -EINVAL;
}
/* allocate transmit buffer for a complete frame, add 10 to be safe */
samples_per_bit = (double)nmt->sender.samplerate / (double)BIT_RATE;
nmt->frame_size = 166.0 * samples_per_bit + 10;
spl = calloc(nmt->frame_size, sizeof(*spl));
if (!spl) {
PDEBUG(DDSP, DEBUG_ERROR, "No memory!\n");
return -ENOMEM;
}
nmt->frame_spl = spl;
/* allocate DMS transmit buffer for a complete frame, add 10 to be safe */
nmt->dms.frame_size = 127.0 * samples_per_bit + 10;
spl = calloc(nmt->dms.frame_size, sizeof(*spl));
if (!spl) {
PDEBUG(DDSP, DEBUG_ERROR, "No memory!\n");
return -ENOMEM;
}
nmt->dms.frame_spl = spl;
/* allocate ring buffer for supervisory signal detection */
nmt->super_samples = (int)((double)nmt->sender.samplerate * SUPER_DURATION + 0.5);
spl = calloc(1, nmt->super_samples * sizeof(*spl));
@@ -179,16 +160,8 @@ void dsp_cleanup_sender(nmt_t *nmt)
{
PDEBUG_CHAN(DDSP, DEBUG_DEBUG, "Cleanup DSP for Transceiver.\n");
ffsk_cleanup(&nmt->ffsk);
fsk_cleanup(&nmt->fsk);
if (nmt->frame_spl) {
free(nmt->frame_spl);
nmt->frame_spl = NULL;
}
if (nmt->dms.frame_spl) {
free(nmt->dms.frame_spl);
nmt->dms.frame_spl = NULL;
}
if (nmt->super_filter_spl) {
free(nmt->super_filter_spl);
nmt->super_filter_spl = NULL;
@@ -344,7 +317,8 @@ void sender_receive(sender_t *sender, sample_t *samples, int length)
}
nmt->super_filter_pos = pos;
ffsk_receive(&nmt->ffsk, samples, length);
/* fsk signal */
fsk_receive(&nmt->fsk, samples, length);
/* muting audio while receiving frame */
for (i = 0; i < length; i++) {
@@ -377,50 +351,31 @@ void sender_receive(sender_t *sender, sample_t *samples, int length)
nmt->sender.rxbuf_pos = 0;
}
static int fsk_frame(nmt_t *nmt, sample_t *samples, int length)
static int fsk_send_bit(void *inst)
{
nmt_t *nmt = (nmt_t *)inst;
const char *frame;
sample_t *spl;
int i;
int count, max;
next_frame:
if (!nmt->frame_length) {
/* request frame */
frame = nmt_get_frame(nmt);
if (!frame) {
PDEBUG_CHAN(DDSP, DEBUG_DEBUG, "Stop sending frames.\n");
return length;
}
/* render frame */
nmt->frame_length = ffsk_render_frame(&nmt->ffsk, frame, 166, nmt->frame_spl);
nmt->frame_pos = 0;
if (nmt->frame_length > nmt->frame_size) {
PDEBUG_CHAN(DDSP, DEBUG_ERROR, "Frame exceeds buffer, please fix!\n");
abort();
/* send frame bit (prio) */
if (nmt->dsp_mode == DSP_MODE_FRAME) {
if (!nmt->tx_frame_length || nmt->tx_frame_pos == nmt->tx_frame_length) {
/* request frame */
frame = nmt_get_frame(nmt);
if (!frame) {
nmt->tx_frame_length = 0;
PDEBUG_CHAN(DDSP, DEBUG_DEBUG, "Stop sending frames.\n");
return -1;
}
memcpy(nmt->tx_frame, frame, 166);
nmt->tx_frame_length = 166;
nmt->tx_frame_pos = 0;
}
return nmt->tx_frame[nmt->tx_frame_pos++];
}
/* send audio from frame */
max = nmt->frame_length;
count = max - nmt->frame_pos;
if (count > length)
count = length;
spl = nmt->frame_spl + nmt->frame_pos;
for (i = 0; i < count; i++) {
*samples++ = *spl++;
}
length -= count;
nmt->frame_pos += count;
/* check for end of telegramm */
if (nmt->frame_pos == max) {
nmt->frame_length = 0;
/* we need more ? */
if (length)
goto next_frame;
}
return length;
/* send dms bit */
return dms_send_bit(nmt);
}
/* Generate audio stream with supervisory signal. Keep phase for next call of function. */
@@ -465,7 +420,7 @@ static void dial_tone(nmt_t *nmt, sample_t *samples, int length)
void sender_send(sender_t *sender, sample_t *samples, int length)
{
nmt_t *nmt = (nmt_t *) sender;
int len;
int count;
again:
switch (nmt->dsp_mode) {
@@ -473,8 +428,8 @@ again:
case DSP_MODE_DTMF:
jitter_load(&nmt->sender.dejitter, samples, length);
/* send after dejitter, so audio is flushed */
if (nmt->dms.frame_valid) {
fsk_dms_frame(nmt, samples, length);
if (nmt->dms.tx_frame_valid) {
fsk_send(&nmt->fsk, samples, length, 0);
break;
}
if (nmt->supervisory)
@@ -489,15 +444,14 @@ again:
case DSP_MODE_FRAME:
/* Encode frame into audio stream. If frames have
* stopped, process again for rest of stream. */
len = fsk_frame(nmt, samples, length);
count = fsk_send(&nmt->fsk, samples, length, 0);
/* special case: add supervisory signal to frame at loop test */
if (nmt->sender.loopback && nmt->supervisory)
super_encode(nmt, samples, length);
if (len) {
samples += length - len;
length = len;
super_encode(nmt, samples, count);
samples += count;
length -= count;
if (length)
goto again;
}
break;
}
}
@@ -525,9 +479,11 @@ const char *nmt_dsp_mode_name(enum dsp_mode mode)
void nmt_set_dsp_mode(nmt_t *nmt, enum dsp_mode mode)
{
/* reset telegramm */
if (mode == DSP_MODE_FRAME && nmt->dsp_mode != mode)
nmt->frame_length = 0;
/* reset frame */
if (mode == DSP_MODE_FRAME && nmt->dsp_mode != mode) {
fsk_tx_reset(&nmt->fsk);
nmt->tx_frame_length = 0;
}
PDEBUG_CHAN(DDSP, DEBUG_DEBUG, "DSP mode %s -> %s\n", nmt_dsp_mode_name(nmt->dsp_mode), nmt_dsp_mode_name(mode));
nmt->dsp_mode = mode;

View File

@@ -427,6 +427,3 @@ fail:
return 0;
}
// dummy, will be replaced by DMS test program
void test_dms_frame(const char __attribute__((unused)) *frame, int __attribute__((unused)) length) {}

View File

@@ -1532,7 +1532,7 @@ void nmt_receive_frame(nmt_t *nmt, const char *bits, double quality, double leve
frame_t frame;
int rc;
PDEBUG_CHAN(DDSP, DEBUG_INFO, "RX Level: %.0f%% Quality=%.0f\n", level * 100.0, quality * 100.0);
PDEBUG_CHAN(DDSP, DEBUG_INFO, "RX Level: %.0f%% Quality=%.0f%%\n", level * 100.0, quality * 100.0);
rc = decode_frame(nmt->sysinfo.system, &frame, bits, (nmt->sender.loopback) ? MTX_TO_XX : XX_TO_MTX, (nmt->state == STATE_MT_PAGING));
if (rc < 0) {

View File

@@ -2,7 +2,8 @@
#include "../common/compandor.h"
#include "../common/dtmf.h"
#include "../common/call.h"
#include "../common/ffsk.h"
#include "../common/fsk.h"
#include "../common/goertzel.h"
#include "dms.h"
#include "sms.h"
@@ -96,7 +97,7 @@ typedef struct nmt {
/* dsp states */
enum dsp_mode dsp_mode; /* current mode: audio, durable tone 0 or 1, paging */
ffsk_t ffsk; /* ffsk processing */
fsk_t fsk; /* fsk processing */
int super_samples; /* number of samples in buffer for supervisory detection */
goertzel_t super_goertzel[5]; /* filter for supervisory decoding */
sample_t *super_filter_spl; /* array with sample buffer for supervisory detection */
@@ -112,15 +113,14 @@ typedef struct nmt {
int rx_count; /* next bit to receive */
double rx_level[256]; /* level infos */
double rx_quality[256]; /* quality infos */
sample_t *frame_spl; /* samples to store a complete rendered frame */
int frame_size; /* total size of sample buffer */
int frame_length; /* current length of data in sample buffer */
int frame_pos; /* current sample position in frame_spl */
uint64_t rx_bits_count; /* sample counter */
uint64_t rx_bits_count_current; /* sample counter of current frame */
uint64_t rx_bits_count_last; /* sample counter of last frame */
int super_detected; /* current detection state flag */
int super_detect_count; /* current number of consecutive detections/losses */
char tx_frame[166]; /* carries bits of one frame to transmit */
int tx_frame_length;
int tx_frame_pos;
/* DMS/SMS states */
dms_t dms; /* DMS states */