ASCIIMath creating images

Thursday, July 2, 2015

A bit of Python path hackery

Like many people, I have a directory in $HOME for useful python code I write. Similar to MATLAB's "Documents/MATLAB" directory, I want those files to be easily available if I'm hacking new stuff. The typical way of handling this is to do:

import sys
but that is a bit ugly, and has the problem that if I'm doing an IPython Notebook (as I often do), this append function gets reevaluated if I reexecute the cell in which I do all my imports (since as I'm edition the notebook and adding stuff, I would do a lot). Besides I now have sys in my namespace.

I could just dump it into ".local/lib/python3.4/site-packages" (too hidden, outside the tree that is synced between machines), or link "Documents/Python" to that path, but then I don't want to have pip install stuff there, and also, there might be times I don't want "Documents/Python" to be in the path.

So here's my solution.  I place a file in ".local/lib/python3.4/site-packages" and ".local/lib/python2.7/site-packages" with a name like "", and this file contains

import sys

myPythonDir = '/home/jthiem/Documents/Python'
if myPythonDir not in sys.path:
I can now do
import LocalPath
and have instant no-fuss access to my homebrew packages.  Now isn't that nice.


It's becoming a habit: I'm going to EUSIPCO again.  They seem to have a preference for the sunnier parts of Europe (fine by me!) so this time I'm off to Nice, France, where I will be presenting some of my current work on CASA for hearing devices.  While what I'll be presenting is just a small part of the whole, it's an important initial result: evaluating the features I'm extracting from the hearing aid microphones to use for localization of sources.  I'll be presenting my paper "Features for Speaker Localization in Multichannel Bilateral Hearing Aids" as part of the Acoustic scene analysis using microphone array special session, on Wednesday at 14:30.  (A bit of a pity, I would have liked to attend the "Audio and speech source separation and enhancement" session as well)

I'll be going straight to Nice after visiting the in-laws in Canada, so it'll be quite the trip.  Hope to see you there!

Friday, November 28, 2014

The new and improved method of real-time MIDI control of MATLAB (or Python, or ... whatever!)

In an older post on my blog, I had this method to get the state of a MIDI controller into MATLAB (though readable by pretty much anything that can read files).  This method is quite clunky and when I wanted to do something similar again, I re-thought the problem and came up with a better method based on memory mapped files. You can find the code on GitHub.

Basically, I'm creating a file of fixed length (260 bytes, in /tmp), mmap() it and update the contents based on the MIDI stream I'm receiving.  The first 128 bytes are for keys, where each byte is the last seen velocity (0 means "off").  The following 128 bytes are for the cc messages.  Bytes 257 and 258 store the pitch bend, a 14-bit value with MSB in byte 257.  Byte 259 is the last received program change value and 260 is the channel aftertouch.


Accessing the values from MATLAB does not require any special functions, it can simply be done using

mm = memmapfile('/tmp/midibroadcast');

then the data can be obtained in real-time from mm.Data.  As described above, mm.Data(1:128) are the last seen key down velocities (0 meaning key is not pressed) and mm.Data(129:256) are controllers 0..127.  The pitch bend value can be obtained as mm.Data(257)*256+mm.Data(258) (note this may be buggy; my cheap controller (KeyRig 25) does not let me set and hold precise pitch bends, so I can't test it properly). mm.Data(259) is the program change and mm.Data(260) shows the current aftertouch amount.

Python Access

From Python the values can be accessed as easily:

import os
import mmap
mfd ='/tmp/midibroadcast', os.O_RDONLY)
mfile = mmap.mmap(mfd, 0, prot=mmap.PROT_READ)

Remembering that Python counts from 0, the controllers can be read in real-time as mfile[128] to mfile[255]: just subtract 1 from the descriptions above.

Any language which can do memory-mapping should be able to do the same, but it should even be possible to read the current state just by re-reading the /tmp/midibroadcast file.


Sunday, October 5, 2014

Using an ARM Chromebook for Scientific (and Academic) Computing

