Skip to content

Commit

Permalink
Merge branch 'devel': Bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dbdexter-dev committed Jul 25, 2018
2 parents fe624e3 + 72de273 commit 05aa832
Show file tree
Hide file tree
Showing 19 changed files with 68 additions and 60 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ PREFIX=/usr

default: release

debug: CFLAGS += -g -D__DEBUG -Werror
debug: CFLAGS += -g -D__DEBUG -Wextra
debug: src
release: CFLAGS += -O2 -ffast-math -flto
release: LDFLAGS += -flto
Expand Down
3 changes: 1 addition & 2 deletions src/agc.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#define AGC_MAX_GAIN 20
#define AGC_BIAS_WINSIZE 1024*1024


/* Initialize an AGC object */
Agc*
agc_init()
Expand All @@ -34,7 +33,7 @@ agc_apply(Agc *self, float complex sample)
sample -= self->bias;

/* Update the sample magnitude average */
rho = sqrt(creal(sample)*creal(sample) + cimag(sample)*cimag(sample));
rho = sqrtf(crealf(sample)*crealf(sample) + cimagf(sample)*cimagf(sample));
self->avg = (self->avg * (self->window_size - 1) + rho) / self->window_size;

self->gain = self->target_ampl / self->avg;
Expand Down
16 changes: 10 additions & 6 deletions src/demod.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ demod_get_gain(const Demod *self)
}

/* XXX not thread-safe */
const int8_t* const
const int8_t*
demod_get_buf(const Demod *self)
{
return self->out_buf;
Expand Down Expand Up @@ -149,9 +149,13 @@ demod_thr_run(void* x)
if (args->out_fname) {
if (!(out_fd = fopen(args->out_fname, "w"))) {
fatal("Could not open file for writing");
/* Not reached */
return NULL;
}
} else {
out_fd = 0;
fatal("No output filename specified");
/* Not reached */
return NULL;
}

/* Main processing loop */
Expand All @@ -170,19 +174,19 @@ demod_thr_run(void* x)
/* The current sample is in the correct time slot: process it */
/* Calculate the symbol timing error (Gardner algorithm) */
resync_offset -= resync_period;
resync_error = (cimag(cur) - cimag(before)) * cimag(mid);
resync_error = (cimagf(cur) - cimagf(before)) * cimagf(mid);
resync_offset += (resync_error*resync_period/2000000.0);
before = cur;

/* Fine frequency/phase tuning */
cur = costas_resync(self->cst, cur);

/* Append the new samples to the output buffer */
out_buf[buf_offset++] = clamp(creal(cur)/2);
out_buf[buf_offset++] = clamp(cimag(cur)/2);
out_buf[buf_offset++] = clamp(crealf(cur)/2);
out_buf[buf_offset++] = clamp(cimagf(cur)/2);

/* Write binary stream to file and/or to socket */
if (buf_offset >= SYM_CHUNKSIZE) {
if (buf_offset >= SYM_CHUNKSIZE - 1) {
fwrite(out_buf, buf_offset, 1, out_fd);
buf_offset = 0;
}
Expand Down
24 changes: 13 additions & 11 deletions src/filters.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Filter*
filter_new(unsigned fwd_count, unsigned back_count, ...)
{
Filter *flt;
int i;
unsigned i;
va_list flt_parm;
double *fwd_coeff;
double *back_coeff;
Expand Down Expand Up @@ -52,7 +52,7 @@ Filter*
filter_copy(const Filter *orig)
{
Filter *ret;
int i;
unsigned i;

ret = safealloc(sizeof(*ret));

Expand Down Expand Up @@ -83,7 +83,7 @@ filter_copy(const Filter *orig)
Filter*
filter_rrc(unsigned order, unsigned factor, float osf, float alpha)
{
int i;
unsigned i;
unsigned taps;
double *coeffs;
Filter *rrc;
Expand Down Expand Up @@ -111,7 +111,7 @@ filter_fwd(Filter *const self, float complex in)
float complex out;

/* Calculate the new mem[0] value through the feedback coefficients */
for (i=1; i<self->back_count; i++) {
for (i=1; i<(int)self->back_count; i++) {
in -= self->mem[i] * self->back_coeff[i];
}

Expand All @@ -132,12 +132,13 @@ filter_fwd(Filter *const self, float complex in)
void
filter_free(Filter *self)
{
if (self->fwd_count) {
if (self->mem) {
free(self->mem);
}
if (self->fwd_count) {
free(self->fwd_coeff);
}
if (self->back_count) {
free(self->mem);
free(self->back_coeff);
}
free(self);
Expand All @@ -152,18 +153,19 @@ compute_rrc_coeff(int stage_no, unsigned taps, float osf, float alpha)
float coeff;
float t;
float interm;
unsigned order;

order = (taps-1)/2;
int order;

t = abs(order - stage_no)/osf;
order = (taps - 1)/2;

if (t==0) {
/* Handle the 0/0 case */
if (order == stage_no) {
return 1-alpha+4*alpha/M_PI;
}

t = abs(order - stage_no)/osf;
coeff = sin(M_PI*t*(1-alpha)) + 4*alpha*t*cos(M_PI*t*(1+alpha));
interm = M_PI*t*(1-(4*alpha*t)*(4*alpha*t));

return coeff / interm;
}
/*}}}*/
6 changes: 3 additions & 3 deletions src/include/agc.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* Moving average AGC, there's not much to it really
*/
#ifndef _METEOR_AGC_H
#define _METEOR_AGC_H
#ifndef METEOR_AGC_H
#define METEOR_AGC_H

#include <complex.h>

Expand All @@ -14,7 +14,7 @@ typedef struct {
float complex bias;
} Agc;

Agc* agc_init();
Agc* agc_init(void);
float complex agc_apply(Agc *agc, float complex sampl);
void agc_free(Agc *agc);

Expand Down
6 changes: 3 additions & 3 deletions src/include/demod.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* process the incoming samples, so it'll interpolate and resample them,
* normalize their amplitude, recover the carrier, and write the decoded symbols
* to disk */
#ifndef _METEOR_DEMOD_H
#define _METEOR_DEMOD_H
#ifndef METEOR_DEMOD_H
#define METEOR_DEMOD_H

#include <pthread.h>
#include "agc.h"
Expand Down Expand Up @@ -40,6 +40,6 @@ uint64_t demod_get_done(const Demod *self);
uint64_t demod_get_size(const Demod *self);
float demod_get_freq(const Demod *self);
float demod_get_gain(const Demod *self);
const int8_t* const demod_get_buf(const Demod *self);
const int8_t* demod_get_buf(const Demod *self);

#endif
4 changes: 2 additions & 2 deletions src/include/filters.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* If back_count == 0, the filter will be FIR, otherwise it'll be IIR. Right now
* this is used only to build the interpolating root-raised cosine filter
*/
#ifndef _METEOR_FILTERS_H
#define _METEOR_FILTERS_H
#ifndef METEOR_FILTERS_H
#define METEOR_FILTERS_H

#include <complex.h>

Expand Down
4 changes: 2 additions & 2 deletions src/include/interpolator.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* easily interpolate the incoming samples, and hopefully this is the best way
* to reconstruct a nicer looking signal
*/
#ifndef _METEOR_INTERPOLATOR_H
#define _METEOR_INTERPOLATOR_H
#ifndef METEOR_INTERPOLATOR_H
#define METEOR_INTERPOLATOR_H

#include "source.h"

Expand Down
4 changes: 2 additions & 2 deletions src/include/options.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* All accepted short and long command-line flags are defined here
*/
#ifndef _METEOR_OPTIONS_H
#define _METEOR_OPTIONS_H
#ifndef METEOR_OPTIONS_H
#define METEOR_OPTIONS_H

#include <getopt.h>
#include <stdlib.h>
Expand Down
4 changes: 2 additions & 2 deletions src/include/pll.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* costas_resync, and it'll return the samples resync'd to the reconstructed
* carrier.
*/
#ifndef _METEOR_PLL_H
#define _METEOR_PLL_H
#ifndef METEOR_PLL_H
#define METEOR_PLL_H

#include <complex.h>

Expand Down
4 changes: 2 additions & 2 deletions src/include/source.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* stores the data read as well as some metadata that might be useful when
* parsing the data stream.
*/
#ifndef _METEOR_SOURCE_H
#define _METEOR_SOURCE_H
#ifndef METEOR_SOURCE_H
#define METEOR_SOURCE_H

#include <stdlib.h>
#include <stdint.h>
Expand Down
17 changes: 10 additions & 7 deletions src/include/tui.h
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
#ifndef _METEOR_TUI_H
#define _METEOR_TUI_H
/**
* Auxiliary functions abstracting the ncurses text user interface
*/
#ifndef METEOR_TUI_H
#define METEOR_TUI_H

#include <stdint.h>

#define CONSTELL_MAX 31

void tui_init();
void tui_deinit();
void tui_handle_resize();
void tui_init(unsigned upd_interval);
void tui_deinit(void);
void tui_handle_resize(void);

int tui_process_input();
int tui_process_input(void);

int tui_print_info(const char *msg, ...);
void tui_update_pll(float freq, int islocked, float gain);
void tui_draw_constellation(const int8_t *dots, unsigned count);
void tui_update_file_in(unsigned rate, uint64_t done, uint64_t duration);
void tui_update_data_out(unsigned nbytes);
int tui_wait_for_user_input();
int tui_wait_for_user_input(void);

#endif
10 changes: 5 additions & 5 deletions src/include/utils.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* Various utility functions that don't really fit in any other file.
*/
#ifndef _METEOR_UTILS_H
#define _METEOR_UTILS_H
#ifndef METEOR_UTILS_H
#define METEOR_UTILS_H

#define MAX(X, Y) (X > Y) ? X : Y
#define MIN(X, Y) (X < Y) ? X : Y
Expand All @@ -15,13 +15,13 @@ float float_clamp(float x, float max_abs);
int slice(float x);

void humanize(size_t count, char *buf);
char* gen_fname();
char* gen_fname(void);
void seconds_to_str(unsigned secs, char *buf);

void usage(const char *pname);
void fatal(const char *msg);
void splash();
void version();
void splash(void);
void version(void);
void* safealloc(size_t size);

#endif
4 changes: 2 additions & 2 deletions src/include/wavfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* Basic functions and structs to deal with I/Q samples coming from a .wav file,
* as well as raw I/Q recordings like those coming from rtl_fm.
*/
#ifndef _METEOR_WAVFILE_H
#define _METEOR_WAVFILE_H
#ifndef METEOR_WAVFILE_H
#define METEOR_WAVFILE_H

#include <stdint.h>
#include "source.h"
Expand Down
2 changes: 1 addition & 1 deletion src/interpolator.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ interp_read(Source *const self, size_t count)
InterpState *status;
Filter *rrc;
Source *src;
int i;
unsigned i;
int factor;
size_t true_samp_count;

Expand Down
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ main(int argc, char *argv[])
if (batch_mode) {
if (!quiet) {
log("(%5.1f%%) Carrier: %+7.1f Hz, Locked: %s\n",
(float)in_done/in_total, freq, pll_locked ? "Yes" : "No");
(float)in_done/in_total*100, freq, pll_locked ? "Yes" : "No");
}
nanosleep(&timespec, NULL);
} else {
Expand Down
8 changes: 4 additions & 4 deletions src/pll.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ costas_resync(Costas *self, float complex samp)
retval = samp * nco_out;

/* Calculate phase delta and updothe the running average */
error = costas_compute_delta(creal(retval), cimag(retval))/255.0;
self->moving_avg = (self->moving_avg * (AVG_WINSIZE-1) + abs(error))/AVG_WINSIZE;
error = costas_compute_delta(crealf(retval), cimagf(retval))/255.0;
self->moving_avg = (self->moving_avg * (AVG_WINSIZE-1) + fabs(error))/AVG_WINSIZE;
error = float_clamp(error, 1.0);

/* Apply phase and frequency corrections, and advance the phase */
Expand All @@ -71,10 +71,10 @@ costas_resync(Costas *self, float complex samp)
}

/* Detect whether the PLL is locked, and decrease the BW if it is */
if (!self->locked && self->moving_avg < 0.002) {
if (!self->locked && self->moving_avg < 0.3) {
costas_recompute_coeffs(self, self->damping, self->bw/2);
self->locked = 1;
} else if (self->locked && self->moving_avg > 0.025) {
} else if (self->locked && self->moving_avg > 0.35) {
costas_recompute_coeffs(self, self->damping, self->bw);
self->locked = 0;
}
Expand Down
4 changes: 2 additions & 2 deletions src/tui.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ tui_draw_constellation(const int8_t *dots, unsigned count)

werase(tui.iq);
for (i=0; i<count; i++) {
x = round(dots[i++]*nc/255);
y = round(dots[i]*nr/255);
x = dots[i++]*nc/255;
y = dots[i]*nr/255;

prev = mvwinch(tui.iq, nr/2-y, x+nc/2);
switch(prev) {
Expand Down
4 changes: 2 additions & 2 deletions src/wavfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ open_samples_file(const char *fname, unsigned samplerate)
state->total_samples = 0;
}
state->samples_read = 0;
state->tmp = safealloc(2*sizeof(state->tmp));
state->tmp = safealloc(2*sizeof(*state->tmp));
} else {
fatal("Could not find specified file");
/* Not reached */
Expand Down Expand Up @@ -99,7 +99,7 @@ int
wav_read(Source *self, size_t count)
{
WavState *state;
int i;
unsigned i;

state = (WavState*)self->_backend;

Expand Down

0 comments on commit 05aa832

Please sign in to comment.