diff --git a/OOequipment/RC_TransferFunction.mlx b/OOequipment/RC_TransferFunction.mlx index 03fce8b..0d34e1c 100644 Binary files a/OOequipment/RC_TransferFunction.mlx and b/OOequipment/RC_TransferFunction.mlx differ diff --git a/OOequipment/RLC_TransferFunction.mlx b/OOequipment/RLC_TransferFunction.mlx index cf69d6d..f9f205a 100644 Binary files a/OOequipment/RLC_TransferFunction.mlx and b/OOequipment/RLC_TransferFunction.mlx differ diff --git a/OOequipment/initializeEquipment.m b/OOequipment/initializeEquipment.m index a281035..78b196d 100644 --- a/OOequipment/initializeEquipment.m +++ b/OOequipment/initializeEquipment.m @@ -1,9 +1,9 @@ -addpath('subfiles'); +if ~exist('theory_only','var') || theory_only == false + if ~exist('functiongenerator','var') + functiongenerator = FunctionGenerator('10.0.0.3',1234,10); + end -if ~exist('functiongenerator','var') - functiongenerator = FunctionGenerator('10.0.0.3',1234,10); -end - -if ~exist('oscilloscope','var') - oscilloscope = Oscilloscope('10.0.0.2',5025,2); + if ~exist('oscilloscope','var') + oscilloscope = Oscilloscope('10.0.0.2',5025,2); + end end \ No newline at end of file diff --git a/OOequipment/subfiles/Oscilloscope.m b/OOequipment/subfiles/Oscilloscope.m index e484385..e599c82 100644 --- a/OOequipment/subfiles/Oscilloscope.m +++ b/OOequipment/subfiles/Oscilloscope.m @@ -72,15 +72,18 @@ classdef Oscilloscope < Equipment m = str2double(sc.query(['MEAS',num2str(measurement),':RES:ACT?'])); end - function setWaveformSettings(sc,window,format) - if nargin < 3 - format = 'REAL'; - if nargin < 2 - window = 'DEF'; + function setWaveformSettings(sc,window,format,type) + if nargin < 4 + type = 'HRES'; + if nargin < 3 + format = 'REAL'; + if nargin < 2 + window = 'DEF'; + end end end sc.clear; - sc.write('CHAN:TYPE HRES'); + sc.write(['CHAN:TYPE ',type]); sc.write(['FORM ',format]); sc.write('FORM:BORD MSBF'); sc.write(['CHAN:DATA:POIN ',window]); @@ -96,15 +99,18 @@ classdef Oscilloscope < Equipment end - function data = waveform(sc,channels,window) - if nargin < 3 - window = 'DEF'; - if nargin < 2 - channels = 1:sc.nchannels; + function data = waveform(sc,channels,window,type) + if nargin < 4 + type = 'HRES'; + if nargin < 3 + window = 'DEF'; + if nargin < 2 + channels = 1:sc.nchannels; + end end end sc.enable_channels; - sc.setWaveformSettings(window); + sc.setWaveformSettings(window,'REAL',type); for i = channels curcha = ['ch',num2str(i)]; wave = sc.(curcha).getWaveform; diff --git a/OOequipment/subfiles/RC_TransferFunction_script.m b/OOequipment/subfiles/RC_TransferFunction_script.m index 8ccf40f..de8eb9a 100644 --- a/OOequipment/subfiles/RC_TransferFunction_script.m +++ b/OOequipment/subfiles/RC_TransferFunction_script.m @@ -1,20 +1,88 @@ -%% Run the measurements -data = transferFunction(oscilloscope,functiongenerator,f_start,f_stop,n_steps,amplitude); - -%% plot the data +%% Plot theory only S = asymptote(S_smaller,S_larger,f,Fc); P = mod(P,pi); + figure; -subplot(2,1,1) -semilogx(data.frequency,data.magnitude,f,A,f,S) +ax1 = subplot(2,1,1); +semilogx(f,A,f,S) ylabel('Magnitude [dB]','Fontsize',10); xlabel('Frequency [Hz]','Fontsize',10); -legend('Measurement','Theory','Asymptote'); +xlim([f_start,f_stop]); +legend('Theory','Asymptote'); grid on -title('RC-circuit Bodeplot') -subplot(2,1,2); -semilogx(data.frequency,data.phase,f,P) +title('RC-circuit Bodeplot Theory') +ax2 = subplot(2,1,2); +semilogx(f,P) ylabel('Phase [rad]','Fontsize',10); xlabel('Frequency [Hz]','Fontsize',10); -legend('Measurement','Theory'); -grid on \ No newline at end of file +xlim([f_start,f_stop]); +legend('Theory'); +grid on +linkaxes([ax1,ax2],'x') + +%% Run the measurements +if theory_only == false + data = transferFunction(oscilloscope,functiongenerator,f_start,f_stop,amplitude,n_steps,n_measurements); + %% Process the data + A_mean = mean(data.magnitude); + P_mean = mean(data.phase); + A_std = std(data.magnitude); + P_std = std(data.phase); + + %% plot average measurement, theory and asymptote. + figure; + ax1 = subplot(2,1,1); + semilogx(data.frequency,20*log10(A_mean),f,A,f,S) + ylabel('Magnitude [dB]','Fontsize',10); + xlabel('Frequency [Hz]','Fontsize',10); + legend('Average Measurement','Theory','Asymptote'); + grid on + title('RC-circuit Bodeplot') + ax2 = subplot(2,1,2); + semilogx(data.frequency,P_mean,f,P) + ylabel('Phase [rad]','Fontsize',10); + xlabel('Frequency [Hz]','Fontsize',10); + legend('Average Measurement','Theory'); + grid on + + %% plot avearage measurement with standard deviation and theory. + figure; + ax1 = subplot(2,1,1); + ax1.XScale = 'log'; + errorbar(data.frequency,20*log10(A_mean),20*log10(A_std)) + hold on + semilogx(f,A) + hold off + ylabel('Magnitude [dB]','Fontsize',10); + xlabel('Frequency [Hz]','Fontsize',10); + legend('Average Measurement','Theory','Asymptote'); + grid on + title('RC-circuit Bodeplot') + ax2 = subplot(2,1,2); + ax2.XScale = 'log'; + errorbar(data.frequency,P_mean,P_std); + hold on + semilogx(data.frequency,P_mean,f,P) + hold off + ylabel('Phase [rad]','Fontsize',10); + xlabel('Frequency [Hz]','Fontsize',10); + legend('Average Measurement','Theory'); + grid on + %% plot avearage measurement with standard deviation. + figure; + ax1 = subplot(2,1,1); + ax1.XScale = 'log'; + errorbar(data.frequency,20*log10(A_mean),20*log10(A_std)) + ylabel('Magnitude [dB]','Fontsize',10); + xlabel('Frequency [Hz]','Fontsize',10); + legend('Average Measurement'); + grid on + title('RC-circuit Bodeplot') + ax2 = subplot(2,1,2); + ax2.XScale = 'log'; + errorbar(data.frequency,P_mean,P_std); + ylabel('Phase [rad]','Fontsize',10); + xlabel('Frequency [Hz]','Fontsize',10); + legend('Average Measurement'); + grid on +end \ No newline at end of file diff --git a/OOequipment/subfiles/RLC_TransferFunction_script.m b/OOequipment/subfiles/RLC_TransferFunction_script.m index bcaa57c..944a171 100644 --- a/OOequipment/subfiles/RLC_TransferFunction_script.m +++ b/OOequipment/subfiles/RLC_TransferFunction_script.m @@ -1,25 +1,85 @@ -%% Run the measurements -data = transferFunction(oscilloscope,functiongenerator,f_start,f_stop,n_steps,amplitude); -%% plot the data +%% Plot theory only S = asymptote(S_smaller,S_larger,f,Fc); P = mod(P,pi); + figure; -subplot(2,1,1) -semilogx(data.frequency,data.magnitude) -hold on +subplot(2,1,1); semilogx(f,A,f,S) -hold off ylabel('Magnitude [dB]','Fontsize',10); xlabel('Frequency [Hz]','Fontsize',10); -legend('Measurement','Theory','Asymptote'); +legend('Theory','Asymptote'); grid on -title('RLC-circuit Bodeplot') +title('RLC-circuit Bodeplot Theory') subplot(2,1,2); -semilogx(data.frequency,data.phase) -hold on semilogx(f,P) -hold off ylabel('Phase [rad]','Fontsize',10); xlabel('Frequency [Hz]','Fontsize',10); -legend('Measurement','Theory'); -grid on \ No newline at end of file +legend('Theory'); +grid on + +%% Run the measurements +if theory_only == false + data = transferFunction(oscilloscope,functiongenerator,f_start,f_stop,amplitude,n_steps,n_measurements); + %% Process the data + A_mean = mean(data.magnitude); + P_mean = mean(data.phase); + A_std = std(data.magnitude); + P_std = std(data.phase); + + %% plot average measurement, theory and asymptote. + figure; + subplot(2,1,1); + semilogx(data.frequency,20*log10(A_mean),f,A,f,S) + ylabel('Magnitude [dB]','Fontsize',10); + xlabel('Frequency [Hz]','Fontsize',10); + legend('Average Measurement','Theory','Asymptote'); + grid on + title('RLC-circuit Bodeplot') + subplot(2,1,2); + semilogx(data.frequency,P_mean,f,P) + ylabel('Phase [rad]','Fontsize',10); + xlabel('Frequency [Hz]','Fontsize',10); + legend('Average Measurement','Theory'); + grid on + + %% plot avearage measurement with standard deviation and theory. + figure; + ax = subplot(2,1,1); + ax.XScale = 'log'; + errorbar(data.frequency,20*log10(A_mean),20*log10(A_std)) + hold on + semilogx(f,A) + hold off + ylabel('Magnitude [dB]','Fontsize',10); + xlabel('Frequency [Hz]','Fontsize',10); + legend('Average Measurement','Theory','Asymptote'); + grid on + title('RLC-circuit Bodeplot') + ax = subplot(2,1,2); + ax.XScale = 'log'; + errorbar(data.frequency,P_mean,P_std); + hold on + semilogx(data.frequency,P_mean,f,P) + hold off + ylabel('Phase [rad]','Fontsize',10); + xlabel('Frequency [Hz]','Fontsize',10); + legend('Average Measurement','Theory'); + grid on + %% plot avearage measurement with standard deviation. + figure; + ax = subplot(2,1,1); + ax.XScale = 'log'; + errorbar(data.frequency,20*log10(A_mean),20*log10(A_std)) + ylabel('Magnitude [dB]','Fontsize',10); + xlabel('Frequency [Hz]','Fontsize',10); + legend('Average Measurement'); + grid on + title('RLC-circuit Bodeplot') + ax = subplot(2,1,2); + ax.XScale = 'log'; + errorbar(data.frequency,P_mean,P_std); + ylabel('Phase [rad]','Fontsize',10); + xlabel('Frequency [Hz]','Fontsize',10); + legend('Average Measurement'); + grid on +end \ No newline at end of file diff --git a/OOequipment/subfiles/phamag.m b/OOequipment/subfiles/phamag.m index 2556382..27fcd6e 100644 --- a/OOequipment/subfiles/phamag.m +++ b/OOequipment/subfiles/phamag.m @@ -8,6 +8,6 @@ n = 2^nextpow2(length); X = fft(wave,n,2)/n; idx = ceil(frequency*n/Fs); phase = mod(angle(mean(X(2,idx)))-angle(mean(X(1,idx))),pi); -magnitude = 20*log10(abs(X(2,idx))/abs(X(1,idx))); +magnitude = abs(X(2,idx))/abs(X(1,idx)); end diff --git a/OOequipment/subfiles/transferFunction.m b/OOequipment/subfiles/transferFunction.m index 3a6383b..131bf52 100644 --- a/OOequipment/subfiles/transferFunction.m +++ b/OOequipment/subfiles/transferFunction.m @@ -1,4 +1,4 @@ -function data = transferFunction(oscilloscope,functiongenerator,f_start,f_stop,n_steps,amplitude) +function data = transferFunction(oscilloscope,functiongenerator,f_start,f_stop,amplitude,n_steps,n_measurements) %TRANSFERFUNCTION Summary of this function goes here % Detailed explanation goes here @@ -7,14 +7,16 @@ function data = transferFunction(oscilloscope,functiongenerator,f_start,f_stop,n functiongenerator.waveform = 'SINUSOID'; oscilloscope.enable_channels; oscilloscope.trigger.source = 'CH1'; - emptydata = zeros(n_steps,1); + emptydata = zeros(n_measurements,n_steps); data = struct('magnitude',emptydata,'phase',emptydata,'frequency',f_array); - for i = 1:n_steps - fprintf('Measurement %i of %i - Frequency: %.2f Hertz\n',i,n_steps,f_array(i)); - functiongenerator.frequency = f_array(i); - oscilloscope.auto; - wavedata = oscilloscope.waveform(1:2); - [data.phase(i),data.magnitude(i)] = phamag(wavedata.ch1,wavedata.ch2,wavedata.length,f_array(i),wavedata.sampletime); + for j = 1:n_measurements + for i = 1:n_steps +% fprintf('Measurement %i of %i - Frequency: %.2f Hertz\n',i,n_steps,f_array(i)); + functiongenerator.frequency = f_array(i); + oscilloscope.auto; + 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); + end end %bodePlot(data.magnitude,data.phase,data.frequency) end