Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

336 rindas
9.5KB

  1. % clear;
  2. % clc;
  3. fprintf('Bode diagram of the RC-circuit\n\n');
  4. fprintf('Figure 1: Bode diagram theory+asymptote + average measurement\n')
  5. fprintf('Figure 2: Bode diagram average measurement+SD + theory\n');
  6. fprintf('Figure 3: Bode diagram average measurement+SD\n\n');
  7. fprintf('N.B. SD=0 is not displayed in the figures!\n\n');
  8. % fprintf('\nWatch out! This script searches the file in "D:\"\n\n');
  9. %
  10. % f_name=input('Filename: ','s');
  11. % f_name1=sprintf('%s%s',f_name,'_theorie');
  12. % f_name2=sprintf('%s%s',f_name,'_meting');
  13. %
  14. % wd=cd;
  15. % cd 'D:\'
  16. %
  17. % [th]=dlmread(f_name1,' ',0,0);
  18. % [met]=dlmread(f_name2,' ',0,0);
  19. %
  20. % cd(wd);
  21. f_th=th(1,:);
  22. amp_th=th(2,:);
  23. fase_th=th(3,:);
  24. asym=th(4,:);
  25. if mean(met(1,:))==0;
  26. nr_met=0;
  27. end;
  28. if mean(met(1,:))~=0;
  29. size_met=size(met);
  30. nr_met=(size_met(1)-1)/2;
  31. nr_punt=size_met(2);
  32. f_m=met(1,:);
  33. for i=1:nr_met;
  34. for j=1:nr_punt;
  35. amp_met(i,j)=met(i+1,j);
  36. fase_met(i,j)=met(i+1+nr_met,j);
  37. end;
  38. end;
  39. if nr_met~=1;
  40. for i=1:nr_punt;
  41. av_amp(i)=mean(amp_met(:,i));
  42. av_log_amp(i)=20*log10(av_amp(i));
  43. av_fase(i)=mean(fase_met(:,i));
  44. end;
  45. sd_amp=std(amp_met,0,1);
  46. for i=1:nr_punt;
  47. if sd_amp(i)==0;
  48. sd_log_amp(i)=0;
  49. elseif av_amp(i)==1;
  50. av_amp(i)=1.0001;
  51. else
  52. sd_log_amp(i)=2/log(av_amp(i))*sd_amp(i);
  53. end;
  54. end;
  55. sd_fase=std(fase_met,0,1);
  56. else
  57. av_log_amp=20*log10(amp_met);
  58. av_fase=fase_met;
  59. sd_log_amp=zeros(1,nr_punt);
  60. sd_fase=zeros(1,nr_punt);
  61. end;
  62. end;
  63. figure(1);
  64. set(clf,'PaperType','A4');
  65. set(clf,'Color',[1,1,1]);
  66. %set(clf,'PaperPosition',[1.13386 3.97146 5.5 4.125]);
  67. subplot(2,1,1);
  68. if mean(met(1,:))~=0;
  69. graf=semilogx(f_th,amp_th,'r',f_th,asym,'g',f_m,av_log_amp,'.-b','LineWidth',1);
  70. legend([graf],'theory','asymp.','measurement',3);
  71. else
  72. graf=semilogx(f_th,amp_th,'r',f_th,asym,'g','LineWidth',1);
  73. legend([graf],'theory','asymp.',3);
  74. end;
  75. ylabel('Amplitude [dB]','Fontsize',10)
  76. % grid bepaling
  77. startf=floor(log10(f_th(1)));
  78. sizef=size(f_th);
  79. endf=ceil(log10(f_th(sizef(2))));
  80. xf(1)=10^startf;
  81. i=startf+1;
  82. count=2;
  83. while i<=endf;
  84. xf(count)=10^(i);
  85. count=count+1;
  86. i=i+1;
  87. end;
  88. set(gca, 'XTick',xf,'Fontsize',9);
  89. if mean(met(1,:))~=0;
  90. min_amp_th=floor(min(amp_th));
  91. min_amp_met=floor(min(av_log_amp-abs(sd_log_amp)));
  92. min_amp=min(min_amp_th,min_amp_met);
  93. max_amp_th=ceil(max(amp_th));
  94. max_amp_met=ceil(max(av_log_amp+abs(sd_log_amp)));
  95. max_amp=max(max_amp_th,max_amp_met)+2;
  96. else
  97. min_amp=floor(min(amp_th));
  98. max_amp=ceil(max(amp_th))+2;
  99. end;
  100. axis([10^startf,10^endf,min_amp,max_amp]);
  101. grid on;
  102. title('Bode diagram RC-circuit','Fontsize',10,'Fontweight','bold');
  103. subplot(2,1,2);
  104. if mean(met(1,:))~=0;
  105. graf=semilogx(f_th,fase_th,'r',f_m,av_fase,'.-b','LineWidth',1);
  106. legend([graf],'theory','measurement',3);
  107. else
  108. graf=semilogx(f_th,fase_th,'LineWidth',1);
  109. legend([graf],'theory',3);
  110. end;
  111. xlabel('Frequency [Hz]','Fontsize',10);
  112. ylabel('Phase [rad]','Fontsize',10);
  113. set(gca, 'XTick',xf,'Fontsize',9);
  114. if mean(met(1,:))~=0;
  115. min_fase_th=floor(min(fase_th));
  116. min_fase_met=floor(min(av_fase-abs(sd_fase)));
  117. min_fase=min(min_fase_th,min_fase_met);
  118. max_fase_th=ceil(max(fase_th));
  119. max_fase_met=ceil(max(av_fase+abs(sd_fase)));
  120. max_fase=max(max_fase_th,max_fase_met);
  121. else
  122. min_fase=floor(min(fase_th));
  123. max_fase=ceil(max(fase_th));
  124. end;
  125. axis([10^startf,10^endf,min_fase,max_fase]);
  126. grid on;
  127. if nr_met~=0;
  128. figure(2);
  129. set(clf,'PaperType','A4');
  130. set(clf,'Color',[1,1,1]);
  131. % set(clf,'PaperPosition',[1.13386 3.97146 5.5 4.125]);
  132. subplot(2,1,1);
  133. graf=semilogx(f_m,av_log_amp,'.-b',f_th,amp_th,'-r','LineWidth',1);
  134. legend([graf],'measurement','theory',3);
  135. hold on;
  136. width_sd=(log10(f_m(2))-log10(f_m(1)))/3;
  137. for i=1:nr_punt;
  138. if sd_log_amp(i)~=0;
  139. x=[f_m(i),f_m(i)];
  140. y=[av_log_amp(i)+sd_log_amp(i),av_log_amp(i)-sd_log_amp(i)];
  141. xl1=[10^(log(f_m(i))/log(10)-width_sd),10^(log(f_m(i))/log(10)+width_sd)];
  142. yl1=[av_log_amp(i)+sd_log_amp(i),av_log_amp(i)+sd_log_amp(i)];
  143. yl2=[av_log_amp(i)-sd_log_amp(i),av_log_amp(i)-sd_log_amp(i)];
  144. plot(x,y,'-b');
  145. plot(xl1,yl1,'-b');
  146. plot(xl1,yl2,'-b');
  147. end;
  148. end;
  149. hold off;
  150. ylabel('Amplitude [dB]','Fontsize',10);
  151. % grid bepaling
  152. startfm=floor(log10(f_m(1)));
  153. sizefm=size(f_m);
  154. endfm=ceil(log10(f_m(sizefm(2))));
  155. % xfm(1)=10^startfm;
  156. xfm(1)=f_m(1);
  157. i=startfm+1;
  158. countm=2;
  159. flag=0;
  160. while flag~=1;
  161. if xfm(countm-1)<f_m(nr_punt);
  162. xfm(countm)=10^(i);
  163. countm=countm+1;
  164. i=i+1;
  165. else flag=1;
  166. end;
  167. end;
  168. xfm(countm-1)=f_m(nr_punt);
  169. % set(gca,'XTick',xfm,'Fontsize',9);
  170. fmin_th=f_th(1);
  171. count_min=1;
  172. while fmin_th<f_m(1);
  173. count_min=count_min+1;
  174. fmin_th=f_th(count_min);
  175. end;
  176. size_fth=size(f_th);
  177. size_fm=size(f_m);
  178. fmax_th=f_th(size_fth(2));
  179. count_max=size_fth(2);
  180. while fmax_th>f_m(size_fm(2));
  181. count_max=count_max-1;
  182. fmax_th=f_th(count_max);
  183. end;
  184. amp_th2=amp_th(count_min:count_max);
  185. min_amp_met=floor(min(av_log_amp-abs(sd_log_amp)));
  186. min_amp_th2=floor(min(amp_th2));
  187. max_amp_met=ceil(max(av_log_amp+abs(sd_log_amp)));
  188. max_amp_th2=ceil(max(amp_th2));
  189. min_amp_met2=floor(min(min_amp_met,min_amp_th2))-2;
  190. max_amp_met2=ceil(max(max_amp_met,max_amp_th2))+2;
  191. set(gca,'Fontsize',9);
  192. axis([xfm(1),xfm(countm-1),min_amp_met2,max_amp_met2]);
  193. grid on;
  194. title('Bode diagram RC-circuit','Fontsize',10,'Fontweight','bold');
  195. subplot(2,1,2);
  196. graf=semilogx(f_m,av_fase,'.-b',f_th,fase_th,'-r','LineWidth',1);
  197. legend([graf],'measurement','theory',3);
  198. hold on;
  199. width_sd=(log10(f_m(2))-log10(f_m(1)))/3;
  200. for i=1:nr_punt;
  201. if sd_fase(i)~=0;
  202. x=[f_m(i),f_m(i)];
  203. y=[av_fase(i)+sd_fase(i),av_fase(i)-sd_fase(i)];
  204. xl1=[10^(log10(f_m(i))-width_sd),10^(log10(f_m(i))+width_sd)];
  205. yl1=[av_fase(i)+sd_fase(i),av_fase(i)+sd_fase(i)];
  206. yl2=[av_fase(i)-sd_fase(i),av_fase(i)-sd_fase(i)];
  207. plot(x,y,'-b');
  208. plot(xl1,yl1,'-b');
  209. plot(xl1,yl2,'-b');
  210. end;
  211. end;
  212. hold off;
  213. xlabel('Frequency [Hz]','Fontsize',10);
  214. ylabel('Phase [rad]','Fontsize',10);
  215. % set(gca, 'XTick',xfm,'Fontsize',9);
  216. min_fase_met=floor(min(av_fase-abs(sd_fase)));
  217. max_fase_met=ceil(max(av_fase+abs(sd_fase)));
  218. fase_th2=fase_th(count_min:count_max);
  219. min_fase_th2=floor(min(fase_th2));
  220. max_fase_th2=ceil(max(fase_th2));
  221. min_fase_met2=floor(min(min_fase_met,min_fase_th2))-1;
  222. max_fase_met2=ceil(max(max_fase_met,max_fase_th2));
  223. set(gca,'Fontsize',9);
  224. axis([f_m(1),f_m(nr_punt),min_fase_met2,max_fase_met2]);
  225. grid on;
  226. end;
  227. if nr_met~=0;
  228. figure(3);
  229. set(clf,'PaperType','A4');
  230. set(clf,'Color',[1,1,1]);
  231. % set(clf,'PaperPosition',[1.13386 3.97146 5.5 4.125]);
  232. subplot(2,1,1);
  233. graf=semilogx(f_m,av_log_amp,'.-b','LineWidth',1);
  234. % legend([graf],'meting',3);
  235. hold on;
  236. width_sd=(log10(f_m(2))-log10(f_m(1)))/3;
  237. for i=1:nr_punt;
  238. if sd_log_amp(i)~=0;
  239. x=[f_m(i),f_m(i)];
  240. y=[av_log_amp(i)+sd_log_amp(i),av_log_amp(i)-sd_log_amp(i)];
  241. xl1=[10^(log(f_m(i))/log(10)-width_sd),10^(log(f_m(i))/log(10)+width_sd)];
  242. yl1=[av_log_amp(i)+sd_log_amp(i),av_log_amp(i)+sd_log_amp(i)];
  243. yl2=[av_log_amp(i)-sd_log_amp(i),av_log_amp(i)-sd_log_amp(i)];
  244. plot(x,y,'-b');
  245. plot(xl1,yl1,'-b');
  246. plot(xl1,yl2,'-b');
  247. end;
  248. end;
  249. hold off;
  250. ylabel('Amplitude [dB]','Fontsize',10);
  251. % grid bepaling
  252. startfm=floor(log10(f_m(1)));
  253. sizefm=size(f_m);
  254. endfm=ceil(log10(f_m(sizefm(2))));
  255. % xfm(1)=10^startfm;
  256. xfm(1)=f_m(1);
  257. i=startfm+1;
  258. countm=2;
  259. flag=0;
  260. while flag~=1;
  261. if xfm(countm-1)<f_m(nr_punt);
  262. xfm(countm)=10^(i);
  263. countm=countm+1;
  264. i=i+1;
  265. else flag=1;
  266. end;
  267. end;
  268. xfm(countm-1)=f_m(nr_punt);
  269. % set(gca,'XTick',xfm,'Fontsize',9);
  270. min_amp_met=floor(min(av_log_amp-abs(sd_log_amp)))-2;
  271. max_amp_met=ceil(max(av_log_amp+abs(sd_log_amp)))+2;
  272. set(gca,'Fontsize',9);
  273. axis([xfm(1),xfm(countm-1),min_amp_met,max_amp_met]);
  274. grid on;
  275. title('Bode diagram RC-circuit (measurement)','Fontsize',10,'Fontweight','bold');
  276. subplot(2,1,2);
  277. graf=semilogx(f_m,av_fase,'.-b','LineWidth',1);
  278. % legend([graf],'meting',3);
  279. hold on;
  280. width_sd=(log10(f_m(2))-log10(f_m(1)))/3;
  281. for i=1:nr_punt;
  282. if sd_fase(i)~=0;
  283. x=[f_m(i),f_m(i)];
  284. y=[av_fase(i)+sd_fase(i),av_fase(i)-sd_fase(i)];
  285. xl1=[10^(log10(f_m(i))-width_sd),10^(log10(f_m(i))+width_sd)];
  286. yl1=[av_fase(i)+sd_fase(i),av_fase(i)+sd_fase(i)];
  287. yl2=[av_fase(i)-sd_fase(i),av_fase(i)-sd_fase(i)];
  288. plot(x,y,'-b');
  289. plot(xl1,yl1,'-b');
  290. plot(xl1,yl2,'-b');
  291. end;
  292. end;
  293. hold off;
  294. xlabel('Frequency [Hz]','Fontsize',10);
  295. ylabel('Phase [rad]','Fontsize',10);
  296. % set(gca, 'XTick',xfm,'Fontsize',9);
  297. min_fase_met=floor(min(av_fase-abs(sd_fase)));
  298. max_fase_met=ceil(max(av_fase+abs(sd_fase)))+2;
  299. set(gca,'Fontsize',9);
  300. axis([f_m(1),f_m(nr_punt),min_fase,max_fase]);
  301. grid on;
  302. end;