Wednesday 8 September 2010

Curve fitting with gnuplot

Let's say you want to fit a curve to some messy experimental data. You can do this in gnuplot using the following:


# Define the shape of the fitting function. Here, I have used the
# Gauss error function as an example
f(x) = a * erf((x-x0)/b) + c

# Give some initial estimates for the fitting parameters
a = 1600
b = 1
c = 1600
x0 = 45

# This command fits your fitting function to the
# x and y values contained in columns 1 and 2
# of the data file "row-data.dat". The fitting
# parameters are listed after the "via" keyword.
fit f(x) "row-data.dat" using 1:2 via a,b,c,x0

# Plot your scattered data and the fitted curve
# on the same axes for comparison.
plot f(x), "row-data.dat"

# Dump a table of the results to "fit-plot.dat"
set table "fit-plot.dat"
plot f(x), "row-data.dat"
unset table


So, there you have it. This gnuplot script will fit a nice neat curve to your messy data. A log of the fitting process will be saved as "fit.log" and the plotting data will be saved as
"fit-plot.dat".

Quite often, you'll need to play with the initial estimates in order to get a good fit but
usually this works first time.

Wednesday 17 March 2010

Options in bash scripts part 2

Here's a little demo script I wrote, which reads a couple of options from the command line and performs some basic error checking.  It also makes use of regex matching using

if [[ $test_variable =~ $a_regular_expression ]]; then
  # you have matched the regular expression
else
  # you haven't matched the regular expression
fi

Here's the script:

#! /bin/bash

#TODO: Fill in help message
usage()
{
cat << EOF
usage: $0 FILENAME NPROCS options

This script reads command-line args and dumps them to stdout

# describe arguments here

OPTIONS:
 -t    blah
 -r    blah
EOF
}

# Set default values for variables
ram=1G
time="48:00:00"

#TODO: This time format only specifies that times are
#  numbers up to 49:00:00.  Correct this so that
#  only times up to 48:00:00 are allowed
time_format='[0-4][0-9]:[0-5][0-9]:[0-5][0-9]'

# Read command-line arguments and set variables
while getopts "r:t:h?" OPTION
do
 case $OPTION in
  ?)
   usage
   exit 1
   ;;
  h)
   usage
   exit 1
   ;;
  r)
   ram="$OPTARG"G

   # TODO: Check that ram is an integer
   #  within the range 1-12

   # Remove "used" argument
   shift $((OPTIND-1)); OPTIND=1
   ;;
  t)
   time=$OPTARG

   # Check that time format is correct
   if [[ $OPTARG =~ $time_format ]]; then
    time=$OPTARG
   else
    echo "ERROR: Time format wrong"
    exit
   fi

   # Remove "used" argument
   shift $((OPTIND-1)); OPTIND=1
   ;;
 esac
done

filename=$1
# TODO: Check that filename is a valid file identifier

nprocs=$2
# TODO: Check that nprocs is a valid integer

# Dump all variable values to stdout
echo time=$time
echo ram=$ram
echo filename=$filename
echo nprocs=$nprocs

Friday 26 February 2010

Options in bash scripts

Bash scripting is really very powerful and lets you bolt together many UNIX programs with ease.  Scripts can be made even more powerful by accepting command-line options.  getopts makes this really easy.  There's a nice little tutorial here

Tuesday 9 February 2010

Old textbooks

There's something very different about the writing in old science textbooks.  Maybe it's the more formal language and the outdated terminology.  I discovered some old radiometric units today: the foot-candle, the foot-Lambert, the nit and the stilb are particular favourites.

Tuesday 5 January 2010

New grant

Leeds has just become part of a UK collaboration to make the world's first ever silicon-based quantum cascade laser.  I researched the theory of Si-based QCLs for my PhD, so hopefully I can contribute something to the new project...

Monday 4 January 2010

Imaging plots


My experiments generate image data in three columns: (x, y, brightness)

Converting the raw data into a pretty image was always a slightly tedious task, as I used GNU Octave (or MATLAB) to generate the plot

function plot_3column_data(filename)
%   Grab data from file
data_from_file=load(filename);
x=data_from_file(:,1);
y=data_from_file(:,2);
z=data_from_file(:,3);

%   Find range of x and y axes and make arrays of axis data for plot
xmin=min(x);
xmax=max(x);
ymin=min(y);
ymax=max(y);
xstep=x(2)-x(1);
x_plot=[xmin:xstep:xmax];
y_plot=[ymin:xstep:ymax];

%   Reshape data column into a matrix
z_plot=reshape(z,length(x_plot),length(y_plot));

%   Finally, make a surface plot of the data, using a
%   chosen colour scheme and tweak the shape of the
%   axes
surface(x_plot,y_plot,z_plot','EdgeColor','None')
colormap bone
axis equal


Note that this fairly laborious script didn't even contain axis labelling or exporting to a PNG image. The trouble is, I think, that Octave is a very powerful matrix manipulation language and using it just as an image plotter is overkill. The script is ugly and the interpreter is pretty slow to load.

I'm sure there are much better ways to do this in Octave, but there are already some very nice ready-made data plotting tools available. I found that Gnuplot has a built-in "image" plot style, which does exactly what I want:
set terminal png transparent large size 800,600
set output 'image.png'
plot 'data.dat' with image

It's very simple, and very fast :)

Liquid helium

It's surprising how long a sealed dewar of liquid helium lasts. I opened and measured a three-week-old 120 litre dewar today and found 107 litres still inside.

Hi

Hi, I'm a research assistant in optoelectronics at the University of Leeds, and I thought I'd try something new with my research notekeeping here.

Most of the notes a scientific researcher makes are, frankly, pretty uninteresting to anyone else; they document the meticulous preparation of experiments, and countless dead ends. Every so often, the work pays off and some conclusive results are revealed, which can be published in journals or presented at conferences.

My aim here is to cover some of the middle-ground; the semi-interesting things that pop up along the way. Obviously, I won't be giving away any confidential material before I publish it. Instead, expect to find notes on experimental techniques and curiosities and my personal musings. This might be of some interest to other research scientists and engineers... maybe...