Advertisement
makispaiktis

3. Sound - Two sided and single sided spectrum

Mar 9th, 2022 (edited)
1,179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.75 KB | None | 0 0
  1. clc
  2. clear all
  3.  
  4. % Data standard
  5. Fs = 4000;
  6. T = 1 / Fs;
  7. LEN = 8000;
  8. t = (0 : LEN-1) * T;
  9. % Selecting data
  10. A1 = 0.7;
  11. A2 = 0.9;
  12. f1 = 294;
  13. f2 = 440;
  14. s1 = A1 * cos(2*pi*f1*t);
  15. s2 = A2 * cos(2*pi*f2*t);
  16. s = s1 + s2;
  17. % Corrupt my signal s ---> create x
  18. x = s + 0.75 * randn(size(t));
  19.  
  20. % 1. Define the frequency domain based on 'Fs'
  21. f = Fs/LEN * (0:LEN/2);
  22. % 2. Calculate the fft's
  23. S = fft(s);
  24. X = fft(x);
  25. % 3. Two sided spectrums
  26. % two_sided = P2 and single_sided = P1
  27. P2_S = abs(S / LEN);
  28. P1_S = P2_S(1 : 1+LEN/2);
  29. P1_S(2:end-1) = 2 * P1_S(2:end-1);
  30. P2_X = abs(X / LEN);
  31. P1_X = P2_X(1 : 1+LEN/2);
  32. P1_X(2:end-1) = 2 * P1_X(2:end-1);
  33.  
  34. % 4. Plots (time, two sided spectrum, single sided)
  35. stop = LEN / 50;
  36. figure();
  37. title_all = "A_1=" + num2str(A1) + ", A_2=" + num2str(A2) + ...
  38.           ", f_1=" + num2str(f1) + ", f_2=" + num2str(f2);
  39. title(title_all);
  40.  
  41. subplot(3, 2, 1);
  42. plot(1000*t(1:stop), s(1:stop));
  43. title("Original in time (first samples)");
  44. xlabel('t (milliseconds)')
  45. ylabel('s(t)')
  46. subplot(3, 2, 3);
  47. plot(real(P2_S));
  48. title("Two sided spectrum - original");
  49. subplot(3, 2, 5);
  50. plot(f, real(P1_S));
  51. title("Single sided spectrum - original");
  52. xlabel('f (Hz)')
  53. ylabel('|P1_S(f)|');
  54.  
  55. subplot(3, 2, 2);
  56. plot(1000*t(1:stop), x(1:stop));
  57. title("Corrupted in time (first samples)");
  58. xlabel('t (milliseconds)')
  59. ylabel('x(t)')
  60. subplot(3, 2, 4);
  61. plot(real(P2_X));
  62. title("Two sided spectrum - corrupted");
  63. subplot(3, 2, 6);
  64. plot(f, real(P1_X));
  65. title("Single sided spectrum - corrupted");
  66. xlabel('f (Hz)')
  67. ylabel('|P1_X(f)|');
  68.  
  69. % Sound
  70. sound(s, Fs);
  71. delay(2.2);
  72. sound(x, Fs);
  73.  
  74.  
  75.  
  76. % AUXILIARY FUNCTIONS
  77. function y = delay(seconds)
  78.     c = 0;
  79.     for i = 1 : seconds * 10^9;
  80.         c = c + 1;
  81.     end
  82.     y = c;
  83. end
  84.  
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement