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/nmt/dms.c | 5 ++-- src/nmt/dms.h | 4 +-- src/nmt/dsp.c | 83 ++++++++++++++++++++------------------------------- src/nmt/dsp.h | 2 +- src/nmt/frame.c | 1 + src/nmt/main.c | 1 + src/nmt/nmt.c | 5 ++-- src/nmt/nmt.h | 11 +++---- src/nmt/sms.c | 1 + src/nmt/transaction.c | 1 + 10 files changed, 52 insertions(+), 62 deletions(-) (limited to 'src/nmt') diff --git a/src/nmt/dms.c b/src/nmt/dms.c index 1027e34..2fcaf3c 100644 --- a/src/nmt/dms.c +++ b/src/nmt/dms.c @@ -21,6 +21,7 @@ #include #include #include +#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; diff --git a/src/nmt/dms.h b/src/nmt/dms.h index b03b91e..e4bfd37 100644 --- a/src/nmt/dms.h +++ b/src/nmt/dms.h @@ -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); diff --git a/src/nmt/dsp.c b/src/nmt/dsp.c index ac3018c..7fb8fe5 100644 --- a/src/nmt/dsp.c +++ b/src/nmt/dsp.c @@ -25,9 +25,9 @@ #include #include #include +#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; diff --git a/src/nmt/dsp.h b/src/nmt/dsp.h index de5dcf4..a2dbb83 100644 --- a/src/nmt/dsp.h +++ b/src/nmt/dsp.h @@ -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); diff --git a/src/nmt/frame.c b/src/nmt/frame.c index b9c0f0d..bb3adc2 100644 --- a/src/nmt/frame.c +++ b/src/nmt/frame.c @@ -22,6 +22,7 @@ #include #include #include +#include "../common/sample.h" #include "../common/debug.h" #include "../common/timer.h" #include "nmt.h" diff --git a/src/nmt/main.c b/src/nmt/main.c index e1ea69f..9aedd3e 100644 --- a/src/nmt/main.c +++ b/src/nmt/main.c @@ -26,6 +26,7 @@ #include #include #include +#include "../common/sample.h" #include "../common/main.h" #include "../common/debug.h" #include "../common/timer.h" diff --git a/src/nmt/nmt.c b/src/nmt/nmt.c index 5076fd6..79c759c 100644 --- a/src/nmt/nmt.c +++ b/src/nmt/nmt.c @@ -25,6 +25,7 @@ #include #include #include +#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); diff --git a/src/nmt/nmt.h b/src/nmt/nmt.h index f09d33d..8883bb4 100644 --- a/src/nmt/nmt.h +++ b/src/nmt/nmt.h @@ -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 */ diff --git a/src/nmt/sms.c b/src/nmt/sms.c index c1263b2..2c8d311 100644 --- a/src/nmt/sms.c +++ b/src/nmt/sms.c @@ -23,6 +23,7 @@ #include #include #include +#include "../common/sample.h" #include "../common/debug.h" #include "../common/timer.h" #include "nmt.h" diff --git a/src/nmt/transaction.c b/src/nmt/transaction.c index 26a1ccd..71d671f 100644 --- a/src/nmt/transaction.c +++ b/src/nmt/transaction.c @@ -21,6 +21,7 @@ #include #include #include +#include "../common/sample.h" #include "../common/debug.h" #include "../common/timer.h" #include "nmt.h" -- cgit v1.2.3