Advertisement
makispaiktis

4. Sound - Time domain and single-sided spectrum of a signal = 4 tones

Mar 9th, 2022
1,478
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. clc
  2. clear all
  3.  
  4. % Declare time
  5. Fs = 6000;                  % Max frequency of signal must be lower than Fs/2
  6. LEN = 10000;
  7. t = (0:LEN-1) / Fs;
  8. % Ranges - Define the expression of signal s(t) - find Amplitudes and
  9. % Frequencies
  10. f_ranges = 200:1000;
  11. A_ranges = 0.25:0.01:0.99;
  12. NUM = 4;
  13. f_list = f_ranges(randi(length(f_ranges), 1, NUM))
  14. A_list = A_ranges(randi(length(A_ranges), 1, NUM))
  15. s = 0;
  16. for i = 1:NUM
  17.     A = A_list(i);
  18.     f = f_list(i);
  19.     s = s + A * cos(2*pi*f*t);
  20. end
  21.  
  22.  
  23. % Create the single sided spectrum of time signal s(t)
  24. stop = LEN / 50;
  25. S = fft(s);
  26. f = Fs/LEN * (0:LEN/2);
  27. P2 = abs(S / LEN);                  % Two sided P2
  28. P1 = P2(1 : 1+LEN/2);               % Single sided P1
  29. P1(2:end-1) = 2*P1(2:end-1);
  30.  
  31. subplot(2, 1, 1);
  32. plot(1000*t(1:stop), s(1:stop));
  33. title("Time domain of 4 tones(first samples)");
  34. xlabel("t (ms)");
  35. ylabel("s(t) = 4 tones");
  36.  
  37. subplot(2, 1, 2);
  38. plot(f, P1);
  39. title("Single sided spectrum");
  40. xlabel("f (Hz)");
  41. ylabel("|P_1(f)|");
  42.  
  43. % Sound
  44. sound(s, Fs);
  45.  
Advertisement
RAW Paste Data Copied
Advertisement