Tuesday, March 27, 2012

A quick phone navigation tip

If you find yourself navigating from written instructions (either received in email, or given on paper) then a simple tip is to take a photograph of the email (or paper) and set it as the image on the lock screen.

Then when you are walking around there's no need to unlock your phone to read the directions.

Here, for example, is how to do that on the iPhone. Suppose you've received directions in an email (if you have a paper set of directions then just photograph them and jump ahead).

First, take a snapshot of the email containing the directions. You can do that by clicking the power button and home button at the same time. A screen shot will appear in Photos.

Find the image in Photos and click on the "Forward" button (bottom left hand corner). That button is used for photograph actions:

Then choose Use as Wallpaper and then position the image so that the directions are in the middle of the screen (which prevents them from being hidden by the clock). Once done choose Set Lock Screen:

And you're done. Now when you look at your locked phone the directions are there to see:

Tuesday, March 20, 2012

Met Office and CRU argue for open data and open code

Regular readers of this blog will recall that back in 2009 I looked at data released by the Met Office and CRU and found some errors and software bugs in the land surface temperature record known as CRUTEM3.

That experience led to a collaboration between myself and two professors and a joint paper in Nature entitled The Case for Open Computer Programs which used my experience with the closed Met Office code as one example. Our paper argued for the open release of data and code with all academic papers.

