morse code encoder with sound
Show older comments
I am current trying to make a function that accepts a string of text and outputs the translated morse code then generates the sound to go along with the morse code. I have finished the part that transaltes to the text but I am currently haveing issues generating the sound. when I try the part of my code that generates sound in a seperate script file with me manually inserting the morse code it works however, it does not work when it is accepting the morse code from the part of my script that translates a string into the morse code. How do I fix this?
function morsecode=morsetranslator(text,wpm)
morse={'.---- ','..--- ','...-- ','....- ','..... ','-.... ','--... ',...
'---.. ','----. ','----- ','.- ','-... ','-.-. ','-.. ','. ',...
'..-. ','--. ','.... ','.. ','.--- ','-.- ','.-.. ','-- ','-. ',...
'--- ','.--. ','--.- ','.-. ','... ','- ','..- ','...- ','.-- ',...
'-..- ','-.-- ','--.. ','---- ','---. ',' ','.- ','-... ',...
'-.-. ','-.. ','. ','..-. ','--. ','.... ','.. ','.--- ','-.- ',...
'.-.. ','-- ','-. ','--- ','.--. ','--.- ','.-. ','... ','- ',...
'..- ','...- ','.-- ','-..- ','-.-- ','--.. '};
number_and_letter={'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'};
morsecode=[];
for i=1:length(text)
[~ , Morsecode] = ismember(text(i), number_and_letter);
morsecode=[morsecode,morse(Morsecode)];
fprintf('%s',morse{Morsecode});
end
%sound Part
morsecode2=string(morsecode);
morsecode3=strjoin(morsecode2);
alldot=count(morsecode3,'.');
alldash=count(morsecode3,'-');
allspace=count(morsecode3,' ');
letterspace=count(morsecode3,' ');
numdot=(2*alldot)+(2*alldash)+(3*alldash)+(7*(2*allspace))+(3*letterspace)
dot_duration=(60/(wpm*numdot));
t_dot=0:0.001:dot_duration;
t_dash=0:0.001:3*dot_duration;
t_code_space=0:0.001:dot_duration;
t_letter_space=0:0.001:3*dot_duration;
t_word_space=0:0.001:7*dot_duration;
y_dot=cos(2*pi*700*t_dot);
y_dash=cos(2*pi*700*t_dash);
y_code_space=0*t_code_space;
y_letter_space=0*t_letter_space;
y_word_space=0*t_word_space;
sound_signal=[];
for t=(1:length(morsecode3))
if morsecode3(t)=='.'
sound_signal=[sound_signal,y_dot,y_code_space];
elseif morsecode3(t)=='-'
sound_signal=[sound_signal,y_dash,y_code_space];
elseif morsecode3(t)==' '
sound_signal=[sound_signal,y_letter_space,y_code_space];
end
end
sound(sound_signal,(1/0.001));
end
Accepted Answer
More Answers (0)
Categories
Find more on Audio I/O and Waveform Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!