Advertisement
makispaiktis

Fourier Series - Pulse with duty cycle = 0.5

Jul 9th, 2023
1,612
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.19 KB | None | 0 0
  1. clear all
  2. close all
  3. clc
  4.  
  5. %% 1a. Create a pulse with duty cycle = 0.5
  6. syms t n
  7. tt = linspace(0, 12, 1201);
  8. yy = zeros(1, length(tt));
  9. for i = 1 : length(tt)
  10.     t_ = tt(i);
  11.     if (0<=t_ && t_<=2) || (4<=t_ && t_<=6) || (8<=t_ && t_<=10)
  12.         yy(i) = 1;
  13.     end
  14. end
  15.  
  16. figure();
  17. plot(tt, yy);
  18. title("Fourier series");
  19. xlabel("t");
  20. hold on
  21.  
  22.  
  23. %% 1b. Find the Fourier series coefficients
  24. T = 4;  f0 = 1/T;   w0  = 2*pi*f0;
  25. N_LIMITS = [10 20 50];
  26. titles = ["Original pulse"];
  27.  
  28. for index = 1 : length(N_LIMITS)
  29.    
  30.     display('************************************************');
  31.     N_LIMIT = N_LIMITS(index);
  32.     n = 1 : N_LIMIT;
  33.     fprintf('N = %d\n', N_LIMIT);
  34.  
  35.     a0 = (1/T) * int(1, t, 0, 2)
  36.     an = (2/T) * int(1*cos(n*w0*t), t, 0, 2)
  37.     bn = (2/T) * int(1*sin(n*w0*t), t, 0, 2)
  38.  
  39.     y_appr = @(t) a0;
  40.     for i = 1 : N_LIMIT
  41.         an_ = an(i);
  42.         bn_ = bn(i);
  43.         n_ = n(i);
  44.         y_appr = y_appr + an_ * cos(n_*w0*t) + bn_ * sin(n_*w0*t);
  45.     end
  46.     fplot(t, y_appr, [0 12]);
  47.     hold on
  48.     titles(end+1) = num2str(N_LIMIT) + "-terms Fourier series";
  49.     fprintf('************************************************\n\n');
  50. end
  51.  
  52. legend(titles);
  53.  
  54.  
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement