Bläddra i källkod

Veel nieuwe code

publish
Wouter Horlings 8 år sedan
förälder
incheckning
afa0061d5c
6 ändrade filer med 91 tillägg och 19 borttagningar
  1. +7
    -7
      OOequipment/Channel.m
  2. +31
    -0
      OOequipment/Equipment.m
  3. +29
    -9
      OOequipment/Oscilloscope.m
  4. +19
    -0
      OOequipment/bodePlot.m
  5. +4
    -3
      OOequipment/phamag.m
  6. +1
    -0
      OOequipment/transferFunction.m

+ 7
- 7
OOequipment/Channel.m Visa fil

@@ -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


+ 31
- 0
OOequipment/Equipment.m Visa fil

@@ -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


+ 29
- 9
OOequipment/Oscilloscope.m Visa fil

@@ -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;


+ 19
- 0
OOequipment/bodePlot.m Visa fil

@@ -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


+ 4
- 3
OOequipment/phamag.m Visa fil

@@ -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


+ 1
- 0
OOequipment/transferFunction.m Visa fil

@@ -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


Laddar…
Avbryt
Spara