| @@ -23,16 +23,20 @@ linkaxes([ax1,ax2],'x') | |||||
| %% Run the measurements | %% Run the measurements | ||||
| if theory_only == false | if theory_only == false | ||||
| data = transferFunction(oscilloscope,functiongenerator,f_start,f_stop,amplitude,n_steps,n_measurements); | data = transferFunction(oscilloscope,functiongenerator,f_start,f_stop,amplitude,n_steps,n_measurements); | ||||
| %% Plotting | |||||
| %% Process the data | %% Process the data | ||||
| A_mean = mean(data.magnitude); | A_mean = mean(data.magnitude); | ||||
| P_mean = mean(data.phase); | P_mean = mean(data.phase); | ||||
| A_std = std(data.magnitude); | A_std = std(data.magnitude); | ||||
| P_std = std(data.phase); | P_std = std(data.phase); | ||||
| A_mean_log = 20*log10(A_mean); | |||||
| A_std_log_neg = A_mean_log - 20*log10(A_mean - A_std); | |||||
| A_std_log_pos = 20*log10(A_mean + A_std) - A_mean_log; | |||||
| %% plot average measurement, theory and asymptote. | %% plot average measurement, theory and asymptote. | ||||
| figure; | figure; | ||||
| ax1 = subplot(2,1,1); | ax1 = subplot(2,1,1); | ||||
| semilogx(data.frequency,20*log10(A_mean),f,A,f,S) | |||||
| semilogx(data.frequency,A_mean_log,f,A,f,S) | |||||
| ylabel('Magnitude [dB]','Fontsize',10); | ylabel('Magnitude [dB]','Fontsize',10); | ||||
| xlabel('Frequency [Hz]','Fontsize',10); | xlabel('Frequency [Hz]','Fontsize',10); | ||||
| legend('Average Measurement','Theory','Asymptote'); | legend('Average Measurement','Theory','Asymptote'); | ||||
| @@ -49,21 +53,21 @@ if theory_only == false | |||||
| %% plot avearage measurement with standard deviation and theory. | %% plot avearage measurement with standard deviation and theory. | ||||
| figure; | figure; | ||||
| ax1 = subplot(2,1,1); | ax1 = subplot(2,1,1); | ||||
| errorbar(data.frequency,A_mean_log,A_std_log_neg,A_std_log_pos) | |||||
| ax1.XScale = 'log'; | ax1.XScale = 'log'; | ||||
| errorbar(data.frequency,20*log10(A_mean),20*log10(A_std)) | |||||
| hold on | hold on | ||||
| semilogx(f,A) | semilogx(f,A) | ||||
| hold off | hold off | ||||
| ylabel('Magnitude [dB]','Fontsize',10); | ylabel('Magnitude [dB]','Fontsize',10); | ||||
| xlabel('Frequency [Hz]','Fontsize',10); | xlabel('Frequency [Hz]','Fontsize',10); | ||||
| legend('Average Measurement','Theory','Asymptote'); | |||||
| legend('Average Measurement','Theory'); | |||||
| grid on | grid on | ||||
| title('RC-circuit Bodeplot') | title('RC-circuit Bodeplot') | ||||
| ax2 = subplot(2,1,2); | ax2 = subplot(2,1,2); | ||||
| ax2.XScale = 'log'; | |||||
| errorbar(data.frequency,P_mean,P_std); | errorbar(data.frequency,P_mean,P_std); | ||||
| ax2.XScale = 'log'; | |||||
| hold on | hold on | ||||
| semilogx(data.frequency,P_mean,f,P) | |||||
| semilogx(f,P) | |||||
| hold off | hold off | ||||
| ylabel('Phase [rad]','Fontsize',10); | ylabel('Phase [rad]','Fontsize',10); | ||||
| xlabel('Frequency [Hz]','Fontsize',10); | xlabel('Frequency [Hz]','Fontsize',10); | ||||
| @@ -73,16 +77,16 @@ if theory_only == false | |||||
| %% plot avearage measurement with standard deviation. | %% plot avearage measurement with standard deviation. | ||||
| figure; | figure; | ||||
| ax1 = subplot(2,1,1); | ax1 = subplot(2,1,1); | ||||
| errorbar(data.frequency,A_mean_log,A_std_log_neg,A_std_log_pos) | |||||
| ax1.XScale = 'log'; | ax1.XScale = 'log'; | ||||
| errorbar(data.frequency,20*log10(A_mean),20*log10(A_std)) | |||||
| ylabel('Magnitude [dB]','Fontsize',10); | ylabel('Magnitude [dB]','Fontsize',10); | ||||
| xlabel('Frequency [Hz]','Fontsize',10); | xlabel('Frequency [Hz]','Fontsize',10); | ||||
| legend('Average Measurement'); | legend('Average Measurement'); | ||||
| grid on | grid on | ||||
| title('RC-circuit Bodeplot') | title('RC-circuit Bodeplot') | ||||
| ax2 = subplot(2,1,2); | ax2 = subplot(2,1,2); | ||||
| ax2.XScale = 'log'; | |||||
| errorbar(data.frequency,P_mean,P_std); | errorbar(data.frequency,P_mean,P_std); | ||||
| ax2.XScale = 'log'; | |||||
| ylabel('Phase [rad]','Fontsize',10); | ylabel('Phase [rad]','Fontsize',10); | ||||
| xlabel('Frequency [Hz]','Fontsize',10); | xlabel('Frequency [Hz]','Fontsize',10); | ||||
| legend('Average Measurement'); | legend('Average Measurement'); | ||||
| @@ -10,7 +10,7 @@ xlabel('Frequency [Hz]','Fontsize',10); | |||||
| xlim([f_start,f_stop]); | xlim([f_start,f_stop]); | ||||
| legend('Theory','Asymptote'); | legend('Theory','Asymptote'); | ||||
| grid on | grid on | ||||
| title('RLC-circuit Bodeplot Theory') | |||||
| title('RC-circuit Bodeplot Theory') | |||||
| ax2 = subplot(2,1,2); | ax2 = subplot(2,1,2); | ||||
| semilogx(f,P) | semilogx(f,P) | ||||
| ylabel('Phase [rad]','Fontsize',10); | ylabel('Phase [rad]','Fontsize',10); | ||||
| @@ -23,16 +23,24 @@ linkaxes([ax1,ax2],'x') | |||||
| %% Run the measurements | %% Run the measurements | ||||
| if theory_only == false | if theory_only == false | ||||
| data = transferFunction(oscilloscope,functiongenerator,f_start,f_stop,amplitude,n_steps,n_measurements); | data = transferFunction(oscilloscope,functiongenerator,f_start,f_stop,amplitude,n_steps,n_measurements); | ||||
| %% Plotting | |||||
| %% Process the data | %% Process the data | ||||
| A_mean = mean(data.magnitude); | |||||
| P_mean = mean(data.phase); | |||||
| A_std = std(data.magnitude); | |||||
| P_std = std(data.phase); | |||||
| A_mean = mean(data.magnitude,1); | |||||
| P_mean = mean(data.phase,1); | |||||
| A_std = zeros(1,data.steps); | |||||
| P_std = zeros(1,data.steps); | |||||
| if data.measurements > 1 | |||||
| A_std = std(data.magnitude,1); | |||||
| P_std = std(data.phase,1); | |||||
| end | |||||
| A_mean_log = 20*log10(A_mean); | |||||
| A_std_log_neg = A_mean_log - 20*log10(A_mean - A_std); | |||||
| A_std_log_pos = 20*log10(A_mean + A_std) - A_mean_log; | |||||
| %% plot average measurement, theory and asymptote. | %% plot average measurement, theory and asymptote. | ||||
| figure; | figure; | ||||
| ax1 = subplot(2,1,1); | ax1 = subplot(2,1,1); | ||||
| semilogx(data.frequency,20*log10(A_mean),f,A,f,S) | |||||
| semilogx(data.frequency,A_mean_log,f,A,f,S) | |||||
| ylabel('Magnitude [dB]','Fontsize',10); | ylabel('Magnitude [dB]','Fontsize',10); | ||||
| xlabel('Frequency [Hz]','Fontsize',10); | xlabel('Frequency [Hz]','Fontsize',10); | ||||
| legend('Average Measurement','Theory','Asymptote'); | legend('Average Measurement','Theory','Asymptote'); | ||||
| @@ -49,21 +57,21 @@ if theory_only == false | |||||
| %% plot avearage measurement with standard deviation and theory. | %% plot avearage measurement with standard deviation and theory. | ||||
| figure; | figure; | ||||
| ax1 = subplot(2,1,1); | ax1 = subplot(2,1,1); | ||||
| errorbar(data.frequency,A_mean_log,A_std_log_neg,A_std_log_pos) | |||||
| ax1.XScale = 'log'; | ax1.XScale = 'log'; | ||||
| errorbar(data.frequency,20*log10(A_mean),20*log10(A_std)) | |||||
| hold on | hold on | ||||
| semilogx(f,A) | semilogx(f,A) | ||||
| hold off | hold off | ||||
| ylabel('Magnitude [dB]','Fontsize',10); | ylabel('Magnitude [dB]','Fontsize',10); | ||||
| xlabel('Frequency [Hz]','Fontsize',10); | xlabel('Frequency [Hz]','Fontsize',10); | ||||
| legend('Average Measurement','Theory','Asymptote'); | |||||
| legend('Average Measurement','Theory'); | |||||
| grid on | grid on | ||||
| title('RLC-circuit Bodeplot') | title('RLC-circuit Bodeplot') | ||||
| ax2 = subplot(2,1,2); | ax2 = subplot(2,1,2); | ||||
| ax2.XScale = 'log'; | |||||
| errorbar(data.frequency,P_mean,P_std); | errorbar(data.frequency,P_mean,P_std); | ||||
| ax2.XScale = 'log'; | |||||
| hold on | hold on | ||||
| semilogx(data.frequency,P_mean,f,P) | |||||
| semilogx(f,P) | |||||
| hold off | hold off | ||||
| ylabel('Phase [rad]','Fontsize',10); | ylabel('Phase [rad]','Fontsize',10); | ||||
| xlabel('Frequency [Hz]','Fontsize',10); | xlabel('Frequency [Hz]','Fontsize',10); | ||||
| @@ -73,16 +81,16 @@ if theory_only == false | |||||
| %% plot avearage measurement with standard deviation. | %% plot avearage measurement with standard deviation. | ||||
| figure; | figure; | ||||
| ax1 = subplot(2,1,1); | ax1 = subplot(2,1,1); | ||||
| errorbar(data.frequency,A_mean_log,A_std_log_neg,A_std_log_pos) | |||||
| ax1.XScale = 'log'; | ax1.XScale = 'log'; | ||||
| errorbar(data.frequency,20*log10(A_mean),20*log10(A_std)) | |||||
| ylabel('Magnitude [dB]','Fontsize',10); | ylabel('Magnitude [dB]','Fontsize',10); | ||||
| xlabel('Frequency [Hz]','Fontsize',10); | xlabel('Frequency [Hz]','Fontsize',10); | ||||
| legend('Average Measurement'); | legend('Average Measurement'); | ||||
| grid on | grid on | ||||
| title('RLC-circuit Bodeplot') | title('RLC-circuit Bodeplot') | ||||
| ax2 = subplot(2,1,2); | ax2 = subplot(2,1,2); | ||||
| ax2.XScale = 'log'; | |||||
| errorbar(data.frequency,P_mean,P_std); | errorbar(data.frequency,P_mean,P_std); | ||||
| ax2.XScale = 'log'; | |||||
| ylabel('Phase [rad]','Fontsize',10); | ylabel('Phase [rad]','Fontsize',10); | ||||
| xlabel('Frequency [Hz]','Fontsize',10); | xlabel('Frequency [Hz]','Fontsize',10); | ||||
| legend('Average Measurement'); | legend('Average Measurement'); | ||||
| @@ -8,11 +8,12 @@ function data = transferFunction(oscilloscope,functiongenerator,f_start,f_stop,a | |||||
| oscilloscope.enable_channels; | oscilloscope.enable_channels; | ||||
| oscilloscope.trigger.source = 'CH1'; | oscilloscope.trigger.source = 'CH1'; | ||||
| emptydata = zeros(n_measurements,n_steps); | emptydata = zeros(n_measurements,n_steps); | ||||
| data = struct('magnitude',emptydata,'phase',emptydata,'frequency',f_array); | |||||
| data = struct('magnitude',emptydata,'phase',emptydata,'frequency',f_array,'measurements',n_measurements,'steps',n_steps); | |||||
| for j = 1:n_measurements | for j = 1:n_measurements | ||||
| for i = 1:n_steps | for i = 1:n_steps | ||||
| % fprintf('Measurement %i of %i - Frequency: %.2f Hertz\n',i,n_steps,f_array(i)); | % fprintf('Measurement %i of %i - Frequency: %.2f Hertz\n',i,n_steps,f_array(i)); | ||||
| functiongenerator.frequency = f_array(i); | functiongenerator.frequency = f_array(i); | ||||
| oscilloscope.run; | |||||
| oscilloscope.auto; | oscilloscope.auto; | ||||
| wavedata = oscilloscope.waveform(1:2,'DEF','SAMP'); | wavedata = oscilloscope.waveform(1:2,'DEF','SAMP'); | ||||
| [data.phase(j,i),data.magnitude(j,i)] = phamag(wavedata.ch1,wavedata.ch2,wavedata.length,f_array(i),wavedata.sampletime); | [data.phase(j,i),data.magnitude(j,i)] = phamag(wavedata.ch1,wavedata.ch2,wavedata.length,f_array(i),wavedata.sampletime); | ||||