| @@ -107,8 +107,12 @@ classdef Channel | |||||
| out = str2double(ch.MEAS('phas')); | out = str2double(ch.MEAS('phas')); | ||||
| end | end | ||||
| function data = waveform(ch) | |||||
| ch.scope.setWaveformSettings; | |||||
| function data = waveform(ch,window) | |||||
| ch.scope.single; | |||||
| if nargin < 2 | |||||
| window = 'DEF'; | |||||
| end | |||||
| ch.scope.setWaveformSettings(window,'REAL') | |||||
| data = ch.getWaveform; | data = ch.getWaveform; | ||||
| end | end | ||||
| @@ -120,11 +124,7 @@ classdef Channel | |||||
| data.sampletime = str2double(ch.CHAN('DATA:XINC?')); | data.sampletime = str2double(ch.CHAN('DATA:XINC?')); | ||||
| ch.scope.opc; | ch.scope.opc; | ||||
| ch.scope.write_unsafe(['CHAN',num2str(ch.channelnumber),':DATA?']); | ch.scope.write_unsafe(['CHAN',num2str(ch.channelnumber),':DATA?']); | ||||
| prefixstring = fscanf(ch.scope.tcp,'%c',2); | |||||
| prefixlength = str2double(prefixstring(2)); | |||||
| datalength = fscanf(ch.scope.tcp,'%c',prefixlength); | |||||
| data.data = fread(ch.scope.tcp,data.length,'float')'; | |||||
| flushinput(ch.scope.tcp); | |||||
| data.data = ch.scope.readWaveform(data.length); | |||||
| end | end | ||||
| end | end | ||||
| @@ -45,6 +45,11 @@ classdef Equipment < handle | |||||
| ecq.write_unsafe('*rst'); | ecq.write_unsafe('*rst'); | ||||
| end | end | ||||
| function flush(ecq) | |||||
| flushinput(ecq.tcp); | |||||
| flushoutput(ecq.tcp); | |||||
| end | |||||
| function opc(ecq) | function opc(ecq) | ||||
| %OPC executes 'operation complete query' to device. | %OPC executes 'operation complete query' to device. | ||||
| %Function holds untill device returns '1'. Must be | %Function holds untill device returns '1'. Must be | ||||
| @@ -97,6 +102,31 @@ classdef Equipment < handle | |||||
| ecq.error; | ecq.error; | ||||
| end | end | ||||
| function data = bin_read(ecq,datalength,type,typesize) | |||||
| if nargin < 4 | |||||
| typesize = 8; | |||||
| end | |||||
| data = zeros(1,datalength); | |||||
| totalread = 0; | |||||
| maxlength = floor(ecq.tcp.InputBufferSize/typesize); | |||||
| while datalength > 0 | |||||
| readlength = min(datalength,maxlength); | |||||
| [lastdata,lastcount] = fread(ecq.tcp,[1,readlength],type); | |||||
| if lastcount == 0 | |||||
| error('Data stream from device shorter than expected'); | |||||
| end | |||||
| data(totalread+1:totalread+lastcount) = lastdata; | |||||
| totalread = totalread+lastcount; | |||||
| datalength = datalength - lastcount; | |||||
| end | |||||
| flushinput(ecq.tcp) | |||||
| ecq.clear | |||||
| end | |||||
| function data = bin_read_float(ecq,datalength) | |||||
| data = bin_read(ecq,datalength,'float',4); | |||||
| end | |||||
| function output = read(ecq) | function output = read(ecq) | ||||
| %READ Extracts recieved data from the TCP-buffer. | %READ Extracts recieved data from the TCP-buffer. | ||||
| %This function sends the '++read'-command to the Prologix if | %This function sends the '++read'-command to the Prologix if | ||||
| @@ -240,6 +270,7 @@ classdef Equipment < handle | |||||
| %If only one handle uses this connection. The | %If only one handle uses this connection. The | ||||
| %connection is closed and the field is removed from the | %connection is closed and the field is removed from the | ||||
| %tcpconnection struct. | %tcpconnection struct. | ||||
| flushinput(sc.tcp); | |||||
| fclose(tcpconnection.(ipname).tcp); | fclose(tcpconnection.(ipname).tcp); | ||||
| tcpconnection = rmfield(tcpconnection,ipname); | tcpconnection = rmfield(tcpconnection,ipname); | ||||
| end | end | ||||
| @@ -72,23 +72,43 @@ classdef Oscilloscope < Equipment | |||||
| m = str2double(sc.query(['MEAS',num2str(measurement),':RES:ACT?'])); | m = str2double(sc.query(['MEAS',num2str(measurement),':RES:ACT?'])); | ||||
| end | end | ||||
| function setWaveformSettings(sc) | |||||
| function setWaveformSettings(sc,window,format) | |||||
| if nargin < 3 | |||||
| format = 'REAL'; | |||||
| if nargin < 2 | |||||
| window = 'DEF'; | |||||
| end | |||||
| end | |||||
| sc.clear; | sc.clear; | ||||
| sc.write('CHAN:TYPE HRES'); | sc.write('CHAN:TYPE HRES'); | ||||
| sc.write('FORM REAL'); | |||||
| sc.write(['FORM ',format]); | |||||
| sc.write('FORM:BORD MSBF'); | sc.write('FORM:BORD MSBF'); | ||||
| sc.write('CHAN:DATA:POIN DEF'); | |||||
| sc.write(['CHAN:DATA:POIN ',window]); | |||||
| sc.single; | sc.single; | ||||
| end | end | ||||
| function data = waveform(sc,channels) | |||||
| if nargin < 2 | |||||
| channels = 1:sc.nchannels; | |||||
| function data = readWaveform(sc,datalength) | |||||
| prefixstring = fscanf(sc.tcp,'%c',2); | |||||
| prefixlength = str2double(prefixstring(2)); | |||||
| fscanf(sc.tcp,'%c',prefixlength); | |||||
| data = sc.bin_read_float(datalength); | |||||
| flushinput(sc.tcp); | |||||
| end | |||||
| function data = waveform(sc,channels,window) | |||||
| if nargin < 3 | |||||
| window = 'DEF'; | |||||
| if nargin < 2 | |||||
| channels = 1:sc.nchannels; | |||||
| end | |||||
| end | end | ||||
| sc.setWaveformSettings | |||||
| sc.enable_channels; | |||||
| sc.setWaveformSettings(window); | |||||
| for i = channels | for i = channels | ||||
| wave = sc.(['ch',num2str(i)]).getWaveform; | |||||
| data.(['ch',num2str(i)]) = wave.data; | |||||
| curcha = ['ch',num2str(i)]; | |||||
| wave = sc.(curcha).getWaveform; | |||||
| data.(curcha) = wave.data; | |||||
| end | end | ||||
| data.sampletime = wave.sampletime; | data.sampletime = wave.sampletime; | ||||
| data.length = wave.length; | data.length = wave.length; | ||||
| @@ -0,0 +1,19 @@ | |||||
| function bodePlot(magnitude,phase,frequency) | |||||
| %UNTITLED Summary of this function goes here | |||||
| % Detailed explanation goes here | |||||
| figure; | |||||
| subplot(2,1,1) | |||||
| semilogx(frequency,magnitude) | |||||
| ylabel('Magnitude [dB]','Fontsize',10); | |||||
| xlabel('Frequency [Hz]','Fontsize',10); | |||||
| grid on | |||||
| title('Bodeplot') | |||||
| ax = subplot(2,1,2); | |||||
| semilogx(frequency,phase) | |||||
| ylabel('Phase [rad]','Fontsize',10); | |||||
| xlabel('Frequency [Hz]','Fontsize',10); | |||||
| grid on | |||||
| end | |||||
| @@ -1,12 +1,13 @@ | |||||
| function [phase,magnitude] = phamag(wave1,wave2,length,frequency,T) | function [phase,magnitude] = phamag(wave1,wave2,length,frequency,T) | ||||
| %PHAMAG Summary of this function goes here | %PHAMAG Summary of this function goes here | ||||
| % Detailed explanation goes here | % Detailed explanation goes here | ||||
| wave = [wave1;wave2]; | |||||
| W=hann(length); | |||||
| wave = [wave1;wave2].*W'; | |||||
| Fs = 1/T; | Fs = 1/T; | ||||
| n = 2^nextpow2(length); | n = 2^nextpow2(length); | ||||
| X = fft(wave,n,2)/n; | X = fft(wave,n,2)/n; | ||||
| idx = round(frequency*n/Fs); | idx = round(frequency*n/Fs); | ||||
| phase = mod(angle(mean(X(1,(-1:1)+idx)))-angle(mean(X(2,(-1:1)+idx)))+pi,2*pi)-pi; | |||||
| magnitude = 10*log10(abs(X(2,idx))/abs(X(1,idx))); | |||||
| phase = mod(angle(mean(X(1,idx)))-angle(mean(X(2,idx)))+pi,2*pi)-pi; | |||||
| magnitude = 20*log10(abs(X(2,idx))/abs(X(1,idx))); | |||||
| end | end | ||||
| @@ -16,5 +16,6 @@ function [data,raw] = transferFunction(oscilloscope,functiongenerator,f_start,f_ | |||||
| raw(i) = wavedata; | raw(i) = wavedata; | ||||
| [data.phase(i),data.magnitude(i)] = phamag(wavedata.ch1,wavedata.ch2,wavedata.length,f_array(i),wavedata.sampletime); | [data.phase(i),data.magnitude(i)] = phamag(wavedata.ch1,wavedata.ch2,wavedata.length,f_array(i),wavedata.sampletime); | ||||
| end | end | ||||
| bodePlot(data.magnitude,data.phase,data.frequency) | |||||
| end | end | ||||