-
Notifications
You must be signed in to change notification settings - Fork 2
/
calcDFoF.m
45 lines (34 loc) · 1.24 KB
/
calcDFoF.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
function traceDFoF = calcDFoF(traces,Fps)
% function traceDFoF = calcDFoF(trace)
%
% calculates DFoF and does some minor baseline shift correction
%
% MF 2010-11-06
params.WindowBaseline = 15;
params.Percentile = 10;
traceDFoF = nan(size(traces));
winSize = ceil(params.WindowBaseline*Fps);
for itrace = 1:size(traces,2)
trace = reshape(traces(:,itrace), 1, []);
if length(trace)>winSize
% Cut the trace into windows
if mod(length(trace), winSize) ~= 0
nbRepElements = winSize - mod(length(trace), winSize);
traceWin = [trace repmat(trace(end), 1, nbRepElements)];
else
traceWin = trace;
end
traceWin = reshape(traceWin, winSize, []);
segMin = prctile((traceWin)',10,2);
% Prepare interpolation
segPos = (winSize/2) + winSize * (0:length(segMin)-1);
segPos(1) = 1;
segPos(end) = length(trace);
baseline = interp1(segPos, segMin, 1:length(trace), 'spline');
Trace = (trace - (baseline-segMin(1)))';
else
Trace = trace';
end
% Calculate DF/F
traceDFoF(:,itrace) = (Trace - (prctile(Trace,params.Percentile)))/prctile(Trace,params.Percentile);
end