-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlaunch_fc.m
More file actions
608 lines (542 loc) · 17.5 KB
/
launch_fc.m
File metadata and controls
608 lines (542 loc) · 17.5 KB
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
% Version 1.3
% last update: 2022 01 11
% updated to make timestamps and tones more in line with each other
% Fear conditioning script
% started ZZ 5/13/21
% Goal: make fear conditioning script to work with modern MATLAB Arduino
% interface, and provide way to also do light cue
% hardware setup on arduino: pin 4 - shock, pin 5 - tone/blank, pin 6 - laser,
% pin 7 - light, pin 13, miniscope trigger
%% setup params
% experiment structure
function launch_fc(P)
a = P.a;
exp_ID = P.exp_ID;
cs_plus = P.cs_plus;
cs_minus = P.cs_minus;
t_baseline = P.t_baseline; % (s) baseline time before use
min_trial_int = P.min_trial_int; % (s)
max_trial_int = P.max_trial_int; % (s)
% tone settings
tone_freq1 = P.tone_freq1; %(hz) for pure tone
tone_freq2 = P.tone_freq2;
start_freq1 = P.start_freq1; % f0 for FM sweep
start_freq2 = P.start_freq2;
end_freq1 = P.end_freq1; % f_end for FM sweep
end_freq2 = P.end_freq2;
sweep_dur = P.sweep_dur; %(s) duration of sweep, repeated over cs_dur. NB must evenly divide with cs_dur
% light settings
flicker_freq1 = P.flicker_freq1 ;% (hz) from on to off to back on again
flicker_freq2 = P.flicker_freq2;
light_dc1 = P.light_dc1; %duty cycle of light (0.5 = 50% duty cycle)
light_dc2 = P.light_dc2;
% get CS+ and CS- params
if isequal(cs_plus,'Tone1')
csp_p.tone_freq = tone_freq1;
csp_p.name = "Tone";
elseif isequal(cs_plus,'Tone2')
csp_p.tone_freq = tone_freq2;
csp_p.name = "Tone";
elseif isequal(cs_plus,'FM Sweep1')
csp_p.start_freq = start_freq1;
csp_p.end_freq = end_freq1;
csp_p.sweep_dur = sweep_dur;
csp_p.name = "FM";
elseif isequal(cs_plus,'FM Sweep2')
csp_p.start_freq = start_freq2;
csp_p.end_freq = end_freq2;
csp_p.sweep_dur = sweep_dur;
csp_p.name = "FM";
elseif isequal (cs_plus, 'Light')
csp_p = [];
csp_p.name = 'Light';
elseif isequal(cs_plus, 'Pulsed Light1')
csp_p.flicker_freq = flicker_freq1;
csp_p.light_dc = light_dc1;
csp_p.name = 'Pulsed Light';
elseif isequal(cs_plus, 'Pulsed Light2')
csp_p.flicker_freq = flicker_freq2;
csp_p.light_dc = light_dc2;
csp_p.name = 'Pulsed Light';
end
% do cs_minus
if isequal(cs_minus,'Tone1')
csm_p.tone_freq = tone_freq1;
csm_p.name = "Tone";
elseif isequal(cs_minus,'Tone2')
csm_p.tone_freq = tone_freq2;
csm_p.name = "Tone";
elseif isequal(cs_minus,'FM Sweep1')
csm_p.start_freq = start_freq1;
csm_p.end_freq = end_freq1;
csm_p.sweep_dur = sweep_dur;
csm_p.name = "FM";
elseif isequal(cs_minus,'FM Sweep2')
csm_p.start_freq = start_freq2;
csm_p.end_freq = end_freq2;
csm_p.sweep_dur = sweep_dur;
csm_p.name = "FM";
elseif isequal(cs_minus, 'Light')
csm_p = [];
csm_p.name = 'Light';
elseif isequal(cs_minus, 'Pulsed Light1')
csm_p.flicker_freq = flicker_freq1;
csm_p.light_dc = light_dc1;
csm_p.name = 'Pulsed Light';
elseif isequal(cs_minus, 'Pulsed Light2')
csm_p.flicker_freq = flicker_freq2;
csm_p.light_dc = light_dc2;
csm_p.name = 'Pulsed Light';
end
% cs and us settings
cs_dur = P.cs_dur; % (s)
us_dur = P.us_dur;
xd = P.expdesign;
xd_labels = ["CS+";"CS-";"Shock";"Laser"];
%% initialization
tonep = 'D3'; %tonepin
shockp = 'D4'; %shockpin
lightp = 'D7'; %lightpin
optop = 'D6'; % opto pin
minip = 'D13'; % pin to trigger miniscipe
int_range = [min_trial_int, max_trial_int];
ts = struct;
ts.csp_on = [];
ts.csp_off = [];
ts.csm_on = [];
ts.csm_off = [];
ts.us_on = [];
ts.us_off = [];
ts.laser_on = [];
ts.laser_off = [];
ts.miniscope_on = [];
ts.miniscope_off = [];
%% trigger miniscope
if P.doMiniscope
a.writeDigitalPin(minip, 1);
ts.miniscope_on = clock;
disp('Miniscope recording initiated')
end
%% baseline
if t_baseline > 0
disp(['baseline period. No stimulus presentations for ' num2str(t_baseline) ' seconds'])
pause(t_baseline)
end
%% do presentations
% presentation are decoded from the four digit code in xd, indicating which
% cs, if shock, and if laser. For each combination of cs, shock, and laser,
% there is a specific function
first = 1; % skip ITI for first run
disp('Now doing events')
for i = 1:size(xd,2)
% on first run, skip ITI
if first == 0
pause(randi(int_range));
elseif first == 1
first = 0;
end
disp(['Now doing event #' num2str(i)]);
this = xd(:,i);
if isequal(this,[1;0;0;0])
ts = doStim('csp', csp_p, a, tonep, lightp, cs_dur, ts);
elseif isequal(this,[0;1;0;0])
ts = doStim('csm', csm_p, a, tonep, lightp, cs_dur, ts);
elseif isequal(this,[1;0;1;0])
ts = doStimShock('csp', csp_p, a, tonep, lightp, shockp, cs_dur, us_dur, ts);
elseif isequal(this,[0;1;1;0])
ts = doStimShock('csm', csm_p, a, tonep, lightp, shockp, cs_dur, us_dur, ts);
elseif isequal(this,[1;0;0;1])
ts = doStimLaser('csp', csp_p, a, tonep, lightp, cs_dur, optop, ts);
elseif isequal(this,[0;1;0;1])
ts = doStimLaser('csm', csm_p, a, tonep, lightp, cs_dur, optop, ts);
elseif isequal(this,[1;0;1;1])
ts = doStimShockLaser('csp', csp_p, a, tonep, lightp, shockp, cs_dur, us_dur, optop, ts);
elseif isequal(this,[0;1;1;1])
ts = doStimShockLaser('csm', csm_p, a, tonep, lightp, shockp, cs_dur, us_dur, optop, ts);
elseif isequal(this,[2;2;0;1])
ts = doLaser(a, cs_dur, optop, ts);
elseif isequal(this, [2;2;1;0])
ts = doShock(a, shockp, cs_dur, us_dur, ts);
elseif isequal(this, [2;2;1;1])
ts = doLaserShock(a, shockp, optop, cs_dur, us_dur, ts);
end
disp(['Done ' num2str(i) ' of ' num2str(size(xd,2)) ' events']);
end
if P.postHoldTime > 0
pause(P.postHoldTime);
end
if P.doMiniscope
a.writeDigitalPin(minip, 0);
ts.miniscope_off = clock;
disp('Miniscope recording ended')
end
disp('experiment complete')
%% post processing
time = datestr(clock,'YYYY-mm-dd_HH-MM-SS');
savevars = {'cs_dur', 'cs_minus', 'cs_plus', 'csm_p', 'csp_p', 'exp_ID', 'min_trial_int', 'max_trial_int', 't_baseline', 'ts', 'us_dur', 'xd', 'xd_labels'};
savename = [exp_ID '_' time '.mat'];
save(savename, savevars{:});
clear;
end
%% functions
% for doing just stimulus presentation. input: cs (csp or csm),
% cs_params, arduino handle, tonepin, lightpin)
function ts = doStim(cs, csP, a, tonep, lightp, cs_dur, ts)
%%
if isequal(csP.name, 'Tone')
[y, Fs] = prepSineWave(cs_dur, csP.tone_freq);
sound(y,Fs);
a.writeDigitalPin(tonep, 1);
if isequal(cs, 'csp')
ts.csp_on = [ts.csp_on; clock];
else
ts.csm_on = [ts.csm_on; clock];
end
pause(cs_dur);
a.writeDigitalPin(tonep, 0);
if isequal(cs, 'csp')
ts.csp_off = [ts.csp_off; clock];
else
ts.csm_off = [ts.csm_off; clock];
end
%%
elseif isequal(csP.name, 'FM')
[y, Fs] = prepFMSweep(csP.start_freq, csP.end_freq, csP.sweep_dur, cs_dur);
sound(y,Fs);
a.writeDigitalPin(tonep, 1);
if isequal(cs, 'csp')
ts.csp_on = [ts.csp_on; clock];
else
ts.csm_on = [ts.csm_on; clock];
end
pause(cs_dur);
a.writeDigitalPin(tonep, 0);
if isequal(cs, 'csp')
ts.csp_off = [ts.csp_off; clock];
else
ts.csm_off = [ts.csm_off; clock];
end
%%
elseif isequal(csP.name, 'Light')
a.writeDigitalPin(lightp,1);
if isequal(cs, 'csp')
ts.csp_on = [ts.csp_on; clock];
else
ts.csm_on = [ts.csm_on; clock];
end
pause(cs_dur)
a.writeDigitalPin(lightp,0);
if isequal(cs, 'csp')
ts.csp_off = [ts.csp_off; clock];
else
ts.csm_off = [ts.csm_off; clock];
end
%%
elseif isequal(csP.name, 'Pulsed Light')
if isequal(cs, 'csp')
ts.csp_on = [ts.csp_on; clock];
else
ts.csm_on = [ts.csm_on; clock];
end
flickerLight(csP.flicker_freq, csP.light_dc, a, lightp, cs_dur);
if isequal(cs, 'csp')
ts.csp_off = [ts.csp_off; clock];
else
ts.csm_off = [ts.csm_off; clock];
end
end
end
%%
function ts = doStimShock(cs, csP, a, tonep, lightp, shockp, cs_dur, us_dur, ts)
if isequal(csP.name, 'Tone')
[y, Fs] = prepSineWave(cs_dur, csP.tone_freq);
sound(y,Fs);
a.writeDigitalPin(tonep, 1);
if isequal(cs, 'csp')
ts.csp_on = [ts.csp_on; clock];
else
ts.csm_on = [ts.csm_on; clock];
end
pause(cs_dur-us_dur);
a.writeDigitalPin(shockp,1);
ts.us_on = [ts.us_on; clock];
pause(us_dur);
a.writeDigitalPin(shockp,0);
a.writeDigitalPin(tonep, 0);
ts.us_off = [ts.us_off; clock];
if isequal(cs, 'csp')
ts.csp_off = [ts.csp_off; clock];
else
ts.csm_off = [ts.csm_off; clock];
end
%%
elseif isequal(csP.name, 'FM')
[y, Fs] = prepFMSweep(csP.start_freq, csP.end_freq, csP.sweep_dur, cs_dur);
sound(y,Fs);
a.writeDigitalPin(tonep, 1);
if isequal(cs, 'csp')
ts.csp_on = [ts.csp_on; clock];
else
ts.csm_on = [ts.csm_on; clock];
end
pause(cs_dur-us_dur);
a.writeDigitalPin(shockp,1);
ts.us_on = [ts.us_on; clock];
pause(us_dur);
a.writeDigitalPin(shockp,0);
a.writeDigitalPin(tonep, 0);
ts.us_off = [ts.us_off; clock];
if isequal(cs, 'csp')
ts.csp_off = [ts.csp_off; clock];
else
ts.csm_off = [ts.csm_off; clock];
end
%%
elseif isequal(csP.name, 'Light')
a.writeDigitalPin(lightp,1);
if isequal(cs, 'csp')
ts.csp_on = [ts.csp_on; clock];
else
ts.csm_on = [ts.csm_on; clock];
end
pause(cs_dur-us_dur)
a.writeDigitalPin(shockp,1);
ts.us_on = [ts.us_on; clock];
pause(us_dur);
a.writeDigitalPin(lightp,0);
if isequal(cs, 'csp')
ts.csp_off = [ts.csp_off; clock];
else
ts.csm_off = [ts.csm_off; clock];
end
a.writeDigitalPin(shockp,0);
ts.us_off = [ts.us_off; clock];
%%
elseif isequal(csP.name, 'Pulsed Light')
if isequal(cs, 'csp')
ts.csp_on = [ts.csp_on; clock];
else
ts.csm_on = [ts.csm_on; clock];
end
flickerLight(csP.flicker_freq, csP.light_dc, a, lightp, cs_dur-us_dur);
a.writeDigitalPin(shockp,1);
ts.us_on = [ts.us_on; clock];
flickerLight(csP.flicker_freq, csP.light_dc, a, lightp, us_dur);
if isequal(cs, 'csp')
ts.csp_off = [ts.csp_off; clock];
else
ts.csm_off = [ts.csm_off; clock];
end
a.writeDigitalPin(shockp,0);
ts.us_off = [ts.us_off; clock];
end
end
%%
function ts = doStimLaser(cs, csP, a, tonep, lightp, cs_dur, optop, ts)
if ismember(csP.name, ["Tone", "FM"])
if isequal(csP.name, 'Tone')
[y, Fs] = prepSineWave(cs_dur, csP.tone_freq);
elseif isequal(csP.name, 'FM')
[y, Fs] = prepFMSweep(csP.start_freq, csP.end_freq, csP.sweep_dur, cs_dur);
end
a.writeDigitalPin(optop, 1);
sound(y,Fs);
ts.laser_on = [ts.laser_on; clock];
a.writeDigitalPin(tonep,1);
if isequal(cs, 'csp')
ts.csp_on = [ts.csp_on; clock];
else
ts.csm_on = [ts.csm_on; clock];
end
pause(cs_dur);
a.writeDigitalPin(tonep, 0);
if isequal(cs, 'csp')
ts.csp_off = [ts.csp_off; clock];
else
ts.csm_off = [ts.csm_off; clock];
end
%%
elseif isequal(csP.name, 'Light')
a.writeDigitalPin(lightp,1);
if isequal(cs, 'csp')
ts.csp_on = [ts.csp_on; clock];
else
ts.csm_on = [ts.csm_on; clock];
end
pause(cs_dur)
a.writeDigitalPin(lightp,0);
if isequal(cs, 'csp')
ts.csp_off = [ts.csp_off; clock];
else
ts.csm_off = [ts.csm_off; clock];
end
%%
elseif isequal(csP.name, 'Pulsed Light')
if isequal(cs, 'csp')
ts.csp_on = [ts.csp_on; clock];
else
ts.csm_on = [ts.csm_on; clock];
end
flickerLight(csP.flicker_freq, csP.light_dc, a, lightp, cs_dur);
if isequal(cs, 'csp')
ts.csp_off = [ts.csp_off; clock];
else
ts.csm_off = [ts.csm_off; clock];
end
end
a.writeDigitalPin(optop,0);
ts.laser_off = [ts.laser_off; clock];
end
%%
function ts = doStimShockLaser(cs, csP, a, tonep, lightp, shockp, cs_dur, us_dur, optop, ts)
%disp(csP.name)
if ismember(csP.name, ["Tone", "FM"])
if isequal(csP.name, 'Tone')
[y, Fs] = prepSineWave(cs_dur, csP.tone_freq);
elseif isequal(csP.name, 'FM')
[y, Fs] = prepFMSweep(csP.start_freq, csP.end_freq, csP.sweep_dur, cs_dur);
end
a.writeDigitalPin(optop, 1);
sound(y,Fs);
ts.laser_on = [ts.laser_on; clock];
a.writeDigitalPin(tonep,1);
if isequal(cs, 'csp')
ts.csp_on = [ts.csp_on; clock];
else
ts.csm_on = [ts.csm_on; clock];
end
pause(cs_dur-us_dur);
a.writeDigitalPin(shockp, 1);
ts.us_on = [ts.us_on; clock];
pause(us_dur);
a.writeDigitalPin(shockp, 0);
a.writeDigitalPin(tonep, 0);
ts.us_off = [ts.us_off; clock];
if isequal(cs, 'csp')
ts.csp_off = [ts.csp_off; clock];
else
ts.csm_off = [ts.csm_off; clock];
end
%%
elseif isequal(csP.name, 'Light')
a.writeDigitalPin(lightp,1);
if isequal(cs, 'csp')
ts.csp_on = [ts.csp_on; clock];
else
ts.csm_on = [ts.csm_on; clock];
end
pause(cs_dur-us_dur)
a.writeDigitalPin(shockp,1);
ts.us_on = [ts.us_on; clock];
pause(us_dur);
a.writeDigitalPin(lightp,0);
if isequal(cs, 'csp')
ts.csp_off = [ts.csp_off; clock];
else
ts.csm_off = [ts.csm_off; clock];
end
a.writeDigitalPin(shockp,0);
ts.us_off = [ts.us_off; clock];
%%
elseif isequal(csP.name, 'Pulsed Light')
if isequal(cs, 'csp')
ts.csp_on = [ts.csp_on; clock];
else
ts.csm_on = [ts.csm_on; clock];
end
flickerLight(csP.flicker_freq, csP.light_dc, a, lightp, cs_dur-us_dur);
a.writeDigitalPin(shockp,1);
ts.us_on = [ts.us_on; clock];
flickerLight(csP.flicker_freq, csP.light_dc, a, lightp, us_dur);
if isequal(cs, 'csp')
ts.csp_off = [ts.csp_off; clock];
else
ts.csm_off = [ts.csm_off; clock];
end
a.writeDigitalPin(shockp,0);
ts.us_off = [ts.us_off; clock];
end
a.writeDigitalPin(optop,0);
ts.laser_off = [ts.laser_off; clock];
end
%%
function ts = doLaser(a, cs_dur, optop, ts)
a.writeDigitalPin(optop, 1);
ts.laser_on = [ts.laser_on; clock];
pause(cs_dur);
a.writeDigitalPin(optop, 0);
ts.laser_off = [ts.laser_off; clock];
end
%%
function ts = doShock(a, shockp, cs_dur, us_dur, ts)
pause(cs_dur-us_dur);
a.writeDigitalPin(shockp,1);
ts.us_on = [ts.us_on; clock];
pause(us_dur);
a.writeDigitalPin(shockp, 0);
ts.us_off = [ts.us_off; clock];
end
%%
function ts = doLaserShock(a, shockp, optop, cs_dur, us_dur, ts)
a.writeDigitalPin(optop, 1);
ts.laser_on = [ts.laser_on; clock];
pause(cs_dur-us_dur);
a.writeDigitalPin(shockp,1);
ts.us_on = [ts.us_on; clock];
pause(us_dur);
a.writeDigitalPin(shockp, 0);
ts.us_off = [ts.us_off; clock];
a.writeDigitalPin(optop, 0);
ts.laser_off = [ts.laser_off; clock];
end
%%
function ts = playSineWave(y, Fs, ts, tonep, a)
a.writeDigitalPin(tonep,1);
sound(y,Fs);
end
%%
function flickerLight(flicker_freq, light_dc, a, lightp, cs_dur)
count = 0;
while count < cs_dur
a.writeDigitalPin(lightp, 1);
pause((1/flicker_freq)*light_dc);
a.writeDigitalPin(lightp,0);
pause((1/flicker_freq)*(1-light_dc));
count = count + (1/flicker_freq);
end
end
%%
function playFMSweep(start_freq, end_freq, sweep_dur, cs_dur)
% note: cs_dur must be evenly divisble by sweep_dur
Fs = 100000;
t = 0:1/Fs:sweep_dur;
f_in_start = start_freq;
f_in_end = end_freq;
f_in = linspace(f_in_start, f_in_end, length(t));
phase_in = cumsum(f_in/Fs);
y = sin(2*pi*phase_in);
rep = round(cs_dur/sweep_dur);
y_cs = repmat(y,[1,rep]);
sound(y_cs,Fs)
end
function [y, Fs] = prepSineWave(cs_dur,tone_freq)
Fs = 44100; % sampling freq (hz) NOTE: tone limit is 22 kHz
Ts = 1/Fs; % sampling interval (s)
T = 0:Ts:(Fs*Ts*cs_dur);
y = sin(2*pi*tone_freq*T); % tone
end
function [y_cs, Fs] = prepFMSweep(start_freq, end_freq, sweep_dur, cs_dur)
Fs = 44100;
t = 0:1/Fs:sweep_dur;
f_in_start = start_freq;
f_in_end = end_freq;
f_in = linspace(f_in_start, f_in_end, length(t));
phase_in = cumsum(f_in/Fs);
y = sin(2*pi*phase_in);
if sweep_dur ~= 1
dif = Fs - length(y);
y = [y, zeros(1,dif)];
end
rep = round(cs_dur/1); % edit this line to include hz in future upload
y_cs = repmat(y,[1,rep]);
end