Last active
March 25, 2018 09:25
-
-
Save kagaya/3802904 to your computer and use it in GitHub Desktop.
Automatic Recording of Force Data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ForceData=autorec(duration, strikeThreshold, rewardThreshold) | |
%% DISCRIPTION | |
% This program automatically records foce data by detecting rising phase of | |
% the voltage change and it produce digital output whether the peak of the | |
% voltage above a threshold. The condition of output can be customized. | |
%% HOW TO USE | |
% The termination of this program is tricky. Please press "Ctrl - c" to | |
% quit the program and after that, command "daqreset" in the MATLAB prompt. | |
%% ARGUMENTS | |
% "duration" sets the duration of recording of each strike. | |
% "strikeThreshold" sets a threshold of detecting a strike (keep low). | |
% "rewardThreshold" sets a threshold of digital output to a feeder. | |
%% PARAMETERS | |
% nidaqtimeout sets a recording duration | |
nidaqtimeout = 72000; | |
samplingrate = 500000; | |
chd=1; | |
ch=2; | |
% ratio | |
ratioForOffset = 1/4; | |
ratioForTriggerDelay = -1/2; | |
%% create an instantaneous folder name that represents a recording session | |
savetime=clock; | |
a=num2str(savetime(2)); | |
b=num2str(savetime(3)); | |
c=num2str(savetime(1)); | |
d=num2str(savetime(4)); | |
e=num2str(savetime(5)); | |
f=num2str(savetime(6)); | |
DataDirName=[a,'-', b, '-' c, '-', d, '-', e, '-', f]; | |
mkdir(DataDirName); | |
%% prepare digital output to switch on a feeder | |
DIO = digitalio('nidaq','Dev3'); | |
addline(DIO, 0,'out'); | |
% set the digital output to zero for initialization | |
putvalue(DIO.Line(1), 0); | |
putvalue(DIO.Line(1), 1); | |
% pause(1); | |
putvalue(DIO.Line(1), 0); | |
tStart = tic; | |
%% prepare analogue input | |
AI = analoginput('nidaq','Dev3'); | |
% offset analogue input | |
set(AI,'InputType','SingleEnded') | |
chan = addchannel(AI,ch); | |
chand = addchannel(AI,chd); | |
AI.Channel.InputRange=[-10 10]; | |
% sampling rate should be remained at 500 kHz | |
set(AI, 'SampleRate', samplingrate) | |
ActualRate = get(AI, 'SampleRate'); | |
samplenum = duration * ActualRate; | |
set(AI, 'SamplesPerTrigger', samplenum) | |
set(AI, 'TriggerType', 'Software') | |
set(AI, 'TriggerChannel', chand) | |
set(AI, 'TriggerCondition', 'Rising') | |
set(AI, 'TriggerConditionValue', strikeThreshold) | |
set(AI, 'TriggerDelay', ratioForTriggerDelay * duration) | |
set(AI, 'Timeout', nidaqtimeout) | |
Fs = ActualRate; | |
%% record strikes | |
r=0; | |
while 1 | |
r = r + 1; | |
strike.isRewarded = 0; | |
pause(0.005); | |
start(AI); | |
force = getdata(AI); | |
tElapsed = toc(tStart); | |
disp(r) | |
strike.time = tElapsed; | |
rawForce = force(:,1); | |
difForce = force(:,2); | |
offset = - mean(rawForce(1:round(ratioForOffset * samplenum))); | |
realForce = rawForce + offset; | |
maxRealForce = max(realForce); | |
if maxRealForce > rewardThreshold % condition of digital output | |
putvalue(DIO.Line(1), 1) | |
pause(0.01); | |
putvalue(DIO.Line(1), 0) | |
strike.isRewarded = 1; | |
end | |
strike.force = rawForce; | |
strike.offset = offset; | |
strike.duration = duration; | |
strike.strikeThreshold = strikeThreshold; | |
strike.rewardThreshold = rewardThreshold; | |
save([DataDirName, '/', 'strike', num2str(r), '.mat'], 'strike') | |
end | |
delete(AI) | |
clear AI | |
delete(DIO) | |
clear DIO | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment