Advertisement
makispaiktis

Fourier Series - Periodic signal - Sum of sinus waves

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