From afa0061d5c16b90d685b15b805867af9e1c0cd61 Mon Sep 17 00:00:00 2001 From: Wouter Horlings Date: Thu, 7 Dec 2017 13:41:26 +0100 Subject: [PATCH] Veel nieuwe code --- OOequipment/Channel.m | 14 ++++++------- OOequipment/Equipment.m | 31 +++++++++++++++++++++++++++ OOequipment/Oscilloscope.m | 38 ++++++++++++++++++++++++++-------- OOequipment/bodePlot.m | 19 +++++++++++++++++ OOequipment/phamag.m | 7 ++++--- OOequipment/transferFunction.m | 1 + 6 files changed, 91 insertions(+), 19 deletions(-) create mode 100644 OOequipment/bodePlot.m diff --git a/OOequipment/Channel.m b/OOequipment/Channel.m index 7ae5c4c..8dd0fc8 100644 --- a/OOequipment/Channel.m +++ b/OOequipment/Channel.m @@ -107,8 +107,12 @@ classdef Channel out = str2double(ch.MEAS('phas')); 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; end @@ -120,11 +124,7 @@ classdef Channel data.sampletime = str2double(ch.CHAN('DATA:XINC?')); ch.scope.opc; 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 diff --git a/OOequipment/Equipment.m b/OOequipment/Equipment.m index 4fee19d..0702bbd 100644 --- a/OOequipment/Equipment.m +++ b/OOequipment/Equipment.m @@ -45,6 +45,11 @@ classdef Equipment < handle ecq.write_unsafe('*rst'); end + function flush(ecq) + flushinput(ecq.tcp); + flushoutput(ecq.tcp); + end + function opc(ecq) %OPC executes 'operation complete query' to device. %Function holds untill device returns '1'. Must be @@ -97,6 +102,31 @@ classdef Equipment < handle ecq.error; 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) %READ Extracts recieved data from the TCP-buffer. %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 %connection is closed and the field is removed from the %tcpconnection struct. + flushinput(sc.tcp); fclose(tcpconnection.(ipname).tcp); tcpconnection = rmfield(tcpconnection,ipname); end diff --git a/OOequipment/Oscilloscope.m b/OOequipment/Oscilloscope.m index cce45f8..e484385 100644 --- a/OOequipment/Oscilloscope.m +++ b/OOequipment/Oscilloscope.m @@ -72,23 +72,43 @@ classdef Oscilloscope < Equipment m = str2double(sc.query(['MEAS',num2str(measurement),':RES:ACT?'])); end - function setWaveformSettings(sc) + function setWaveformSettings(sc,window,format) + if nargin < 3 + format = 'REAL'; + if nargin < 2 + window = 'DEF'; + end + end sc.clear; sc.write('CHAN:TYPE HRES'); - sc.write('FORM REAL'); + sc.write(['FORM ',format]); sc.write('FORM:BORD MSBF'); - sc.write('CHAN:DATA:POIN DEF'); + sc.write(['CHAN:DATA:POIN ',window]); sc.single; 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 - sc.setWaveformSettings + sc.enable_channels; + sc.setWaveformSettings(window); 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 data.sampletime = wave.sampletime; data.length = wave.length; diff --git a/OOequipment/bodePlot.m b/OOequipment/bodePlot.m new file mode 100644 index 0000000..71a553d --- /dev/null +++ b/OOequipment/bodePlot.m @@ -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 + diff --git a/OOequipment/phamag.m b/OOequipment/phamag.m index 9b4402a..2dcef0c 100644 --- a/OOequipment/phamag.m +++ b/OOequipment/phamag.m @@ -1,12 +1,13 @@ function [phase,magnitude] = phamag(wave1,wave2,length,frequency,T) %PHAMAG Summary of this function goes here % Detailed explanation goes here -wave = [wave1;wave2]; +W=hann(length); +wave = [wave1;wave2].*W'; Fs = 1/T; n = 2^nextpow2(length); X = fft(wave,n,2)/n; 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 diff --git a/OOequipment/transferFunction.m b/OOequipment/transferFunction.m index ff17a4f..58db1a0 100644 --- a/OOequipment/transferFunction.m +++ b/OOequipment/transferFunction.m @@ -16,5 +16,6 @@ function [data,raw] = transferFunction(oscilloscope,functiongenerator,f_start,f_ raw(i) = wavedata; [data.phase(i),data.magnitude(i)] = phamag(wavedata.ch1,wavedata.ch2,wavedata.length,f_array(i),wavedata.sampletime); end + bodePlot(data.magnitude,data.phase,data.frequency) end