Scientific Computing

Raspberry Pi radio IQ transmit

As a radio scientist, I need software defined radio (SDR) transmitters that ingest raw IQ (in-phase, quadrature) data. IQ data allows transmitting anything within the instantaneous bandwidth and dynamic range limitations of the particular SDR.

IQ RF Transmit

The rpitx program by F5OEO ingests IQ data (and has utilities to convert from audio to popular analog formats such as SSB, AM, and FM voice) to transmit on air from the Raspberry Pi onboard PLL via PWM accessed on the GPIO 12 pin using DMA.

The Raspberry Pi BCM2835 PLL PLLD operates at 500 MHz, decimated down to the carrier frequency desired and modulated, with claimed resolution ~16 bit.

Install rpitx at Raspberry Pi Terminal:

apt install git

git clone https://github.com/F5OEO/rpitx

cd rpitx
./install.sh

Run rpitx IQ transmitter:

rpitx -i myfile.bin -m iq -f 97500

This command transmits single-precision complex64 IQ file myfile.bin at 97.5 MHz center frequency (consider the error in the Raspberry Pi PLL baseclock).

F5OEO notes live video streaming 64 k-symbol/sec - 4 M-symbol/sec DVS-B using rpidatv, which sends DVS-B HD video receivable by the $20 RTL-SDR dongles.

With the Raspberry Pi 2 collecting samples at 2.048 MS/s, about 3% of one CPU core is used. This does not include demodulation, just passing samples. After installing rtl-sdr, you can playback over the Raspberry Pi headphone jack using aplay.

Radio receiver examples

99.5 FM broadcast, in stereo on the Raspberry Pi:

rtl_fm -f 99.5M -M wbfm | aplay -r 24k -f S16_LE -t raw -c 1

162.475 MHz NOAA weather radio (NBFM)

rtl_fm -f 162.475M -M fm -s 64k -Afast -r 32k -E demp | aplay -r 24k -f S16_LE -t raw -c 1

CPU Utilization in percent:

Rpi Model Stream 2.048 MS/s, no demod WBFM playback over headphone jack
2 3.0 12.0

Double precision complex in f2py

f2py and Complex*16 double precision

A subtle yet critical problem using double precision complex numbers can arise between Fortran and Python using f2py. f2py doesn’t seem to understand the kind parameters we specify when assigning a variable. f2py assumes your complex numbers are single precision (8 bytes per complex number) instead of double precision (16 bytes per complex number).

You won’t get an error on compiling, but your Python program importing the Fortran .so module will give erroneous results.

Solution

Assuming the program has

use, intrinsic :: iso_c_binding, only: sp=>C_FLOAT, dp=>C_DOUBLE

Assign a double-precision complex variable like

complex(dp) :: x

Copy file .f2py_f2cmap into the top directory of your Python project (where setup.py is), containing:

dict(real= dict(sp='float', dp='double'),
complex = dict(sp='complex_float',dp="complex_double"))

Notes

At this time it appears we don’t use the C_DOUBLE_COMPLEX from iso_c_binding, I notice that F90Wrap does the same.

Quick Charge 3.0 USB power analyzer

Upon plugging USB power from the Aukey Quick Charge 3.0 charger into the Quick Charge 2.0 capable device, a small fraction of a second is spent at 5 V input, while the Quick Charge 2.0 rate is negotiated. Starting from 20% charge, in less than 2 hours the device is completely charged. Battery charge of 20% → 60% is achieved in 40 minutes.

Quick Charge 2.0 voltage/current vs time: the table below is approximate, with the display off. At 100% charge, the rate bounced between 0.02-0.2 amps as the phone pings Wifi, checks for email, etc. Most of the time, it was at about 50 mA.

time [minutes] Charge % Input [Volts] Current [Amps]
0 20 9.24 1.65
24 45 9.18 0.92
44 65 9.18 0.93
100 95 9.16 0.55
120 100 9.13 0.05, peak 0.2

Science Magazine on Open Data

Comments on June 2014 AAAS Science article on open data and open code for scientists are given below. The article refers to the E. White paper on nine easy ways to share code.

Maybe you don’t have the time to read those, so let me give you six quick tips to:

  • increase your citation count
  • increase your data usage (more citations)
  • increase your code sharing (more citations and lucrative job opportunities).
  1. Learn a popular science/engineering programming language. It will boost productivity and job opportunities. The language you should be using is Python, as it can run on supercomputers down to the $5 Raspberry Pi Zero. Python can streamline/inline CUDA, Fortran, and C for very fast speed while being easy to code for controlling hardware and doing simulations and image/data analysis of datasets of all sizes.
  2. Learn how to use HDF5. Try to use it right at your data acquisition source if it’s not a very high data rate application. Try to avoid making up your own formats or using text to store data unless completely necessary.
  3. Learn how to use version control. Git is an excellent choice. You will save massive amounts of time when it comes to some typo you made and you didn’t keep an old filename version.
  4. Learn how to use Github and be appropriately prolific about posting your code there. This leads to visibility and opportunity.
  5. Put examples, plots, and documentation of how to install and use your code in a README file.
  6. Publish your data online. Don’t leave it on some RAID drive or USB drive somewhere. The drive will fail eventually or the room will get flooded. Worst of all is CD/DVD, they have a very short lifespan, less that the length of your PhD studies. Keep your data in multiple online places.

Black screen after installing more RAM

After installing more RAM the laptop turned on to a black screen. There was no response to Fn keys, no Fn Lock LED, no mute LED, couldn’t get the keyboard to light up beyond the initial flareup/flaredown. After 45 seconds and the system restarted normally. It seems there’s some kind of self-check the laptop runs once upon RAM size change. Too bad they don’t put a “RAM testing” notice on the screen to avoid panic of the user!

Windows VirtualBox guest Shared Folders

If a VirtualBox virtual machine can’t see Shared Folders in your Windows guest with Linux host, fix by:

In Linux Terminal:

adduser $(whoami) vboxusers

In Virtualbox Windows guest, click Install Guest Additions in the VirtualBox menu Run setup on the virtual drive that appears. Reboot the Windows guest. From Windows guest Command Prompt:

net use x: \\vboxsvr\share

where “share” is the name assigned in the VirtualBox shared folders dialog.

Rescue corrupt IDL .sav files to NetCDF or HDF5

IDL has been buggy in the past for writing .sav` files in the proprietary data format. One should instead save data to NetCDF4 or HDF5.

Read IDL .sav files from Python:

from scipy.io import readsav

data = readsav('mydata.sav')
data
dict() of IDL variables.

Our Scipy contribution: IDL was able to read the .sav file, so I wrote a one-off IDL script to convert from IDL .sav to NetCDF4 .NC: sav2nc.pro format. I also have code there to convert from IDL .sav to HDF5.

I also successfully submitted a patch to SciPy to fix some of these IDL .sav reading issues.

Bluetooth headsets not recognized in Pulseaudio

Fix Bluetooth no audio: force Bluetooth headset/speaker to be recognized in Linux sound control panel with

pactl load-module module-bluetooth-discover

Force A2DP/HSP audio mode by opening Pulseaudio control panel:

pavucontrol

Under “Configuration” select Bluetooth device audio mode:

Profile audio type direction
A2DP high-fidelity 0-24 kHz stereo one way playback
HSP low-fidelity 0-4 kHz monaural two way talk/listen for Skype et al

reference

Read THEMIS all-sky imager ASI data

THEMIS uses the CDF (Common Data Format) for many L1 data products.

CDF is NOT the same as NetCDF, so programs such as ncBrowse and ncview will NOT work with Themis CDF files.

Download THEMIS L1 CDF file by first looking at the L0 64x64 pixel GIF previews.

Matlab and Python functions for THEMIS ASI images make movies and the like.