From a9d66c6a759515c7061e2e8aac661eeb0082ea1e Mon Sep 17 00:00:00 2001 From: FreeArtMan Date: Wed, 30 Dec 2015 19:20:20 +0000 Subject: More configurable sources. Move some math functions to core/math.h. Move delay filter from sdr_fm --- Makefile | 3 +- README.md | 6 +++- config/config_bsd.h | 4 +-- config/config_linux.h | 4 +-- core/make.mk | 11 ++++++ core/math.c | 48 +++++++++++++++++++++++++ core/math.h | 16 +++++++++ draw/glui.c | 4 +-- draw/glui.h | 4 +-- filt/f_5th.c | 32 ----------------- filt/filt.h | 7 +++- filt/filt_5th.c | 32 +++++++++++++++++ filt/filt_delay.c | 51 +++++++++++++++++++++++++++ filt/make.mk | 2 +- hw/aud.c | 2 +- hw/aud.h | 4 +-- radiola.c | 9 ++++- test/Makefile | 2 +- test/sdr_fm.c | 94 ++----------------------------------------------- test/ui_gl_waterfall.c | 5 ++- test/ui_tui_waterfall.c | 16 ++++----- 21 files changed, 207 insertions(+), 149 deletions(-) create mode 100644 core/make.mk create mode 100644 core/math.c create mode 100644 core/math.h delete mode 100644 filt/f_5th.c create mode 100644 filt/filt_5th.c create mode 100644 filt/filt_delay.c diff --git a/Makefile b/Makefile index b8a6721..d347f98 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,7 @@ SOURCES= OBJECTS= OBJECTS_FINAL= +include core/make.mk include draw/make.mk include filt/make.mk include hw/make.mk @@ -50,4 +51,4 @@ menuconfig-bsd: kconf2h bsd: $(OBJECTS) $(CC) $(OBJECTS_FINAL) $(PROJECT).c -o $(PROJECT) $(LDFLAGS_BSD) - ld -r $(OBJECTS_FINAL) -o $(PROJECT).o \ No newline at end of file + ld -r $(OBJECTS_FINAL) -o $(PROJECT).o diff --git a/README.md b/README.md index b0e125e..0d4eecf 100644 --- a/README.md +++ b/README.md @@ -12,4 +12,8 @@ rtlsdr based sdr software, early prototype ## graphical waterfall -![graphical waterfall](test/ui_gl.png) \ No newline at end of file +![graphical waterfall](test/ui_gl.png) + +## Compile for *BSD + +make CFLAGS=-DCONFIG_OS_NETBSD bsd diff --git a/config/config_bsd.h b/config/config_bsd.h index a54486f..f74e647 100644 --- a/config/config_bsd.h +++ b/config/config_bsd.h @@ -1,5 +1,5 @@ -#ifndef __CONFIG_H -#define __CONFIG_H +#ifndef __CONFIG_BSD_H +#define __CONFIG_BSD_H #define CONFIG_CORE #define CONFIG_OS_NETBSD #define CONFIG_DRAW diff --git a/config/config_linux.h b/config/config_linux.h index 5fc9189..51c4a13 100644 --- a/config/config_linux.h +++ b/config/config_linux.h @@ -1,5 +1,5 @@ -#ifndef __CONFIG_H -#define __CONFIG_H +#ifndef __CONFIG_LINUX_H +#define __CONFIG_LINUX_H #define CONFIG_CORE #define CONFIG_OS_LINUX #define CONFIG_DRAW diff --git a/core/make.mk b/core/make.mk new file mode 100644 index 0000000..a1fe655 --- /dev/null +++ b/core/make.mk @@ -0,0 +1,11 @@ +DIR_CORE = core/ +SOURCES_CORE += core/math.c +OBJECTS_CORE += $(SOURCES_CORE:.c=.o) +LDFLAGS += -lm +LDFLAGS_BSD += -lm + + +OBJECTS_DIR_CORE += $(subst $(DIR_CORE),$(BUILD_DIR)$(DIR_CORE),$(OBJECTS_CORE)) + +OBJECTS += $(OBJECTS_CORE) +OBJECTS_FINAL += $(OBJECTS_DIR_CORE) \ No newline at end of file diff --git a/core/math.c b/core/math.c new file mode 100644 index 0000000..f962534 --- /dev/null +++ b/core/math.c @@ -0,0 +1,48 @@ +#include "math.h" + + +float to_float(uint8_t x) +{ + return (1.0f/127.0f)*(((float)x)-127.0f); +} + + +//float ph_ch( uint8_t i1, uint8_t q1, uint8_t i2, uint8_t q2) +float ph_ch( uint8_t i1, uint8_t q1 ) +{ + static float complex last=0.0+0.0i; + float out; + float complex xy,c1; + //float c2; + + c1 = to_float(i1) + I*to_float(q1); + //c1 = CMPLXF( to_float(i1), to_float(q1) ); + //c2 = to_float(i2) + I*to_float(q2); + //c2 = CMPLXF( to_float(i2), to_float(q2) ); + xy = conjf(last)*c1; + out = cargf( xy ); + last = c1; + + return out; +} + +void rotate_90(uint8_t *buf, uint32_t len) +/* 90 rotation is 1+0j, 0+1j, -1+0j, 0-1j + or [0, 1, -3, 2, -4, -5, 7, -6] */ +{ + uint32_t i; + uint8_t tmp; + for (i=0; i +#include +#include +#include +#include + +float to_float(uint8_t x); + +float ph_ch( uint8_t i1, uint8_t q1 ); + +void rotate_90(uint8_t *buf, uint32_t len); + +#endif \ No newline at end of file diff --git a/draw/glui.c b/draw/glui.c index e056324..0f1e360 100644 --- a/draw/glui.c +++ b/draw/glui.c @@ -1,6 +1,6 @@ #include "glui.h" -#if defined(OS_LINUX) +#if def(OS_LINUX) #define DEFAULT_TITLE "RADIOLA" #define SCREEN_X 1024 @@ -171,4 +171,4 @@ int glui_close( glui_t *t ) return ret; } -#endif \ No newline at end of file +#endif diff --git a/draw/glui.h b/draw/glui.h index 17c49da..9206bdf 100644 --- a/draw/glui.h +++ b/draw/glui.h @@ -9,7 +9,7 @@ #include #include -#if defined(OS_LINUX) +#if def(OS_LINUX) #include @@ -60,4 +60,4 @@ glui_color_t glui_waterfall_color( uint8_t d ); //close terminal ui int glui_close( glui_t *t ); #endif -#endif \ No newline at end of file +#endif diff --git a/filt/f_5th.c b/filt/f_5th.c deleted file mode 100644 index 811fb18..0000000 --- a/filt/f_5th.c +++ /dev/null @@ -1,32 +0,0 @@ -#include "filt.h" - -void f_5th(int16_t *data, int length, int16_t *hist) -/* for half of interleaved data */ -{ - int i; - int16_t a, b, c, d, e, f; - a = hist[1]; - b = hist[2]; - c = hist[3]; - d = hist[4]; - e = hist[5]; - f = data[0]; - /* a downsample should improve resolution, so don't fully shift */ - data[0] = (a + (b+e)*5 + (c+d)*10 + f) >> 4; - for (i=4; i> 4; - } - /* archive */ - hist[0] = a; - hist[1] = b; - hist[2] = c; - hist[3] = d; - hist[4] = e; - hist[5] = f; -} \ No newline at end of file diff --git a/filt/filt.h b/filt/filt.h index d235449..9ce2caa 100644 --- a/filt/filt.h +++ b/filt/filt.h @@ -3,9 +3,14 @@ #include #include +#include +#include #include //fifth order filter from rtlsdr -void f_5th(int16_t *data, int length, int16_t *hist); +void filt_5th(int16_t *data, int length, int16_t *hist); + +//delay filtering from https://github.com/dpiponi/mini_fm.git +void filt_delay( uint8_t *buf, int buf_len ); #endif \ No newline at end of file diff --git a/filt/filt_5th.c b/filt/filt_5th.c new file mode 100644 index 0000000..23d5aab --- /dev/null +++ b/filt/filt_5th.c @@ -0,0 +1,32 @@ +#include "filt.h" + +void f_5th(int16_t *data, int length, int16_t *hist) +/* for half of interleaved data */ +{ + int i; + int16_t a, b, c, d, e, f; + a = hist[1]; + b = hist[2]; + c = hist[3]; + d = hist[4]; + e = hist[5]; + f = data[0]; + /* a downsample should improve resolution, so don't fully shift */ + data[0] = (a + (b+e)*5 + (c+d)*10 + f) >> 4; + for (i=4; i> 4; + } + /* archive */ + hist[0] = a; + hist[1] = b; + hist[2] = c; + hist[3] = d; + hist[4] = e; + hist[5] = f; +} diff --git a/filt/filt_delay.c b/filt/filt_delay.c new file mode 100644 index 0000000..53da0b0 --- /dev/null +++ b/filt/filt_delay.c @@ -0,0 +1,51 @@ +#include "filt.h" + +//delay filtering +void filt_delay( uint8_t *buf, int buf_len ) +{ + //delay length + const int n=10; + + int i=0; + uint32_t cycle=0; + uint32_t avg_i=0, avg_q=0; + uint32_t cyc_buffer_i[n],delay_i=0; + uint32_t cyc_buffer_q[n],delay_q=0; + uint8_t in1=0,in2=0,out1=0,out2=0; + + memset( cyc_buffer_i, 0, n*sizeof(uint32_t) ); + memset( cyc_buffer_q, 0, n*sizeof(uint32_t) ); + + //for (i=0; i<(buf_len-(n*2));i+=2) + for (i=0 ; i<(buf_len-1) ; i+=2 ) + //for (i=0; i<1000; i+=2) + { + in1 = buf[i]; + in2 = buf[i+1]; + + //average + avg_i += (uint32_t)in1; + avg_q += (uint32_t)in2; + + delay_i = cyc_buffer_i[cycle]; + delay_q = cyc_buffer_q[cycle]; + + cyc_buffer_i[cycle] = avg_i; + cyc_buffer_q[cycle] = avg_q; + cycle = cycle + 1; + if ( cycle >= n ) + { + cycle = 0; + } + + out1 = (avg_i - delay_i)/n; + out2 = (avg_q - delay_q)/n; + + buf[i] = out1; + buf[i+1] = out2; + + //printf("%d,avg=[%d,%d],delay=[%d,%d],in=[%d,%d],out=[%d,%d]\n", + // cycle, avg_i,avg_q,delay_i,delay_q,in1,in2,out1,out2); + + } +} \ No newline at end of file diff --git a/filt/make.mk b/filt/make.mk index bde2fe3..be7468f 100644 --- a/filt/make.mk +++ b/filt/make.mk @@ -1,5 +1,5 @@ DIR_FILT = filt/ -SOURCES_FILT += filt/f_5th.c +SOURCES_FILT += filt/filt_5th.c filt/filt_delay.c OBJECTS_FILT += $(SOURCES_FILT:.c=.o) LDFLAGS += LDFLAGS_BSD += diff --git a/hw/aud.c b/hw/aud.c index 4c51dcc..ade60cb 100644 --- a/hw/aud.c +++ b/hw/aud.c @@ -1,6 +1,6 @@ #include "aud.h" -#if defined(OS_LINUX) +#if def(OS_LINUX) //harc-copy from aplay.c static void device_list(void) int audio_get_devices() { diff --git a/hw/aud.h b/hw/aud.h index 396c27d..974646a 100644 --- a/hw/aud.h +++ b/hw/aud.h @@ -6,9 +6,9 @@ #include #include -#if defined(OS_LINUX) +#if def(OS_LINUX) #include #endif int audio_get_devices(); -#endif \ No newline at end of file +#endif diff --git a/radiola.c b/radiola.c index c7279fa..aaaf047 100644 --- a/radiola.c +++ b/radiola.c @@ -6,5 +6,12 @@ int main() { +#if def(OS_LINUX) + printf("Compiled in Linux comp mode\n"); +#elif def(OS_NETBSD) + printf("Compiled in NetBSD comp mode\n"); +#else + printf("Unknown mode\n"); +#endif return 0; -} \ No newline at end of file +} diff --git a/test/Makefile b/test/Makefile index c447b55..05714ae 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,5 +1,5 @@ CC=gcc -CFLAGS=-std=gnu11 -I../ +CFLAGS=-I../ #LDFLAGS= `pkg-config --libs libusb` -L../../../r820t -lr820t -Wl,-rpath=../../../r820t LDFLAGS=`pkg-config --libs libusb` `sdl2-config --cflags --libs` -lrtlsdr -lm ../radiola.o -lasound SOURCE = $(wildcard *.c) diff --git a/test/sdr_fm.c b/test/sdr_fm.c index a7f5551..4fc4c98 100644 --- a/test/sdr_fm.c +++ b/test/sdr_fm.c @@ -8,8 +8,10 @@ #include +#include #include #include +#include #define SAMPLE_RATE 1000000 #define RESAMPLE_RATE 50000 @@ -149,99 +151,9 @@ uint8_t super_div20u8( uint16_t num ) return (uint8_t)r; } -void rotate_90(uint8_t *buf, uint32_t len) -/* 90 rotation is 1+0j, 0+1j, -1+0j, 0-1j - or [0, 1, -3, 2, -4, -5, 7, -6] */ -{ - uint32_t i; - uint8_t tmp; - for (i=0; i= n ) - { - cycle = 0; - } - - out1 = (avg_i - delay_i)/n; - out2 = (avg_q - delay_q)/n; - - buf[i] = out1; - buf[i+1] = out2; - - //printf("%d,avg=[%d,%d],delay=[%d,%d],in=[%d,%d],out=[%d,%d]\n", - // cycle, avg_i,avg_q,delay_i,delay_q,in1,in2,out1,out2); - - } -} int main( int argc, char **argv ) { @@ -334,7 +246,7 @@ int main( int argc, char **argv ) // fbuf[i] = to_float(sample_buf[i]); //} //delay boxed filter - delay_filt( sample_buf, sample_len ); + filt_delay( sample_buf, sample_len ); //rotate by 90 degrees uint8_t diff --git a/test/ui_gl_waterfall.c b/test/ui_gl_waterfall.c index 837383f..33fbdb6 100644 --- a/test/ui_gl_waterfall.c +++ b/test/ui_gl_waterfall.c @@ -6,6 +6,9 @@ #include //radiola +//#define CONFIG_OS_LINUX +//#include +//#include #include #include #include @@ -329,4 +332,4 @@ main_exit: sdr_close( sdr ); return 0; -} \ No newline at end of file +} diff --git a/test/ui_tui_waterfall.c b/test/ui_tui_waterfall.c index cc63fc3..82362c1 100644 --- a/test/ui_tui_waterfall.c +++ b/test/ui_tui_waterfall.c @@ -8,14 +8,14 @@ #include #include -#define SAMPLE_RATE 2048000 - -#define CENTER_FREQ 445500000 -#define FFT_LEVEL 10 -#define FFT_SIZE (1 << FFT_LEVEL) -#define SAMPLE_LENGHT (2 * FFT_SIZE) -#define PRESCALE 8 -#define POSTSCALE 2 +#define SAMPLE_RATE 2048000 + +#define CENTER_FREQ 445500000 +#define FFT_LEVEL 10 +#define FFT_SIZE (1 << FFT_LEVEL) +#define SAMPLE_LENGHT (2 * FFT_SIZE) +#define PRESCALE 8 +#define POSTSCALE 2 int16_t* Sinewave; -- cgit v1.2.3