-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrectest.m
102 lines (90 loc) · 2.68 KB
/
rectest.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
function rectest(doDec)
%RECTEST - test DAQ timer/recording interaction
%
% specify nonzero DODEC for decimation
if nargin < 1 || isempty(doDec), doDec = 0; end;
%% params
dur = 10;
sr = 48000;
%% init DAQ object
info = daqhwinfo('winsound');
AI = eval(info.ObjectConstructorName{1});
addchannel(AI, 1);
sr = setverify(AI,'sampleRate',sr);
AI.BufferingMode = 'auto';
AI.TriggerType = 'manual';
AI.TriggerRepeat = 0;
AI.LogFileName = 'junk.daq';
AI.LoggingMode = 'Disk';
%AI.LoggingMode = 'Disk&Memory';
nSamps = round(dur * sr);
AI.SamplesPerTrigger = nSamps;
set(AI,'StopFcn',@Finalize);
%% init plotting
fh = figure('doubleBuffer','on');
cla;
if doDec,
x = linspace(0,dur,2*ceil(nSamps/200));
y = zeros(1,2*ceil(nSamps/200));
else,
x = linspace(0,dur,nSamps);
y = zeros(nSamps,1);
end;
plot(x,y,'color','g','linestyle',':');
lh = line(x,NaN*y,'eraseMode','none','hittest','off','clipping','off');
set(lh, 'userData', [0;0;doDec]); % sampsAcquired, decimated tail, doDec
set(gca,'xlim',[0 dur],'ylim',[-1 1],'drawmode','fast', ...
'ytick',[-1 0 1],'yticklabel',strvcat('-1','','0','','1'));
set(AI,'userData',lh);
AI.TimerPeriod = .1; % 500 ms update
set(AI,'TimerFcn', @UpdatePlot);
start(AI);
while ~isrunning(AI), end;
trigger(AI);
%% ===== UpdatePlot ============================================================
% update plot using peekdata
function UpdatePlot(AI, event)
lh = get(AI, 'userData'); % line handle
q = get(lh,'userData');
q = [q;event.Data.AbsTime(6)];
sampsAcquired = q(1);
tail = q(2);
doDec = q(3);
q(1:3)=[];
if AI.SamplesAcquired <= sampsAcquired, return; end;
sampsThisUpdate = AI.SamplesAcquired - sampsAcquired;
newSamps = peekdata(AI, sampsThisUpdate);
nSamps = length(newSamps);
if doDec,
ns = ceil(nSamps/200)*200;
newSamps = reshape([newSamps;NaN*zeros(ns-nSamps,1)],[200 ns/200]);
newSampsMax = nanmax(newSamps);
newSampsMin = nanmin(newSamps);
newSamps = reshape([newSampsMax;newSampsMin],[ns/100,1]);
end;
sampsAcquired = sampsAcquired + nSamps;
ht = tail + [1 , length(newSamps)];
s = get(lh,'ydata');
if ht(2) > length(s),
ht(2) = length(s);
newSamps = newSamps(1:diff(ht)+1);
end;
s(ht(1):ht(2)) = newSamps;
set(lh,'ydata',s,'userData',[sampsAcquired;ht(2);doDec;q]);
%set(lh,'userData',[ht(2);q(2:end)]);
%% ===== Finalize ============================================================
% finalize plot display on acquisition completion
function Finalize(AI, event)
lh = get(AI, 'userData'); % line handle
y = daqread(AI.LogFileName);
sampsAcquired = length(y);
x = get(gca,'xlim');
x = linspace(0,x(2),length(y));
set(lh, 'xdata',x, 'ydata', y);
q = get(lh,'userData');
q(1:3) = [];
dq = diff(q);
k = find(dq<0);
dq(k)=[];
set(gca,'userData',q);
figure; stem(diff(q));