diff options
author | Andreas Eversberg <jolly@eversberg.eu> | 2017-01-29 07:25:12 +0100 |
---|---|---|
committer | Andreas Eversberg <jolly@eversberg.eu> | 2017-02-18 21:01:13 +0100 |
commit | 7e45f556cec493c3c77fcb6400d8ae211faf2220 (patch) | |
tree | cb5903509228c36e7dcd52ae0a049b380e81f401 /src/cnetz | |
parent | bd7ccc5fa05587606757adbacb6e1bf12f12fd2c (diff) |
Correcting all levels and move all remaining integer samples to sample_t
The leves are based on the standards of each mobile network. They
are adjusted to the specified frequency deviation now.
Diffstat (limited to 'src/cnetz')
-rw-r--r-- | src/cnetz/dsp.c | 38 | ||||
-rw-r--r-- | src/cnetz/fsk_fm_demod.c | 43 | ||||
-rw-r--r-- | src/cnetz/fsk_fm_demod.h | 4 | ||||
-rw-r--r-- | src/cnetz/scrambler.c | 20 | ||||
-rw-r--r-- | src/cnetz/scrambler.h | 4 | ||||
-rw-r--r-- | src/cnetz/telegramm.c | 4 |
6 files changed, 58 insertions, 55 deletions
diff --git a/src/cnetz/dsp.c b/src/cnetz/dsp.c index a927c55..1fc404a 100644 --- a/src/cnetz/dsp.c +++ b/src/cnetz/dsp.c @@ -43,9 +43,12 @@ extern int voice_deviation; #define PI M_PI -#define BANDWIDTH 5500.0 /* maximum bandwidth */ -#define FSK_DEVIATION 10000 -#define COMPANDOR_0DB 30000 +#define MAX_DEVIATION 4000.0 +#define MAX_MODULATION 5280.0 +#define DBM0_DEVIATION 4000.0 /* deviation of dBm0 at 1 kHz */ +#define COMPANDOR_0DB 1.0 /* A level of 0dBm (1.0) shall be unaccected */ +#define FSK_DEVIATION (2500.0 / DBM0_DEVIATION) /* no emphasis */ +#define MAX_DISPLAY 1.4 /* something above dBm0, no emphasis */ #define BITRATE 5280.0 /* bits per second */ #define BLOCK_BITS 198 /* duration of one time slot including pause at beginning and end */ #define CUT_OFF_OFFSET 300.0 /* cut off frequency for offset filter (level correction between subsequent audio chunks) */ @@ -76,7 +79,7 @@ static void dsp_init_ramp(cnetz_t *cnetz) c = -sqrt(-c); else c = sqrt(c); - ramp_down[i] = (int)(c * (double)cnetz->fsk_deviation); + ramp_down[i] = c * (double)cnetz->fsk_deviation; ramp_up[i] = -ramp_down[i]; } } @@ -90,9 +93,8 @@ int dsp_init_sender(cnetz_t *cnetz, int measure_speed, double clock_speed[2], do PDEBUG_CHAN(DDSP, DEBUG_DEBUG, "Init FSK for 'Sender'.\n"); - /* set deviation and modulation parameters */ - cnetz->sender.bandwidth = BANDWIDTH; - cnetz->sender.sample_deviation = 2500.0 / (double)FSK_DEVIATION; + /* set modulation parameters */ + sender_set_fm(&cnetz->sender, MAX_DEVIATION, MAX_MODULATION, DBM0_DEVIATION, MAX_DISPLAY); if (measure_speed) { cnetz->measure_speed = measure_speed; @@ -119,7 +121,7 @@ int dsp_init_sender(cnetz_t *cnetz, int measure_speed, double clock_speed[2], do } /* create devation and ramp */ - cnetz->fsk_deviation = FSK_DEVIATION; /* be sure not to overflow -32767 .. 32767 */ + cnetz->fsk_deviation = FSK_DEVIATION; dsp_init_ramp(cnetz); cnetz->fsk_noise = noise; @@ -465,7 +467,7 @@ static int fsk_distributed_encode(cnetz_t *cnetz, const char *bits) } while (phase < 256.0); phase -= 256.0; } - *marker = -32768; /* indicator for inserting speech */ + *marker = 999; /* marker for inserting speech */ } /* add 46 * (1+4+1 + 60) bits */ for (i = 0; i < 46; i++) { @@ -556,7 +558,7 @@ static int fsk_distributed_encode(cnetz_t *cnetz, const char *bits) } while (phase < 256.0); phase -= 256.0; } - *marker = -32768; /* indicator for inserting speech */ + *marker = 999; /* marker for inserting speech */ } /* depending on the number of samples, return the number */ @@ -706,7 +708,7 @@ again: if (length - count < copy) copy = length - count; for (i = 0; i < copy; i++) { - if (*spl == -32768) { + if (*spl == 999) { /* marker found to insert new chunk of audio */ jitter_load(&cnetz->sender.dejitter, speech_buffer, 100); /* 1. compress dynamics */ @@ -722,15 +724,9 @@ again: pre_emphasis(&cnetz->estate, speech_buffer, speech_length); /* change level */ if (voice_deviation != 1) { - int sample, j; - for (j = 0; j < speech_length; j++) { - sample = speech_buffer[j] * voice_deviation; - if (sample > 32767) - sample = 32767; - if (sample < -32768) - sample = -32768; - speech_buffer[j] = sample; - } + int j; + for (j = 0; j < speech_length; j++) + speech_buffer[j] *= (double)voice_deviation; } speech_pos = 0; } @@ -801,7 +797,7 @@ void unshrink_speech(cnetz_t *cnetz, sample_t *speech_buffer, int count) factor = cnetz->offset_factor; for (i = 0; i < count; i++) { /* change level */ - x = speech_buffer[i] / voice_deviation; + x = speech_buffer[i] / (double)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; diff --git a/src/cnetz/fsk_fm_demod.c b/src/cnetz/fsk_fm_demod.c index 74dd8ab..c151fb7 100644 --- a/src/cnetz/fsk_fm_demod.c +++ b/src/cnetz/fsk_fm_demod.c @@ -148,7 +148,7 @@ int fsk_fm_init(fsk_fm_demod_t *fsk, cnetz_t *cnetz, int samplerate, double bitr goto error; } - fsk->level_threshold = 655; + fsk->level_threshold = 0.1; return 0; @@ -170,9 +170,10 @@ void fsk_fm_exit(fsk_fm_demod_t *fsk) } /* get levels, sync time and jitter from sync sequence or frame data */ -static inline void get_levels(fsk_fm_demod_t *fsk, int *_min, int *_max, int *_avg, int *_probes, int num, double *_time, double *_jitter) +static inline void get_levels(fsk_fm_demod_t *fsk, double *_min, double *_max, double *_avg, int *_probes, int num, double *_time, double *_jitter) { - int min = 32767, max = -32768, avg = 0, count = 0, level; + int count = 0; + double min = 0, max = 0, avg = 0, level; double time = 0, t, sync_average, sync_time, jitter = 0; int bit_offset; int i; @@ -192,19 +193,20 @@ static inline void get_levels(fsk_fm_demod_t *fsk, int *_min, int *_max, int *_a if (t > BITS_PER_SUPERFRAME / 2) t -= BITS_PER_SUPERFRAME; //if (fsk->cnetz->dsp_mode == DSP_MODE_SPK_V) -// printf("%d: level=%d%% @%.2f difference=%.2f\n", bit_offset, level * 100 / 65536, fsk->change_when[(fsk->change_pos - 1 - i) & 0xff], t); +// printf("%d: level=%.0f%% @%.2f difference=%.2f\n", bit_offset, level * 100, fsk->change_when[(fsk->change_pos - 1 - i) & 0xff], t); time += t; - if (level < min) + if (i == 0 || level < min) min = level; - if (level > max) + if (i == 0 || level > max) max = level; avg += level; count++; } + /* should never happen */ if (!count) { - *_min = *_max = *_avg = 0; + *_min = *_max = *_avg = 0.0; return; } @@ -219,7 +221,7 @@ static inline void get_levels(fsk_fm_demod_t *fsk, int *_min, int *_max, int *_a *_probes = count; *_min = min; *_max = max; - *_avg = avg / count; + *_avg = avg / (double)count; if (_time) { // if (fsk->cnetz->dsp_mode == DSP_MODE_SPK_V) @@ -254,15 +256,16 @@ static inline void get_levels(fsk_fm_demod_t *fsk, int *_min, int *_max, int *_a } } -static inline void got_bit(fsk_fm_demod_t *fsk, int bit, int change_level) +static inline void got_bit(fsk_fm_demod_t *fsk, int bit, double change_level) { - int min, max, avg, probes; + int probes; + double min, max, avg; /* count bits, but do not exceed 4 bits per SPK block */ if (fsk->cnetz->dsp_mode == DSP_MODE_SPK_V) { /* for first bit, we have only half of the modulation deviation, so we multiply level by two */ if (fsk->bit_count == 0) - change_level *= 2; + change_level *= 2.0; if (fsk->bit_count == 4) return; } @@ -284,20 +287,20 @@ static inline void got_bit(fsk_fm_demod_t *fsk, int bit, int change_level) * for all other bits in the sync sequence. * after sync, the theshold is set to half of the average of * all changes in the sync sequence */ - if (change_level) { - fsk->level_threshold = (double)change_level / 2.0; + if (change_level > 0.0) { + fsk->level_threshold = change_level / 2.0; } else if ((fsk->rx_sync & 0x1f) == 0x00 || (fsk->rx_sync & 0x1f) == 0x1f) { if (fsk->cnetz->dsp_mode != DSP_MODE_SPK_V) - fsk->level_threshold = 655; + fsk->level_threshold = 0.01; } if (detect_sync(fsk->rx_sync)) { fsk->sync = FSK_SYNC_POSITIVE; got_sync: get_levels(fsk, &min, &max, &avg, &probes, 30, &fsk->sync_time, &fsk->sync_jitter); - fsk->sync_level = (double)avg / 65535.0; + fsk->sync_level = avg / 2.0; if (fsk->sync == FSK_SYNC_NEGATIVE) fsk->sync_level = -fsk->sync_level; -// printf("sync (change min=%d%% max=%d%% avg=%d%% sync_time=%.2f jitter=%.2f probes=%d)\n", min * 100 / 65535, max * 100 / 65535, avg * 100 / 65535, fsk->sync_time, fsk->sync_jitter, probes); +// printf("sync (change min=%.0f%% max=%.0f%% avg=%.0f%% sync_time=%.2f jitter=%.2f probes=%d)\n", min * 100, max * 100, avg * 100, fsk->sync_time, fsk->sync_jitter, probes); fsk->level_threshold = (double)avg / 2.0; fsk->rx_sync = 0; fsk->rx_buffer_count = 0; @@ -342,6 +345,10 @@ static inline void find_change(fsk_fm_demod_t *fsk) change_at = -1; change_positive = -1; + /* get level range (level_min and level_max) and also + * get maximum slope (change_max) and where it was + * (change_at) and what direction it went (change_positive) + */ for (i = 0; i < fsk->bit_buffer_len; i++) { last_s = s; s = fsk->bit_buffer_spl[fsk->bit_buffer_pos++]; @@ -417,7 +424,7 @@ void fsk_fm_demod(fsk_fm_demod_t *fsk, sample_t *samples, int length) if (fsk->cnetz->dsp_mode != DSP_MODE_SPK_V) { #ifdef DEBUG_DECODER DEBUG_DECODER - puts(debug_amplitude(samples[i] / 32768.0)); + puts(debug_amplitude(samples[i])); #endif find_change(fsk); } else { @@ -441,7 +448,7 @@ void fsk_fm_demod(fsk_fm_demod_t *fsk, sample_t *samples, int length) if (t >= 0.5 && t < 5.5) { #ifdef DEBUG_DECODER DEBUG_DECODER - puts(debug_amplitude(samples[i] / 32768.0)); + puts(debug_amplitude(samples[i])); #endif find_change(fsk); } else diff --git a/src/cnetz/fsk_fm_demod.h b/src/cnetz/fsk_fm_demod.h index bf782e5..c8692c3 100644 --- a/src/cnetz/fsk_fm_demod.h +++ b/src/cnetz/fsk_fm_demod.h @@ -24,7 +24,7 @@ typedef struct fsk_fm_demod { 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 */ - int level_threshold; /* threshold for detection of next level change */ + double level_threshold; /* threshold for detection of next level change */ double bits_per_sample; /* duration of one sample in bits */ double next_bit; /* count time to detect bits */ int bit_count; /* counts bits, to match 4 bits at distributed signaling */ @@ -45,7 +45,7 @@ typedef struct fsk_fm_demod { int rx_buffer_count; /* counter when receiving bits */ /* statistics */ - int change_levels[256]; /* ring buffer to store levels */ + double change_levels[256]; /* ring buffer to store levels */ double change_when[256]; /* ring buffer to store time when level has changed */ uint8_t change_pos; /* index for next write */ } fsk_fm_demod_t; diff --git a/src/cnetz/scrambler.c b/src/cnetz/scrambler.c index 439e054..f5adc03 100644 --- a/src/cnetz/scrambler.c +++ b/src/cnetz/scrambler.c @@ -36,22 +36,22 @@ #define TEST_1000HZ_DB 55.0 /* sine wave for carrier to modulate to */ -static double carrier[256]; +static double carrier[65536]; void scrambler_init(void) { int i; - for (i = 0; i < 256; i++) { + for (i = 0; i < 65536; i++) { /* 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; + carrier[i] = sin((double)i / 65536.0 * 2 * PI) * 2.0; } } void scrambler_setup(scrambler_t *scrambler, int samplerate) { filter_lowpass_init(&scrambler->lp, CARRIER_HZ - FILTER_BELOW, samplerate, FILTER_TURNS); - scrambler->carrier_phaseshift256 = 256.0 / ((double)samplerate / CARRIER_HZ); + scrambler->carrier_phaseshift65536 = 65536.0 / ((double)samplerate / CARRIER_HZ); } /* Modulate samples to carriere that is twice the mirror frequency. @@ -63,18 +63,18 @@ void scrambler(scrambler_t *scrambler, sample_t *samples, int length) double phaseshift, phase; int i; - phaseshift = scrambler->carrier_phaseshift256; - phase = scrambler->carrier_phase256; + phaseshift = scrambler->carrier_phaseshift65536; + phase = scrambler->carrier_phase65536; for (i = 0; i < length; i++) { /* modulate samples to carrier */ - samples[i] *= carrier[((uint8_t)phase) & 0xff]; + samples[i] *= carrier[(uint16_t)phase]; phase += phaseshift; - if (phase >= 256.0) - phase -= 256.0; + if (phase >= 65536.0) + phase -= 65536.0; } - scrambler->carrier_phase256 = phase; + scrambler->carrier_phase65536 = phase; /* cut off carrier frequency and modulation above carrier frequency */ filter_process(&scrambler->lp, samples, length); diff --git a/src/cnetz/scrambler.h b/src/cnetz/scrambler.h index ffc2d77..19adba7 100644 --- a/src/cnetz/scrambler.h +++ b/src/cnetz/scrambler.h @@ -1,8 +1,8 @@ #include "../common/filter.h" typedef struct scrambler { - double carrier_phaseshift256; /* carrier phase shift per sample */ - double carrier_phase256; /* current phase of carrier frequency */ + double carrier_phaseshift65536;/* carrier phase shift per sample */ + double carrier_phase65536; /* current phase of carrier frequency */ filter_t lp; /* filter to remove carrier frequency */ } scrambler_t; diff --git a/src/cnetz/telegramm.c b/src/cnetz/telegramm.c index 8edbb2e..e3565eb 100644 --- a/src/cnetz/telegramm.c +++ b/src/cnetz/telegramm.c @@ -1492,9 +1492,9 @@ void cnetz_decode_telegramm(cnetz_t *cnetz, const char *bits, double level, doub telegramm.jitter = jitter; if (bit_errors) - PDEBUG_CHAN(DDSP, DEBUG_INFO, "RX Level: %.0f%% Jitter: %.2f Sync Time: %.2f (TS %.2f) Bit errors: %d %s\n", fabs(level) * 32767.0 / cnetz->fsk_deviation * 100.0, jitter, sync_time, sync_time / 396.0, bit_errors, (level < 0) ? "NEGATIVE (phone's mode)" : "POSITIVE (base station's mode)"); + PDEBUG_CHAN(DDSP, DEBUG_INFO, "RX Level: %.0f%% Jitter: %.2f Sync Time: %.2f (TS %.2f) Bit errors: %d %s\n", fabs(level) / cnetz->fsk_deviation * 100.0, jitter, sync_time, sync_time / 396.0, bit_errors, (level < 0) ? "NEGATIVE (phone's mode)" : "POSITIVE (base station's mode)"); else - PDEBUG_CHAN(DDSP, DEBUG_INFO, "RX Level: %.0f%% Jitter: %.2f Sync Time: %.2f (TS %.2f) %s\n", fabs(level) * 32767.0 / cnetz->fsk_deviation * 100.0, jitter, sync_time, sync_time / 396.0, (level < 0) ? "NEGATIVE (phone's mode)" : "POSITIVE (base station's mode)"); + PDEBUG_CHAN(DDSP, DEBUG_INFO, "RX Level: %.0f%% Jitter: %.2f Sync Time: %.2f (TS %.2f) %s\n", fabs(level) / cnetz->fsk_deviation * 100.0, jitter, sync_time, sync_time / 396.0, (level < 0) ? "NEGATIVE (phone's mode)" : "POSITIVE (base station's mode)"); if (cnetz->sender.loopback) { PDEBUG(DFRAME, DEBUG_NOTICE, "Received Telegramm in loopback test mode (opcode %d = %s)\n", opcode, definition_opcode[opcode].message_name); |