From 4b39365588206128e4d9324d36803ca6aa96dddd Mon Sep 17 00:00:00 2001 From: Christophe Jacquet Date: Sat, 5 Apr 2014 01:26:45 +0200 Subject: [PATCH] Fix filtering --- src/generate_waveforms.py | 8 ++++---- src/rds.c | 39 +++++++++++++++++++++++++++------------ src/rds_wav.c | 16 ++++++++++------ src/waveforms.c | 4 +--- src/waveforms.h | 3 +-- 5 files changed, 43 insertions(+), 27 deletions(-) diff --git a/src/generate_waveforms.py b/src/generate_waveforms.py index b7bbe67..9a38816 100755 --- a/src/generate_waveforms.py +++ b/src/generate_waveforms.py @@ -37,7 +37,7 @@ def generate_bit_in_context(pattern, name): shapedSamples = rds.unmodulated_signal(pattern, sample_rate) - out = shapedSamples[offset:offset+l*count] + out = shapedSamples #[offset:offset+l*count] iout = (out * 20000./max(abs(out)) ).astype(numpy.dtype('>i2')) wavfile.write(u"waveform_{}.wav".format(name), sample_rate, iout) @@ -46,11 +46,11 @@ def generate_bit_in_context(pattern, name): name = name, values = u", ".join(map(format_number, out)))) - outh.write(u"extern float waveform_{name}[];\n".format(name=name)) + outh.write(u"extern float waveform_{name}[{size}];\n".format(name=name, size=len(out))) -generate_bit_in_context([0, 0, 0], "identical") -generate_bit_in_context([0, 1, 0], "different") +generate_bit_in_context([1], "biphase") +#generate_bit_in_context([0, 1, 0], "different") outc.close() outh.close() \ No newline at end of file diff --git a/src/rds.c b/src/rds.c index 7d91ac9..437d858 100644 --- a/src/rds.c +++ b/src/rds.c @@ -41,6 +41,7 @@ struct { #define BITS_PER_GROUP (GROUP_LENGTH * (BLOCK_SIZE+POLY_DEG)) #define SAMPLES_PER_BIT 192 +#define FILTER_SIZE (sizeof(waveform_biphase)/sizeof(float)) uint16_t offset_words[] = {0x0FC, 0x198, 0x168, 0x1B4}; @@ -117,17 +118,20 @@ void get_rds_group(int *buffer) { void get_rds_samples(float *buffer, int count) { static int bit_buffer[BITS_PER_GROUP]; static int bit_pos = BITS_PER_GROUP; + static float sample_buffer[2*FILTER_SIZE] = {0}; static int prev_output = 0; static int cur_output = 0; static int cur_bit = 0; - static int sample_pos = SAMPLES_PER_BIT; - static float *current_waveform = NULL; + static int sample_count = SAMPLES_PER_BIT; static int inverting = 0; static int phase = 0; - + + static int in_sample_index = 0; + static int out_sample_index = FILTER_SIZE; + for(int i=0; i= SAMPLES_PER_BIT) { + if(sample_count >= SAMPLES_PER_BIT) { if(bit_pos >= BITS_PER_GROUP) { get_rds_group(bit_buffer); bit_pos = 0; @@ -138,18 +142,29 @@ void get_rds_samples(float *buffer, int count) { prev_output = cur_output; cur_output = prev_output ^ cur_bit; - // select appropriate waveform - current_waveform = (prev_output == cur_output) ? - waveform_identical : waveform_different; - - inverting = (prev_output == 0) ? 1 : -1; + inverting = (cur_output == 1) ? 1 : -1; + + float *src = waveform_biphase; + int idx = in_sample_index; + for(int j=0; j= 2*FILTER_SIZE) idx = 0; + } + in_sample_index += SAMPLES_PER_BIT; + if(in_sample_index >= 2*FILTER_SIZE) in_sample_index -= 2*FILTER_SIZE; bit_pos++; - sample_pos = 0; + sample_count = 0; //printf("%d", cur_bit); fflush(stdout); } - float sample = current_waveform[sample_pos] * inverting; + float sample = sample_buffer[out_sample_index++]; + if(out_sample_index >= 2*FILTER_SIZE) out_sample_index = 0; // modulate at 57 kHz // use phase for this @@ -163,7 +178,7 @@ void get_rds_samples(float *buffer, int count) { if(phase >= 4) phase = 0; *buffer++ = sample; - sample_pos++; + sample_count++; } } diff --git a/src/rds_wav.c b/src/rds_wav.c index dd624f4..1996a41 100644 --- a/src/rds_wav.c +++ b/src/rds_wav.c @@ -2,19 +2,23 @@ #include "rds.h" -#define LENGTH 99840 +#define LENGTH 100000 /* Simple test program */ int main(int argc, char **argv) { - set_rds_params(0x1234, "Hello"); + set_rds_params(0x1234, "Hello! World of the RaspberryPi!"); float buffer[LENGTH]; + int count = 0; - get_rds_samples(buffer, LENGTH); - - for(int j=0; j<50; j++) { + for(int j=0; j<20; j++) { + get_rds_samples(buffer, LENGTH); + + fprintf(stderr, "Iteration %d count=%d\n", j, count); + for(int i=0; i