You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

334 lines
9.5KB

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