Apr 13 2009

Adventures in making morse code

Published by at 3:50 pm under iphone,Morse Code

As part of the process of working on the new iPhone app, HamMorse, I’ve been delving into the gory details of making digital sound. At one level things are going really well but as is often the way, there’s some weirdness cropping up that is getting in the way. I thought I share this just in case someone can help fix the problem!

A quick recap – to make sound you need to make a sound wave. For morse code this is usually a simple sine wave with a frequency somewhere around 600-800 Hz. To make morse code out of this you need to modulate it to make the dits and dahs, typically by turning the sound on and off as appropriate. If you are making this digitally (vs modulating a RF carrier frequency) you write your code so that there is silence when you want no sound and then when you need to have a dit or a dah you start the sine wave at the beginning of the dit/dah and then stop it after the appropriate length of time.

How long is an appropriate length of time? Well, the wikipedia page on morse code explains this all rather well but basically the timing works out to be one dit (in ms) = 1200/(desired code speed in words per minute). So, at 15 wpm, this works out to be 1200/15 = 80ms per dit. A dah is three times the length of a dit so that makes it 240ms long. There’s a one dit-length silence between each part of a letter (eg. between the dit and the dah of the character A), a three dit-length silence between characters in the same word and a seven dit-length silence between words. Throw all that together along with a translation table between the letters in your piece of text and the morse code needed for those letters and you are off and running.

Skipping over a bunch of bits for now (see a great explanation by Walt Fair Jr., W5ALT if you want more info), I’ve written code that gets this done on the iPhone and tested it on the iPhone Simulator that Apple provides for you to test your application. It sounds great, the audio waveform looks like this when I record the sound coming out of the computer’s speaker into Audacity.

Simulator wave form

Once things are working on the simulator you can connect your iPhone and run the same code on the actual device. When I do this and listen to the audio that comes out of the phone’s speakers, its not quite so nice, when you record the audio into audacity here’s what it looks like:

iPhone wave form

Not so pretty as before and while the audio still sounds passable, its clearly not as it should be. My challenge at the moment is to figure out why this is going on! The wave looks a bit like another sine wave is being added to the main one, causing the regular distortion observed but I cant for the life of me see where that is creeping in. I get the same distortion via headphones so I dont think its the speakers, I’ve made the audio mono to see if one of the stero channels was out of phase and causing the problem, this had no effect. When I look at the numerical values being created in the code, it should be making a nice smooth sine wave but…

This is the entertainment that is software development! I am currently waiting for the “Duh!” moment to occur where upon I will add back the missing semi-colon and everything will work as planned. Until that happens, if anyone has any suggestions I’d be very interested to hear them – I’m hoping that my fellow hams who are used to looking at wave forms and harmonics and things can get me pointed in the right direction.


Simon, AA9PW

3 responses so far

3 Responses to “Adventures in making morse code”

  1. Simonon 14 Apr 2009 at 8:16 am

    Well, I’ve made some progress towards solving the problem but not necessarily towards understanding the problem. As part of the audio generation step you typically multiply the sine wave value (which varies from -1 to +1) by a multiplier to get the appropriate amplitude needed. From reading around it seemed like sound cards on PCs are typically able to go up to a value of +/- 32767 in order to stay in the range of their digital-analog converters that convert the digital signal to a sound wave.

    I was originally multiplying by around 32000 to stay clear of exceeding this limit but still keep a decent volume signal. By doing a bit of experimentation I’ve found that if I back this down to around 12,000 then I no longer get the distortion on the audio that I described above, the trade off being the audio isnt as loud. If there were differences in the sound card between the iPhone and macbook laptop that might explain the different performance I’ve seen. I need to track down some stats on the iPhone’s audio system to see if this is really the case.

    Anyway, for now the problem seems to be fixed so I can move on to fixing a few last minute bugs and then getting this into the App Store.

  2. Ken Billson 26 Sep 2009 at 5:09 am

    How about a tone control?? Maybe it could be selected on the menu page… I find the pitch somewhat too high for my preference.

  3. Paul Bigwoodon 02 Jun 2010 at 4:51 am

    Is this not simply overdriving of the Iphone Sound system causing the distortion?

Trackback URI | Comments RSS

Leave a Reply