Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write a program that converts multiple words and multiple numbers to its Morse c

ID: 3772316 • Letter: W

Question

write a program that converts multiple words and multiple numbers to its Morse code using Matlab. (For example convert the sentences: "I love my car" to its morse code and also numbers as "187553" to morse code also) Please YOU MUST USE LOOPS (for loop or while loop)... you CANNOT USE cell array.

Please note that, your program must put the Morse code on one line and use a space to separate the Morse codes of two consecutive characters. For example, for a word 'AB', its Morse code is '.- -...' , since the Morse code of 'A' is '.-' and the Morse code of 'B' is '-...' . It is not correct to put '.-' and '-...' together without the space in between (i.e. '.--...') or to put them on different lines, i.e.

Explanation / Answer

function [morseText,morseSound] = text2morse(string,playSound)

    string = lower(string);

    %Defined such that the ascii code of the characters in the string map
    %to the indecies of the dictionary.
    morseDictionary = {{' ',' '},{'',''},{'',''},{'',''},...
                       {'',''},{'',''},{'',''},{'',''},{'',''},{'',''},...
                       {'',''},{'',''},{'',''},{'',''},{'',''},{'',''},...
                       {'0','-----'},{'1','.----'},{'2','..---'},{'3','...--'},...
                       {'4','....-'},{'5','.....'},{'6','-....'},{'7','--...'},...
                       {'8','---..'},{'9','----.'},...
                       {'',''},{'',''},{'',''},{'',''},{'',''},{'',''},...
                       {'',''},{'',''},{'',''},{'',''},{'',''},{'',''},...
                       {'',''},{'',''},{'',''},{'',''},{'',''},{'',''},...
                       {'',''},{'',''},{'',''},{'',''},{'',''},{'',''},...
                       {'',''},{'',''},{'',''},{'',''},{'',''},{'',''},...
                       {'',''},{'',''},{'',''},{'',''},{'',''},{'',''},...
                       {'',''},{'',''},{'',''},...
                       {'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','--..'}};

    %Iterates through each letter in the string and converts it to morse
    %code
    morseText = arrayfun(@(x)[morseDictionary{x}{2} '|'],(string - 31),'UniformOutput',false);

    %The output of the previous operation is a cell array, we want it to be
    %a string. This line accomplishes that.
    morseText = cell2mat(morseText);

    morseText(end) = []; %delete extra pipe

%% Translate Morse Text to Morse Audio

    %Generate the tones for each element of the code
    SamplingFrequency = 8192; %Hz
    ditLength = .1; %s
    dit = (0:1/SamplingFrequency:ditLength);
    dah = (0:1/SamplingFrequency:3*ditLength);
    dit = sin(3520*dit);
    dah = sin(3520*dah);
    silent = zeros(1,length(dit));

    %A dictionary of the audio components of each symbol
    morseTiming = {{'.',[dit silent]},{'-',[dah silent]},{'|',[silent silent]},{' ',[silent silent]}};
    morseSound = [];

    for i = (1:length(morseText))

        %Iterate through each cell in the morseTiming cell array and
        %find which timing sequence corresponds to the current morse
        %text symbol.
        cellNum = find(cellfun(@(x)(x{1}==morseText(i)),morseTiming));

        morseSound = [morseSound morseTiming{cellNum}{2}];
    end

    morseSound(end-length(silent):end) = []; %Delete the extra silent tone at the end

    if(playSound)
        sound(morseSound,SamplingFrequency); %Play sound
    end

end