function [greenAdjusted, greenTime, firstTimesig, blueOnly, fs] = extractFPdata(data,Option) if nargin == 1 Option =1; end %create a time vector in seconds, relative to the trial start time=NaN(length(data),1); for i=1:length(data) %convert all time to seconds tHours=data(i,3)*60*60*1000; tMinutes=data(i,4)*60*1000; tSeconds=data(i,5)*1000; tMillisecs=data(i,6); time(i,1)=tHours+tMinutes+tSeconds+tMillisecs; end time=time/1000; firstTimesig=time(1); time=time-firstTimesig; greenData=data(:,2); redData=data(:,1); if greenData(2,1)>greenData(1,1) %odd rows greenOnly=greenData(2:2:end,:); blueOnly=greenData(1:2:end-1,:); redOnly = redData(2:2:end,:); greenTime=time(2:2:end,:); blueTime=time(1:2:end-1,:); redTime = time(2:2:end,:); else blueOnly=greenData(2:2:end,:); greenOnly=greenData(1:2:end-1,:); redOnly = redData(1:2:end-1,:); blueTime=time(2:2:end,:); greenTime=time(1:2:end-1,:); redTime=time(1:2:end-1,:); end l = min([length(greenTime) length(greenOnly)]); greenTime = greenTime(1:l); greenOnly = greenOnly(1:l); blueOnly = blueOnly(1:l); blueTime = blueTime(1:l); fs = round(1/mean(diff(greenTime))); switch Option case 1 % NPM code p=polyfit(blueOnly,greenOnly,1); greenPredicted=(blueOnly*p(1))+p(2); greenAdjusted=greenOnly(1:l)-greenPredicted; case 2 % lowpass filter [B,A]=ellip(6,.1,50,[0.001 0.5]); greenOnly = lowpass(greenOnly,3,fs); blueOnly = lowpass(blueOnly,2,fs); p=polyfit(blueOnly,greenOnly,1); greenPredicted=(blueOnly*p(1))+p(2); greenPredicted(1:fs) = greenPredicted(fs+1); greenPredicted((end-fs):end) = greenPredicted(end-(fs+1)); greenOnly(1:fs) = greenOnly((fs+1)); greenOnly((end-fs):end) = greenOnly(end-(fs+1)); greenAdjusted=greenOnly-greenPredicted; case 3 Offset = 1; FP.calcium_dependent = greenOnly; FP.calcium_independent = blueOnly; %fit blue with an exp2 temp_fit = fit(blueTime(Offset:end),FP.calcium_independent(Offset:end,1),'exp2'); %scale fit to calcium dependent data fit2 = fitlm(temp_fit(blueTime(Offset:end)),FP.calcium_dependent(Offset:end)); fitted_BlueChannel = fit2.Fitted; ProcessedSignal(:,1) = greenTime(Offset:end); ProcessedSignal(:,2) = 100*(((FP.calcium_dependent(Offset:end) -(fit2.Fitted)) ./ (fit2.Fitted)) + abs(min(((FP.calcium_dependent(Offset:end) -(fit2.Fitted)) ./ (fit2.Fitted))))); greenAdjusted = smooth(ProcessedSignal(:,2)-mean(ProcessedSignal(:,2)), 'moving'); greenAdjusted = medfilt1(greenAdjusted); greenTime = ProcessedSignal(:,1); blueOnly = fitted_BlueChannel; case 4 % NPM code with red channel p=polyfit(redOnly,greenOnly,1); greenPredicted=(redOnly*p(1))+p(2); greenAdjusted=greenOnly(1:l)-greenPredicted; end