Software Radio with CTypes

T M Winningham

July 27, 2014

In this talk

Device – What it is

“RTL-SDR is a very cheap software defined radio that uses a DVB-T TV tuner dongle based on the RTL2832U chipset.”

rtl-sdr.com

radiohobbyist.org

Device – Definitions

Software Defined Radio

Digital sampling or synthesis of electromagnetic radation using software at a specific center frequency and again 90° out of phase, allowing for variable modes and bandwith

Digital Video Broadcasting – Terrestrial (DVB-T)

European Standard for broadcast digital terrestrial television

Device – SDR Capability Discovery

Device – Uses

Device – Limitations

Device – Software – GQRX

Device – Software – librtlsdr

Command line tools

rtl_sdr, rtl_test, rtl_tcp

Raw data, device testing, network sharing

rtl_fm, rtl_power, rtl_adsb

Audio or digital signals, spectrum scanning, aircraft tracking

Device – Find out more

RTL_FM_PYTHON – What it is

RTL_FM_PYTHON – Python Precedents

Process – Niche

Process – Target Program

RTL_FM

Process – Target Program Example

rtl_fm -M wbfm -f 101.1M - |aplay -r 32000 -f S16_LE -t raw -c 1 

Process – Reading rtl_fm.c

Process – Reading rtl_fm.c

void usb_demod(struct demod_state *fm)
{
    int i, pcm;
    int16_t *lp = fm->lowpassed;
    int16_t *r  = fm->result;
    for (i = 0; i < fm->lp_len; i += 2) {
        pcm = lp[i] + lp[i+1];
        r[i/2] = (int16_t)pcm * fm->output_scale;
    }
    fm->result_len = fm->lp_len/2;
}

Process – Re-arranging

some_type some_function (some_type some_parameter) {
   do_a_bunch_of_stuff(some_parameter);
   do_more_stuff();
   do_stuff_i_dont_understand();
   finish_up();
}

to:

some_type some_new_function(){
   do_more_stuff();
}

Process – Compilation

gcc -I /usr/include/libusb-1.0 
    -I ./convenience/ 
    -I ./getopt 
    -shared 
    -Wl,-soname,rtl_fm_python 
    -o rtl_fm_python.so 
    -fPIC 
    rtl_fm_python.c 
    convenience/convenience.c getopt/getopt.c 
    -lrtlsdr

Process – Iteration

Process – Composing Simple Blocks

uint32_t lib_get_frequency(){
    return controller.freqs[controller.freq_now];
}

Process – Python CTypes

fm  = ctypes.CDLL('./rtl_fm_python.so')
get_s_level     = fm.lib_get_s_level
get_frequency   = fm.lib_get_frequency
set_demod_fm    = fm.lib_set_demod_fm
set_demod_wbfm  = fm.lib_set_demod_wbfm
set_demod_am    = fm.lib_set_demod_am
set_demod_lsb   = fm.lib_set_demod_lsb
set_demod_usb   = fm.lib_set_demod_usb
set_demod_raw   = fm.lib_set_demod_raw
set_frequency   = lambda f : fm.lib_set_frequency(ctypes.c_uint32(f))
set_squelch     = lambda l : fm.lib_set_squelch_level(ctypes.c_int(l))
get_demod       = lambda   : chr(fm.lib_get_demod_mode())
str_to_freq     = fm.lib_frequency_convert

Process – Achievements

Process – Future

Demo