-
Notifications
You must be signed in to change notification settings - Fork 1
/
TonePuffSystem3.m
610 lines (594 loc) · 22 KB
/
TonePuffSystem3.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
classdef TonePuffSystem3 < SubSystem
properties
experimentStartDelay = 10
frameClkFrequency = 20
nTrials = 60
interTrialIntervalRange = [30 35]
interTrialInterval
laserDelay = 0
laserDuration = 1
toneFrequency = [14500 10000 6000]
toneVolume = [6 .15]
toneDelay = 0.050
toneDuration =.350
puffDelay = 0.650
puffDuration = 0.100
sineGenerator
chirpGenerator
dataLogger
end
properties % OBLIGATORY
experimentSyncObj
trialSyncObj
frameSyncObj
end
properties
laserObj@NiClockedTriggeredOutputEdit %scaler
toneObj@NiClockedTriggeredOutputEdit %scalar
puffObj@NiClockedTriggeredOutputEdit %scalar
strobeObj@NiClockedTriggeredOutputEdit %scalar
stimulusSet
laserSignal
toneSignal
puffSignal
currentStimulusNumber = 0
nextStimulusNumber
currentTrialNumber
frameClkChannel
frameCounterSession
frameCounterChannel
frameCounterListener
stimulusSampleFrequency = 100000
daqDeviceName = 'Dev2'
daqCounterOutNum = 0
daqCounterInNum = 1
daqLaserChannel = 'port1/line3'
daqToneChannel = 'ao0'
daqPuffChannel = 'port1/line2'
daqStrobeChannel = 'port1/line0'
trialStartTime
trialFirstFrame
trialStimulusNumber
camInputSession
camInputChannel
camInputListener
camInputData
camInputTimeStamp
end
properties (Hidden)
lastError
end
events
ExperimentStart
ExperimentStop
NewTrial
NewStimulus
FrameAcquired
end
methods % SETUP
function obj = TonePuffSystem3(varargin)
if nargin > 1
for k = 1:2:length(varargin)
obj.(varargin{k}) = varargin{k+1};
end
end
obj.defineDefaults()
end
function setup(obj)
obj.checkProperties()
obj.updateExperimentName()
obj.createSystemComponents()
obj.loadStandardStimulus();
obj.autoSaveFrequency = obj.nTrials+1;
obj.puffObj.prepareOutput(obj.puffSignal{obj.nextStimulusNumber});
obj.toneObj.prepareOutput(obj.toneSignal{obj.nextStimulusNumber});
obj.laserObj.prepareOutput(obj.laserSignal{obj.nextStimulusNumber});
end
function defineDefaults(obj)
obj.defineDefaults@SubSystem;
% Override some defaults in parent class
obj.default.sessionPath = fullfile(['F:\Data\',...
'TonePuff_',datestr(date,'yyyy_mm_dd')]);
obj.default.autoSaveFrequency = 10;
end
function checkProperties(obj)
obj.savedDataFiles = TonePuffFile.empty(1,0);
obj.currentDataFileSet = TonePuffFile.empty(1,0);
obj.framesAcquired = 0;
obj.checkProperties@SubSystem;
end
function createSystemComponents(obj)
% (Required by SubSystem Parent Class)
obj.experimentRunning = false;
% todo: use daqfind?
% FRAME-CLOCK OUTPUT (GLOBAL)
obj.frameClkSession = setGlobalFrameClock(...
obj.frameClkFrequency,...
obj.daqDeviceName,...
obj.daqCounterOutNum);
obj.frameClkChannel = obj.frameClkSession.Channels(1);
% TONE (ANALOG) OUTPUT
obj.toneObj = NiClockedTriggeredOutputEdit(...
'deviceId', obj.daqDeviceName,...
'type', 'analog',...
'channelId', obj.daqToneChannel,...
'aoNumber', sscanf(obj.daqToneChannel, 'ao%f'),...
'signalRate',obj.stimulusSampleFrequency);
setup(obj.toneObj);
%LASER(DIGITAL) OUTPUT
[port, ~] = sscanf(obj.daqLaserChannel, 'port%f/line%f');
obj.laserObj = NiClockedTriggeredOutputEdit(...
'deviceId', obj.daqDeviceName,...
'type', 'digital',...
'channelId', obj.daqLaserChannel,...
'portNumber', port(1),...
'lineNumber', port(2),...
'signalRate', obj.stimulusSampleFrequency);
setup(obj.laserObj);
% PUFF (DIGITAL) OUTPUT
[port, ~] = sscanf(obj.daqPuffChannel, 'port%f/line%f');
obj.puffObj = NiClockedTriggeredOutputEdit(...
'deviceId', obj.daqDeviceName,...
'type', 'digital',...
'channelId', obj.daqPuffChannel,...
'portNumber', port(1),...
'lineNumber', port(2),...
'signalRate',obj.stimulusSampleFrequency);
setup(obj.puffObj);
% LINK CLOCK-RATES AND USE FRAME-CLOCK AS TRIGGER
% Trigger From Frame-Clock
frameClkString = [obj.frameClkChannel.Device.ID,'/',obj.frameClkChannel.Terminal];
obj.toneObj.setTriggerSource(frameClkString);
% obj.puffObj.setTriggerSource(frameClkString);
% obj.laserObj.setTriggerSource(frameClkString);
% Share Clock
sampleClkSrc = obj.toneObj.getClockSource('PFI1');
obj.puffObj.setClockSource(sampleClkSrc);
obj.laserObj.setClockSource(sampleClkSrc);
% FRAME-RATE FUNCTION (CHANNEL)
obj.frameCounterSession = daq.createSession('ni');
obj.frameCounterChannel = obj.frameCounterSession.addDigitalChannel(...
obj.daqDeviceName,...
'port0/line16',...
'InputOnly');
obj.frameCounterSession.Rate = obj.frameClkFrequency;
obj.frameCounterSession.IsContinuous = true;
obj.frameCounterSession.addAnalogInputChannel(obj.daqDeviceName, 'ai0', 'Voltage')
obj.frameCounterSession.Rate = obj.stimulusSampleFrequency;
obj.frameCounterListener = obj.frameCounterSession.addlistener('DataAvailable', ...
@(src,evnt)frameAcquiredFcn(obj,src,evnt));
obj.frameCounterSession.NotifyWhenDataAvailableExceeds = 1;
obj.frameCounterSession.addClockConnection('External', [obj.daqDeviceName,'/',obj.frameClkChannel.Terminal], 'ScanClock');
% obj.frameCounterSession.addClockConnection('external',[obj.daqDeviceName,'/PFI1'], 'ScanClock');
% TRIAL-RATE FUNCTION
obj.toneObj.sessionObj.NotifyWhenScansQueuedBelow = 1;
% INTER-TRIAL-INTERVAL
if isempty(obj.interTrialInterval)
if numel(obj.interTrialIntervalRange) == 2
obj.interTrialInterval = diff(obj.interTrialIntervalRange) .* rand(obj.nTrials,1) + obj.interTrialIntervalRange(1);
obj.trialStartTime = cat(1, obj.experimentStartDelay, cumsum(obj.interTrialInterval) + obj.experimentStartDelay);
obj.trialFirstFrame = ceil(obj.frameClkFrequency .* obj.trialStartTime);
else
warning('TonePuffSystem3:NoInterTrialIntervalRange', 'No automatic generation of Inter-Trial Intervals without set range')
end
end
if isempty(obj.experimentSyncObj) || ~isvalid(obj.experimentSyncObj)
obj.experimentSyncObj = obj;
end
if isempty(obj.trialSyncObj) || ~isvalid(obj.trialSyncObj)
obj.trialSyncObj = obj;
end
if isempty(obj.frameSyncObj)
obj.frameSyncObj = obj;
end
end
end
methods % CONTROL
function start(obj)
% (Required by SubSystem Parent Class)
obj.updateExperimentName()
fprintf('STARTING TONE-PUFF-SYSTEM:\n\tSession-Path: %s\n',...
obj.sessionPath);
if ~isdir(obj.sessionPath)
mkdir(obj.sessionPath)
end
% STIMULUS NUMBERS
if ~isempty(obj.nextStimulusNumber)
obj.currentStimulusNumber = obj.nextStimulusNumber;
else
obj.currentStimulusNumber = obj.trialStimulusNumber(1);
end
obj.nextStimulusNumber = obj.trialStimulusNumber(2);
% DATALOGGER
obj.dataLogger = DataLogger;
obj.dataLogger.savePath = obj.sessionPath;
setup(obj.dataLogger)
obj.dataLogger.logObjectEvents(obj.toneObj)
obj.dataLogger.logObjectEvents(obj.puffObj)
obj.dataLogger.logObjectEvents(obj.laserObj)
obj.dataLogger.logObjectEvents(obj)
start(obj.dataLogger)
if isempty(obj.frameSyncListener)
warning('TonePuffSystem3:start:NoFrameSyncListener',...
'The Behavior-Control sysem is not connected to a camera, and will not record data every frame');
else
obj.frameSyncListener.Enabled = true;
end
obj.trialStateListener.Enabled = true;
obj.experimentStateListener.Enabled = true;
obj.frameCounterListener.Enabled = true;
% if ~isempty(obj.clockPulseObj)
% if obj.clockPulseObj.IsRunning
% stop(obj.clockPulseObj);
% end
% ch = obj.clockPulseObj.Channels(1);
% obj.clockPulseObj.Rate = obj.clockRate;
% ch.Frequency = obj.clockRate;
% obj.clockPulseObj.prepare();
% end
obj.ready = true;
obj.experimentRunning = true;
notify(obj, 'ExperimentStart')
startBackground(obj.frameCounterSession);
startBackground(obj.frameClkSession);
% startBackground(obj.camInputSession);
% if obj.toneObj.sessionObj.ScansQueued < 1
% obj.puffObj.prepareOutput();
% obj.toneObj.prepareOutput();
% obj.laserObj.prepareOutput();
% end
if isempty(obj.currentDataFile)
obj.currentDataFile = TonePuffFile(...
'rootPath',obj.currentDataSetPath,...
'experimentName',obj.currentExperimentName);%changed rootPath from sessionPath
end
fprintf('TonePuffSystem3 STARTED\n');
end
function stop(obj)
% (Required by SubSystem Parent Class)
% if~isempty(obj.camInputSession)
% stop(obj.camInputSession)
% end
try
if ~isempty(obj.frameClkSession)
stop(obj.frameClkSession);
end
if ~isempty(obj.frameCounterSession)
stop(obj.frameCounterSession);
end
if ~isempty(obj.frameSyncListener)
obj.frameSyncListener.Enabled = false;
end
obj.trialStateListener.Enabled = false;
obj.experimentStateListener.Enabled = false;
if obj.experimentRunning
obj.experimentRunning = false;
if ~isempty(obj.currentDataFile) ...
&& isopen(obj.currentDataFile) ...
&& ~issaved(obj.currentDataFile)
obj.saveDataFile;
obj.currentDataFile = TonePuffFile.empty(1,0);
end
obj.saveDataSet();
obj.clearDataSet();
stop(obj.dataLogger)
% SAVE EXPERIMENT STRUCTURE
experimentStructure = struct(obj);
save(fullfile(obj.currentDataSetPath,'ExperimentStructure'),'experimentStructure')
notify(obj, 'ExperimentStop')
% SAVE FIRST FRAMES TO TEXT FILE
textfilepath = fullfile(obj.currentExperimentName, ['first_frames_',obj.currentExperimentName,'.txt']);
fid = fopen(textfilepath, 'wt');
fprintf(fid, '%i\n',obj.trialFirstFrame);
fclose(fid);
if logical(obj.autoSyncTrialTime) && ~isempty(obj.autoSyncTimerObj)
obj.autoSyncTimerObj.stop();
end
fprintf('Experiment Stopped\n');
end
catch me
notify(obj,'ExperimentStop')
keyboard
end
end
end
methods % TONE-PUFF-LASER SPECIFIC
function loadStandardStimulus(obj)
% SIGNAL DURATION & DELAY
obj.puffObj.signalDuration = obj.puffDuration;
obj.puffObj.signalDelay = obj.puffDelay;
obj.toneObj.signalDuration = obj.toneDuration;
obj.toneObj.signalDelay = obj.toneDelay;
obj.laserObj.signalDuration = obj.laserDuration;
obj.laserObj.signalDelay = obj.laserDelay;
M_laser = ceil(obj.stimulusSampleFrequency*obj.laserObj.signalDuration)+ceil(obj.stimulusSampleFrequency*obj.laserObj.signalDelay);
M_puff = ceil(obj.stimulusSampleFrequency*obj.puffObj.signalDuration)+ceil(obj.stimulusSampleFrequency*obj.puffObj.signalDelay);
M_tone = ceil(obj.stimulusSampleFrequency*obj.toneObj.signalDuration)+ceil(obj.stimulusSampleFrequency*obj.toneObj.signalDelay);
M = [M_laser M_puff M_tone];
M = max(M)+ round(obj.stimulusSampleFrequency/10);
obj.toneObj.outputNumSamples = M+10000;
obj.puffObj.outputNumSamples = M+1000;
obj.laserObj.outputNumSamples = M+1000;
% obj.puffObj.nextSignal = zeros(M,1);
% obj.puffObj.nextSignal(floor(obj.puffObj.signalDelay*aFs) + (1:ceil(obj.puffObj.signalDuration*aFs))) = 1;
% obj.puffObj.nextSignal((end-10):end) = 0; % important!!
% obj.puffObj.signalGeneratingFcn = @()ones(floor(obj.puffObj.signalDuration*obj.puffObj.signalRate),1);
% SINE-WAVE
if isempty(obj.sineGenerator)
obj.sineGenerator = dsp.SineWave;
else
release(obj.sineGenerator);
end
obj.sineGenerator.SampleRate = obj.stimulusSampleFrequency;
obj.sineGenerator.SamplesPerFrame = ceil(obj.stimulusSampleFrequency*obj.toneObj.signalDuration);
obj.sineGenerator.Frequency = obj.toneFrequency;
% CHIRP
if isempty(obj.chirpGenerator)
obj.chirpGenerator = dsp.Chirp;
else
release(obj.chirpGenerator);
end
obj.chirpGenerator.InitialFrequency = 1500;
obj.chirpGenerator.TargetFrequency = 2000;
obj.chirpGenerator.SampleRate = obj.sineGenerator.SampleRate;
obj.chirpGenerator.SamplesPerFrame = obj.sineGenerator.SamplesPerFrame;
% COMBINE
% obj.toneVolume = .9;
% DEFINE A LINEAR ENVELOPE (RISE AND DECAY)
envelopeFcn = @(riseTime, duration, fs) cat(1, ...
(1:ceil(riseTime*fs))'./ceil(riseTime*fs),...
ones(ceil(duration*fs) - 2*ceil(riseTime*fs), 1) ,...
flipud( (1:ceil(riseTime*fs))'./ceil(riseTime*fs) ) );
% COSINE ENVELOPE (not working as it should)
% envelopeFcn = @(riseTime, duration, fs) cat(1, ...
% cos(pi.*(1/2 + (1:ceil(riseTime*fs))'./ceil(riseTime*fs) )),...
% ones(ceil(duration*fs) - 2*ceil(riseTime*fs), 1) ,...
% flipud( cos(pi.*(1/2 + (1:ceil(riseTime*fs))'./ceil(riseTime*fs) ))) );
% WITH ENVELOPE
envelopeRiseTime = .025;
obj.toneObj.signalGeneratingFcn = @()obj.toneVolume.*obj.sineGenerator.step .* envelopeFcn( envelopeRiseTime, obj.toneObj.signalDuration, obj.stimulusSampleFrequency);
% WITHOUT ENVELOPE
% obj.toneObj.signalGeneratingFcn = @()obj.toneVolume.*obj.sineGenerator.step
% WITH CHIRP
% obj.toneObj.signalGeneratingFcn = @()obj.toneVolume.*obj.sineGenerator.step.*obj.chirpGenerator.step;
% STORE MULTIPLE TONES
toneSig = zeros(obj.toneObj.outputNumSamples,1);
if numel(obj.toneVolume) == 1
toneVol = [obj.toneVolume obj.toneVolume];
else
toneVol = obj.toneVolume;
end
activeToneSig = toneVol(1).*obj.sineGenerator.step ...
.* envelopeFcn( envelopeRiseTime, obj.toneObj.signalDuration, obj.stimulusSampleFrequency);
toneSig(floor(obj.toneObj.signalRate*obj.toneObj.signalDelay) + (1:numel(activeToneSig))) = activeToneSig;
obj.toneSignal{1} = toneSig;
release(obj.sineGenerator)
obj.sineGenerator.Frequency = 4500;
toneSig = zeros(obj.toneObj.outputNumSamples,1);
activeToneSig = toneVol(2).*obj.sineGenerator.step ...
.* envelopeFcn( envelopeRiseTime, obj.toneObj.signalDuration, obj.stimulusSampleFrequency);
toneSig(floor(obj.toneObj.signalRate*obj.toneObj.signalDelay) + (1:numel(activeToneSig))) = activeToneSig;
obj.toneSignal{2} = toneSig;
% STORE MULTIPLE PUFFS
puffSig = zeros(obj.puffObj.outputNumSamples,1);
activePuffSig = ones(floor(obj.puffObj.signalDuration*obj.puffObj.signalRate),1); % (puff)
puffSig(floor(obj.puffObj.signalRate*obj.puffObj.signalDelay) + (1:numel(activePuffSig))) = activePuffSig;
obj.puffSignal{1} = puffSig;
puffSig = zeros(obj.puffObj.outputNumSamples,1);
activePuffSig = 1; % (no puff)
puffSig(floor(obj.puffObj.signalRate*obj.puffObj.signalDelay) + (1:numel(activePuffSig))) = activePuffSig;
obj.puffSignal{2} = puffSig;
%STORE MULTIPLE LASERS
laserSig = zeros(obj.laserObj.outputNumSamples,1);
activeLaserSig = ones(floor(obj.laserObj.signalDuration*obj.laserObj.signalRate,1)); %(laser)
laserSig(floor(obj.laserObj.signalRate*obj.laserobj.signalDelay) + (1:numel(activeLaserSig)))= activeLaserSig;
obj.laserSignal{1} = laserSig;
laserSig = zeros(obj.puffObj.outputNumSamples,1);
activeLaserSig = 1; %(no laser)
laserSig(floor(obj.laserObj.signalRate*obj.laserObj.signalDelay) + (1:numel(activeLaserSig))) = activeLaserSig;
obj.laserSignal{2} = laserSig;
% ASSIGN STIMULI TO TRIALS RANDOMLY
obj.trialStimulusNumber = zeros(obj.nTrials, 1);
nStims = 3;
nonRandomStimNumbers = repmat(1:nStims, [floor(obj.nTrials/nStims), 1]);
nonRandomStimNumbers = nonRandomStimNumbers(:);
randomIndex = randperm(60);
randomStimNumbers = zeros(60,1);
for i=1:60
randomStimNumbers(i) = nonRandomStimNumbers(randomIndex(i));
end
obj.trialStimulusNumber = randomStimNumbers;
obj.currentStimulusNumber = 0;
obj.nextStimulusNumber = obj.trialStimulusNumber(1);
end
end
methods % EVENT RESPONSE
function experimentStateChangeFcn(obj,~,evnt)
% (Required by SubSystem Parent Class)
fprintf('TonePuffSystem3: Received ExperimentStateChange event\n')
switch evnt.EventName
case 'ExperimentStart'
if ~logical(obj.experimentRunning)
start(obj)
end
case 'ExperimentStop'
if logical(obj.experimentRunning)
stop(obj);
end
end
end
function trialStateChangeFcn(obj,~,~)
% (Required by SubSystem Parent Class)
fprintf('TonePuffSystem3: Received TrialStateChange event\n')
% persistent trialNumberLocal;
% if isempty(trialNumberLocal)
% trialNumberLocal = 0;
% end
% obj.currentTrialNumber = trialNumberLocal;
% fprintf('Queuing Trial %i\n', trialNumberLocal + 1)
try
% STIMULUS NUMBERS
if isempty(obj.currentTrialNumber)
obj.currentTrialNumber = 0;
else
obj.currentTrialNumber = obj.currentTrialNumber + 1;
end
if obj.currentTrialNumber > 0
% QUEUE OUTPUT
obj.puffObj.queueOutput();
obj.toneObj.queueOutput();
obj.laserObj.queueOutput();
obj.currentStimulusNumber = obj.trialStimulusNumber(obj.currentTrialNumber);
else
obj.currentStimulusNumber = 0;
end
if obj.currentTrialNumber >= length(obj.trialStimulusNumber) % last trial
obj.nextStimulusNumber = 0;
else
obj.nextStimulusNumber = obj.trialStimulusNumber(obj.currentTrialNumber + 1);
end
% DATA FILE
if ~isempty(obj.currentDataFile)
obj.currentDataFile.experimentName = obj.currentExperimentName;
if ~isempty(obj.currentDataFile.trialNumber)
% previous data-file -> save it
obj.saveDataFile();
% exits after creating new (blank) currentDataFile
% for next trial
end
end
% trialNumberLocal = trialNumberLocal + 1;
% prepare next DataFile with info for next trial
% (or minute of recording)
obj.currentDataFile.trialNumber = obj.currentTrialNumber;
obj.currentDataFile.stimulusNumber = obj.currentStimulusNumber;
obj.currentDataFile.experimentName = obj.currentExperimentName;
% fprintf('Start of Trial: %i\n', trialNumberLocal);
catch me
obj.lastError = me;
keyboard
end
end
function frameAcquiredFcn(obj,src,evnt)
timeAtStart = hat;
% (Required by SubSystem Parent Class)
try
% INCREMENT FRAME-COUNT
frameNum = obj.framesAcquired + 1;
obj.framesAcquired = frameNum;
% FIRST FRAME
if isempty(obj.currentDataFile)
obj.currentDataFile = TonePuffFile(...
'rootPath',obj.currentDataSetPath,...
'experimentName',obj.currentExperimentName);%changed rootPath from sessionPath
end
% EVERY FRAME - ADD FRAME INFO TO FILE
frameInfo.frameNumber = frameNum;
frameInfo.triggerTime = evnt.TriggerTime;
frameInfo.timeStamp = evnt.TimeStamps;
frameInfo.trialNumber = obj.currentTrialNumber;
frameInfo.stimulusNumber = obj.currentStimulusNumber;
frameInfo.highAccuracyTime = hat;
frameInfo.firstFrame = double(any(frameNum==obj.trialFirstFrame));
frameInfo.scansAcquired = evnt.Source.ScansAcquired;
frameInfo.toneSamplesQueued = obj.toneObj.sessionObj.ScansQueued;
frameInfo.toneSamplesOutput = obj.toneObj.sessionObj.ScansOutputByHardware;
frameInfo.puffSamplesQueued = obj.puffObj.sessionObj.ScansQueued;
frameInfo.puffSamplesOutput = obj.puffObj.sessionObj.ScansOutputByHardware;
frameInfo.laserSamplesQueued = obj.laserObj.sessionObj.ScansQueued;
frameInfo.laserSamplesOutput = obj.laserObj.sessionObj.ScansOutputByHardware;
frameData = hat;
if isclosed(obj.currentDataFile)
if ~issaved(obj.currentDataFile)
obj.saveDataFile;
end
obj.currentDataFile = TonePuffFile(...
'rootPath',obj.currentDataSetPath,...
'experimentName',obj.currentExperimentName);
end
addFrame2File(obj.currentDataFile,frameData,frameInfo);
% NOTIFY NEW TRIAL BEGINS WITH NEXT FRAME
if any((frameNum+1) == obj.trialFirstFrame(1:end-1))
notify(obj, 'NewTrial')
frameInfo.firstFrame = 1;
elseif frameNum >= obj.trialFirstFrame(end) %last frame
stop(obj)
else
% LOAD NEXT STIMULUS (+/-puff, +/-tone)
if (hat - timeAtStart) < (.9/obj.frameClkFrequency)
if (obj.puffObj.sessionObj.ScansQueued < 1) && (obj.toneObj.sessionObj.ScansQueued < 1) && (obj.laserObj.sessionObj.ScansQueued < 1)
obj.puffObj.prepareOutput(obj.puffSignal{obj.nextStimulusNumber});
obj.laserObj.prepareOutput(obj.laserSignal(obj.nextStimulusNumber)); %check if i need this
end
end
if (hat - timeAtStart) < (.9/obj.frameClkFrequency)
if obj.toneObj.sessionObj.ScansQueued < 1
obj.toneObj.prepareOutput(obj.toneSignal{obj.nextStimulusNumber});
end
end
end
catch me
obj.lastError = me;
end
end
end
methods % SET
function set.experimentSyncObj(obj,bhv)
if ~isempty(obj.experimentStateListener)
obj.experimentStateListener.Enabled = false;
end
obj.experimentSyncObj = bhv;
obj.experimentStateListener = addlistener(obj.experimentSyncObj,...
'ExperimentStart',@(src,evnt)experimentStateChangeFcn(obj,src,evnt));
addlistener(obj.experimentSyncObj,...
'ExperimentStop',@(src,evnt)experimentStateChangeFcn(obj,src,evnt));
obj.experimentStateListener.Enabled = true;
end
function set.trialSyncObj(obj,bhv)
obj.trialSyncObj = bhv;
if ~isempty(obj.trialStateListener)
obj.trialStateListener.Enabled = false;
end
obj.trialStateListener = addlistener(obj.trialSyncObj,...
'NewTrial',@(src,evnt)trialStateChangeFcn(obj,src,evnt));
obj.trialStateListener.Enabled = false;
end
function set.frameSyncObj(obj,cam)
if ~isempty(obj.frameSyncListener)
obj.frameSyncListener.Enabled = false;
end
obj.frameSyncObj = cam;
% Define Listener
obj.frameSyncListener = addlistener(obj.frameSyncObj,...
'FrameAcquired',@(src,evnt)frameAcquiredFcn(obj,src,evnt));
obj.frameSyncListener.Enabled = false;
end
end
methods
function delete(obj)
global CURRENT_EXPERIMENT_NAME
CURRENT_EXPERIMENT_NAME = [];
try
obj.saveDataSet();
stop(obj.frameClkSession);
stop(obj.frameCounterSession);
delete(obj.frameClkSession);
delete(obj.frameCounterSession);
close(obj.dataLogger.logFig)
delete(obj.dataLogger)
if isvalid(obj.toneObj)
delete(obj.toneObj);
end
if isvalid(obj.puffObj)
delete(obj.puffObj);
end
if isvalid(obj.laserObj)
delete(obj.laserObj);
end
catch me
disp(me.message)
end
end
end
end