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:
@@ -129,6 +129,7 @@
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <math.h>
|
||||
#include "../common/sample.h"
|
||||
#include "../common/debug.h"
|
||||
#include "../common/timer.h"
|
||||
#include "../common/call.h"
|
||||
@@ -428,7 +429,7 @@ static void cnetz_release(transaction_t *trans, uint8_t 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;
|
||||
cnetz_t *cnetz;
|
||||
|
@@ -85,12 +85,12 @@ typedef struct cnetz {
|
||||
/* dsp states */
|
||||
enum dsp_mode dsp_mode; /* current mode: audio, "Telegramm", .... */
|
||||
fsk_fm_demod_t fsk_demod; /* demod process */
|
||||
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_noise; /* send static between OgK frames */
|
||||
double fsk_bitduration; /* duration of a bit in samples */
|
||||
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 */
|
||||
@@ -98,7 +98,7 @@ typedef struct cnetz {
|
||||
double fsk_tx_phase; /* current bit position */
|
||||
uint64_t fsk_tx_scount; /* sample counter (used to sync multiple channels) */
|
||||
int scrambler; /* 0 = normal speech, 1 = scrambled speech */
|
||||
int16_t *dsp_speech_buffer; /* samples in one chunk */
|
||||
sample_t *dsp_speech_buffer; /* samples in one chunk */
|
||||
int dsp_speech_length; /* number of samples */
|
||||
int dsp_speech_pos; /* current position in buffer */
|
||||
|
||||
|
@@ -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 "cnetz.h"
|
||||
|
@@ -25,6 +25,7 @@
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <errno.h>
|
||||
#include "../common/sample.h"
|
||||
#include "../common/debug.h"
|
||||
#include "../common/timer.h"
|
||||
#include "../common/call.h"
|
||||
@@ -55,7 +56,7 @@ scrambler_t scrambler_test_scrambler1;
|
||||
scrambler_t scrambler_test_scrambler2;
|
||||
#endif
|
||||
|
||||
static int16_t ramp_up[256], ramp_down[256];
|
||||
static sample_t ramp_up[256], ramp_down[256];
|
||||
|
||||
void dsp_init(void)
|
||||
{
|
||||
@@ -110,7 +111,7 @@ int dsp_init_sender(cnetz_t *cnetz, int measure_speed, double clock_speed[2], do
|
||||
|
||||
size = cnetz->fsk_bitduration * (double)BLOCK_BITS * 16.0; /* 16 blocks for distributed frames */
|
||||
cnetz->fsk_tx_buffer_size = size * 1.1; /* more to compensate clock speed */
|
||||
cnetz->fsk_tx_buffer = calloc(sizeof(int16_t), cnetz->fsk_tx_buffer_size);
|
||||
cnetz->fsk_tx_buffer = calloc(sizeof(sample_t), cnetz->fsk_tx_buffer_size);
|
||||
if (!cnetz->fsk_tx_buffer) {
|
||||
PDEBUG(DDSP, DEBUG_DEBUG, "No memory!\n");
|
||||
rc = -ENOMEM;
|
||||
@@ -123,7 +124,7 @@ int dsp_init_sender(cnetz_t *cnetz, int measure_speed, double clock_speed[2], do
|
||||
cnetz->fsk_noise = noise;
|
||||
|
||||
/* create speech buffer */
|
||||
cnetz->dsp_speech_buffer = calloc(sizeof(int16_t), cnetz->sender.samplerate); /* buffer is greater than sr/1.1, just to be secure */
|
||||
cnetz->dsp_speech_buffer = calloc(sizeof(sample_t), cnetz->sender.samplerate); /* buffer is greater than sr/1.1, just to be secure */
|
||||
if (!cnetz->dsp_speech_buffer) {
|
||||
PDEBUG(DDSP, DEBUG_DEBUG, "No memory!\n");
|
||||
rc = -ENOMEM;
|
||||
@@ -242,7 +243,7 @@ void calc_clock_speed(cnetz_t *cnetz, uint64_t samples, int tx, int result)
|
||||
|
||||
static int fsk_testtone_encode(cnetz_t *cnetz)
|
||||
{
|
||||
int16_t *spl;
|
||||
sample_t *spl;
|
||||
double phase, bitstep;
|
||||
int i, count;
|
||||
|
||||
@@ -275,7 +276,7 @@ static int fsk_testtone_encode(cnetz_t *cnetz)
|
||||
|
||||
static int fsk_nothing_encode(cnetz_t *cnetz)
|
||||
{
|
||||
int16_t *spl;
|
||||
sample_t *spl;
|
||||
double phase, bitstep, r;
|
||||
int i, count;
|
||||
|
||||
@@ -288,7 +289,7 @@ static int fsk_nothing_encode(cnetz_t *cnetz)
|
||||
/* add 198 bits of noise */
|
||||
for (i = 0; i < 198; i++) {
|
||||
do {
|
||||
*spl++ = (double)((int16_t)(random() & 0xffff)) * r;
|
||||
*spl++ = (double)((int16_t)random()) * r / 32768.0;
|
||||
phase += bitstep;
|
||||
} while (phase < 256.0);
|
||||
phase -= 256.0;
|
||||
@@ -320,7 +321,7 @@ static int fsk_nothing_encode(cnetz_t *cnetz)
|
||||
static int fsk_block_encode(cnetz_t *cnetz, const char *bits)
|
||||
{
|
||||
/* alloc samples, add 1 in case there is a rest */
|
||||
int16_t *spl;
|
||||
sample_t *spl;
|
||||
double phase, bitstep, deviation;
|
||||
int i, count;
|
||||
char last;
|
||||
@@ -437,7 +438,7 @@ static int fsk_block_encode(cnetz_t *cnetz, const char *bits)
|
||||
static int fsk_distributed_encode(cnetz_t *cnetz, const char *bits)
|
||||
{
|
||||
/* alloc samples, add 1 in case there is a rest */
|
||||
int16_t *spl, *marker;
|
||||
sample_t *spl, *marker;
|
||||
double phase, bitstep, deviation;
|
||||
int i, j, count;
|
||||
char last;
|
||||
@@ -570,7 +571,7 @@ static int fsk_distributed_encode(cnetz_t *cnetz, const char *bits)
|
||||
/* decode samples and hut for bit changes
|
||||
* use deviation to find greatest slope of the signal (bit change)
|
||||
*/
|
||||
void sender_receive(sender_t *sender, int16_t *samples, int length)
|
||||
void sender_receive(sender_t *sender, sample_t *samples, int length)
|
||||
{
|
||||
cnetz_t *cnetz = (cnetz_t *) sender;
|
||||
|
||||
@@ -590,10 +591,10 @@ void sender_receive(sender_t *sender, int16_t *samples, int length)
|
||||
return;
|
||||
}
|
||||
|
||||
static int fsk_telegramm(cnetz_t *cnetz, int16_t *samples, int length)
|
||||
static int fsk_telegramm(cnetz_t *cnetz, sample_t *samples, int length)
|
||||
{
|
||||
int count = 0, pos, copy, i, speech_length, speech_pos;
|
||||
int16_t *spl, *speech_buffer;
|
||||
sample_t *spl, *speech_buffer;
|
||||
const char *bits;
|
||||
|
||||
speech_buffer = cnetz->dsp_speech_buffer;
|
||||
@@ -756,7 +757,7 @@ again:
|
||||
}
|
||||
|
||||
/* 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)
|
||||
{
|
||||
cnetz_t *cnetz = (cnetz_t *) sender;
|
||||
int count;
|
||||
@@ -780,10 +781,9 @@ void sender_send(sender_t *sender, int16_t *samples, int length)
|
||||
}
|
||||
|
||||
/* unshrink audio segment from the duration of 60 bits to 12.5 ms */
|
||||
void unshrink_speech(cnetz_t *cnetz, int16_t *speech_buffer, int count)
|
||||
void unshrink_speech(cnetz_t *cnetz, sample_t *speech_buffer, int count)
|
||||
{
|
||||
int16_t *spl;
|
||||
int32_t value;
|
||||
sample_t *spl;
|
||||
int pos, i;
|
||||
double x, y, x_last, y_last, factor;
|
||||
|
||||
@@ -801,17 +801,12 @@ void unshrink_speech(cnetz_t *cnetz, int16_t *speech_buffer, int count)
|
||||
factor = cnetz->offset_factor;
|
||||
for (i = 0; i < count; i++) {
|
||||
/* change level */
|
||||
x = (double)speech_buffer[i] / voice_deviation;
|
||||
x = speech_buffer[i] / voice_deviation;
|
||||
/* high-pass to remove low level frequencies, caused by level jump between audio chunks */
|
||||
y = factor * (y_last + x - x_last);
|
||||
x_last = x;
|
||||
y_last = y;
|
||||
value = (int32_t)y;
|
||||
if (value < -32768.0)
|
||||
value = -32768.0;
|
||||
else if (value > 32767)
|
||||
value = 32767;
|
||||
speech_buffer[i] = value;
|
||||
speech_buffer[i] = y;
|
||||
}
|
||||
cnetz->offset_y_last = y_last;
|
||||
|
||||
@@ -823,7 +818,7 @@ void unshrink_speech(cnetz_t *cnetz, int16_t *speech_buffer, int count)
|
||||
if (cnetz->scrambler)
|
||||
scrambler(&cnetz->scrambler_rx, speech_buffer, count);
|
||||
/* 2. decompress time */
|
||||
count = samplerate_downsample(&cnetz->sender.srstate, speech_buffer, count, speech_buffer);
|
||||
count = samplerate_downsample(&cnetz->sender.srstate, speech_buffer, count);
|
||||
/* 1. expand dynamics */
|
||||
expand_audio(&cnetz->cstate, speech_buffer, count);
|
||||
/* to call control */
|
||||
|
@@ -3,7 +3,7 @@ void dsp_init(void);
|
||||
int dsp_init_sender(cnetz_t *cnetz, int measure_speed, double clock_speed[2], double noise);
|
||||
void dsp_cleanup_sender(cnetz_t *cnetz);
|
||||
void calc_clock_speed(cnetz_t *cnetz, uint64_t samples, int tx, int result);
|
||||
void unshrink_speech(cnetz_t *cnetz, int16_t *speech_buffer, int count);
|
||||
void unshrink_speech(cnetz_t *cnetz, sample_t *speech_buffer, int count);
|
||||
void cnetz_set_dsp_mode(cnetz_t *cnetz, enum dsp_mode mode);
|
||||
void cnetz_set_sched_dsp_mode(cnetz_t *cnetz, enum dsp_mode mode, int frames_ahead);
|
||||
|
||||
|
@@ -105,6 +105,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include "../common/sample.h"
|
||||
#include "../common/timer.h"
|
||||
#include "../common/debug.h"
|
||||
#include "cnetz.h"
|
||||
@@ -330,15 +331,13 @@ got_sync:
|
||||
/* DOC TBD: find change for bit change */
|
||||
static inline void find_change(fsk_fm_demod_t *fsk)
|
||||
{
|
||||
int32_t level_min, level_max, change_max;
|
||||
int change_at, change_positive;
|
||||
int16_t s, last_s = 0;
|
||||
int threshold;
|
||||
sample_t level_min = 0, level_max = 0, change_max = -1;
|
||||
int change_at = -1, change_positive = -1;
|
||||
sample_t s, last_s = 0;
|
||||
sample_t threshold;
|
||||
int i;
|
||||
|
||||
/* levels at total reverse */
|
||||
level_min = 32767;
|
||||
level_max = -32768;
|
||||
change_max = -1;
|
||||
change_at = -1;
|
||||
change_positive = -1;
|
||||
@@ -359,14 +358,14 @@ static inline void find_change(fsk_fm_demod_t *fsk)
|
||||
change_positive = 0;
|
||||
}
|
||||
}
|
||||
if (s > level_max)
|
||||
if (i == 0 || s > level_max)
|
||||
level_max = s;
|
||||
if (s < level_min)
|
||||
if (i == 0 || s < level_min)
|
||||
level_min = s;
|
||||
}
|
||||
/* for first bit, we have only half of the modulation deviation, so we divide the threshold by two */
|
||||
if (fsk->cnetz->dsp_mode == DSP_MODE_SPK_V && fsk->bit_count == 0)
|
||||
threshold = fsk->level_threshold / 2;
|
||||
threshold = fsk->level_threshold / 2.0;
|
||||
else
|
||||
threshold = fsk->level_threshold;
|
||||
/* if we are not in sync, for every detected change we set
|
||||
@@ -380,7 +379,7 @@ static inline void find_change(fsk_fm_demod_t *fsk)
|
||||
if (level_max - level_min > threshold && change_at == fsk->bit_buffer_half) {
|
||||
#ifdef DEBUG_DECODER
|
||||
DEBUG_DECODER
|
||||
printf("receive bit change to %d (level=%d, threshold=%d)\n", change_positive, level_max - level_min, threshold);
|
||||
printf("receive bit change to %d (level=%.3f, threshold=%.3f)\n", change_positive, level_max - level_min, threshold);
|
||||
#endif
|
||||
fsk->last_change_positive = change_positive;
|
||||
if (!fsk->sync) {
|
||||
@@ -404,7 +403,7 @@ static inline void find_change(fsk_fm_demod_t *fsk)
|
||||
}
|
||||
|
||||
/* receive FM signal from receiver */
|
||||
void fsk_fm_demod(fsk_fm_demod_t *fsk, int16_t *samples, int length)
|
||||
void fsk_fm_demod(fsk_fm_demod_t *fsk, sample_t *samples, int length)
|
||||
{
|
||||
int i;
|
||||
double t;
|
||||
@@ -418,7 +417,7 @@ void fsk_fm_demod(fsk_fm_demod_t *fsk, int16_t *samples, int length)
|
||||
if (fsk->cnetz->dsp_mode != DSP_MODE_SPK_V) {
|
||||
#ifdef DEBUG_DECODER
|
||||
DEBUG_DECODER
|
||||
puts(debug_amplitude((double)samples[i] / 32768.0));
|
||||
puts(debug_amplitude(samples[i] / 32768.0));
|
||||
#endif
|
||||
find_change(fsk);
|
||||
} else {
|
||||
@@ -442,7 +441,7 @@ void fsk_fm_demod(fsk_fm_demod_t *fsk, int16_t *samples, int length)
|
||||
if (t >= 0.5 && t < 5.5) {
|
||||
#ifdef DEBUG_DECODER
|
||||
DEBUG_DECODER
|
||||
puts(debug_amplitude((double)samples[i] / 32768.0));
|
||||
puts(debug_amplitude(samples[i] / 32768.0));
|
||||
#endif
|
||||
find_change(fsk);
|
||||
} else
|
||||
|
@@ -20,7 +20,7 @@ typedef struct fsk_fm_demod {
|
||||
double bit_time_uncorrected; /* same as above, but not corrected by sync */
|
||||
|
||||
/* bit detection */
|
||||
int16_t *bit_buffer_spl; /* samples ring buffer */
|
||||
sample_t *bit_buffer_spl; /* samples ring buffer */
|
||||
int bit_buffer_len; /* number of samples in ring buffer */
|
||||
int bit_buffer_half; /* half of ring buffer */
|
||||
int bit_buffer_pos; /* current position to write next sample */
|
||||
@@ -35,7 +35,7 @@ typedef struct fsk_fm_demod {
|
||||
double sync_jitter; /* what was the jitter of the sync */
|
||||
|
||||
/* speech */
|
||||
int16_t *speech_buffer; /* holds one chunk of 12.5ms */
|
||||
sample_t *speech_buffer; /* holds one chunk of 12.5ms */
|
||||
int speech_size;
|
||||
int speech_count;
|
||||
|
||||
@@ -52,7 +52,7 @@ typedef struct fsk_fm_demod {
|
||||
|
||||
int fsk_fm_init(fsk_fm_demod_t *fsk, cnetz_t *cnetz, int samplerate, double bitrate);
|
||||
void fsk_fm_exit(fsk_fm_demod_t *fsk);
|
||||
void fsk_fm_demod(fsk_fm_demod_t *fsk, int16_t *samples, int length);
|
||||
void fsk_fm_demod(fsk_fm_demod_t *fsk, sample_t *samples, int length);
|
||||
void fsk_correct_sync(fsk_fm_demod_t *fsk, double offset);
|
||||
void fsk_copy_sync(fsk_fm_demod_t *fsk_to, fsk_fm_demod_t *fsk_from);
|
||||
void fsk_demod_reset(fsk_fm_demod_t *fsk);
|
||||
|
@@ -22,6 +22,7 @@
|
||||
#include <getopt.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "../common/sample.h"
|
||||
#include "../common/main.h"
|
||||
#include "../common/debug.h"
|
||||
#include "../common/timer.h"
|
||||
|
@@ -19,6 +19,7 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include <math.h>
|
||||
#include "../common/sample.h"
|
||||
#include "scrambler.h"
|
||||
|
||||
#define PI M_PI
|
||||
@@ -42,7 +43,8 @@ void scrambler_init(void)
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 256; i++) {
|
||||
carrier[i] = sin((double)i / 256.0 * 2 * PI);
|
||||
/* our amplitude must be doubled, since we have one spectrum above and one below carrier */
|
||||
carrier[i] = sin((double)i / 256.0 * 2 * PI) * 2.0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,10 +58,8 @@ void scrambler_setup(scrambler_t *scrambler, int samplerate)
|
||||
* Then we got spectrum above carrier and mirrored spectrum below carrier.
|
||||
* Afterwards we cut off carrier frequency and frequencies above carrier.
|
||||
*/
|
||||
void scrambler(scrambler_t *scrambler, int16_t *samples, int length)
|
||||
void scrambler(scrambler_t *scrambler, sample_t *samples, int length)
|
||||
{
|
||||
double spl[length];
|
||||
int32_t sample;
|
||||
double phaseshift, phase;
|
||||
int i;
|
||||
|
||||
@@ -68,7 +68,7 @@ void scrambler(scrambler_t *scrambler, int16_t *samples, int length)
|
||||
|
||||
for (i = 0; i < length; i++) {
|
||||
/* modulate samples to carrier */
|
||||
spl[i] = (double)samples[i] / 32768.0 * carrier[((uint8_t)phase) & 0xff];
|
||||
samples[i] *= carrier[((uint8_t)phase) & 0xff];
|
||||
phase += phaseshift;
|
||||
if (phase >= 256.0)
|
||||
phase -= 256.0;
|
||||
@@ -77,17 +77,7 @@ void scrambler(scrambler_t *scrambler, int16_t *samples, int length)
|
||||
scrambler->carrier_phase256 = phase;
|
||||
|
||||
/* cut off carrier frequency and modulation above carrier frequency */
|
||||
filter_process(&scrambler->lp, spl, length);
|
||||
|
||||
for (i = 0; i < length; i++) {
|
||||
/* store result */
|
||||
sample = spl[i] * 2.0 * 32768.0;
|
||||
if (sample > 32767)
|
||||
sample = 32767;
|
||||
else if (sample < -32768)
|
||||
sample = -32768;
|
||||
*samples++ = sample;
|
||||
}
|
||||
filter_process(&scrambler->lp, samples, length);
|
||||
}
|
||||
|
||||
|
||||
|
@@ -8,5 +8,5 @@ typedef struct scrambler {
|
||||
|
||||
void scrambler_init(void);
|
||||
void scrambler_setup(scrambler_t *scrambler, int samplerate);
|
||||
void scrambler(scrambler_t *scrambler, int16_t *samples, int length);
|
||||
void scrambler(scrambler_t *scrambler, sample_t *samples, int length);
|
||||
|
||||
|
@@ -26,6 +26,7 @@
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <math.h>
|
||||
#include "../common/sample.h"
|
||||
#include "../common/debug.h"
|
||||
#include "../common/timer.h"
|
||||
#include "cnetz.h"
|
||||
|
@@ -20,6 +20,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include "../common/sample.h"
|
||||
#include "../common/debug.h"
|
||||
#include "../common/timer.h"
|
||||
#include "cnetz.h"
|
||||
|
Reference in New Issue
Block a user