Today comes the news of a new paper from the Met Office and CRU describing an update to CRUTEM3 (called CRUTEM4). The paper is Hemispheric and large-scale land surface air temperature variations: An extensive revision and an update to 2010. It contains a couple of pleasing, if slightly surprising, paragraphs (my emphasis):
Given the importance of the CRUTEM land temperature analysis for monitoring climate change (e.g. Trenberth et al. 2007), our preference is that the underlying station data, and software to produce the gridded data, be made openly available. This will enhance transparency, and also allow more rapid identification of possible errors or improvements that might be necessary (see e.g. the earlier discussion of homogeneity adjustments in the SH).
and
As a result of these efforts, we are able to make the station data for all the series in the CRUTEM4 network freely available, together with software to produce the gridded data (http://www.cru.uea.ac.uk/cru/data/temperature/ and http://www.metoffice.gov.uk/hadobs/).
The Met Office site says the following of the code release:
The code required to produce the CRUTEM4 fields and timeseries will soon be made available. Note that code previously released for CRUTEM3 cannot be used to exactly reproduce CRUTEM4 temperature series due to changes in processing methodology.
Hurrah!

Let's hope that this becomes a trend in science.

Saturday, March 17, 2012

Ambient bus arrival monitor from hacked Linksys WRT54GL

London's Transport for London has a wonderful service called Countdown that can give live bus arrival times. For example, here's a page showing live buses passing No. 10 Downing St.

Underlying this is a simple JSON API that, while not public, seems to be usable by the average programmer as long as I'm not abusive. So with its details deciphered (hardly hard since the web site uses the API) I set about building an ambient bus monitor into a model London bus. The idea is that I can glance at the bus and see the times of up to the next two buses that I'm likely to want to catch and know when to leave the house.

Here's a picture of the completed unit:

To make that work I needed a computer of some kind and I'd originally planned on using a eRaspberry Pi. But with the delay in being able to buy one I switched to another host: a hacked Linksys WRT54GL. It's possible to reflash the Linksys with a custom Linux installation that lets me control the box completely (and still use it as a wireless router). There are various project, but I used OpenWRT.

With OpenWRT it's possible to SSH into the box and treat it as any Linux server (albeit a rather slow one). But there's plenty of power to grab bus times and update an LED display connected to the WRT54GL's serial port. Yes, there's a serial port inside the WRT54GL that uses TTL levels and can be easily accessed by soldering on three wires.

First you remove the cover of the router. It's easy because it simply pulls apart, but first you have to cut through a sticker warning you that your warranty is about to end.

The front part pulls away easily to reveal the circuit board:

And the back is just as easy to remove leaving you with the circuit board itself (you must remove the two antennas at the back):

The two serial ports are conveniently easy to access at the edge of the board:

The LED module I'm using is from SparkFun and accepts TTL levels and needs a 3.3V power supply.

Happily the WRT54GL's serial port is TTL and has Vcc and GND, so three wires supply all that's needed:

(Yes, those wires are rather large because I'm using up surplus wire from my homemade matrix display made from hacked Christmas lights).

A quick test with the multimeter showed that I was getting about the right voltage:

Next I used a standard three pin 'headphone' socket and drilled a hole in the front of the Linksys for it:

And put it all back together it makes a neat little unit that's got a serial port on the front:

To wire up the display I took an old sound card cable that came with a PC and cut one end off and soldered the wires to the display. The plug end goes into the Linksys. Here is it showing a number for the first time:

With the hardware half done (no bus yet for the display) it was time for software. The code for the project (written in Lua, which is installed by default as part of OpenWRT) is here.

It relies on STTY so install it with apt-get install coreutils-stty so that it can set the serial port up correctly. On my box that second serial port is /dev/ttyS1.

The program has three parameters: a comma separated list of bus routes, a bus stop number and a 'walking time'. For example, it's possible to do:
lua ambibus.lua 3,12 50906 2
Which means get the times for buses 3 and 12 arriving at stop 50906 (close to No. 10 Downing St.) and allow me two minutes to walk there. The program will find the buses, adjust the times by 2 minutes to allow you to arrive and once a minute update the display with the times of the next two buses.

Next up is a nice case for the display. What better than a London bus. I chose a Plaxton Pointer as it was the right size for LED display to show through the side windows.

If you collect model buses you may want to avert your eyes as I decapitate the bus, gut it, cut out some seats and then put it back together again.

I prised the top off using a screwdriver to reveal the plastic window piece:

Then I removed the windows themselves to reveal the seats:

And then removed the seats to get access to the 'engine' at the back which I removed to make a space for the cable to pass out of the bus.


Then I wired in the display with a small hole cut in the back seats to allow the cable to pass through:

The display was glued in (upside down because of the available clearance: a problem which is fixed in the code) and a few seats cut out to make space.

Then put it all back together and power on the display for a quick test:


And here it is running. The display is capable of showing two buses, but here there was only one bus coming in four minutes:

Wednesday, March 14, 2012

Teaching yourself Morse Code with iPhone's Accessibility options

The iPhone allows the user to create custom vibration patterns, they are considered part of the Accessibility options. (Side note: I've always found accessibility options to be interesting to an able-bodied person like me: there are often customization options there that are very handy).


These custom vibration patterns are created by selecting a contact and then by touching Vibration. You can use them to teach yourself a bit of Morse Code. On my phone I've made custom vibration patterns for people who call me frequently so I can tell without removing the phone from my pocket who's calling.

You make the pattern by tapping or holding on the screen to create short or long bursts of vibration. Here I've tapped out A I R and used it as the custom vibration pattern for Air France.

The other useful iPhone resource for Morse Code learning is this Koch Trainer.

Wednesday, March 07, 2012

Nine times table on your fingers (and algebraic explanation)

Can't remember that 9 x 7 is 63? Here's the really fast way to do it.

Lay your hands on the table and look at your fingers. Imagine they are numbered 1 to 10 from left to right. Find the 7th finger. There are 6 fingers to the left of it: that's the first digit of 9 x 7. There are three fingers to the right of it: that's the second digit. So the answer is 63.

Same thing works for the rest of the 9 times table (up to 9 x 10).

Here's a quick algebraic explanation of why that works. Suppose we're doing 9 x a where a is a number between 1 and 10. On your fingers you've got (a-1) fingers to the left of finger a and (10-a) fingers to the right. The result is 10 x (a-1) + (10-a) because the (a-1) is in the 10s position.
10 x (a-1) + (10-a)

= 10 x a - 10 + 10 - a

= 10 x a - a

= (10 - 1 ) x a

= 9 x a
Another thing you can spot this way is that the sum of the digits in the 9 times table is always 9. For example, 63 (6 + 3 = 9), 9 x 5 = 45 (4 + 5 = 9). Again, algebra shows why:
  (a-1) + (10-a)

= a - 1 + 10 - a

= 9
PS A reader writes that the 9 times table is also a palindrome (if you add a zero before 9 x 1): 09 18 27 36 45 54 63 72 81 90.

Tuesday, March 06, 2012

How to divide by 9 really, really fast (and why it works)

An interesting post on Hacker News shows tricks for dividing by 9. The fastest trick goes like this. Suppose you want to do 53876 / 9 in your head. You do the following:

1. The first digit is going to be the same as in the left hand side, so it's 5.

2. The second digit is the first digit plus the second digit: 5 + 3 = 8

3. The third digit is the previous answer plus the third digit: 8 + 8 = 16. Because that's bigger than 10 carry it to the digit above (which is now 9) and just take the rest. So it's 6.

4. The fourth digit is previous answer plus the fourth digit: 16 + 7 = 23. Again carry the tens to the preceding digit (which now becomes 8) and you're left with 3.

5. The last digit is the sum of all the digits: 23 + 6 = 29. For this final digit it's necessary to work out how many times 29 can be divided by 9 (it's 3). That gets added to the previous digit which becomes 6. The remainder (29 - 9 x 3 = 2) is the remainder of the whole calculation.

So, reading off the digits 53876 / 9 = 5986 remainder 2.

Why does this work? To show why I'll do a smaller example of a three digit number (with digits a, b, c) being divided by 9. The calculation abc/9 can be written as:
(a x 100 + b x 10 + c) / 9

=  a x 100 + b x 10 + c
   -------   ------   -
      9         9     9

=  a x (11 + 1/9) + b x (1 + 1/9) + c/9

=  a x 11 + b x 1 + (a + b + c)
                    -----------
                         9

=  a x 10 + a x 1 + b x 1 + (a + b + c)
                            -----------
                                 9

=  a x 10 + (a + b) + (a + b + c)
                      -----------
                           9
So, the first digit depends on a and any carry from (a + b), the second digit depends on (a + b) and any carry from the final term, and the only division left is the division of (a + b + c) by 9 and so that's where the remainder comes from.

Exactly the same pattern appears for longer numbers. Just be sure to carry; for very long numbers there could be a lot of carrying.

(Also fun, Squaring two digit numbers in your head.)

The Delilah Secure Speech System

Part of the new exhibit being unveiled at Bletchley Park is the Delilah Secure Speech system that Alan Turing developed during the Second World War. Details of the system are in Andrew Hodges' excellent biography of Turing and have recently been placed in the National Archives (references FO 850/256 and HW 25/36 for people who want to go an see them for themselves).

Delilah was intended to be fairly portable (unlike SIGSALY) and usable in the field (such as in a tank) and allow secure speech communication between people over radio or telephone.

A team at Bletchley Park has been working to rebuild Delilah from the report (with some assistance from GCHQ) and I was able to see and photograph the reconstructed machine. Here are some pictures:







And here are pictures of the original machine taken from recently declassified documents:

Briefly, Delilah worked as follows. The incoming speech was limited a channel of 2kHz which was then sampled at 4kHz to produce 4,000 samples per second of the incoming waveform. These samples were normalized to a range of 0 to 1 and added using modulo arithmetic to a key stream consisting of values in the range 0 to 1.

The resulting waveform was then transmitted and at the opposite end the original waveform could be constructed by adding back (again using modulo arithmetic) the same key stream. Both ends had to be synchronized for this scheme to work (and use the same key).

The key was set on wheels visible in the photograph above that generated a stream of pseudo-random numbers which when added to the incoming signal would result in something close to pure noise being transmitted.