| @@ -0,0 +1,24 @@ | |||||
| tic | |||||
| data = transferFunction(oscilloscope,functiongenerator,f_start,f_stop,amplitude,n_steps); | |||||
| toc | |||||
| figure | |||||
| ax(1)=subplot(2,1,1); | |||||
| semilogx(data.frequency,20*log10(data.magnitude)); | |||||
| legend('Magnitude') | |||||
| ylabel('Magnitude [dB]') | |||||
| xlabel('Frequency [Hz]') | |||||
| ax(2)=subplot(2,1,2); | |||||
| semilogx(data.frequency,data.phase); | |||||
| legend('Phase') | |||||
| ylabel('Phase [rad]') | |||||
| xlabel('Frequency [Hz]') | |||||
| linkaxes(ax,'x') | |||||
| arrayfun(@(x) set(x,{'TickLabelInterpreter','XGrid','YGrid','fontsize'},{'latex','on','on',12}),ax) | |||||
| %%limit in quarter pi | |||||
| yscale = 4; | |||||
| ymax = 4; | |||||
| ymin = -4; | |||||
| ystep = 1; | |||||
| numberticks = (ymin:ystep:ymax)*(pi/yscale); | |||||
| latexticks = {'$-\pi$','$-\frac{3}{4}\pi$','$-\frac{1}{2}\pi$','$-\frac{1}{4}\pi$','$0$','$\frac{1}{4}\pi$','$\frac{1}{2}\pi$','$\frac{3}{4}\pi$','$\pi$'}; | |||||
| set(ax(2), {'YLim','YTick','YTickLabel'},{[-pi,pi],numberticks,latexticks}); | |||||
| @@ -26,6 +26,12 @@ classdef Oscilloscope < Equipment | |||||
| end | end | ||||
| end | end | ||||
| function clear(sc) | |||||
| sc.messageclose; | |||||
| sc.write_unsafe('*cls'); | |||||
| flushinput(sc.tcp); | |||||
| end | |||||
| function s = get.acquisition(sc) | function s = get.acquisition(sc) | ||||
| s = sc.query('ACQ:STAT?'); | s = sc.query('ACQ:STAT?'); | ||||
| end | end | ||||
| @@ -120,6 +126,15 @@ classdef Oscilloscope < Equipment | |||||
| data.length = wave.length; | data.length = wave.length; | ||||
| end | end | ||||
| function message(sc,msg) | |||||
| sc.messageclose; | |||||
| sc.write(['DISP:DIAL:MESS ''',msg,'''']) | |||||
| end | |||||
| function messageclose(sc) | |||||
| sc.write('DISP:DIAL:CLOS'); | |||||
| end | |||||
| % function data = waveform(ch,cha) | % function data = waveform(ch,cha) | ||||
| % ch.scope.clear; | % ch.scope.clear; | ||||
| @@ -22,7 +22,7 @@ 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,'samp'); | |||||
| %% Plotting | %% Plotting | ||||
| %% Process the data | %% Process the data | ||||
| A_mean = mean(data.magnitude,1); | A_mean = mean(data.magnitude,1); | ||||
| @@ -22,7 +22,7 @@ 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,'samp'); | |||||
| %% Plotting | %% Plotting | ||||
| %% Process the data | %% Process the data | ||||
| A_mean = mean(data.magnitude,1); | A_mean = mean(data.magnitude,1); | ||||
| @@ -0,0 +1,6 @@ | |||||
| oscilloscope.run; | |||||
| functiongenerator.frequency = frequency; | |||||
| functiongenerator.voltage = amplitude; | |||||
| oscilloscope.auto; | |||||
| wave = oscilloscope.waveform; | |||||
| @@ -7,7 +7,7 @@ Fs = 1/T; | |||||
| n = 2^nextpow2(length); | n = 2^nextpow2(length); | ||||
| X = fft(wave,n,2)/n; | X = fft(wave,n,2)/n; | ||||
| idx = ceil(frequency*n/Fs); | idx = ceil(frequency*n/Fs); | ||||
| phase = mod(angle(mean(X(2,idx)))-angle(mean(X(1,idx))),pi); | |||||
| phase = mod(angle(mean(X(2,idx)))-angle(mean(X(1,idx)))+pi,2*pi)-pi; | |||||
| magnitude = abs(X(2,idx))/abs(X(1,idx)); | magnitude = abs(X(2,idx))/abs(X(1,idx)); | ||||
| end | end | ||||
| @@ -1,6 +1,12 @@ | |||||
| function data = transferFunction(oscilloscope,functiongenerator,f_start,f_stop,amplitude,n_steps,n_measurements) | |||||
| function data = transferFunction(oscilloscope,functiongenerator,f_start,f_stop,amplitude,n_steps,n_measurements,type) | |||||
| %TRANSFERFUNCTION Summary of this function goes here | %TRANSFERFUNCTION Summary of this function goes here | ||||
| % Detailed explanation goes here | % Detailed explanation goes here | ||||
| if nargin < 8 | |||||
| type = 'HRES'; | |||||
| if nargin < 7 | |||||
| n_measurements = 1; | |||||
| end | |||||
| end | |||||
| f_array = 10.^linspace(log10(f_start),log10(f_stop),n_steps); | f_array = 10.^linspace(log10(f_start),log10(f_stop),n_steps); | ||||
| functiongenerator.voltage = amplitude; | functiongenerator.voltage = amplitude; | ||||
| @@ -13,12 +19,14 @@ function data = transferFunction(oscilloscope,functiongenerator,f_start,f_stop,a | |||||
| 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.message(['Run: ' num2str(j) '/' num2str(n_measurements) ' - Step: ' num2str(i) '/' num2str(n_steps)]) | |||||
| oscilloscope.run; | oscilloscope.run; | ||||
| oscilloscope.auto; | oscilloscope.auto; | ||||
| wavedata = oscilloscope.waveform(1:2,'DEF','SAMP'); | |||||
| wavedata = oscilloscope.waveform(1:2,'DEF',type); | |||||
| [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); | ||||
| end | end | ||||
| end | end | ||||
| oscilloscope.message('Measurement Finished!'); | |||||
| %bodePlot(data.magnitude,data.phase,data.frequency) | %bodePlot(data.magnitude,data.phase,data.frequency) | ||||
| end | end | ||||