Преглед изворни кода

working livescript for RC measurements

publish
Wouter Horlings пре 8 година
родитељ
комит
4d23ed2438
9 измењених фајлова са 76 додато и 34 уклоњено
  1. +32
    -22
      OOequipment/Equipment.m
  2. BIN
      OOequipment/RC_TransferFunction.mlx
  3. +20
    -0
      OOequipment/RC_TransferFunction_script.m
  4. +8
    -0
      OOequipment/asymptote.m
  5. +3
    -4
      OOequipment/bodePlot.m
  6. +0
    -2
      OOequipment/equiment_init.m
  7. +7
    -0
      OOequipment/equipment_init.m
  8. +2
    -2
      OOequipment/phamag.m
  9. +4
    -4
      OOequipment/transferFunction.m

+ 32
- 22
OOequipment/Equipment.m Прегледај датотеку

@@ -103,28 +103,35 @@ classdef Equipment < handle
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');
try
if nargin < 4
typesize = 8;
end
data = zeros(1,datalength);
totalread = 0;
maxlength = floor(ecq.tcp.InputBufferSize/typesize)-16;
while datalength > 0
readlength = min(datalength,maxlength);
lastcount = 0;
[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
data(totalread+1:totalread+lastcount) = lastdata;
totalread = totalread+lastcount;
datalength = datalength - lastcount;
flushinput(ecq.tcp)
ecq.clear
catch ME
flushinput(ecq.tcp);
rethrow(ME);
end
flushinput(ecq.tcp)
ecq.clear
end
function data = bin_read_float(ecq,datalength)
data = bin_read(ecq,datalength,'float',4);
data = bin_read(ecq,datalength,'float',4);
end
function output = read(ecq)
@@ -243,13 +250,14 @@ classdef Equipment < handle
%The connection will be removed if the port is negative number.
persistent tcpconnection; %make variable persistent to share tcp-handles across multiple function calls.
[ipname,ipAddress] = Equipment.ip2structname(ipAddress,abs(port)); %Get a structname and a cleaned ipaddress.
if port > 0 %if port number is positive a connection is made.
if isempty(tcpconnection)
if isempty(tcpconnection)
%if the tcpconnection is empty (first time function
%call) make a struct in the variable.
tcpconnection = struct;
end
[ipname,ipAddress] = Equipment.ip2structname(ipAddress,abs(port)); %Get a structname and a cleaned ipaddress.
if port > 0 %if port number is positive a connection is made.

if ~isfield(tcpconnection, ipname) %check if the handle is already made before.
tcpconnection.(ipname).tcp = tcpip(ipAddress,port); %Make TCP-connection
@@ -262,7 +270,9 @@ classdef Equipment < handle
end
tcpobject = tcpconnection.(ipname).tcp; %return the TCP-connection handle.
elseif port < 0 %If the portnumber is negative the connection is removed.
if tcpconnection.(ipname).nopen > 1
if ~isfield(tcpconnection, ipname)
return;
elseif tcpconnection.(ipname).nopen > 1
%If more than one object uses this tcp-handle. Decrease
%the number of connections in use by 1.
tcpconnection.(ipname).nopen = tcpconnection.(ipname).nopen - 1; %Decrease the number by 1.
@@ -270,7 +280,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);
flushinput(tcpconnection.(ipname).tcp);
fclose(tcpconnection.(ipname).tcp);
tcpconnection = rmfield(tcpconnection,ipname);
end


BIN
OOequipment/RC_TransferFunction.mlx Прегледај датотеку


+ 20
- 0
OOequipment/RC_TransferFunction_script.m Прегледај датотеку

@@ -0,0 +1,20 @@
%% Start with loading the equipment if not done already
run('equipment_init');

%% Run the measurements
data = transferFunction(oscilloscope,functiongenerator,f_start,f_stop,n_steps,amplitude);
S = asymptote(S_smaller,S_larger,f,Fc);
figure;
subplot(2,1,1)
semilogx(data.frequency,data.magnitude,f,A,f,S)
ylabel('Magnitude [dB]','Fontsize',10);
xlabel('Frequency [Hz]','Fontsize',10);
legend('Measurement','Theory','Asymptote');
grid on
title('RC-circuit Bodeplot')
subplot(2,1,2);
semilogx(data.frequency,data.phase,f,P)
ylabel('Phase [rad]','Fontsize',10);
xlabel('Frequency [Hz]','Fontsize',10);
legend('Measurement','Theory');
grid on

+ 8
- 0
OOequipment/asymptote.m Прегледај датотеку

@@ -0,0 +1,8 @@
function S = asymptote(S_smaller,S_larger,f,Fc)
%ASYMPTOTE Summary of this function goes here
% Detailed explanation goes here
[~,I] = min(abs(f-Fc));
S = [S_smaller(1:I) S_larger(I+1:end)];

end


+ 3
- 4
OOequipment/bodePlot.m Прегледај датотеку

@@ -1,17 +1,16 @@
function bodePlot(magnitude,phase,frequency)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here


figure;
subplot(2,1,1)
semilogx(frequency,magnitude)
hold on
ylabel('Magnitude [dB]','Fontsize',10);
xlabel('Frequency [Hz]','Fontsize',10);
grid on
title('Bodeplot')
ax = subplot(2,1,2);
subplot(2,1,2);
semilogx(frequency,phase)
hold on
ylabel('Phase [rad]','Fontsize',10);
xlabel('Frequency [Hz]','Fontsize',10);
grid on


+ 0
- 2
OOequipment/equiment_init.m Прегледај датотеку

@@ -1,2 +0,0 @@
fg = FunctionGenerator('10.0.0.3',1234,10);
sc = Oscilloscope('10.0.0.2',5025,2);

+ 7
- 0
OOequipment/equipment_init.m Прегледај датотеку

@@ -0,0 +1,7 @@
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);
end

+ 2
- 2
OOequipment/phamag.m Прегледај датотеку

@@ -6,8 +6,8 @@ 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,idx)))-angle(mean(X(2,idx)))+pi,2*pi)-pi;
idx = ceil(frequency*n/Fs);
phase = mod(angle(mean(X(2,idx)))-angle(mean(X(1,idx)))+pi,2*pi)-pi;
magnitude = 20*log10(abs(X(2,idx))/abs(X(1,idx)));
end


+ 4
- 4
OOequipment/transferFunction.m Прегледај датотеку

@@ -1,4 +1,4 @@
function [data,raw] = transferFunction(oscilloscope,functiongenerator,f_start,f_stop,n_steps,amplitude)
function data = transferFunction(oscilloscope,functiongenerator,f_start,f_stop,n_steps,amplitude)
%TRANSFERFUNCTION Summary of this function goes here
% Detailed explanation goes here

@@ -10,12 +10,12 @@ function [data,raw] = transferFunction(oscilloscope,functiongenerator,f_start,f_
emptydata = zeros(n_steps,1);
data = struct('magnitude',emptydata,'phase',emptydata,'frequency',f_array);
for i = 1:n_steps
fprintf('Measurement %f of %f - Frequency: %.2f Hertz\n',i,n_steps,f_array(i));
functiongenerator.frequency = f_array(i);
oscilloscope.auto;
wavedata = oscilloscope.waveform;
raw(i) = wavedata;
wavedata = oscilloscope.waveform(1:2);
[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)
%bodePlot(data.magnitude,data.phase,data.frequency)
end


Loading…
Откажи
Сачувај