Move samples of int16_t format to sample_t, that is of type double

This prepares the correction of all levels
This commit is contained in:
Andreas Eversberg
2017-01-27 16:57:34 +01:00
parent 538a959128
commit 7ea3bc188d
74 changed files with 471 additions and 447 deletions

View File

@@ -21,6 +21,7 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "../common/sample.h"
#include "../common/debug.h"
#include "../common/timer.h"
#include "nmt.h"
@@ -414,10 +415,10 @@ static void trigger_frame_transmission(nmt_t *nmt)
}
/* send data using FSK */
int fsk_dms_frame(nmt_t *nmt, int16_t *samples, int length)
int fsk_dms_frame(nmt_t *nmt, sample_t *samples, int length)
{
dms_t *dms = &nmt->dms;
int16_t *spl;
sample_t *spl;
int i;
int count, max;

View File

@@ -25,7 +25,7 @@ struct dms_state {
typedef struct dms {
/* DMS transmission */
int frame_valid; /* set, if there is a valid frame in sample buffer */
int16_t *frame_spl; /* 127 * fsk_bit_length */
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 */
@@ -52,7 +52,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, int16_t *samples, int length);
int fsk_dms_frame(nmt_t *nmt, sample_t *samples, int length);
void fsk_receive_bit_dms(nmt_t *nmt, int bit, double quality, double level);
void dms_reset(nmt_t *nmt);

View File

@@ -25,9 +25,9 @@
#include <string.h>
#include <errno.h>
#include <math.h>
#include "../common/sample.h"
#include "../common/debug.h"
#include "../common/timer.h"
#include "../common/goertzel.h"
#include "nmt.h"
#include "transaction.h"
#include "dsp.h"
@@ -50,7 +50,7 @@
#define BIT_RATE 1200 /* baud rate */
#define STEPS_PER_BIT 10 /* step every 1/12000 sec */
#define DIALTONE_HZ 425.0 /* dial tone frequency */
#define TX_PEAK_DIALTONE 16000 /* dial tone peak */
#define TX_PEAK_DIALTONE 16000.0 /* dial tone peak */
#define SUPER_DURATION 0.25 /* duration of supervisory signal measurement */
#define SUPER_DETECT_COUNT 4 /* number of measures to detect supervisory signal */
#define MUTE_DURATION 0.280 /* a tiny bit more than two frames */
@@ -71,9 +71,9 @@ static double super_freq[5] = {
};
/* table for fast sine generation */
uint16_t dsp_tone_bit[2][2][256]; /* polarity, bit, phase */
uint16_t dsp_sine_super[256];
uint16_t dsp_sine_dialtone[256];
static double dsp_tone_bit[2][2][256]; /* polarity, bit, phase */
static double dsp_sine_super[256];
static double dsp_sine_dialtone[256];
/* global init for FSK */
void dsp_init(void)
@@ -85,24 +85,23 @@ void dsp_init(void)
for (i = 0; i < 256; i++) {
s = sin((double)i / 256.0 * 2.0 * PI);
/* supervisor sine */
dsp_sine_super[i] = (int)(s * TX_PEAK_SUPER);
dsp_sine_super[i] = s * TX_PEAK_SUPER;
/* dialtone sine */
dsp_sine_dialtone[i] = (int)(s * TX_PEAK_DIALTONE);
dsp_sine_dialtone[i] = s * TX_PEAK_DIALTONE;
/* bit(1) 1 cycle */
dsp_tone_bit[0][1][i] = (int)(s * TX_PEAK_FSK);
dsp_tone_bit[1][1][i] = (int)(-s * TX_PEAK_FSK);
dsp_tone_bit[0][1][i] = s * TX_PEAK_FSK;
dsp_tone_bit[1][1][i] = -s * TX_PEAK_FSK;
/* bit(0) 1.5 cycles */
s = sin((double)i / 256.0 * 3.0 * PI);
dsp_tone_bit[0][0][i] = (int)(s * TX_PEAK_FSK);
dsp_tone_bit[1][0][i] = (int)(-s * TX_PEAK_FSK);
dsp_tone_bit[0][0][i] = s * TX_PEAK_FSK;
dsp_tone_bit[1][0][i] = -s * TX_PEAK_FSK;
}
}
/* Init FSK of transceiver */
int dsp_init_sender(nmt_t *nmt)
{
double coeff;
int16_t *spl;
sample_t *spl;
int i;
/* attack (3ms) and recovery time (13.5ms) according to NMT specs */
@@ -165,20 +164,14 @@ int dsp_init_sender(nmt_t *nmt)
nmt->super_filter_spl = spl;
/* count symbols */
for (i = 0; i < 2; i++) {
coeff = 2.0 * cos(2.0 * PI * fsk_freq[i] / (double)nmt->sender.samplerate);
nmt->fsk_coeff[i] = coeff * 32768.0;
PDEBUG(DDSP, DEBUG_DEBUG, "fsk_coeff[%d] = %d\n", i, (int)nmt->fsk_coeff[i]);
}
for (i = 0; i < 2; i++)
audio_goertzel_init(&nmt->fsk_goertzel[i], fsk_freq[i], nmt->sender.samplerate);
nmt->fsk_phaseshift256 = 256.0 / nmt->fsk_samples_per_bit;
PDEBUG(DDSP, DEBUG_DEBUG, "fsk_phaseshift = %.4f\n", nmt->fsk_phaseshift256);
/* count supervidory tones */
for (i = 0; i < 5; i++) {
coeff = 2.0 * cos(2.0 * PI * super_freq[i] / (double)nmt->sender.samplerate);
nmt->super_coeff[i] = coeff * 32768.0;
PDEBUG(DDSP, DEBUG_DEBUG, "supervisory coeff[%d] = %d\n", i, (int)nmt->super_coeff[i]);
audio_goertzel_init(&nmt->super_goertzel[i], super_freq[i], nmt->sender.samplerate);
if (i < 4) {
nmt->super_phaseshift256[i] = 256.0 / ((double)nmt->sender.samplerate / super_freq[i]);
PDEBUG(DDSP, DEBUG_DEBUG, "super_phaseshift[%d] = %.4f\n", i, nmt->super_phaseshift256[i]);
@@ -301,7 +294,7 @@ static inline void fsk_decode_step(nmt_t *nmt, int pos)
{
double level, result[2], softbit, quality;
int max;
int16_t *spl;
sample_t *spl;
int bit;
max = nmt->fsk_filter_size;
@@ -316,7 +309,7 @@ static inline void fsk_decode_step(nmt_t *nmt, int pos)
level = 0.01;
// level = 0.63662 / 2.0;
audio_goertzel(spl, max, pos, nmt->fsk_coeff, result, 2);
audio_goertzel(nmt->fsk_goertzel, spl, max, pos, result, 2);
/* calculate soft bit from both frequencies */
softbit = (result[1] / level - result[0] / level + 1.0) / 2.0;
@@ -368,14 +361,12 @@ static inline void fsk_decode_step(nmt_t *nmt, int pos)
}
/* compare supervisory signal against noise floor on 3900 Hz */
static void super_decode(nmt_t *nmt, int16_t *samples, int length)
static void super_decode(nmt_t *nmt, sample_t *samples, int length)
{
int coeff[2];
double result[2], quality;
coeff[0] = nmt->super_coeff[nmt->supervisory - 1];
coeff[1] = nmt->super_coeff[4]; /* noise floor detection */
audio_goertzel(samples, length, 0, coeff, result, 2);
audio_goertzel(&nmt->super_goertzel[nmt->supervisory - 1], samples, length, 0, &result[0], 1);
audio_goertzel(&nmt->super_goertzel[4], samples, length, 0, &result[1], 1); /* noise floor detection */
#if 0
/* normalize levels */
@@ -424,10 +415,10 @@ void super_reset(nmt_t *nmt)
}
/* Process received audio stream from radio unit. */
void sender_receive(sender_t *sender, int16_t *samples, int length)
void sender_receive(sender_t *sender, sample_t *samples, int length)
{
nmt_t *nmt = (nmt_t *) sender;
int16_t *spl;
sample_t *spl;
int max, pos;
double step, bps;
int i;
@@ -477,18 +468,17 @@ void sender_receive(sender_t *sender, int16_t *samples, int length)
if ((nmt->dsp_mode == DSP_MODE_AUDIO || nmt->dsp_mode == DSP_MODE_DTMF)
&& nmt->trans && nmt->trans->callref) {
int16_t down[length]; /* more than enough */
int count;
count = samplerate_downsample(&nmt->sender.srstate, samples, length, down);
count = samplerate_downsample(&nmt->sender.srstate, samples, length);
if (nmt->compandor)
expand_audio(&nmt->cstate, down, count);
expand_audio(&nmt->cstate, samples, count);
if (nmt->dsp_mode == DSP_MODE_DTMF)
dtmf_tone(&nmt->dtmf, down, count);
dtmf_tone(&nmt->dtmf, samples, count);
spl = nmt->sender.rxbuf;
pos = nmt->sender.rxbuf_pos;
for (i = 0; i < count; i++) {
spl[pos++] = down[i];
spl[pos++] = samples[i];
if (pos == 160) {
call_tx_audio(nmt->trans->callref, spl, 160);
pos = 0;
@@ -500,7 +490,7 @@ void sender_receive(sender_t *sender, int16_t *samples, int length)
}
/* render frame */
int fsk_render_frame(nmt_t *nmt, const char *frame, int length, int16_t *sample)
int fsk_render_frame(nmt_t *nmt, const char *frame, int length, sample_t *sample)
{
int bit, polarity;
double phaseshift, phase;
@@ -528,10 +518,10 @@ int fsk_render_frame(nmt_t *nmt, const char *frame, int length, int16_t *sample)
return count;
}
static int fsk_frame(nmt_t *nmt, int16_t *samples, int length)
static int fsk_frame(nmt_t *nmt, sample_t *samples, int length)
{
const char *frame;
int16_t *spl;
sample_t *spl;
int i;
int count, max;
@@ -575,23 +565,16 @@ next_frame:
}
/* Generate audio stream with supervisory signal. Keep phase for next call of function. */
static void super_encode(nmt_t *nmt, int16_t *samples, int length)
static void super_encode(nmt_t *nmt, sample_t *samples, int length)
{
double phaseshift, phase;
int32_t sample;
int i;
phaseshift = nmt->super_phaseshift256[nmt->supervisory - 1];
phase = nmt->super_phase256;
for (i = 0; i < length; i++) {
sample = *samples;
sample += dsp_sine_super[(uint8_t)phase];
if (sample > 32767)
sample = 32767;
else if (sample < -32767)
sample = -32767;
*samples++ = sample;
*samples++ += dsp_sine_super[(uint8_t)phase];
phase += phaseshift;
if (phase >= 256)
phase -= 256;
@@ -601,7 +584,7 @@ static void super_encode(nmt_t *nmt, int16_t *samples, int length)
}
/* Generate audio stream from dial tone. Keep phase for next call of function. */
static void dial_tone(nmt_t *nmt, int16_t *samples, int length)
static void dial_tone(nmt_t *nmt, sample_t *samples, int length)
{
double phaseshift, phase;
int i;
@@ -620,7 +603,7 @@ static void dial_tone(nmt_t *nmt, int16_t *samples, int length)
}
/* Provide stream of audio toward radio unit */
void sender_send(sender_t *sender, int16_t *samples, int length)
void sender_send(sender_t *sender, sample_t *samples, int length)
{
nmt_t *nmt = (nmt_t *) sender;
int len;

View File

@@ -2,7 +2,7 @@
void dsp_init(void);
int dsp_init_sender(nmt_t *nmt);
void dsp_cleanup_sender(nmt_t *nmt);
int fsk_render_frame(nmt_t *nmt, const char *frame, int length, int16_t *sample);
int fsk_render_frame(nmt_t *nmt, const char *frame, int length, sample_t *sample);
void nmt_set_dsp_mode(nmt_t *nmt, enum dsp_mode mode);
void super_reset(nmt_t *nmt);

View File

@@ -22,6 +22,7 @@
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include "../common/sample.h"
#include "../common/debug.h"
#include "../common/timer.h"
#include "nmt.h"

View File

@@ -26,6 +26,7 @@
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "../common/sample.h"
#include "../common/main.h"
#include "../common/debug.h"
#include "../common/timer.h"

View File

@@ -25,6 +25,7 @@
#include <string.h>
#include <errno.h>
#include <time.h>
#include "../common/sample.h"
#include "../common/debug.h"
#include "../common/timer.h"
#include "../common/cause.h"
@@ -1799,7 +1800,7 @@ void call_out_release(int callref, int __attribute__((unused)) cause)
}
/* Receive audio from call instance. */
void call_rx_audio(int callref, int16_t *samples, int count)
void call_rx_audio(int callref, sample_t *samples, int count)
{
transaction_t *trans;
nmt_t *nmt;
@@ -1812,7 +1813,7 @@ void call_rx_audio(int callref, int16_t *samples, int count)
return;
if (nmt->dsp_mode == DSP_MODE_AUDIO || nmt->dsp_mode == DSP_MODE_DTMF) {
int16_t up[(int)((double)count * nmt->sender.srstate.factor + 0.5) + 10];
sample_t up[(int)((double)count * nmt->sender.srstate.factor + 0.5) + 10];
if (nmt->compandor)
compress_audio(&nmt->cstate, samples, count);
count = samplerate_upsample(&nmt->sender.srstate, samples, count, up);

View File

@@ -1,3 +1,4 @@
#include "../common/goertzel.h"
#include "../common/sender.h"
#include "../common/compandor.h"
#include "../common/dtmf.h"
@@ -94,10 +95,10 @@ typedef struct nmt {
double fsk_samples_per_bit; /* number of samples for one bit (1200 Baud) */
double fsk_bits_per_sample; /* fraction of a bit per sample */
int super_samples; /* number of samples in buffer for supervisory detection */
int fsk_coeff[2]; /* coefficient k = 2*cos(2*PI*f/samplerate), k << 15 */
int super_coeff[5]; /* coefficient for supervisory signal */
goertzel_t fsk_goertzel[2]; /* filter for fsk decoding */
goertzel_t super_goertzel[5]; /* filter for supervisory decoding */
int fsk_polarity; /* current polarity state of bit */
int16_t *fsk_filter_spl; /* array to hold ring buffer for bit decoding */
sample_t *fsk_filter_spl; /* array to hold ring buffer for bit decoding */
int fsk_filter_size; /* size of ring buffer */
int fsk_filter_pos; /* position to write next sample */
double fsk_filter_step; /* counts bit duration, to trigger decoding every 10th bit */
@@ -110,7 +111,7 @@ typedef struct nmt {
int fsk_filter_count; /* next bit to receive */
double fsk_filter_level[256]; /* level infos */
double fsk_filter_quality[256];/* quality infos */
int16_t *super_filter_spl; /* array with sample buffer for supervisory detection */
sample_t *super_filter_spl; /* array with sample buffer for supervisory detection */
int super_filter_pos; /* current sample position in filter_spl */
double super_phaseshift256[4]; /* how much the phase of sine wave changes per sample */
double super_phase256; /* current phase */
@@ -118,7 +119,7 @@ typedef struct nmt {
double dial_phase256; /* current phase */
double fsk_phaseshift256; /* how much the phase of fsk synbol changes per sample */
double fsk_phase256; /* current phase */
int16_t *frame_spl; /* samples to store a complete rendered frame */
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 */

View File

@@ -23,6 +23,7 @@
#include <string.h>
#include <time.h>
#include <errno.h>
#include "../common/sample.h"
#include "../common/debug.h"
#include "../common/timer.h"
#include "nmt.h"

View File

@@ -21,6 +21,7 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "../common/sample.h"
#include "../common/debug.h"
#include "../common/timer.h"
#include "nmt.h"