blog

0x0014 - Morse Code

According to Wikipedia, Morse code is a method of transmitting text information as a series of on-off tones, lights, or clicks that can be directly understood by a skilled listener. It…

According to Wikipedia, Morse code is a method of transmitting text information as a series of on-off tones, lights, or clicks that can be directly understood by a skilled listener. It was developed in 1844 and is credited to Samuel F.B. Morse. As a child, the Morse code had caught my attention and I couldn’t stop but learning it.

The essentials of Morse code can be summarized easily. The code consists of only two symbols, a dot ‘.’ and a dash ‘-‘. A combination of these symbols results in a rudimentary representation of the Latin alphabets and decimal numbers. Each character is represented by a unique sequence of dots and dashes, which are also referred to as ‘dit’ and ‘dah’. The duration of a dash is 3 times that of a dot. Additionally, a gap equivalent to the duration of transmission of a dot is put between each symbol of the code. This duration is called one unit. Two letters are separated by a gap of 3 units and two words are separated by a gap of 7 units. The rate of transmission is measured in terms of words per minute (wpm) or characters per minute(cpm).

It is common knowledge that the alphabet ‘E’ is the most frequently used among its 26 peers. Hence, it is assigned the shortest code, a single dot ‘.’. Other assignments to the alphabet were made according to the frequencies of usage. Later on, the decimal digits and other symbols were included as well. The following tree depicts the dichotomic search table. The user branches left at every dot and right at every dash until the character is finished.

Morse code binary search tree

As apparent, this is similar to Huffman coding. If one wishes to find the Morse code for, say, SOS (Save Our Souls – a common distress signal), then using the above search tree, one would get ‘. . . – – – . . .’ as the result.

The following tables give the Morse code for letters, digits and punctuation symbols:

CharacterCodeCharacterCodeCharacterCode
A· —J· — — —S· · ·
B— · · ·K— · —T
C— · — ·L· — · ·U· · —
D— · ·M— —V· · · —
E·N— ·W· — —
F· · — ·O— — —X— · · —
G— — ·P· — — ·Y— · — —
H· · · ·Q— — · —Z— — · ·
I· ·R· — ·0— — — — —
CharacterCodeCharacterCodeCharacterCode
1· — — — —Period [.]· — · — · —Colon [:]— — — · · ·
2· · — — —Comma [,]— — · · — —Semicolon [;]— · — · — ·
3· · · — —Question mark [?]· · — — · ·Double dash [=]— · · · —
4· · · · —Apostrophe [‘]· — — — — ·Plus [+]· — · — ·
5· · · · ·Exclamation mark [!]— · — · — —Hyphen, Minus [-]— · · · · —
6— · · · ·Slash [/], Fraction bar— · · — ·Underscore [_]· · — — · —
7— — · · ·Parenthesis open [(]— · — — ·Quotation mark [“]· — · · — ·
8— — — · ·Parenthesis closed [)]— · — — · —Dollar sign [$]· · · — · · —
9— — — — ·Ampersand [&], Wait· — · · ·At sign [@]· — — · — ·

For developing a program to translate text into Morse code, a simple Dictionary data structure would suffice for holding the information in the table above. A method in Python to convert a given character to its equivalent code is:


def set_code(ch) :
        ch = ch.lower()
        dict = {'a': '.-',
                'b': '-...',
                'c': '-.-.',
                'd': '-..',
                'e': '.',
                'f': '..-.',
                'g': '--.',
                'h': '....',
                'i': '..',
                'j': '.---',
                'k': '-.-',
                'l': '.-..',
                'm': '--',
                'n': '-.',
                'o': '---',
                'p': '.--.',
                'q': '--.-',
                'r': '.-.',
                's': '...',
                't': '-',
                'u': '..-',
                'v': '...-',
                'w': '.--',
                'x': '-..-',
                'y': '-.--',
                'z': '--..',
                '1': '.----',
                '2': '..---',
                '3': '...--',
                '4': '....-',
                '5': '.....',
                '6': '-....',
                '7': '--...',
                '8': '---..',
                '9': '----.',
                '0': '-----',
                '.': '.-.-.-',
                ',': '--..--',
                '?': '..--..',
                '\'': '.----.',
                '!': '-.-.--',
                '/': '-..-.',
                '(': '-.--.',
                ')': '-.--.-',
                '&': '.-...',
                ':': '---...',
                ';': '-.-.-.',
                '=': '-...-',
                '+': '.-.-.',
                '-': '-....-',
                '_': '..--.-',
                '\"': '.-..-.',
                '$': '...-..-',
                '@': '.--.-.',
                ' ': ' '
                }
        return dict[ch]

Then a method to convert all characters of a word to Morse code is:


def morse_code(english) :
        code = []
        for i in english:
                code.append(set_code(i))
        return code

To convert an entire text to Morse code would require the following method, which takes care of gaps between symbols and words, and takes input from the standard input:


def dit_dah() :
        input_string = raw_input()
        result_string = ""
        for i in input_string:
                result_string += morse_code(i)[0] + '   '
        return result_string[:-1]

To ensure correctness, if the input is given as SOS, the output is as expected: ‘. . . – – – . . .’ with a gap of one unit between two consecutive letters.

Going a step further, the generated Morse code can be easily converted to audio signals using the alarm/bell character ‘\a’. A method to achieve this is:


def return_audio(string) :
        audio_result = ""
        bell = '\a'
        for i in string:
                for j in i:
                        if j == '.': audio_result += bell+' '
                        elif j == '-': audio_result += bell+' '+bell+' '+bell+' '
                        elif j == ' ': audio_result+='      '
        print audio_result[:-1]

This method takes care of the gaps between symbols, letters and words. The entire program, with all the methods together, can be simply called as follows:


ip = dit_dah()  # get input text and convert to Morse code
print ip        # return Morse code in dots and dashes
return_audio(ip)# return audio result of the input text