HackRF Shell: Fun with filters

I had added bindings earlier for pulling data from the HackRF, and then more recently a binding to liquid-dsp frequency demodulation. But in between the two, filtering will be needed. So, I added a binding for the firdespm_lowpass function from liquid-dsp. This function gives you the coefficients for a Finite Impulse Response (FIR) filter, using the Parks-McClellan algorithm.

I wanted to see something interesting from that before going to bed, so I threw together a scheme procedure to generate and then print out the coefficients:

;; -*- geiser-scheme-implementation: guile -*-

;; Copyright 2020 Christopher Howard

;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

(define-module (sdr filters)
  #:export (print-firdespm-lowpass-coefficients))

(use-modules (rnrs bytevectors)
             (sdr liquid-dsp))

(define (print-firdespm-lowpass-coefficients filter-len
                                             cutoff-freq
                                             stop-band-atten
                                             fract-samp-offset)
  (let ((output-bv (make-bytevector (* 4 filter-len))))
    (firdespm-lowpass filter-len
                      cutoff-freq
                      stop-band-atten
                      fract-samp-offset
                      output-bv)
    (let lp ((coeff 0))
      (when (< coeff filter-len)
        (display
         (number->string
          (bytevector-ieee-single-ref output-bv (* 4 coeff) (native-endianness))))
        (display "\n")
        (lp (1+ coeff))))))

Then I dumped some coefficients to a file:

(with-output-to-file "out.csv"
  (lambda () (print-firdespm-lowpass-coefficients 201 0.2 60.0 0)))

Plotting out the coefficients, I got this visual:

I don’t really know enough yet to be sure if those are sane results or not, but it looks pretty. I was hoping to pick up a DSP book from the library sometime this week and dive more into the details of FIR filters.

Curseradio

No, I am not calling down an imprecation on radio technology. Rather, I want to highlight a great free software streaming radio system that uses a Curses-based interface:

Curseradio allows you to browse streaming radio stations listed in the opml.radiotime.com index. There is a large listing, and no proprietary software is required to view or to listen to the stations. Use arrow keys to select a station, “enter key” to start streaming, the “q” key to quit, and “k” to stop the streaming without quitting (i.e., pause).

In Guix, the command is “guix package -i curseradio” to install it. Note that a few days ago I had Guix fix a bug that was preventing the streaming from starting (a command path issue) so you may need to update your Guix channel, or cherry-pick Guix commit 8d8c6bf.

HackRF Shell Update, Explanations

What is the (current) goal of the HackRF Shell project?

I aim to provide a free software program which sets up a Guile Scheme shell, allowing lisp-style control of the HackRF Software Defined Radio (SDR). The shell will provide procedures for (1) controlling the HackRF, including pulling from and pushing data to the radio, (2) Digital Signal Processing (DSP) e.g., frequency modulation, and (3) some i/o interfaces, specifically to the file system and to PulseAudio.

Why does this project exist?

Partly to help me learn about SDR and DSP. Also, I want to be able to code applications for HackRF SDR in Scheme, rather than in Python (Gnu Radio).

Wouldn’t it be better to use Guile FFI than embedding Guile in a C program?

Erm, well, maybe. But the embedded approach seems easy to understand, develop, and troubleshoot.

Shouldn’t you make something modular to work with any SDR?

I wanted to focus on getting somewhere practical with the SDR I have and the code I already wrote. But I believe it would be possible to expand the shell by adding or swapping in osmocom bindings instead of just the libhackrf bindings.

What have you done so far?

  • Created the C program with libhackrf bindings
  • Set up autotools files for development
  • Created Guix package definitions for HackRF Shell as well as the hackrf and liquid-dsp libraries.

What are you working on now?

Adding liquid-dsp bindings.

Where is the code?

git clone git://git.librehacker.com/pub/git/hackrf-rkt.git

Why has it taken you so long to make progress on this project?

  • Toddlers
  • Moving to a new apartment.
  • A job with no PTO.

What makes Guix such an awesome development environment?

Once you’ve made package definitions, which isn’t very hard, you can set up your development environment (exposed packages and environment variables) quite easily. Right now, for HackRF I simple cd into my source code and run

guix environment hackrfshell --ad-hoc liquid-dsp

The I run ./configure, make, etc. In the next iteration of the hackrfshell package definition, I will add liquid-dsp as a dependency, so then it would just be guix environment hackrfshell.

Not all my package definitions have made it into the official Guix git repository yet, but I’ve added them to my local Guix channel for the same effect.

Also because of Guix’s functional package management approach, I can use any verions of libhackrf, etc. I want, and not have to worry about wrestling with base system dependencies.

liquid-dsp guix package definition

;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2019 Christopher Howard <christopher@librehacker.com>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gnu packages liquid-dsp)
  #:use-module ((guix licenses) #:prefix license:)
  #:use-module (guix packages)
  #:use-module (guix utils)
  #:use-module (guix download)
  #:use-module (guix git-download)
  #:use-module (guix build-system gnu)
  #:use-module (gnu packages)
  #:use-module (gnu packages algebra)
  #:use-module (gnu packages autotools))

(define-public liquid-dsp
  (package
    (name "liquid-dsp")
    (version "1.3.2")
    (source
     (origin (method git-fetch)
             (uri (git-reference
                   (url (string-append "https://github.com/jgaeddert/liquid-dsp.git"))
                   (commit (string-append "v" version))))
             (file-name (git-file-name name version))
             (sha256
              (base32 "1n6dbg13q8ga5qhg1yiszwly4jj0rxqr6f1xwm9waaly5z493xsd"))))
    (build-system gnu-build-system)
    (native-inputs
     `(("autoconf" ,autoconf)))
    (inputs
     `(("fftw" ,fftw)
       ("fftwf" ,fftwf)))
    (home-page "https://liquidsdr.org")
    (synopsis "Signal processing library for software-defined radios written in C")
    (description
     "Liquid DSP provides a set of extensible DSP modules that do not
rely on external dependencies or cumbersome frameworks")
    (license license:expat)))

Here is the guix patch request:

https://debbugs.gnu.org/cgi/bugreport.cgi?bug=38865

HackRF Shell Guix Definition

;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2019 Christopher Howard <christopher@librehacker.com>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gnu packages hackrfshell)
  #:use-module (guix)
  #:use-module ((guix licenses) #:prefix license:)
  #:use-module (guix packages)
  #:use-module (guix utils)
  #:use-module (guix download)
  #:use-module (guix git-download)
  #:use-module (guix build-system gnu)
  #:use-module (gnu packages)
  #:use-module (gnu packages algebra)
  #:use-module (gnu packages glib)
  #:use-module (gnu packages guile)
  #:use-module (gnu packages hackrf)
  #:use-module (gnu packages pkg-config))

(define-public hackrfshell
  (package
    (name "hackrfshell")
    (version "0.0.1")
    (source
     (origin (method url-fetch)
             (uri (string-append
                   "ftp://git.librehacker.com/hackrfshell/hackrfshell-"
                   version ".tar.gz"))
             (file-name (string-append "hackrfshell-" version "-tar.gz"))
             (sha256
              (base32 "0iqnd1jbhq5wjrp30d9i0yy9jqshyy00nzsmh78dnxp1yjm2wppa"))))
    (build-system gnu-build-system)
    (native-inputs
     `(("pkg-config" ,pkg-config)))
    (inputs
     `(("hackrf" ,hackrf)
       ("glib" ,glib)
       ("guile-2.2" ,guile-2.2)
       ("fftwf", fftwf)))
    (home-page "https://librehacker.com")
    (synopsis
     "Program which loads HackRF control procedures into a Scheme shell")
    (description
     "This is a C program loads up a Guile Scheme shell with primitive
HackRF control functions, as well as some other useful core functions
for Software Defined Radio (SDR) data processing.  This is something
that maybe could be done with a FFI interface, but this approach works
also and arguably has one or two advantages.")
    (license license:gpl3+)))