Samsung ARM Chromebook
A couple of months ago, I decided to get myself a Chromebook. The Samsung ARM Chromebook is cheap (to the point of being almost disposable) and it's got an ARM CPU - and for performance at the least possible amount of juice it's hard to beat. I really like the fact that this thing emits no noise that I can detect even in a very quiet room.

But how useful is it, for someone in a standard engineering/academic setting? The answer is that it works well, for me at least - with some special considerations. Especially for the last few weeks, it has been my primary laptop, having been dragged to research cluster meetings and one conference. I will explain the details of a few typical things I do, such as (LaTeX) document editing, intensive numerical computation, etc. Read below the break for details.  

Wednesday, September 24, 2014

Transplant: yet another bridge between MATLAB and Python, but a good one!

This post is basically just an advertisement for a project done by a Master's student that I'm co-supervising at the moment.  While there are numerous methods already out there to link MATLAB to Python (and rumour has it that the next(?) release will make it easier to call Python from MATLAB), I think Basti's "transplant" (github link) strikes a good balance between simplicity and capability.  Bastian's code is elegant and reliable.  My own contribution has just been a small bug fix, the ability to transfer logical (boolean) matrices, and an attempt to add the ability to capture MATLAB's stdout (Bastian came up with a much better solution).

For me the resulting killer feature is that I can write IPython notebooks that call MATLAB code in a sane way.  Complex code which would take too much effort to convert to Python can be called, then the results can be plotted in the Notebook, which is great when working remotely (a longer post on my workflow is in the works...).  Results become more accessible, with a lot of the complexity hidden away in .m files.

So, check it out and spread the word!

Friday, July 4, 2014

My workspace for the next month or two

The two arc source positioning system in the anechoic chamber
of the Carl-von-Ossietzky University of Oldenburg
Yeah, I'll be doing HRTF measurements on dummy heads with multichannel hearing aids. Should be fun.

Monday, June 30, 2014

Samsung ARM Chromebook XE303 with VGA adapter - power problems

I have a Samsung ARM Chromebook (the 303 series) which is pretty nice, and I really like it for its size, weight and complete lack of fan and hard drive noise.
Samsung XE303C12-H01DE Chromebook: with 3G modem and
a German keyboard which I'm still not used to.

However, I need to give presentations on occasions.  No problem, I just get one of these:
HDMI to VGA adapter from (SKU 156981)
For less than $10, that's hard to beat.  Unfortunately, I found it doesn't work as-is with the Samsung Chromebook.  Already thinking I need to either give up on the idea of using the Chromebook for presenting or getting a more expensive adapter, I decided to try and see why the bloody thing doesn't work. I verified it does work with my Raspberry Pi, so the problem must be with the Chromebook - perhaps a power problem?  According to Wikipedia, HDMI should have +5V on pin 18.  I opened up the adapter, which can be done with nothing more than one's fingernail, running it along the seam around the VGA socket, then confirmed the absence of 5V on the HDMI port.  Lukily, getting 5V is no problem if you have a USB port nearby and here is my solution to this particular annoyance:
HDMI to VGA adapter with power bypass fitted
I take no responibility for damage you might be inflicting to your Chromebook or any other device you might want to use this mod on. Try this at your own risk, there is a good chance of frying delicate electronics. 
How to solder the power onto the adapter: +5 on
pin 18, ground to wherever convenient.
Basically, take a USB plug and solder two wires to just the outside pins.  Drill a hole through the plastic and the rubber fitting of the HDMI to VGA adapter (slide the insides out first!)  Solder the 5V wire to the pin 18 endpoint of the cable and the ground to a ground point on the adapter PCB.  Done!  I put a knot into the cable for strain relief, to prevent the solder points being ripped off.

That's it! It works nicely, although I don't think the adapter queries the monitor for modes, a decent selection is given to the ChromeOS as soon as the adapter is plugged in.  I've only tried it with one projector so far, but there is no reason it should not work with just about any that accept VGA signals.

Hopefully this information is useful to other (ARM) Chromebook users, but note this is a no-name adapter - yours may look entirely different.  Just remember these adapters are active devices which need power (even if very little), and since the Chromebook isn't delivering it on its HDMI port, you need to get it there somehow.