From 7ea3bc188df54a4dbe3026bc30ed39a5cded8fdb Mon Sep 17 00:00:00 2001 From: Andreas Eversberg Date: Fri, 27 Jan 2017 16:57:34 +0100 Subject: Move samples of int16_t format to sample_t, that is of type double This prepares the correction of all levels --- src/amps/amps.c | 5 ++-- src/amps/amps.h | 17 ++++++----- src/amps/dsp.c | 80 +++++++++++++++++++++----------------------------- src/amps/frame.c | 1 + src/amps/main.c | 1 + src/amps/sysinfo.c | 1 + src/amps/transaction.c | 1 + 7 files changed, 50 insertions(+), 56 deletions(-) (limited to 'src/amps') diff --git a/src/amps/amps.c b/src/amps/amps.c index 1923a30..d600b4b 100644 --- a/src/amps/amps.c +++ b/src/amps/amps.c @@ -42,6 +42,7 @@ #include #include #include +#include "../common/sample.h" #include "../common/debug.h" #include "../common/timer.h" #include "../common/call.h" @@ -902,7 +903,7 @@ void call_out_release(int callref, int 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) { sender_t *sender; amps_t *amps; @@ -916,7 +917,7 @@ void call_rx_audio(int callref, int16_t *samples, int count) return; if (amps->dsp_mode == DSP_MODE_AUDIO_RX_AUDIO_TX) { - int16_t up[(int)((double)count * amps->sender.srstate.factor + 0.5) + 10]; + sample_t up[(int)((double)count * amps->sender.srstate.factor + 0.5) + 10]; compress_audio(&s->cstate, samples, count); count = samplerate_upsample(&s->sender.srstate, samples, count, up); jitter_save(&s->sender.dejitter, up, count); diff --git a/src/amps/amps.h b/src/amps/amps.h index 4ff910b..4e2aa81 100644 --- a/src/amps/amps.h +++ b/src/amps/amps.h @@ -1,3 +1,4 @@ +#include "../common/goertzel.h" #include "../common/sender.h" #include "../common/compandor.h" #include "sysinfo.h" @@ -54,15 +55,15 @@ typedef struct amps { /* dsp states */ enum dsp_mode dsp_mode; /* current mode: audio, durable tone 0 or 1, paging */ int flip_polarity; /* 1 = flip */ - int16_t fsk_deviation; /* deviation of FSK signal on sound card */ - int16_t fsk_ramp_up[256]; /* samples of upward ramp shape */ - int16_t fsk_ramp_down[256]; /* samples of downward ramp shape */ + double fsk_deviation; /* deviation of FSK signal on sound card */ + sample_t fsk_ramp_up[256]; /* samples of upward ramp shape */ + sample_t fsk_ramp_down[256]; /* samples of downward ramp shape */ double fsk_bitduration; /* duration of one bit in samples */ double fsk_bitstep; /* fraction of one bit each sample */ /* tx bits generation */ char fsk_tx_frame[FSK_MAX_BITS + 1]; /* +1 because 0-termination */ int fsk_tx_frame_pos; /* current position sending bits */ - int16_t *fsk_tx_buffer; /* tx buffer for one data block */ + sample_t *fsk_tx_buffer; /* tx buffer for one data block */ int fsk_tx_buffer_size; /* size of tx buffer (in samples) */ int fsk_tx_buffer_length; /* usage of buffer (in samples) */ int fsk_tx_buffer_pos; /* current position sending buffer */ @@ -73,7 +74,7 @@ typedef struct amps { double highpass_x_last; /* last input value */ double highpass_y_last; /* last output value */ /* rx detection of bits and sync */ - int16_t fsk_rx_last_sample; /* last sample (for level change detection) */ + sample_t fsk_rx_last_sample; /* last sample (for level change detection) */ double fsk_rx_elapsed; /* bit duration since last level change */ enum fsk_rx_sync fsk_rx_sync; /* sync state */ uint16_t fsk_rx_sync_register; /* shift register to detect sync word */ @@ -88,7 +89,7 @@ typedef struct amps { /* the ex buffer holds the duration of one bit, and wrapps every * bit. */ double fsk_rx_bitcount; /* counts the bit. if it reaches or exceeds 1, the bit is complete and the next bit starts */ - int16_t *fsk_rx_window; /* rx buffer for one bit */ + sample_t *fsk_rx_window; /* rx buffer for one bit */ int fsk_rx_window_length; /* length of rx buffer */ int fsk_rx_window_half; /* half of length of rx buffer */ int fsk_rx_window_begin; /* where to begin detecting level */ @@ -131,8 +132,8 @@ typedef struct amps { /* SAT tone */ int sat; /* use SAT tone 0..2 */ int sat_samples; /* number of samples in buffer for supervisory detection */ - int sat_coeff[5]; /* coefficient for SAT signal decoding */ - int16_t *sat_filter_spl; /* array with sample buffer for supervisory detection */ + goertzel_t sat_goertzel[5]; /* filter for SAT signal decoding */ + sample_t *sat_filter_spl; /* array with sample buffer for supervisory detection */ int sat_filter_pos; /* current sample position in filter_spl */ double sat_phaseshift256[3]; /* how much the phase of sine wave changes per sample */ double sat_phase256; /* current phase */ diff --git a/src/amps/dsp.c b/src/amps/dsp.c index 1a1e3d1..9c6dc2e 100644 --- a/src/amps/dsp.c +++ b/src/amps/dsp.c @@ -81,10 +81,10 @@ #include #include #include +#include "../common/sample.h" #include "../common/debug.h" #include "../common/timer.h" #include "../common/call.h" -#include "../common/goertzel.h" #include "amps.h" #include "frame.h" #include "dsp.h" @@ -180,8 +180,7 @@ static void sat_reset(amps_t *amps, const char *reason); /* Init FSK of transceiver */ int dsp_init_sender(amps_t *amps, int high_pass, int tolerant) { - double coeff; - int16_t *spl; + sample_t *spl; int i; int rc; double RC, dt; @@ -206,12 +205,13 @@ int dsp_init_sender(amps_t *amps, int high_pass, int tolerant) PDEBUG(DDSP, DEBUG_DEBUG, "Use %.4f samples for full bit duration @ %d.\n", amps->fsk_bitduration, amps->sender.samplerate); amps->fsk_tx_buffer_size = amps->fsk_bitduration + 10; /* 10 extra to avoid overflow due to rounding */ - amps->fsk_tx_buffer = calloc(sizeof(int16_t), amps->fsk_tx_buffer_size); - if (!amps->fsk_tx_buffer) { + spl = calloc(sizeof(*spl), amps->fsk_tx_buffer_size); + if (!spl) { PDEBUG(DDSP, DEBUG_DEBUG, "No memory!\n"); rc = -ENOMEM; goto error; } + amps->fsk_tx_buffer = spl; amps->fsk_rx_window_length = ceil(amps->fsk_bitduration); /* buffer holds one bit (rounded up) */ half = amps->fsk_rx_window_length >> 1; @@ -221,12 +221,13 @@ int dsp_init_sender(amps_t *amps, int high_pass, int tolerant) PDEBUG(DDSP, DEBUG_DEBUG, "Bit window length: %d\n", amps->fsk_rx_window_length); PDEBUG(DDSP, DEBUG_DEBUG, " -> Samples in window to analyse level left of edge: %d..%d\n", amps->fsk_rx_window_begin, amps->fsk_rx_window_half - 1); PDEBUG(DDSP, DEBUG_DEBUG, " -> Samples in window to analyse level right of edge: %d..%d\n", amps->fsk_rx_window_half, amps->fsk_rx_window_end - 1); - amps->fsk_rx_window = calloc(sizeof(int16_t), amps->fsk_rx_window_length); - if (!amps->fsk_rx_window) { + spl = calloc(sizeof(*amps->fsk_rx_window), amps->fsk_rx_window_length); + if (!spl) { PDEBUG(DDSP, DEBUG_DEBUG, "No memory!\n"); rc = -ENOMEM; goto error; } + amps->fsk_rx_window = spl; /* create devation and ramp */ amps->fsk_deviation = FSK_DEVIATION; /* be sure not to overflow 32767 */ @@ -234,7 +235,7 @@ int dsp_init_sender(amps_t *amps, int high_pass, int tolerant) /* allocate ring buffer for SAT signal detection */ amps->sat_samples = (int)((double)amps->sender.samplerate * SAT_DURATION + 0.5); - spl = calloc(1, amps->sat_samples * sizeof(*spl)); + spl = calloc(sizeof(*spl), amps->sat_samples); if (!spl) { PDEBUG(DDSP, DEBUG_ERROR, "No memory!\n"); return -ENOMEM; @@ -243,10 +244,7 @@ int dsp_init_sender(amps_t *amps, int high_pass, int tolerant) /* count SAT tones */ for (i = 0; i < 5; i++) { - coeff = 2.0 * cos(2.0 * PI * sat_freq[i] / (double)amps->sender.samplerate); - amps->sat_coeff[i] = coeff * 32768.0; - PDEBUG(DDSP, DEBUG_DEBUG, "sat_coeff[%d] = %d\n", i, (int)amps->sat_coeff[i]); - + audio_goertzel_init(&s->sat_goertzel[i], sat_freq[i], amps->sender.samplerate); if (i < 3) { amps->sat_phaseshift256[i] = 256.0 / ((double)amps->sender.samplerate / sat_freq[i]); PDEBUG(DDSP, DEBUG_DEBUG, "sat_phaseshift256[%d] = %.4f\n", i, amps->sat_phaseshift256[i]); @@ -300,7 +298,7 @@ void dsp_cleanup_sender(amps_t *amps) static int fsk_encode(amps_t *amps, char bit) { - int16_t *spl; + sample_t *spl; double phase, bitstep, deviation; int count; char last; @@ -368,10 +366,10 @@ static int fsk_encode(amps_t *amps, char bit) return count; } -static int fsk_frame(amps_t *amps, int16_t *samples, int length) +static int fsk_frame(amps_t *amps, sample_t *samples, int length) { int count = 0, len, pos, copy, i; - int16_t *spl; + sample_t *spl; int rc; char c; @@ -430,7 +428,7 @@ done: } /* Generate audio stream with SAT signal. Keep phase for next call of function. */ -static void sat_encode(amps_t *amps, int16_t *samples, int length) +static void sat_encode(amps_t *amps, sample_t *samples, int length) { double phaseshift, phase; int32_t sample; @@ -455,7 +453,7 @@ static void sat_encode(amps_t *amps, int16_t *samples, int length) amps->sat_phase256 = phase; } -static void test_tone_encode(amps_t *amps, int16_t *samples, int length) +static void test_tone_encode(amps_t *amps, sample_t *samples, int length) { double phaseshift, phase; int i; @@ -474,7 +472,7 @@ static void test_tone_encode(amps_t *amps, 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) { amps_t *amps = (amps_t *) sender; int count; @@ -505,12 +503,12 @@ again: } } -static void fsk_rx_bit(amps_t *amps, int16_t *spl, int len, int pos, int begin, int half, int end) +static void fsk_rx_bit(amps_t *amps, sample_t *spl, int len, int pos, int begin, int half, int end) { int i; - int32_t first, second; + double first, second; int bit; - int32_t max = -32768, min = 32767; + double max = 0, min = 0; /* decode one bit. substact the first half from the second half. * the result shows the direction of the bit change: 1 == positive. @@ -522,9 +520,9 @@ static void fsk_rx_bit(amps_t *amps, int16_t *spl, int len, int pos, int begin, pos += len; //printf("second %d: %d\n", pos, spl[pos]); second += spl[pos]; - if (spl[pos] > max) + if (i == 0 || spl[pos] > max) max = spl[pos]; - if (spl[pos] < min) + if (i == 0 || spl[pos] < min) min = spl[pos]; } second /= (half - begin); @@ -683,13 +681,13 @@ static void fsk_rx_dotting(amps_t *amps, double _elapsed) } /* decode frame */ -static void sender_receive_frame(amps_t *amps, int16_t *samples, int length) +static void sender_receive_frame(amps_t *amps, sample_t *samples, int length) { int i; for (i = 0; i < length; i++) { #ifdef DEBUG_DECODER - puts(debug_amplitude((double)samples[i] / (double)FSK_DEVIATION)); + puts(debug_amplitude(samples[i] / (double)FSK_DEVIATION)); #endif /* push sample to detection window and shift */ amps->fsk_rx_window[amps->fsk_rx_window_pos++] = samples[i]; @@ -731,15 +729,13 @@ static void sender_receive_frame(amps_t *amps, int16_t *samples, int length) /* decode signaling tone */ /* compare supervisory signal against noise floor on 5800 Hz */ -static void sat_decode(amps_t *amps, int16_t *samples, int length) +static void sat_decode(amps_t *amps, sample_t *samples, int length) { - int coeff[3]; double result[3], quality[2]; - coeff[0] = amps->sat_coeff[amps->sat]; - coeff[1] = amps->sat_coeff[3]; /* noise floor detection */ - coeff[2] = amps->sat_coeff[4]; /* signaling tone */ - audio_goertzel(samples, length, 0, coeff, result, 3); + audio_goertzel(&s->sat_goertzel[amps->sat], samples, length, 0, &result[0], 1); + audio_goertzel(&s->sat_goertzel[3], samples, length, 0, &result[1], 1); + audio_goertzel(&s->sat_goertzel[4], samples, length, 0, &result[2], 1); quality[0] = (result[0] - result[1]) / result[0]; if (quality[0] < 0) @@ -805,10 +801,10 @@ static void sat_decode(amps_t *amps, int16_t *samples, int length) * time is between SIG_TONE_MINBITS and SIG_TONE_MAXBITS. If it is, the * frequency is close to the singalling tone, so it is detected */ -static void sender_receive_audio(amps_t *amps, int16_t *samples, int length) +static void sender_receive_audio(amps_t *amps, sample_t *samples, int length) { transaction_t *trans = amps->trans_list; - int16_t *spl; + sample_t *spl; int max, pos; int i; @@ -830,21 +826,19 @@ static void sender_receive_audio(amps_t *amps, int16_t *samples, int length) if ((amps->dsp_mode == DSP_MODE_AUDIO_RX_AUDIO_TX || amps->dsp_mode == DSP_MODE_AUDIO_RX_FRAME_TX) && trans && trans->callref && trans->sat_detected) { - int16_t down[length]; /* more than enough */ int pos, count; - int16_t *spl; int i; /* de-emphasis */ if (amps->de_emphasis) de_emphasis(&s->estate, samples, length); /* downsample */ - count = samplerate_downsample(&s->sender.srstate, samples, length, down); - expand_audio(&s->cstate, down, count); + count = samplerate_downsample(&s->sender.srstate, samples, length); + expand_audio(&s->cstate, samples, count); spl = amps->sender.rxbuf; pos = amps->sender.rxbuf_pos; for (i = 0; i < count; i++) { - spl[pos++] = down[i]; + spl[pos++] = samples[i]; if (pos == 160) { call_tx_audio(trans->callref, spl, 160); pos = 0; @@ -856,11 +850,10 @@ static void sender_receive_audio(amps_t *amps, int16_t *samples, int length) } /* 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) { amps_t *amps = (amps_t *) sender; double x, y, x_last, y_last, factor; - int32_t value; int i; /* high pass filter to remove 0-level @@ -874,12 +867,7 @@ void sender_receive(sender_t *sender, int16_t *samples, int length) y = factor * (y_last + x - x_last); x_last = x; y_last = y; - value = (int32_t)(y + 0.5); - if (value < -32768.0) - value = -32768.0; - else if (value > 32767) - value = 32767; - samples[i] = value; + samples[i] = y; } amps->highpass_x_last = x_last; amps->highpass_y_last = y_last; diff --git a/src/amps/frame.c b/src/amps/frame.c index 478739b..ea98c18 100644 --- a/src/amps/frame.c +++ b/src/amps/frame.c @@ -26,6 +26,7 @@ #include #include #include +#include "../common/sample.h" #include "../common/debug.h" #include "../common/timer.h" #include "amps.h" diff --git a/src/amps/main.c b/src/amps/main.c index 8394c37..b853b76 100644 --- a/src/amps/main.c +++ b/src/amps/main.c @@ -22,6 +22,7 @@ #include #include #include +#include "../common/sample.h" #include "../common/main.h" #include "../common/debug.h" #include "../common/timer.h" diff --git a/src/amps/sysinfo.c b/src/amps/sysinfo.c index f57de95..db35fb4 100644 --- a/src/amps/sysinfo.c +++ b/src/amps/sysinfo.c @@ -3,6 +3,7 @@ #include #include #include +#include "../common/sample.h" #include "../common/timer.h" #include "amps.h" #include "frame.h" diff --git a/src/amps/transaction.c b/src/amps/transaction.c index b874f58..ea47a67 100644 --- a/src/amps/transaction.c +++ b/src/amps/transaction.c @@ -20,6 +20,7 @@ #include #include #include +#include "../common/sample.h" #include "../common/debug.h" #include "../common/timer.h" #include "amps.h" -- cgit v1.2.3