-
Notifications
You must be signed in to change notification settings - Fork 2
/
Measurement.m
400 lines (370 loc) · 15 KB
/
Measurement.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
classdef Measurement < matlab.mixin.SetGet
% Measurement class for frequency response measurements, Bode Plots
%
% m1 = Measurement()
%
% m1.makeMeasurement(scopeIp, fgenIp)
%
% Jakob Holz 2022
properties
% Settings (except ip-adresses):
samples % Number of samples
distr % Distribution of samples over frequency range
fstart % Lowest measured frequency
fstop % Highest measured frequency
vpp % Peak to peak voltage
voff % Voltage offset
imp % Output impedance
eas % Enhanced auto-scaling, faster, wider frequency range
ch1Att % Channel 1 attenuation
ch2Att % Channel 2 attenuation
bwl % 20 MHz bandwith limit
lock % Lock frontpanels
end
properties (SetAccess = private)
%finished = false;
aborted = false;
progress = 0; % Progress of the measurement in percent
time % Date and time when the measurement was taken
% Measured data:
ch1Vpp % Vpp measured at channel 1
ch2Vpp % Vpp measured at channel 2
rawPhase % unprocessed phase data in degree
% Calculated data:
mag % Magnitude of ch2Vpp/ch1Vpp
magdB % Magnitude in dB
attdB % Attenuation in dB
phase % Phase
freq % Frequency
omega % Angular frequency
end
methods
function obj = Measurement(samples, fstart, fstop, distr, ch1Att,...
ch2Att, bwLimit, enhancedScaling, vpp, voff, imp, lockPanels)
if nargin == 0
return
elseif nargin == 1
vpp = 1;
voff = 0;
imp = 'HighZ';
ch1Att = 1;
ch2Att = 1;
bwLimit = true;
lockPanels = false;
enhancedScaling = true;
fstart = 50;
fstop = 5000000;
distr = 'log';
end
obj.fstart = fstart;
obj.fstop = fstop;
obj.samples = samples;
obj.distr = distr;
obj.vpp = vpp;
obj.voff = voff;
obj.imp = imp;
obj.ch1Att = ch1Att;
obj.ch2Att = ch2Att;
obj.bwl = bwLimit;
obj.lock = lockPanels;
obj.eas = enhancedScaling;
end
function makeMeasurement(obj, scopeIp, fgenIp)
obj.time = datetime;
obj.freq = Measurement.makeFreq(obj.fstart, obj.fstop, obj.samples, obj.distr);
scope = Measurement.visaObj(scopeIp);
fgen = Measurement.visaObj(fgenIp);
scope.InputBufferSize = 2048; % useless?!?!
fopen(scope);
fopen(fgen);
Measurement.setupInstr(scope, fgen, obj.lock, obj.imp, obj.ch1Att, obj.ch2Att, obj.bwl)
[obj.ch1Vpp, obj.ch2Vpp, obj.rawPhase] = sweep(obj, scope, fgen, obj.freq, obj.samples, obj.vpp, obj.voff, obj.eas);
[obj.mag, obj.magdB, obj.attdB, obj.phase, obj.omega] = Measurement.processData(obj.ch1Vpp, obj.ch2Vpp, obj.rawPhase, obj.freq);
end
function abortMeasurement(obj)
obj.aborted = true;
end
%% Setter methods
function set.samples(obj, samples)
if isnumeric(samples) && samples > 0
obj.samples = samples;
else
error('samples must a positive integer.')
end
end
function set.distr(obj, distr)
if isequal(distr, 'log') || isequal(distr, 'linear')
obj.distr = distr;
else
error('distr must be of either log or linear.')
end
end
function set.fstart(obj, fstart)
if isnumeric(fstart)
obj.fstart = fstart;
else
error('fstart must be of type numeric.')
end
end
function set.fstop(obj, fstop)
if isnumeric(fstop)
obj.fstop = fstop;
else
error('fstop must be numeric.')
end
end
function set.vpp(obj, vpp)
if isnumeric(vpp) && vpp > 0
obj.vpp = vpp;
else
error('The peak to peak voltage (vpp) must be positive numeric value.')
end
end
function set.voff(obj, voff)
if isnumeric(voff)
obj.voff = voff;
else
error('The offset voltage (voff) must be numeric.')
end
end
function set.imp(obj, imp)
if isequal(imp,'HighZ') || isequal(imp,'50 Ohm') || isnumeric(imp)
obj.imp = imp;
else
error('The output impedance (imp) must be numeric.')
end
end
function set.ch1Att(obj, ch1Att)
if isnumeric(ch1Att)
obj.ch1Att = ch1Att;
else
error('ch1Att must be numeric.')
end
end
function set.ch2Att(obj, ch2Att)
if isnumeric(ch2Att)
obj.ch2Att = ch2Att;
else
error('ch2Att must be numeric.')
end
end
function set.bwl(obj, bwl)
if islogical(bwl)
obj.bwl = bwl;
else
error('Bandwidth Limit (bwl) must be logical type.')
end
end
function set.lock(obj, lock)
if islogical(lock)
obj.lock = lock;
else
error('Lock frontpanels (lock) must be logical type.')
end
end
function set.eas(obj, enhScaling)
if islogical(enhScaling)
obj.eas = enhScaling;
else
error('Enhanced Auto-Scaling (eas) must be logical type.')
end
end
end
methods (Access = private)
function [ch1Vpp, ch2Vpp, phase] = sweep(obj, scope, fgen, freq, samples, vpp, voff, eas)
ch1Vpp(1:samples) = NaN;
ch2Vpp(1:samples) = NaN;
phase(1:samples) = NaN;
for k = 1:samples
if obj.aborted
break
end
fprintf(fgen, append(':SOUR1:APPL:SIN ', num2str(freq(k)), ',', num2str(vpp), ',', num2str(voff), ',0'));
if eas % enhanced auto-scaling
if k == 1
pause(0.1)
fprintf(fgen, ':OUTP1 ON');
pause(0.1)
fprintf(scope, ':AUToscale');
pause(9)
%{
fprintf(scope, ':TRIG:MODE: EDGE');
fprintf(scope, ':TRIG:EDG:SOUR CHAN1');
fprintf(scope, ':TRIG:EDG:SLOP POS');
fprintf(scope, ':TRIG:EDG:LEV 0');
fprintf(scope, ':TRIG:SWE: AUTO');
fprintf(scope, ':TRIG:NREJ ON');
pause(0.1); % auto-scale takes about 9 sec
%}
fprintf(scope, ':CHAN1:OFFS 0');
fprintf(scope, ':CHAN2:OFFS 0');
%fprintf(scope, append(':CHAN1:SCAL ', sprintf('%0.7e', Measurement.calcVScale(vpp, obj.ch1Att))));
% ch1Vpp(1) = query(scope, ':MEAS:ITEM? VPP,CHAN1');
% fprintf(scope, ':MEAS:ITEM? VPP,CHAN1' );
% ch1Vpp(1) = str2double(fscanf(scope, '%s' ));
% fprintf(scope, append(':CHAN1:SCAL ', sprintf('%0.7e', Measurement.calcVScale(ch1Vpp(k)))));
%if freq(k) > 75e3
% fprintf(scope, ':TRIG:COUP: LFR');
%else
% fprintf(scope, ':TRIG:COUP: HFR');
%end
else
period = 1/freq(k);
nP = 2; % number of periods on the screen
timescale = round(period/12*nP, 9);
fprintf(scope, append(':TIM:MAIN:SCAL ', sprintf('%0.7e', timescale)));
end
%if k == 1
%Measurement.findVScale(scope, obj.ch2Att)
%end
pause(0.1)
fprintf(scope, ':MEAS:ITEM? VPP,CHAN1');
ch1Vpp(k) = str2double(fscanf(scope, '%s'));
fprintf(scope, ':MEAS:ITEM? VPP,CHAN2' );
ch2Vpp(k) = str2double(fscanf(scope, '%s'));
fprintf(scope, append(':CHAN1:SCAL ', sprintf('%0.7e', Measurement.calcVScale(ch1Vpp(k), obj.ch1Att))));
fprintf(scope, append(':CHAN2:SCAL ', sprintf('%0.7e', Measurement.calcVScale(ch2Vpp(k), obj.ch2Att))));
pause(1.5)
else
if k == 1
pause(0.1)
fprintf(fgen, ':OUTP1 ON');
pause(0.1)
end
fprintf(scope, ':AUToscale');
pause(9); % auto-scale takes about 8.6 sec
end
fprintf(scope, ':MEAS:ITEM? VPP,CHAN1');
ch1Vpp(k) = str2double(fscanf(scope, '%s'));
fprintf(scope, ':MEAS:ITEM? VPP,CHAN2' );
ch2Vpp(k) = str2double(fscanf(scope, '%s'));
% ch1Vpp(k) = query(scope, ':MEAS:ITEM? VPP,CHAN1', '%s', '%c');
% ch2Vpp(k) = query(scope, ':MEAS:ITEM? VPP,CHAN2', '%s', '%c');
% phase(k) = query(scope, ':MEAS:ITEM? RPH', '%s', '%c');
fprintf(scope, ':MEAS:ITEM? RPH');
phase(k) = str2double(fscanf(scope, '%s'));
obj.progress = round(k/samples*100);
end
Measurement.cleanup(scope, fgen)
end
end
methods (Access = private, Static)
function setupInstr(scope, fgen, lockPanels, z, ch1Att, ch2Att, bwLimit)
% lock frontpanels
if(lockPanels)
fprintf(fgen, ':SYSTEM:KLOCK ALL ON');
fprintf(scope, ':SYST:LOCK ON');
else
fprintf(fgen, ':SYSTEM:KLOCK ALL OFF');
fprintf(scope, ':SYST:LOCK OFF');
end
% set CH1 output impedance
if isequal(z, 'HighZ')
fprintf(fgen, ':OUTP1:IMP INF' );
else
fprintf(fgen, append(':OUTP1:IMP ', z));
end
fprintf(scope, ':CHAN1:COUP DC');
fprintf(scope, ':CHAN2:COUP DC');
fprintf(scope, append(':CHAN1:PROB ', int2str(ch1Att)));
fprintf(scope, append(':CHAN2:PROB ', int2str(ch2Att)));
% enable/disable 20 MHz bandwidthlimit
if bwLimit
fprintf(scope, ':CHAN1:BWL 20M');
fprintf(scope, ':CHAN2:BWL 20M');
else
fprintf(scope, ':CHAN1:BWL OFF');
fprintf(scope, ':CHAN2:BWL OFF');
end
% MEASURE
fprintf(scope, ':MEAS:CLE ALL');
fprintf(scope, ':MEAS:SET:PSA CHAN1'); % Set source A of phase measurement to CH1
fprintf(scope, ':MEAS:SET:PSB CHAN2'); % Set source B of phase measurement to CH2
fprintf(scope, ':CHAN1:DISP ON');
fprintf(scope, ':CHAN2:DISP ON');
end
function freq = makeFreq(fstart, fstop, samples, distr)
if isequal(distr, 'log')
freq = logspace(log10(fstart), log10(fstop), samples);
else
freq = linspace(fstart, fstop, samples);
end
end
function findVScale(scope, att)
vold = 0;
for k = [10 1 0.1 0.01] % coarse
fprintf(scope, append(':CHAN2:SCAL ', sprintf('%0.7e', k*att)));
pause(0.2)
fprintf(scope, ':MEAS:ITEM? VPP,CHAN2');
vpp = str2double(fscanf(scope, '%s'));
if vpp < 9.9e+10
vold = vpp;
break
end
end
for k = 1:5 % fine
fprintf(scope, append(':CHAN2:SCAL ', sprintf('%0.7e', Measurement.calcVScale(vold, att))));
pause(1)
fprintf(scope, ':MEAS:ITEM? VPP,CHAN2');
vnew = str2double(fscanf(scope, '%s'));
if abs(vnew-vold) <= 0.05
pause(3)
return
end
vold = vnew;
end
error('Could not display CH2 correctly, check probe attenuation!')
end
% Calculates and rounds the vertical scale so that the curve
% occupies half of the screen, used for enhanced auto-scaling
function scale = calcVScale(vpp, att)
% *2 because half of the screen, /8 because the screen fits 8 divisions
scale = vpp*0.25;
if scale < 0.001
scale = 0.001;
elseif scale >= 0.001 || scale < 1
scale = round(scale, 3);
elseif scale >= 1 || scale < 10
scale = round(scale, 3);
elseif scale >= 10
scale = 10;
end
scale = scale*att; % Scaling is related to probe attenuation
end
% Processing of aquired measurements
function [mag, magdB, attdB, phase, omega] = processData(vpp1, vpp2, rawPhase, freq)
mag = vpp2./vpp1;
magdB = 20.*log10(mag);
attdB = -magdB;
omega = 2*pi.*freq;
pha = rawPhase;
pha(pha > 1000) = NaN; % replace failed measurements
phase = pha./180.*pi;
end
end
methods (Static)
function instr = visaObj(ip)
% Find VISA-TCPIP objects.
instr = instrfind('Type', 'visa-tcpip', 'RsrcName', append('TCPIP0::', ip, '::inst0::INSTR'), 'Tag', '');
% Create the VISA-TCPIP object if it does not exist
% otherwise use the object that was found.
if isempty(instr)
instr = visa('NI', append('TCPIP0::', ip, '::inst0::INSTR'));
else
fclose(instr);
instr = instr(1);
end
end
% Code to be executed after finishing measurements
function cleanup(scope, fgen)
fprintf(fgen, ':OUTP1 OFF' );
% unlock frontpanels
fprintf(scope, ':SYST:LOCK OFF');
fprintf(fgen, ':SYSTEM:KLOCK ALL OFF');
fclose(fgen);
fclose(scope);
delete(fgen);
delete(scope);
end
end
end