MATLAB Interpolation Toolkit

Posted by matt
on Wednesday, January 21

During the course of my PhD, I’ve spent quite a lot of time examining methods of interpolating scattered data. By scattered, I mean something that looks like this:

plasma_scatter
Uploaded with plasq’s Skitch!

Or alternatively, like a very, very gappy image. This type of data is very common in geoscience, and so interpolating it so it looks more like this:

plasma_nn_surf
Uploaded with plasq’s Skitch!

Is a common activity. As well as examining common methods of interpolating scattered data, such as cubic, linear and nearest neighbour methods, I’ve looked at natural neighbour interpolation, radial basis function interpolation, everyone’s favourite kriging and a less common methods known as adaptive normalised convolution (ANC).

In order to do meaningful comparisons, I needed working implementations of these, and so ended up writing my own versions of ANC, radial basis function interpolation and kriging. I also wrote a wrapper to Pavel Sakov’s natural neighbour interpolation.

I’ve just released these functions as a toolkit for Matlab, with a BSD license. You can find the download links on the toolkit’s homepage, which also gives the commands you need to clone the repository.

To build the toolkit, you’ll need a working build environment, with make, gcc and python 2.5 as well as a copy of Matlab. I’ve tested the kit on Debian/Ubuntu Linux with Matlab 2007a and Mac OS X (10.5). If you’re using a Mac, you’ll need XCode installed.

You’ll probably need to tell make where your mex binary is (MEX is Matlab’s external interface builder). To do this, run:

make MEX=/path/to/mex

You’ll find mex inside your Matlab installation, under the bin/ directory. On linux, it may be symlinked into /usr/local/bin.

Once make has run, you’ll find a directory called toolkit, which contains the interpolation toolkit. Copy this folder somewhere appropriate, and add it to your Matlab path, by running:

addpath('/path/to/toolkit')

Within Matlab. You can then run help toolkit, for some help and examples of how to test the methods.

I’d recommend using natural neighbour interpolation everywhere you would previously have used matlab’s griddata function. You can find more information on interpolation in my publications, which you can read and download here.

Refactoring Commands in the MATLAB Bundle

Posted by matt
on Saturday, November 22

Sometimes, after seeing a few things, concepts collide (like icebergs, perhaps), and I come up with ideas I think are cool. Here’s one of those:


Refactoring with the MATLAB bundle from Matt Foster on Vimeo.

For those of you who prefer some code, say you have a big long ugly line of code. You know it’s bad, but it works and you don’t want to break it. Here’s an example, very, very loosely based on some Matlab code I saw at work:

The obvious thing to do, is pull out that evil range -pi:0.1:pi. With the new ‘Introduce variable’ command (bound to ⌃⇧C, by default), you can do this be selecting any on of them, and running the command.

You’ll see a dialogue box asking you for a name for you new variable, and after you press OK, you’ll see the following:

Much better. You can also repeat the process to further simplify the line, and the command will insert the new variable before the first instance it finds. Cool hey?!

This command is also general enough that is should work for most languages that use assignments based on =. For more info on this concept, see refactoring.com, which I shall also be scouring for more ideas.

Matlab Bundle Merge

Posted by matt
on Tuesday, November 11

I’ve successfully committed Thomas Kjosmoen’s matlab bundle changes into the macromates SVN repository. His work is absolutely stellar, so I recommend you grab the bundle ASAP. I’ve also managed to keep the useful extensions I made to the previous incarnation intact.

As ever I’m eager to hear your feedback and bug reports. Either directly, or via the google group. The code is also available in GitHub for your forking pleasure.

Aesthetics in Plotting and Visualisation

Posted by matt
on Tuesday, October 28

I’m fairly sure that aesthetics play a large part in how people view your work – ugly correct results are probably viewed as being worse than pretty, but wrong results. Aesthetics must play a fairly big role in how people perceive your work.

I’ve just finished generating a lot of quiver plots for my thesis. A quiver plot is a graph of vectors, illustrating a flow field. My results typically show an image with motion vectors overlaid, all plotted with Matlab, a piece of software which is very heavily used in academia in the UK (and probably all over the world).

Matlab quiver plots are ugly, and all of my plotting got me wondering how other system’s quiver plots look. In particular, I’ve started playing with numpy and scipy, so I decided to do a quick comparison between matlab, and matplotlib, the python close of matlab’s plotting tools. The results are very interesting.

I generated a quick example motion field, and plotted it with quiver. Then I took screenshots. I used the Gimp on my Linux box, for the matlab example) and Skitch on my MacBook for the python example.

First, here’s the matlab example. This was generated on linux using Matlab 2007a:

matlab_quiver
Uploaded with plasq’s Skitch!

Here’s the matplotlib python example, generated using the enthought python distribution. I used colour here because I could: matplotlib’s quiver supports it out of the box. Matlab’s doesn’t.

Figure 1
Uploaded with plasq’s Skitch!

I think the difference between the results is abundantly clear. Matplotlib’s arrows look great, they’re shapely and well scaled. Matlab’s look like jaggedy lines in comparison. However, I think the main difference is down to anti-aliasing. The matplotlib’s result looks better because they’re smoother and look far more modern the 90s throwback that is matlab’s interface (on linux and Mac at least – I can’t speak for Windows).

These results have pretty much convinced me to try my next project using Python and scipy instead of Matlab. Not only is it completely free (EPD is free for academic use, and matplotlib, scipy and numpy are just plan free), the aesthetic is there too. It’s almost like python’s elegant simplicity ideals spilled over into their plotting engines.

MATLAB Bundle has been GitHubbed

Posted by matt
on Friday, September 05

This is just a quick note to say that I’ve push a copy of the MATLAB TextMate bundle to GitHub.

You can read my post on google groups, or just hit the site.

Happy forking!