Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.68 KB | None | 0 0
  1. %% Szereg fouriera Zad1
  2. close all;clear all;clc;
  3.  
  4. fs=50;
  5. t=-5:(1/fs):15;
  6.  
  7. % sygnal x(t)
  8. xt=(abs(t)+3).*(abs(t)<=5)+0.*t.*(abs(t)>5);
  9.  
  10. Xa=zeros(size(t));
  11. Xb=zeros(size(t));
  12.  
  13. for n=1:50
  14.     an=(-10/(n*n*pi*pi))*(1-cos(n*pi));
  15.     Xa=Xa+an*cos((n*pi*t)/5);
  16.     if n==4
  17.         Xb = Xa;
  18.     end
  19. end
  20.  
  21. a0 = 5.5;
  22. Xa=(Xa+a0).*(abs(t)<=5);
  23. Xb=(Xb+a0).*(abs(t)<=5);
  24.  
  25. plot(t,xt,'.b',t,Xa,'r',t,Xb,'g');
  26.  
  27. %% Szereg fouriera Zad2
  28. close all;clear all;clc;
  29.  
  30. fs=50;
  31. t=-10:(1/fs):10;
  32.  
  33. xt=(t+2).*(t>= -2 & t<=4)+0.*t.*(t>=-4 & t<=-2);
  34.  
  35. Xa=zeros(size(t));
  36. Xb=zeros(size(t));
  37.  
  38. for n=1:100
  39.     q=n*pi;
  40.     an=(4/(q*q)*(cos(q)-cos(q/2)))*cos(q*t/4);
  41.     bn=(1/q)*(4/q*sin(q/2)-6*cos(q))*sin(q*t/4);
  42.     Xa=Xa+an+bn;
  43.     if n==10
  44.         Xb=Xa;
  45.     end
  46. end
  47.  
  48. a0=(9/4);
  49. Xa=(Xa+a0);
  50. Xb=(Xb+a0);
  51.  
  52. subplot(211),plot(t,xt,'k:');
  53. subplot(212),plot(t,Xb,'r',t,Xa,'b');
  54.  
  55. %% Szereg fouriera Zad3
  56. close all;clear all;clc;
  57.  
  58. fs=50;
  59. t=0:(1/fs):10;
  60.  
  61. xt=0.*t.*(t>-10 & t<0)+(2*t+1).*(t>=0 & t<=2)+0.*t.*(t>2 & t<10);
  62.  
  63. Xa=zeros(size(t));
  64. Xb=zeros(size(t));
  65.  
  66. for n=1:100
  67.     q=n*pi;
  68.     an=(5*sin(2*q/3)+6/q*(cos(2*q/3)-1))*cos(q*t/3);
  69.     bn=(1-5*cos(2*q/3)+6/q*sin(2*q/3))*sin(q*t/3);
  70.     Xa=Xa+(1/q)*(an+bn);
  71.     if n==10
  72.         Xb=Xa;
  73.     end
  74. end
  75.  
  76. a0=1;
  77. Xa=Xa+a0;
  78. Xb=Xb+a0;
  79.  
  80. subplot(211),plot(t,xt,'b');
  81. subplot(212),plot(t,Xb,'g',t,Xa,'r');
  82.  
  83. %% Szereg fouriera Zad4 2018_Geofizyka_termin_1
  84.  
  85. fs=50;
  86. t=-10:(1/fs):10;
  87. xt=sign(t).*t.^2.*(t>=-3 & t<=3);
  88.  
  89. Xa=zeros(size(t));
  90. Xb=zeros(size(t));
  91.  
  92. for n=1:50
  93.     bn=36*(1-cos(n*pi))/(n*n*n*pi*pi*pi)-(18*cos(n*pi)/n*pi);
  94.     Xa=Xa+bn*sin((n*pi*t)/3);
  95.     if n==5
  96.         Xb=Xa;
  97.     end
  98. end
  99.  
  100. Xa=Xa.*(abs(t)<=3);
  101. Xb=Xb.*(abs(t)<=3);
  102. t2=-3:(1/fs):3;
  103. plot(t,xt,'g:',t,Xb,'r',t,Xa,'b');
  104.  
  105. %% Szereg fouriera Zad5 2018_Geofizyka_termin_1_G2
  106.  
  107. fs=100;
  108. t=-3:(1/fs):3;
  109. xt=(t+3).*(t>=-3 & t<=-1)...
  110.     + (-2).*(abs(t)<=1)...
  111.     + (3-t).*(t>1 &t<=3);
  112.  
  113. Xa=zeros(size(t));
  114. Xb=zeros(size(t));
  115.  
  116. MSE=zeros(50,1);
  117.  
  118. for n=1:50
  119.    an=(-8/n*pi)*sin(n*pi/3)-(6/n*n*pi*pi)*(cos(n*pi)-cos(n*pi/3));
  120.    Xa=Xa+an*cos(n*pi*t/3);
  121.    if n==25
  122.       Xb=Xa;
  123.    end
  124.    MSE(n)=(1/length(t))*sqrt(sum((xt-Xa)).^2);
  125. end
  126.  
  127. subplot(212), plot(t,xt,'r:', t,Xa,'k',t,Xb,'b');
  128. subplot(211), plot((1:1:50),MSE);
  129.  
  130. %% Szereg fouriera Zad6 2018_Geofizyka_termin_1_G3
  131.  
  132. fs=50;
  133. t=-10:(1/fs):10;
  134. xt=(2+t).*(t<0)+0.*t.*(t==0)+(t-2).*(t>0);
  135.  
  136. Xa=zeros(size(t));
  137. Xb=zeros(size(t));
  138. L1=zeros(50,1);
  139.  
  140. for n=1:50
  141.     bn=(2*((2-pi)*cos(n*pi)-2)/(n*pi))*sin(n*pi);
  142.     Xa=Xa+bn;
  143.     if n==25
  144.        Xb=Xa;
  145.     end
  146.     L1(n)=(1/length(xt))*sum(abs(xt-Xa));
  147. end
  148.  
  149. subplot(211),plot(t,xt,'r:',t,Xb,'b',t,Xa,'k');
  150. subplot(212),plot((1:1:50),L1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement