-
Notifications
You must be signed in to change notification settings - Fork 1
/
daqBasicAcquisition.m
executable file
·364 lines (312 loc) · 12.5 KB
/
daqBasicAcquisition.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
function [] = daqBasicAcquisition()
% A gui that aquires data from multiple channels of a NI-DAQ and saves it
% as a binary data file. It also displays the acquired data in real time.
%
% Version 0.9
% Last Modified On 6 August 2015
% Author: Dinesh Natesan
% Set Default Values
rundata = struct();
rundata.sampling_freq = 10e3; % Hz
rundata.viewTime = 5; % Seconds
rundata.channels = arrayfun(@num2str,1:20,'UniformOutput',false);
rundata.deviceName = 'Dev1';
rundata.channelNum = 1;
rundata.selected_channels = 0:rundata.channelNum-1;
rundata.saveFolder = pwd;
rundata.saveName = getFileName(rundata.saveFolder,rundata.channelNum);
% Initialize the control figure
handles = struct();
% --- CONTROL FIGURE -----------------------------
handles.controlfig = figure( ...
'Tag', 'controlfig', ...
'Units', 'Normalized', ...
'Position', [0.005 0.5 0.200625 0.12083333333333333], ...
'Name', 'DAQ Basic Acquisition', ...
'MenuBar', 'none', ...
'NumberTitle', 'off', ...
'Color', get(0,'DefaultUicontrolBackgroundColor'));
% --- PANELS -------------------------------------
handles.basicAq = uipanel( ...
'Parent', handles.controlfig, ...
'Tag', 'uipanel2', ...
'Units', 'Normalized', ...
'Position', [0.0280373831775701,0.0896551724137931,0.937694704049844,0.841379310344828], ...
'Title', 'Basic Acquisition');
% --- STATIC TEXTS -------------------------------------
handles.FileName = uicontrol( ...
'Parent', handles.basicAq, ...
'Tag', 'FileName', ...
'Style', 'text', ...
'Units', 'Normalized', ...
'Position', [0.471380471380471,0.923809523809524,0.363636363636364,0.133333333333334], ...
'String', 'FileName');
handles.setDir = uicontrol( ...
'Parent', handles.basicAq, ...
'Tag', 'setDir', ...
'Style', 'text', ...
'Units', 'Normalized', ...
'Position', [0.286195286195286,0.580952380952381,0.683501683501684,0.314285714285715],...
'HorizontalAlignment','left',...
'String', {strjoin(strsplit(rundata.saveName(length(rundata.saveName)-...
iff(length(rundata.saveName)-1>=75,75,length(rundata.saveName)-1):end),' '),'_')});
handles.channelNum = uicontrol( ...
'Parent', handles.basicAq, ...
'Tag', 'channelNum', ...
'Style', 'text', ...
'Units', 'Normalized', ...
'Position', [0.0202020202020202,0.0666666666666665,0.306397306397306,0.152380952380952],...
'String', {strcat('Input Channels: ',num2str(rundata.channelNum), '; Selection: Auto')});
handles.recordingStat = uicontrol( ...
'Parent', handles.basicAq, ...
'Tag', 'recordingStat', ...
'Style', 'text', ...
'Units', 'Normalized', ...
'Position', [0.420875420875421,0.0571428571428572,0.548821548821549,0.133333333333334],...
'ForegroundColor', [0.584 0.388 0.388], ...
'String', {sprintf('Recording Status: Off; Sampling freq: %.2g', rundata.sampling_freq)});
% --- PUSHBUTTONS -------------------------------------
handles.saveFolder = uicontrol( ...
'Parent', handles.basicAq, ...
'Tag', 'saveFolder', ...
'Style', 'pushbutton', ...
'Units', 'Normalized', ...
'Position', [0.0202020202020202,0.571428571428572,0.255892255892256,0.352380952380953],...
'String', 'Save Folder', ...
'Callback', @saveFolder_Callback);
handles.start_basic = uicontrol( ...
'Parent', handles.basicAq, ...
'Tag', 'start_basic', ...
'Style', 'pushbutton', ...
'Units', 'Normalized', ...
'Position', [0.414141414141414,0.219047619047619,0.245791245791246,0.333333333333333],...
'ForegroundColor', [1 1 1], ...
'BackgroundColor', [0 0.498 0], ...
'String', 'Start', ...
'Callback', @start_basic_Callback);
handles.stop_basic = uicontrol( ...
'Parent', handles.basicAq, ...
'Tag', 'stop_basic', ...
'Style', 'pushbutton', ...
'Units', 'Normalized', ...
'Position', [0.713804713804714,0.219047619047619,0.245791245791246,0.333333333333333],...
'ForegroundColor', [1 1 1], ...
'BackgroundColor', [0.584 0.388 0.388], ...
'String', 'Stop', ...
'Callback', @stop_basic_Callback);
handles.select_channels = uicontrol( ...
'Parent', handles.basicAq, ...
'Tag', 'select_channels', ...
'Style', 'pushbutton', ...
'Units', 'Normalized', ...
'Position', [0.18,0.285714285714286,0.125,0.190476190476191],...
'String', 'Select channels', ...
'Callback', @select_channels_Callback);
% --- POPUP MENU -------------------------------------
handles.channels = uicontrol( ...
'Parent', handles.basicAq, ...
'Tag', 'channels', ...
'Style', 'popupmenu', ...
'Units', 'Normalized', ...
'Position', [0.0202020202020202,0.285714285714286,0.156565656565656,0.190476190476191],...
'BackgroundColor', [1 1 1], ...
'String', rundata.channels, ...
'Callback', @channels_Callback, ...
'CreateFcn', @channels_CreateFcn);
% Initializing done
% Save necessary data
setappdata(0,'handles',handles);
setappdata(0,'rundata',rundata);
end
%% Callback functions
%% ---------------------------------------------------------------------------
function saveFolder_Callback(hObject,evendata) %#ok<INUSD>
handles = getappdata(0,'handles');
rundata = getappdata(0,'rundata');
% Update folder and file
folder = uigetdir(rundata.saveFolder);
rundata.saveFolder = folder;
rundata.saveName = getFileName(folder,rundata.channelNum);
% Update file name
set(handles.setDir,'String', {strjoin(strsplit(rundata.saveName(length(rundata.saveName)-iff(length(rundata.saveName)-1>=75,75,length(rundata.saveName)-1):end),' '),'_')});
% Save necessary data
setappdata(0,'handles',handles);
setappdata(0,'rundata',rundata);
end
%% ---------------------------------------------------------------------------
function start_basic_Callback(hObject,evendata) %#ok<INUSD>
handles = getappdata(0,'handles');
rundata = getappdata(0,'rundata');
% Start the daq session
daq_session = daq.createSession('ni');
daq_session.addAnalogInputChannel(rundata.deviceName,rundata.selected_channels,'Voltage');
daq_session.Rate = rundata.sampling_freq;
% Add plot data
fig_handle = figure('Units','Pixels','Position',[320 60 1260 960]);
if rundata.channelNum == 1
plot_handles{1} = plot(0,0);
else
plot_handles = cell(rundata.channelNum,1);
for i=1:rundata.channelNum
subplot(rundata.channelNum,1,i);
plot_handles{i} = plot(0,0);
end
end
% Open files
fid = fopen(rundata.saveName,'w');
lh = addlistener(daq_session,'DataAvailable',@(src, event)saveData(src, event, fid));
daq_session.IsContinuous = true;
daq_session.startBackground();
% Write into the ExperimentLogger file
experimentLogger(1);
% Set Status
set(handles.recordingStat,'ForegroundColor', [0 0.498 0], ...
'String', {sprintf('Recording Status: ON; Sampling freq: %.2g', rundata.sampling_freq)});
handles.fig_handle = fig_handle;
handles.plot_handles = plot_handles;
rundata.daq_session = daq_session;
rundata.fid = fid;
rundata.lh = lh;
% Save necessary data
setappdata(0,'handles',handles);
setappdata(0,'rundata',rundata);
end
%% ---------------------------------------------------------------------------
function stop_basic_Callback(hObject,evendata) %#ok<INUSD>
handles = getappdata(0,'handles');
rundata = getappdata(0,'rundata');
% Stop the daq_session
daq_session = rundata.daq_session;
daq_session.stop();
% Close plots and delete handles
close(handles.fig_handle);
delete(rundata.lh);
fclose(rundata.fid);
% Write into the ExperimentLogger file
experimentLogger(0);
rundata = rmfield(rundata,'daq_session');
rundata = rmfield(rundata,'fid');
rundata = rmfield(rundata,'lh');
handles = rmfield(handles,'fig_handle');
handles = rmfield(handles,'plot_handles');
% Set Status
set(handles.recordingStat,'ForegroundColor', [0.584 0.388 0.388], ...
'String', {sprintf('Recording Status: Off; Sampling freq: %.2g', rundata.sampling_freq)});
% Update Filename
rundata.saveName = getFileName(rundata.saveFolder,rundata.channelNum);
% Update file name
set(handles.setDir,'String', {strjoin(strsplit(rundata.saveName(length(rundata.saveName)-iff(length(rundata.saveName)-1>=75,75,length(rundata.saveName)-1):end),' '),'_')});
% Save necessary data
setappdata(0,'handles',handles);
setappdata(0,'rundata',rundata);
end
%% ---------------------------------------------------------------------------
function select_channels_Callback(hObject,evendata) %#ok<INUSD>
handles = getappdata(0,'handles');
rundata = getappdata(0,'rundata');
% get channel selections
ch = inputdlg(arrayfun(@(x) sprintf('Channel %d',x), 1:rundata.channelNum, 'UniformOutput', false));
if (size(ch,1)~= rundata.channelNum) || any(isnan(str2double(ch)))
warning('Wrong number of inputs or characters instead of numbers. Continuing in auto mode');
return;
else
ch = str2double(ch);
rundata.selected_channels = ch';
set(handles.channelNum,'String',{strcat('Input Channels: ',num2str(rundata.channelNum), '; Selection: Manual')});
end
% Save necessary data
setappdata(0,'handles',handles);
setappdata(0,'rundata',rundata);
end
%% ---------------------------------------------------------------------------
function channels_Callback(hObject,evendata) %#ok<INUSD>
handles = getappdata(0,'handles');
rundata = getappdata(0,'rundata');
% Get Channel Info
rundata.channelNum = str2double(rundata.channels(get(handles.channels,'Value')));
rundata.selected_channels = 0:rundata.channelNum-1;
set(handles.channelNum,'String',{strcat('Input Channels: ',num2str(rundata.channelNum), '; Selection: Auto')});
rundata.saveName = getFileName(rundata.saveFolder,rundata.channelNum);
% Update file name
set(handles.setDir,'String', {strjoin(strsplit(rundata.saveName(length(rundata.saveName)-iff(length(rundata.saveName)-1>=75,75,length(rundata.saveName)-1):end),' '),'_')});
% Save necessary data
setappdata(0,'handles',handles);
setappdata(0,'rundata',rundata);
end
%% ---------------------------------------------------------------------------
function channels_CreateFcn(hObject,evendata) %#ok<INUSD>
end
%% Custom functions
function [file] = getFileName(folder,channelNum)
% function [file] = getFileName(folder)
list = dir(fullfile(folder,'*.bin'));
list = {list(:).name};
searchString = strcat('AcquisitionData_',date,'_(\d+)');
matchedTokens = regexp(list,searchString,'tokens');
numstrs = matchedTokens(~cellfun(@isempty,matchedTokens));
if isempty(numstrs)
file = fullfile(folder,sprintf('AcquisitionData_%s_%04d_In-%02d.bin',date,1,channelNum));
else
nums = sort(cellfun(@(x) str2double(x{:}),numstrs));
file = fullfile(folder,sprintf('AcquisitionData_%s_%04d_In-%02d.bin',date,nums(end)+1,channelNum));
end
end
function saveData(src,event,fid)
% function plotData(src,event,fid)
% Logs and plots data
% Log data
logData(src,event,fid);
% Now plot data
handles = getappdata(0,'handles');
rundata = getappdata(0,'rundata');
time = event.TimeStamps';
for i=1:rundata.channelNum
plot_data = event.Data(:,i)';
plotDuration = rundata.viewTime*rundata.sampling_freq;
% Obtain plotted data
xdata = get(handles.plot_handles{i},'XData');
ydata = get(handles.plot_handles{i},'YData');
if (length(xdata)+length(plot_data))<=plotDuration
newX = [xdata,time];
newY = [ydata,plot_data];
else
newX = [xdata(length(xdata)-(plotDuration-length(time))+1:end),time];
newY = [ydata(length(xdata)-(plotDuration-length(time))+1:end),plot_data];
end
set(handles.plot_handles{i},'XData',newX,'YData',newY);
end
drawnow;
end
function [] = experimentLogger(status)
% function [] = experimentLogger()
% Log Stimulus Details and Start time
rundata = getappdata(0,'rundata');
% Determine filename
filename = fullfile(rundata.saveFolder,sprintf('AcquisitionLog_%s_.log',date));
fileid = fopen(filename,'at+');
if status == 1
saveName = strsplit(rundata.saveName,filesep);
saveName = saveName{end};
% Save important initialization data
fprintf(fileid,'%s: [Start] %s ', saveName, datestr(clock));
elseif status == 0
fprintf(fileid,'[Stop] %s \n', datestr(clock));
end
fclose(fileid);
end
function [out] = iff(cond,a,b)
% function iff(cond,a,b)
% A custom written function that mimic the traditional C+ conditional
% expression: out = cond?true:false
%
% Dinesh Natesam, 6th Mar 2014
if cond
out = a;
else
out = b;
end
end
function logData(src, evt, fid)
data = [evt.TimeStamps, evt.Data]' ;
fwrite(fid,data,'double');
end