% This is the main function of my graphical user interface program % % % % Get some parameters from the user's screen set(h,'Pointer','watch'); datafilesnum = str2num(get(filenum2,'String')); %get parameters from screen datapatho = get(filepatho2,'String'); datapathc = get(filepathc2,'String'); datafiles = get(filedata2,'String'); if strcmp(get(o3000,'Checked'),'on'), NPTS = 3000; else NPTS = 6000; end LAGMAX = 1000; %This is the number of data points in the resulting df files (ie 10sec worth) X = zeros(1,LAGMAX +2); Y = zeros(1,LAGMAX +2); data1 = strtok(datafiles); for i=1:datafilesnum, %Loop through all datafiles: load, calculate correlations, average all trials data = strtok(datafiles); load([datapatho,data,'.cp']) datafiles = strrep(datafiles,strtok(datafiles),' '); temp = eval(data); [a b] = size(temp); if a ~= NPTS, error('Your files do not contain the data points specified'); end tempX = temp(1:NPTS,1); tempY = temp(1:NPTS,2); tempX = tempX'; tempY = tempY'; tempX = tempX - mean(tempX); tempY = tempY - mean(tempY); % corrX = acf(tempX,NPTS,LAGMAX); %This acf function is a c program written with the help of Michael Lauk % corrY = acf(tempY,NPTS,LAGMAX); %This c function does not run as fast anymore and has been commented from this program corrX = xcorr(tempX,tempX,'biased'); fix = corrX; %calculate correlations corrX = corrX(NPTS:NPTS+LAGMAX +1); %take last half of symmetric corrX corrX = 2*var(tempX) - 2.*corrX; corrY = xcorr(tempY,tempY,'biased'); %Repeat for Y corrY = corrY(NPTS:NPTS+LAGMAX+1); corrY = 2*var(tempY) - 2.*corrY; % corrX = corrX(LAGMAX + 1:2*LAGMAX +2); %Only needed if acf c program used % corrY = corrY(LAGMAX + 1:2*LAGMAX +2); % corrX = 2*var(tempX) - 2.*corrX; % corrY = 2*var(tempY) - 2.*corrY; X = X + corrX; %Add all correlated files Y = Y + corrY; if i == 5, fprintf('\n\nAre we having fun yet????\n'); end end X = X./datafilesnum; %average all files Y = Y./datafilesnum; R = X + Y; %save .df data fprintf('\nWriting data to file: %s\n',[data1,'.df']); fid = fopen([datapathc,data1,'.df'],'wt'); Z = [X; Y]; fprintf(fid,'%f %f\n',Z); fclose(fid); %find fit times filt = firls(40,[0 1],[1 1],'differentiator'); %find coefficients for filter and filter data twice for second derivativ derdata = filter(filt,1, X); der2data = filter(filt,1,derdata); [fitTimeX,critpointX] = findtime(der2data); %Use my findtime.m function to find the critical points and times used to fit the regression lines derdata = filter(filt,1, Y); %Repeat for Y der2data = filter(filt,1,derdata); [fitTimeY, critpointY] = findtime(der2data); derdata = filter(filt,1, R); %Repeat for R der2data = filter(filt,1,derdata); [fitTimeR, critpointR] = findtime(der2data); %fit linear lines tau = 0.01:.01:(LAGMAX +2)*.01; tau1Xplot = fitTimeX(1):.01:fitTimeX(2)+1.2; %These extra variables are to plot the lines longer than the times used to fit the curves tau2Xplot = fitTimeX(3)-1.2:.01:fitTimeX(4); fitcoeffx1 = polyfit(fitTimeX(1):.01:fitTimeX(2),X(fitTimeX(1)*100:fitTimeX(2)*100),1); linex1 = polyval(fitcoeffx1,tau1Xplot); fitcoeffx2 = polyfit(fitTimeX(3):.01:fitTimeX(4),X(fitTimeX(3)*100:fitTimeX(4)*100),1); linex2 = polyval(fitcoeffx2,tau2Xplot); tau1Yplot = fitTimeY(1):.01:fitTimeY(2)+1.2; tau2Yplot = fitTimeY(3)-1.2:.01:fitTimeY(4); fitcoeffy1 = polyfit(fitTimeY(1):.01:fitTimeY(2),Y(fitTimeY(1)*100:fitTimeY(2)*100),1); liney1 = polyval(fitcoeffy1,tau1Yplot); fitcoeffy2 = polyfit(fitTimeY(3):.01:fitTimeY(4),Y(fitTimeY(3)*100:fitTimeY(4)*100),1); liney2 = polyval(fitcoeffy2,tau2Yplot); tau1Rplot = fitTimeR(1):.01:fitTimeR(2)+1.2; tau2Rplot = fitTimeR(3)-1.2:.01:fitTimeR(4); fitcoeffr1 = polyfit(fitTimeR(1):.01:fitTimeR(2),R(fitTimeR(1)*100:fitTimeR(2)*100),1); liner1 = polyval(fitcoeffr1,tau1Rplot); fitcoeffr2 = polyfit(fitTimeR(3):.01:fitTimeR(4),R(fitTimeR(3)*100:fitTimeR(4)*100),1); liner2 = polyval(fitcoeffr2,tau2Rplot); %fit log-log lines X(1) = X(1) + .00001; Y(1) = Y(1) + .00001; R(1) = R(1) + .00001; logX = log10(X); logY = log10(Y); logR = log10(R); fitcoeffx1log = polyfit(log10(fitTimeX(1):.01:fitTimeX(2)),logX(fitTimeX(1)*100:fitTimeX(2)*100),1); linex1log = (tau1Xplot.^fitcoeffx1log(1))*10^fitcoeffx1log(2); fitcoeffx2log = polyfit(log10(fitTimeX(3):.01:fitTimeX(4)),logX(fitTimeX(3)*100:fitTimeX(4)*100),1); linex2log = (tau2Xplot.^fitcoeffx2log(1))*10^fitcoeffx2log(2); fitcoeffy1log = polyfit(log10(fitTimeY(1):.01:fitTimeY(2)),logY(fitTimeY(1)*100:fitTimeY(2)*100),1); liney1log = (tau1Yplot.^fitcoeffy1log(1))*10^fitcoeffy1log(2); fitcoeffy2log = polyfit(log10(fitTimeY(3):.01:fitTimeY(4)),logY(fitTimeY(3)*100:fitTimeY(4)*100),1); liney2log = (tau2Yplot.^fitcoeffy2log(1))*10^fitcoeffy2log(2); fitcoeffr1log = polyfit(log10(fitTimeR(1):.01:fitTimeR(2)),logR(fitTimeR(1)*100:fitTimeR(2)*100),1); liner1log = (tau1Rplot.^fitcoeffr1log(1))*10^fitcoeffr1log(2); fitcoeffr2log = polyfit(log10(fitTimeR(3):.01:fitTimeR(4)),logR(fitTimeR(3)*100:fitTimeR(4)*100),1); liner2log = (tau2Rplot.^fitcoeffr2log(1))*10^fitcoeffr2log(2); figure; plotgr; %This plotgr.m script file graphs what is specified by the user axis([0 10 0 max(R)+10]); sparam; %This sparam.m script file prints the numerical data if requested %Save parameters in file with .dfp extension (for df parameters) fprintf('\nWriting parameters to file: %s\n',[data1,'.dfp']); fid = fopen([datapathc,data1,'.dfp'],'wt'); param = [critpointX*.01; X(critpointX); fitcoeffx1(1)/2; fitcoeffx2(1)/2; fitcoeffx1log(1)/2; fitcoeffx2log(1)/2; critpointY*.01; Y(critpointY); fitcoeffy1(1)/2; fitcoeffy2(1)/2; fitcoeffy1log(1)/2; fitcoeffy2log(1)/2; critpointR*.01; R(critpointR); fitcoeffr1(1)/2; fitcoeffr2(1)/2; fitcoeffr1log(1)/2; fitcoeffr2log(1)/2]; fprintf(fid,'%f\n',param); fclose(fid); set(h,'Pointer','arrow'); %This .dfp file is saved as follows: X critical point lag time % The value at this lag time % The slope/2 for the short term linear X % The slope/2 for the long term linear X % The slope/2 for the short term log X % The slope/2 for the long term log X % This pattern repeats for the Y values and finally for the R values where each % parameter is on a separate line. (total of 18 parameters)!