function TC-Ratio(2008)=PFC(tc1,tc2,tc3,tt1,tt2,tt3)
%--------------------------------------------------------------------------
%TC-Ratio(2008)
%Measures amplitude and latency of the N40/P50 (or N2-P2 wave) for the
%conditioning(C):testing(T)paradigm used in auditory-evoked response measurements of sensory gating.
%References: 
%Written by Margarita Zachariou 
%School of Mathematical Sciences & Biomedical Sciences, University of Nottingham
%Date last edited 16/01/2006 - minor revisions & Help Guide 03.11.2008 (RM)

%--------------------------------------------------------------------------
%Load perievent histogram data
load('C:\Program Files\MATLAB\R2007a\work\TC-Ratio(2008)\exampleDATA.mat')
%Remember to click on "SAVE" icon in tool bar afetr revising/completing line above

%In Command Window type the 6 time periods you wish to compute using notation, e.g. 'PFC(0,40,100,500,540,600)'
%This will compute values for the 6 time periods (in ms) defined within (  ), e.g 0, 40, 100ms...etc 


% to convert 0 2000 t0 -1000 1000 ms
tc1=tc1+1000;
tc2=tc2+1000;
tc3=tc3+1000;
tt1=tt1+1000;
tt2=tt2+1000;
tt3=tt3+1000;

% Conditioning
m11=min(P(tc1:tc2));
F11s=find(P(:)==m11);
L11s=F11s(find(F11s<=tc2 & F11s>=tc1));

M1=max(P(L11s:tc2));
F1e=find(P(:)==M1);
L1e=F1e(find(F1e<=tc2 & F1e>=L11s));

m12=min(P(L1e:tc3));
F12s=find(P(:)==m12);
L12s=F12s(find(F12s<=tc3 & F12s>=L1e));

Amp11=abs(M1-m11);
Amp12=abs(M1-m12);

% Testing
m21=min(P(tt1:tt2));
F21s=find(P(:)==m21);
L21s=F21s(find(F21s<=tt2 & F21s>=tt1));

M2=max(P(L21s:tt2));
F2e=find(P(:)==M2);
L2e=F2e(find(F2e<=tt2 & F2e>=L21s));

m22=min(P(L2e:tt3));
F22s=find(P(:)==m22);
L22s=F22s(find(F22s<=tt3 & F22s>=L2e));

Amp21=abs(M2-m21);
Amp22=abs(M2-m22);

TC1=Amp21/Amp11;
TC2=Amp22/Amp12;

size(P);

% disp('POSITIVE PEAK')
% disp('______________')
% disp('The amplitude of the N40 corresponding to the the conditioning (first) click is:')
% disp(Amp11)
% disp('The amplitude of the N40 corresponding to the the testing (second) click is:')
% disp(Amp21)
% disp('The latency of the beggining of the N40 corresponding to the conditioning (first) click is:')
% disp(L11s-1000)
% disp('The latency of the ending of the N40 corresponding to the conditioning (first) click is:')
% disp(L1e-1000)
% disp('The latency of the beggining of the N40 corresponding to testing (second) click is:')
% disp(L21s-1500)
% disp('The latency of the ending of the N40 corresponding to testing (second) click is:')
% disp(L2e-1500)
% disp('The T/C ratio is:')
% disp(TC1)
% disp('--------------------------------------------------------------------------------------------')
% disp('NEGATIVE PEAK')
% disp('______________')
% disp('The amplitude of the N40 corresponding to the the conditioning (first) click is:')
% disp(Amp12)
% disp('The amplitude of the N40 corresponding to the the testing (second) click is:')
% disp(Amp22)
% disp('The latency of the beggining of the N40 corresponding to the conditioning (first) click is:')
% disp(L1e-1000)
% disp('The latency of the ending of the N40 corresponding to the conditioning (first) click is:')
% disp(L12s-1000)
% disp('The latency of the beggining of the N40 corresponding to testing (second) click is:')
% disp(L2e-1500)
% disp('The latency of the ending of the N40 corresponding to testing (second) click is:')
% disp(L22s-1500)
% disp('The T/C ratio is:')
% disp(TC2)
% disp('--------------------------------------------------------------------------------------------')

disp('POSITIVE PEAK')
disp('_____________')
disp('AMP1      AMP2      LStart1   LEnd1     LStart2   LEnd2     TCratio')
fprintf('%-10.4f%-10.4f%-10.0f%-10.0f%-10.0f%-10.0f%-10.4f\n',Amp11,Amp21,L11s-1000,L1e-1000,L21s-1500,L2e-1500,TC1)
disp('--------------------------------------------------------------------------------------------')
disp('NEGATIVE PEAK')
disp('_____________')
disp('AMP1      AMP2      LStart1   LEnd1     LStart2   LEnd2     TCratio')
disp('___________________________________________________________________')
fprintf('%-10.4f%-10.4f%-10.0f%-10.0f%-10.0f%-10.0f%-10.4f\n',Amp12,Amp22,L1e-1000,L12s-1000,L2e-1500,L22s-1500,TC2)

figure(1)
L11s=L11s-1000;
L12s=L12s-1000;
L1e=L1e-1000;
L21s=L21s-1000;
L22s=L22s-1000;
L2e=L2e-1000;


plot(-1000:size(P)-1001,P)

hold on
plot(L11s,m11,'r*')
hold on
plot(L1e,M1,'r*')
hold on
plot(L12s,m12,'r*')
hold on
plot(L2e,M2,'r*')
hold on
plot(0,0,'go')
hold on
plot(500,0,'go')
hold on 
plot(L21s,m21,'r*')
hold on 
plot(L22s,m22,'r*')
