Advertisement
regzarr

DSP lab10

May 4th, 2021 (edited)
914
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.32 KB | None | 0 0
  1. %decode sound to morse
  2.  
  3.     %your code goes here!!!
  4.     % sound to morse
  5.    
  6.  
  7. inputPath = 'morseCode4.wav';
  8. [Y, FS] = audioread(inputPath);
  9. info = audioinfo(inputPath);
  10.  
  11.  
  12. t = 0:seconds(1/FS):seconds(info.Duration);
  13. t = t(1:end-1);
  14.  
  15. plot(t,Y)
  16. xlabel('Time')
  17. ylabel('Audio Signal')
  18.  
  19. max = 0;
  20.  
  21. % for it = 1 :length(Y)
  22. %    if(Y(it) == 0)
  23. %       max = max + 1;
  24. %    else
  25. %        if (max ~= 0) fprintf('space of %d units\n', max); end
  26. %        max = 0;
  27. %    end
  28. % end
  29.  
  30. % for it = 1 :length(Y)
  31. %    if(Y(it) ~= 0)
  32. %       max = max + 1;
  33. %    else
  34. %        if (max ~= 0) fprintf('part length of %d units\n', max); end
  35. %        max = 0;
  36. %    end
  37. % end
  38.  
  39. spaceParts = 2000;
  40. spaceLetters = 6000;
  41. spaceWords = 12000;
  42.  
  43. lenDot = 699;
  44. lenDash = 1999;
  45.  
  46. zeroes = -1;
  47. mcode = [];
  48.  
  49. for it = 1 :length(Y)
  50.     if (zeroes == 1)
  51.        if(Y(it) == 0)
  52.            max = max + 1;
  53.        else
  54.            if (max == spaceLetters)
  55.               mcode = [mcode ' '];
  56.            end
  57.            if (max == spaceWords)
  58.               mcode = [mcode '/'];
  59.            end
  60.            max = 0;
  61.            zeroes = zeroes * -1;
  62.        end
  63.     end
  64.     if (zeroes == -1)
  65.         if(Y(it) ~= 0)
  66.           max = max + 1;
  67.         else
  68.            if (max == lenDot)
  69.                mcode = [mcode '.'];
  70.            end
  71.            if (max == lenDash)
  72.                mcode = [mcode '-'];
  73.            end
  74.            max = 0;
  75.            zeroes = zeroes * -1;
  76.         end
  77.     end
  78. end
  79.  
  80. disp(mcode);
  81.  
  82. %decode morse to text
  83. %for simplicity, the space character is encoded to '/' in Morse Code
  84. code = '-.. ... .--./.-.. .- -... ...';   %dsp labs
  85. deco = [];
  86. code = mcode;
  87. code = [code ' '];
  88. lcode = [];
  89.  
  90. for j=1:length(code)
  91.     if(strcmp(code(j),' ') || strcmp(code(j),'/'))
  92.         for i=double('a'):double('z')
  93.             letter = getfield(morse,char(i));
  94.             if strcmp(lcode, letter)
  95.                 deco = [deco char(i)];
  96.             end
  97.         end
  98.         for i=0:9
  99.             numb = getfield(morse,['n',num2str(i)]);
  100.             if strcmp(lcode, numb)
  101.                 deco = [deco, num2str(i)];
  102.             end
  103.         end
  104.         lcode = [];
  105.     else
  106.         lcode = [lcode code(j)];
  107.     end
  108.     if strcmp(code(j),'/')
  109.         deco = [deco ' '];
  110.     end
  111. end
  112.  
  113. fprintf('Decode : %s \n', deco);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement