morse code translator not outputting sound

The conversion from input to morse code works just fine but I can't get the sound to be outputted.
%Stored values for every character
morse = {'.---- ','..--- ','...-- ','....- ','..... ','-.... ','--... ','---.. ','----. ','----- ', '.- ', '-... ', '-.-. ', '-.. ', '. ', '..-. ', '--. ', '.... ', '.. ', '.--- ', '-.- ', '.-.. ', '-- ', '-. ', '--- ', '.--. ', '--.- ', '.-. ', '... ', '- ', '..- ', '...- ', '.-- ', '-..- ', '-.-- ', '--.. ', '.- ', '-... ', '-.-. ', '-.. ', '. ', '..-. ', '--. ', '.... ', '.. ', '.--- ', '-.- ', '.-.. ', '-- ', '-. ', '--- ', '.--. ', '--.- ', '.-. ', '... ', '- ', '..- ', '...- ', '.-- ', '-..- ', '-.-- ', '--.. ', ' '};
text = {'1','2','3', '4', '5', '6', '7', '8', '9', '0', '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','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',' '};
%User is asked to input text
initial_input = input('Enter text you want converted to morse code', "s");
%Input is converted to morse code
for i = 1:length(initial_input)
[~, index] = ismember(initial_input(i), text);
if index > 0
fprintf('%s', morse{index});
end
end
fprintf('\n');
%Morse code is converted to sound file
%The spacing between words and the spacing within letters is counted separately as they will be played at different time intervals
morsecode = string(index);
dotamount = count(morsecode, '.');
dashamount = count(morsecode, "-");
spaceamount = count(morsecode, " ");
letterspaceammount = count(morsecode, " ");
wpm = 15;
numberdot = (2*dotamount)+(5*dashamount)+(14*spaceamount)+(3*letterspaceammount);
dotduration = (60/wpm*numberdot);
%time length is set
timedot = 0:0.001:dotduration;
timedash = 0:0.001:3*dotduration;
timespace = 0:0.001:dotduration;
timeletterspace = 0:0.001:3*dotduration;
timewordspace = 0:0.001:7*dotduration;
tone_kHz = 700;
height_dot = cos(2*pi*tone_kHz*timedot);
height_dash = cos(2*pi*tone_kHz*timedash);
height_set_space = 0*timespace;
height_letter_space = 0*timeletterspace;
height_word_space = 0*timewordspace;
%sound is generated using coordinates of previous terms
for time = (1:length(morsecode2))
if morsecode2(time)=='.'
sound_signal = [sound_signal,height_dot,height_set_space];
elseif morsecode2(time)=='-'
sound_signal = [sound_signal,height_dash,height_set_space];
elseif morsecode2(time)==' '
sound_signal = [sound_signal,height_letter_space,height_set_space];
end
end
sound(sound_signal,(1/0.001));

1 commentaire

Cris LaPierre
Cris LaPierre le 5 Déc 2024
I see an issue with the line morsecode = string(index);
index is only going to be the last character in initial_input so any sound you do get will only be a single letter, not the entire input.
I also got an error about moresecode2: Unrecognized function or variable 'sound_signal'.

Connectez-vous pour commenter.

Réponses (1)

When I ran your code with an appropriate argument, I got:
Unrecognized function or variable 'morsecode2'.
Beyond that, I juat wanted to see what ‘sound_signal’ produced. I’ll revisit this when that error is corrected.
%Stored values for every character
morse = {'.---- ','..--- ','...-- ','....- ','..... ','-.... ','--... ','---.. ','----. ','----- ', '.- ', '-... ', '-.-. ', '-.. ', '. ', '..-. ', '--. ', '.... ', '.. ', '.--- ', '-.- ', '.-.. ', '-- ', '-. ', '--- ', '.--. ', '--.- ', '.-. ', '... ', '- ', '..- ', '...- ', '.-- ', '-..- ', '-.-- ', '--.. ', '.- ', '-... ', '-.-. ', '-.. ', '. ', '..-. ', '--. ', '.... ', '.. ', '.--- ', '-.- ', '.-.. ', '-- ', '-. ', '--- ', '.--. ', '--.- ', '.-. ', '... ', '- ', '..- ', '...- ', '.-- ', '-..- ', '-.-- ', '--.. ', ' '};
text = {'1','2','3', '4', '5', '6', '7', '8', '9', '0', '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','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',' '};
%User is asked to input text
% initial_input = input('Enter text you want converted to morse code', "s");
initial_input = 'CQ CQ CQ DE N0KF';
%Input is converted to morse code
for i = 1:length(initial_input)
[~, index] = ismember(initial_input(i), text);
if index > 0
fprintf('%s', morse{index});
end
end
-.-. --.- -.-. --.- -.-. --.- -.. . -. ----- -.- ..-.
fprintf('\n');
%Morse code is converted to sound file
%The spacing between words and the spacing within letters is counted separately as they will be played at different time intervals
morsecode = string(index);
dotamount = count(morsecode, '.');
dashamount = count(morsecode, "-");
spaceamount = count(morsecode, " ");
letterspaceammount = count(morsecode, " ");
wpm = 15;
numberdot = (2*dotamount)+(5*dashamount)+(14*spaceamount)+(3*letterspaceammount);
dotduration = (60/wpm*numberdot);
%time length is set
timedot = 0:0.001:dotduration;
timedash = 0:0.001:3*dotduration;
timespace = 0:0.001:dotduration;
timeletterspace = 0:0.001:3*dotduration;
timewordspace = 0:0.001:7*dotduration;
tone_kHz = 700;
height_dot = cos(2*pi*tone_kHz*timedot);
height_dash = cos(2*pi*tone_kHz*timedash);
height_set_space = 0*timespace;
height_letter_space = 0*timeletterspace;
height_word_space = 0*timewordspace;
%sound is generated using coordinates of previous terms
for time = (1:length(morsecode2))
if morsecode2(time)=='.'
sound_signal = [sound_signal,height_dot,height_set_space];
elseif morsecode2(time)=='-'
sound_signal = [sound_signal,height_dash,height_set_space];
elseif morsecode2(time)==' '
sound_signal = [sound_signal,height_letter_space,height_set_space];
end
end
Unrecognized function or variable 'morsecode2'.
ss = sound_signal
sound(sound_signal,(1/0.001));
‘Way back in the 1990s, there was a File Excange contribution called ‘morspeak’ that I downloaded and used to tell me where my code was in a long simulation. It used the computer’s speakers to let me know, while I was off doing other things. I augmented it to include numbers and some punctuation. I still have it, however don’t frequently use it now.
.

Catégories

En savoir plus sur Audio I/O and Waveform Generation dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by