Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %decode sound to morse
- %your code goes here!!!
- % sound to morse
- inputPath = 'morseCode4.wav';
- [Y, FS] = audioread(inputPath);
- info = audioinfo(inputPath);
- t = 0:seconds(1/FS):seconds(info.Duration);
- t = t(1:end-1);
- plot(t,Y)
- xlabel('Time')
- ylabel('Audio Signal')
- max = 0;
- % for it = 1 :length(Y)
- % if(Y(it) == 0)
- % max = max + 1;
- % else
- % if (max ~= 0) fprintf('space of %d units\n', max); end
- % max = 0;
- % end
- % end
- % for it = 1 :length(Y)
- % if(Y(it) ~= 0)
- % max = max + 1;
- % else
- % if (max ~= 0) fprintf('part length of %d units\n', max); end
- % max = 0;
- % end
- % end
- spaceParts = 2000;
- spaceLetters = 6000;
- spaceWords = 12000;
- lenDot = 699;
- lenDash = 1999;
- zeroes = -1;
- mcode = [];
- for it = 1 :length(Y)
- if (zeroes == 1)
- if(Y(it) == 0)
- max = max + 1;
- else
- if (max == spaceLetters)
- mcode = [mcode ' '];
- end
- if (max == spaceWords)
- mcode = [mcode '/'];
- end
- max = 0;
- zeroes = zeroes * -1;
- end
- end
- if (zeroes == -1)
- if(Y(it) ~= 0)
- max = max + 1;
- else
- if (max == lenDot)
- mcode = [mcode '.'];
- end
- if (max == lenDash)
- mcode = [mcode '-'];
- end
- max = 0;
- zeroes = zeroes * -1;
- end
- end
- end
- disp(mcode);
- %decode morse to text
- %for simplicity, the space character is encoded to '/' in Morse Code
- code = '-.. ... .--./.-.. .- -... ...'; %dsp labs
- deco = [];
- code = mcode;
- code = [code ' '];
- lcode = [];
- for j=1:length(code)
- if(strcmp(code(j),' ') || strcmp(code(j),'/'))
- for i=double('a'):double('z')
- letter = getfield(morse,char(i));
- if strcmp(lcode, letter)
- deco = [deco char(i)];
- end
- end
- for i=0:9
- numb = getfield(morse,['n',num2str(i)]);
- if strcmp(lcode, numb)
- deco = [deco, num2str(i)];
- end
- end
- lcode = [];
- else
- lcode = [lcode code(j)];
- end
- if strcmp(code(j),'/')
- deco = [deco ' '];
- end
- end
- fprintf('Decode : %s \n', deco);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement