Question
Download getSymbol.m and put it in your code folder. In the editor in MATLAB (or Octave-online) open a new file and write the function tokensToMessage(tokens,
Download getSymbol.m and put it in your code folder.
In the editor in MATLAB (or Octave-online) open a new file and write the function tokensToMessage(tokens, unittime) which takes a set of tokens and outputs a message. Each element in tokens represents a dot, dash, short-space, long-space, or long-long-space.
Save the file as tokensToMessage.m.
The table in Figure 1 has been implemented in a getSymbol function that will translate a string of dits and dashes to a character. For example:
>> getSymbol('...')
ans =
S
>> getSymbol('---')
ans =
O
Your function tokensToMessage should loop through all of the tokens and accumulate a symbol with dots and dashes. The matrix token is a 2D matrix with two columns. The first column represents the duration of the token and the second column represents the value of the token. Implement the general algorithm presented here:
message initialized to an empty string
symbol initialized to an empty string
for each row in tokens
if token's duration > 2 * timeunit and value == 0
concatenate getSymbol(symbol) to message
if token's duration > 4 * timeunit
concatenate ' ' to message
reset symbol to empty string
else if token's duration > 2*timeunit and value == 1
concatenate '-' to symbol
else if token's duration > timeunit/2 and value == 1
concatenate '.' to symbol
else
do nothing (it is either a character separator or too short)
%after the loop check to see if there is a last symbol
if not isempty(symbol)
concatenate getSymbol(symbol) to message
Submit your function to the quiz server for grading.
getSymbol.m:
function symbol = getSymbol( morse_string ) persistent code; if isempty(code) code{1} = '.-'; code{2} = '-...'; code{3} = '-.-.'; code{4} = '-..'; code{5} = '.'; code{6} = '..-.'; code{7} = '--.'; code{8} = '....'; code{9} = '..'; code{10} = '.---'; code{11} = '-.-'; code{12} = '.-..'; code{13} = '--'; code{14} = '-.'; code{15} = '---'; code{16} = '.--.'; code{17} = '--.-'; code{18} = '.-.'; code{19} = '...'; code{20} = '-'; code{21} = '..-'; code{22} = '...-'; code{23} = '.--'; code{24} = '-..-'; code{25} = '-.--'; code{26} = '--..'; % punct code{27} = '.-.-.-'; code{28} = '--..--'; code{29} = '..--..'; code{30} = '-..-.'; % numbers code{31} = '.----'; code{32} = '..---'; code{33} = '...--'; code{34} = '....-'; code{35} = '.....'; code{36} = '-....'; code{37} = '--...'; code{38} = '---..'; code{39} = '----.'; code{40} = '-----'; end symbols = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ.,?/1234567890'; symbol = symbols(strcmp(code,morse_string)); end
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started