Guest User

Untitled

a guest
Jul 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. >> compare_FT(10);
  2.  
  3. function[] = compare_FT(fs)
  4.  
  5. %% signal parameters (now fake, then from lab)
  6. f = 6/(2*pi);
  7. w_d = 2*pi*f;
  8. T = 100;
  9. num_samples = T*fs;
  10.  
  11. %% vector definitions
  12. t = linspace(0, T, num_samples);
  13. xx = cos(w_d*t); % the "fake" signal
  14. dt = t(2) - t(1); % or, equivalently, dt = 1/fs
  15. % omega vector for DTFT in a specific range:
  16. ww_size = num_samples; % the bigger this size, the more points in the estimated FT
  17. w_start = 5; % start calculating the FT at this omega
  18. w_end = 7; % stop at this omega
  19. ww = linspace(w_start, w_end, ww_size);
  20. Sx = zeros(1, ww_size); % prealocate space for integration
  21.  
  22. % DTFT method
  23. for i = 1:ww_size
  24. int_value = trapz(xx.*exp(-1i*ww(i)*t))*dt;
  25. Sx(i) = int_value.*conj(int_value)/T; % estimate PSD
  26. end
  27.  
  28. %% FFT method
  29. n_fft = 2^14; % number of points in FFT (more points -> better FT discretization)
  30. signal_fft = fft(xx, n_fft); % the fft itself
  31. X2_fft = abs(signal_fft).^2; %signal_fft.*conj(signal_fft);
  32. L = length(X2_fft);
  33. Sx_fft = X2_fft(1:L/2+1)/(fs*num_samples); % proper scaling FFT to estimate PSD
  34. w_fft = 2*pi*fs*(0:L/2)/L;
  35.  
  36. %% Plot results to compare both methods
  37. figure(3);
  38. clf;
  39. box;
  40. hold on;
  41. % Plot theoretical value at top
  42. plot(ww, ones(1,length(ww))*T/4, '--', 'Color', 'g', 'LineWidth', 3.5);
  43. % Plot "direct" calculation
  44. plot(ww, Sx, '.', 'Color', 'b', 'LineWidth', 3.5);
  45. % Plot "FFT" calculation
  46. plot(w_fft, Sx_fft, '-', 'Color', 'r', 'LineWidth', 3.5);
  47. set(gca,'FontSize',18,'FontName', 'CMU Sans Serif');
  48. xlabel('omega', 'fontsize', 24, 'FontName', 'CMU Sans Serif');
  49. ylabel('$S_x$', 'fontsize', 24, 'Interpreter', 'latex', 'FontName', 'CMU Sans Serif');
  50. title('DTFT');
  51. xlim([w_start, w_end]);
  52. grid on;
  53. end
Add Comment
Please, Sign In to add comment