Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the resampler. #95

Merged
merged 1 commit into from
Oct 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ add_executable (
sync.c

firdecim_q15.c
firdes_kaiser.c
resamp_q15.c
math.c

conv_dec.c
Expand Down
100 changes: 21 additions & 79 deletions src/acquire.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ void acquire_process(acquire_t *st)
{
float complex max_v = 0;
float angle, max_mag = -1.0f;
unsigned int samperr = 0, i, j;
unsigned int samperr = 0, i, j, keep;
unsigned int mink = 0, maxk = FFTCP;

if (st->idx != FFTCP * (M + 1))
return;

if (st->ready && fabsf(st->slope) < 1 && st->samperr > 10)
if (st->input->sync.ready)
{
mink = st->samperr - 10;
maxk = st->samperr + 10;
mink = FFTCP / 2 - 25;
maxk = FFTCP / 2 + 25;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to widen the search range here to account for ppm error. Each invocation of acquire_process processes 64 * 2160 samples, so 100 ppm error causes a 14-sample offset between invocations.

}

memset(st->sums, 0, sizeof(float complex) * FFTCP);
Expand Down Expand Up @@ -74,81 +74,30 @@ void acquire_process(acquire_t *st)
angle = 0.5 * st->prev_angle + 0.5 * angle;
}
st->prev_angle = angle;
st->samperr = samperr;

// compare with previous timing offset
if (abs((int)samperr - (int)st->history[(st->history_size-1) % ACQ_HISTORY]) > FFT/2)
for (i = 0; i < M; ++i)
{
// clear the history if we "rolled over"
st->history_size = 0;
}

st->history[st->history_size % ACQ_HISTORY] = samperr;
if (++st->history_size > ACQ_HISTORY)
{
float avgerr, slope;
int sum = 0;
for (i = 0; i < ACQ_HISTORY; i++)
sum += st->history[i];
avgerr = sum / (float)ACQ_HISTORY;
slope = ((float)samperr - avgerr) / (ACQ_HISTORY / 2 * SYMBOLS);
st->ready = 1;
st->slope = slope;

if ((st->history_size % ACQ_HISTORY) == 0)
log_debug("Timing offset: %f, slope: %f", avgerr, slope);

// avoid adjusting the rate too much
if (fabsf(slope) > 1.0)
{
log_info("Timing offset: %f, slope: %f (adjust)", avgerr, slope);

input_rate_adjust(st->input, (-slope / BLKSZ) / FFTCP);

// clear the history so we don't overadjust
st->history_size = 0;
}
// we don't want the samperr to go < 0
else if (slope < 0)
int j;
for (j = 0; j < FFTCP; ++j)
{
input_rate_adjust(st->input, (-slope / BLKSZ / 8) / FFTCP);
st->history_size = 0;
int n = i * FFTCP + j;
float complex sample = fast_cexpf(angle * n / FFT) * st->buffer[i * FFTCP + j + samperr];
if (j < CP)
st->fftin[j] = st->shape[j] * sample;
else if (j < FFT)
st->fftin[j] = sample;
else
st->fftin[j - FFT] += st->shape[j] * sample;
}

// skip samples instead of having samperr > FFT
// NB adjustment must be greater than FFT/2
if (samperr > 7*FFT/8)
{
input_set_skip(st->input, 6*FFT/8);
st->samperr = 0;
}
fftwf_execute(st->fft);
fftshift(st->fftout, FFT);
sync_push(&st->input->sync, st->fftout);
}

if (st->ready)
{
for (i = 0; i < M; ++i)
{
int j;
for (j = 0; j < FFTCP; ++j)
{
int n = i * FFTCP + j;
float complex sample = fast_cexpf(angle * n / FFT) * st->buffer[i * FFTCP + j + samperr];
if (j < CP)
st->fftin[j] = st->shape[j] * sample;
else if (j < FFT)
st->fftin[j] = sample;
else
st->fftin[j - FFT] += st->shape[j] * sample;
}

fftwf_execute(st->fft);
fftshift(st->fftout, FFT);
sync_push(&st->input->sync, st->fftout);
}
}

memmove(&st->buffer[0], &st->buffer[st->idx - FFTCP], sizeof(float complex) * FFTCP);
st->idx = FFTCP;
keep = FFTCP * 3 / 2 - samperr;
memmove(&st->buffer[0], &st->buffer[st->idx - keep], sizeof(float complex) * keep);
st->idx = keep;
}

unsigned int acquire_push(acquire_t *st, float complex *buf, unsigned int length)
Expand All @@ -172,9 +121,6 @@ void acquire_init(acquire_t *st, input_t *input)
st->buffer = malloc(sizeof(float complex) * FFTCP * (M + 1));
st->sums = malloc(sizeof(float complex) * (FFTCP + CP));
st->idx = 0;
st->ready = 0;
st->samperr = 0;
st->slope = 0;
st->prev_angle = 0;

st->shape = malloc(sizeof(float) * FFTCP);
Expand All @@ -189,10 +135,6 @@ void acquire_init(acquire_t *st, input_t *input)
st->shape[i] = cosf(M_PI / 2 * (i - FFT) / CP);
}

st->history_size = 0;
for (i = 0; i < ACQ_HISTORY; ++i)
st->history[i] = 0;

st->fftin = malloc(sizeof(float complex) * FFT);
st->fftout = malloc(sizeof(float complex) * FFT);
st->fft = fftwf_plan_dft_1d(FFT, st->fftin, st->fftout, FFTW_FORWARD, 0);
Expand Down
7 changes: 0 additions & 7 deletions src/acquire.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#include <complex.h>
#include <fftw3.h>

#define ACQ_HISTORY 16

typedef struct
{
struct input_t *input;
Expand All @@ -15,12 +13,7 @@ typedef struct
float *shape;
fftwf_plan fft;

float samperr;
float slope;
unsigned int idx;
int ready;
int history[ACQ_HISTORY];
unsigned int history_size;
float prev_angle;
} acquire_t;

Expand Down
3 changes: 0 additions & 3 deletions src/firdes.h

This file was deleted.

124 changes: 0 additions & 124 deletions src/firdes_kaiser.c

This file was deleted.

19 changes: 1 addition & 18 deletions src/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@

#define INPUT_BUF_LEN (2160 * 512)

#ifdef USE_FAST_MATH
#define RESAMP_NUM_TAPS 8
#else
#define RESAMP_NUM_TAPS 16
#endif

static float filter_taps[] = {
#ifdef USE_FAST_MATH
/*
Expand Down Expand Up @@ -143,11 +137,6 @@ void input_pdu_push(input_t *st, uint8_t *pdu, unsigned int len, unsigned int pr
output_push(st->output, pdu, len, program);
}

void input_rate_adjust(input_t *st, float adj)
{
st->resamp_rate += adj;
}

void input_set_skip(input_t *st, unsigned int skip)
{
st->skip += skip;
Expand Down Expand Up @@ -264,7 +253,6 @@ void input_cb(uint8_t *buf, uint32_t len, void *arg)
}
}
new_avail = st->avail;
resamp_q15_set_rate(st->resamp, st->resamp_rate);
#ifdef USE_THREADS
pthread_mutex_unlock(&st->mutex);
#endif
Expand All @@ -278,7 +266,6 @@ void input_cb(uint8_t *buf, uint32_t len, void *arg)

for (i = 0; i < cnt; i++)
{
unsigned int nw;
cint16_t x[2], y;

x[0].r = U8_Q15(buf[i * 4 + 0]);
Expand All @@ -287,9 +274,7 @@ void input_cb(uint8_t *buf, uint32_t len, void *arg)
x[1].i = -U8_Q15(buf[i * 4 + 3]);

firdecim_q15_execute(st->filter, x, &y);
resamp_q15_execute(st->resamp, &y, &st->buffer[new_avail], &nw);

new_avail += nw;
st->buffer[new_avail++] = y.r / 32768.0 + _Complex_I * y.i / 32768.0;
}

#ifdef USE_THREADS
Expand Down Expand Up @@ -318,7 +303,6 @@ void input_reset(input_t *st)
st->avail = 0;
st->used = 0;
st->skip = 0;
st->resamp_rate = 1.0f;
st->cfo = 0;
st->cfo_idx = 0;
st->cfo_used = 0;
Expand All @@ -339,7 +323,6 @@ void input_init(input_t *st, output_t *output, double center, unsigned int progr
st->snr_cb_arg = NULL;

st->filter = firdecim_q15_create(2, filter_taps, sizeof(filter_taps) / sizeof(filter_taps[0]));
st->resamp = resamp_q15_create(RESAMP_NUM_TAPS / 2, 0.45f, 60.0f, 16);
st->snr_fft = fftwf_plan_dft_1d(64, st->snr_fft_in, st->snr_fft_out, FFTW_FORWARD, 0);

input_reset(st);
Expand Down
4 changes: 0 additions & 4 deletions src/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "firdecim_q15.h"
#include "frame.h"
#include "output.h"
#include "resamp_q15.h"
#include "sync.h"

typedef int (*input_snr_cb_t) (void *, float, float, float);
Expand All @@ -24,8 +23,6 @@ typedef struct input_t
FILE *outfp;

firdecim_q15 filter;
resamp_q15 resamp;
float resamp_rate;
float complex *buffer;
double center;
unsigned int avail, used, skip;
Expand Down Expand Up @@ -55,7 +52,6 @@ typedef struct input_t
void input_init(input_t *st, output_t *output, double center, unsigned int program, FILE *outfp);
void input_cb(uint8_t *, uint32_t, void *);
void input_set_snr_callback(input_t *st, input_snr_cb_t cb, void *);
void input_rate_adjust(input_t *st, float adj);
void input_cfo_adjust(input_t *st, int cfo);
void input_set_skip(input_t *st, unsigned int skip);
void input_wait(input_t *st, int flush);
Expand Down
Loading