Advertisement
makispaiktis

5. Sound - SNR in sound

Apr 17th, 2022 (edited)
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.01 KB | None | 0 0
  1. clc
  2. clear all
  3.  
  4. [d, Fs] = audioread('kanonikosima.mp3');
  5. duration = size(d, 1) / Fs;
  6. sound(d, Fs);
  7. subplot(3, 2, 1);
  8. plot(d);
  9. xlabel('Time');
  10. ylabel('Sound');
  11. title('Original sound');
  12. display('Original sound');
  13. delay(duration);
  14. avg1 = mean(d(:, 1));
  15. power1 = avg1 ^ 2;
  16. SNR_dB_list = [-30, -40, -50, -60, -70];
  17.  
  18. for k = 1:length(SNR_dB_list)
  19.     SNR_dB = SNR_dB_list(k);
  20.     SNR = 10 ^ (SNR_dB / 10);
  21.     power2 = power1 / SNR;
  22.     avg2 = sqrt(power2);
  23.     noise = zeros(size(d, 1), size(d, 2));
  24.     for i = 1 : size(d, 1)
  25.         for j = 1 : size(d, 2)
  26.             noise(i, j) =  avg2 * randn();
  27.         end
  28.     end
  29.     d_noise = d + noise;
  30.     sound(d_noise, Fs);
  31.     subplot(3, 2, k+1);
  32.     plot(d_noise);
  33.     xlabel('Time');
  34.     ylabel('Sound');
  35.     message = "Sound with SNR = " + num2str(SNR_dB) + " dB";
  36.     title(message);
  37.     disp(message);
  38.     delay(duration);
  39. end    
  40.  
  41.  
  42.  
  43. function y = delay(duration)
  44.     c = 0;
  45.     for i = 1 : duration * 1.15 * 10^9
  46.         c = c + i;
  47.     end
  48. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement