Decode the morse code python

Morse audio decoder

This program is in solution to Wunderdog Wundernut vol. 11, whose instructions can be found in their GitHub.

The program reads wav audio file, and outputs decoded morse code in standard output.

Quickstart

Installation

Option 1 — pip

You can install this package from pip, with

pip install morse-audio-decoder 

Option 2 — Local install from sources

Clone code repository from your local machine, install from there:

git clone https://github.com/mkouhia/morse-audio-decoder.git cd morse-audio-decoder poetry build # take note of the build step output, install package from the dist folder pip install dist/PRODUCED_PACKAGE.whl 

Usage

To run the script installed with pip, perform

morse-audio-decoder WAVFILE 
python -m morse_audio_decoder WAVFILE 

where WAVFILE is path to the audio file to be processed.

The program decodes audio morse code in the WAVFILE argument, and writes translation to standard output. See program help with command line flag -h :

$ morse-audio-decoder -h usage: morse-audio-decoder [-h] WAVFILE Read audio file in WAV format, extract the morse code and write translated text into standard output. positional arguments: WAVFILE Input audio file options: -h, --help show this help message and exit 

Usage in Python

The program works in following steps

  1. Read in the WAV file.
  2. Extract analytic envelope from the signal by calculating moving RMS amplitude with Hann window of default 0.01 second width. This envelope signal is smooth and always greater than or equal to zero.
  3. Convert envelope to binary 0/1 signal by applying threshold, by default 0.5 * max(envelope)
  4. Calculate durations of continuous on/off samples
  5. Identify dash/dot characters and different breaks with K-Means clustering. The lengths of periods are compared, and then labeled automatically based on number of samples.
  6. Create dash/dot character array, which is then broken to pieces by character and word space indices
  7. Translate morse coded characters into plain text, print output
Читайте также:  Configure php with mysqli extension

Exploratory data analysis and first program implementation is performed in this jupyter notebook. The notebook is not updated; actual implementation differs.

Restrictions

This decoder has been tested and developed with inputs that have

  • no noise
  • constant keying speed
  • constant tone pitch
  • single input channel.

If the decoder were to be extended to noisy inputs with major differences, at least following changes would be required

  • pitch detection in moving time
  • signal extraction with narrow bandpass filter, based on identified pitch
  • keying speed detection (characters/words per minute)
  • decoding in smaller time steps, taking into account speed changes.

The program is also not intended to identify single characters, as the precision will be lower with shorter inputs.

Development

Environment

  • Numpy
  • Scikit-learn
  1. Install dependencies with poetry install
  2. Enter environment with poetry shell

Code quality and testing

All code is to be formatted with black :

and code quality checked with pylint :

Tests should be written in pytest , targeting maximum practical code coverage. Tests are run with:

and test coverage checked with

Optionally, html test coverage reports can be produced with

pytest --cov morse_audio_decoder --cov-report html 

Contributions

Contributions are welcome. Please place an issue or a pull request.

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Morse Code decoder in Python

mtarsel/Morse-Code-Decoder

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Morse Code decoder in Python

The first section of input.txt contains a mapping between the Latin English character, and the Morse code equivalent. The second section contains a lexicon of English words that are known to exist in the secret Morse Code message. The third section is what appears as Morse Code in “dots” and “dashes”.

Use this input file (» input.txt «) to decode what the third section says. Sections are seperated by ‘ * ‘. Print each decoded word on a new line. Whitespace indicates a word boundary for morse code words. To designate that a Morse code word may mean something else not in the list ( called words<> ), output a question mark (ie “?”) at the end of English word. To designate a Morse code word has multiple matches ( in our list words<> ), output an exclamation mark (ie “!”) at the end of each English word.

About

Morse Code decoder in Python

Источник

Simple Morse Code Decoder in Python

Morse code is a method used in telecommunication where each alphabet, number and punctuation is represented by a series of dots/dashes/spaces. It was first invented by Samuel Morse in 1930s and it has been heavily used in the navy industry. This article will describe the process to build a simple Morse Code decoder in Python.

Morse Code Representation in Python

As seen in the image above, each alphabet and number is represented by a series of dots and dashes. We can represent them as such in Python, but for better clarity, let’s translate them to ‘0’ and ‘1’ instead, where ‘0’ represents a dot, and ‘1’ represents a dash.

character = ['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','0','1','2','3','4','5','6','7','8','9']code = ['01','1000','1010','100','0','0010','110','0000','00','0111','101','0100','11','10','111','0110','1101','010','000','1','001','0001','011','1001','1011','1100','11111','01111','00111','00011','00001','00000','10000','11000','11100','11110']

As each character (i.e. an alphabet or number) corresponds to a series of 0s and 1s, we will use a dictionary structure to store them in Python. You can refer to my previous post on building a dictionary data structure in Python here.

# Define an empty dictionary 'morse_dict'
morse_dict = <>
# Convert the 2 lists into a dictionary using a tuple
zipped_char_code = zip(character, code)
morse_dict = dict(zipped_char_code)
# Print the dictionary 'morse_dict' on the terminal line by line
for key, value in morse_dict.items():
print(key, value)

Building the Morse Code Decoder

The way the Morse Decoder will be built is that we will prompt the user to key in the Morse Code representation (i.e. in 0s and 1s), with each alphabet or number separated by a *. Once the user pressed ‘enter’, the program will decode the Morse Code and displays it in alphanumeric form.

# reverse the previous dict as it's easier to access the keys
zipped_code_char = zip(code,character)
rev_morse_dict = dict(list(zipped_code_char))
# initiating a while loop
while True:
# empty…

Источник

Advanced Morse Code Decoder in Python

In one of my previous post, I’ve designed a simple Morse Code Decoder in Python which is capable of accepting user inputs and outputting them in their original alphanumerical form. One of the limitations of the decoder is that it does not allow the user to input sentences. Remember that we have chosen to represent each alphabet or number using a series of ‘0’ and ‘1’, where ‘0’ represents a dot, and ‘1’ represents a dash. Each alphabet or number is then separated by a ‘*’ in our decoder, as shown in the screenshot below.

In this post, we will improve our simple Morse Code Decoder to be able to decipher sentences as well. Furthermore, we can implement checks in the decoder to inform us of the frequencies in which each alphabet/number, word, or sentence type have been decoded.

Building the Decoder Class

One major difference between this decoder and the previous one is that we will be using Python class to build our decoder and the respective analysis checks. A simple explanation about Python class is that it defines a set of instance variables (data values to be represented for each object) and a set of methods (operations) that can be applied to the objects.

We will build a class called Decoder and it will contain several methods, with the first being __init__(self). This is the essential method for object creation and it usually initialize the values of the instance variables of each object. Remember that we chose to use a dictionary structure to store our Morse Code representation in Python, where each alphabet or number is represented by a series of ‘0’s and ‘1’s. As we are adding sentences analysis checks too, we have also added three punctuation marks “ . ” , “ , ” and “ ? ”.

class Decoder: def __init__(self): 
self.the_dict =…

Источник

Оцените статью