morse code encoder with sound

18 ビュー (過去 30 日間)
McConnell Gou
McConnell Gou 2019 年 6 月 19 日
回答済み: Star Strider 2019 年 6 月 19 日
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

採用された回答

Star Strider
Star Strider 2019 年 6 月 19 日
If you change the ‘morsecode2’ assignment and cast it to a char rather than string data type, it works:
morsecode2 = morsecode;
The string data type cast creates a (1x1) array, so in the second (‘t’) loop, the iteration limits are 1:1, so the loop is satisfied at the outset, and never iterates. Eliminating the string cast allows the ‘t’ loop to iterate through the characters in ‘morsecode3’.
Personally, I would prefer a 1 kHz tone rather than a 700 Hz tone. (A personal preference only.)
Be sure to contribute this to the File Exchange!
—————
An oldie but a goodie was the morspeak function that I originally downloaded from the File Exchange in the mid-1990s. (I didn’t write it, unfortumately.) I modified it as morspeakn to include numbers and some punctuation, typically used in CW communications. I’m attaching it here.
This call to it produces code at about 15 WPM (words per minute).
code = morspeakn('hi there 1 2 3', 0.05, 0.15);
sound(code)
I used it during long simulations, to tell me where the simulation was in its iterations, using the computer’s speakers. (I could go do something else while the simulation was running.) I had it configured so that it would first use my call to alert me, so I could then listen to the message.
73 !

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Import and Export についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by