function  [dpr,sigma_ratio] = raw_dprime(cache,conditions)

%RAW_DPRIME -- D-prime over noise-free PLM stimulus representations.
%
%  dpr = raw_dprime(cache)
%  dpr = raw_dprime(cache,conditions)
%  [dpr,sigma_ratio] = raw_dprime(...)
%
%  CACHE is a structure array produced by CACHE_INPUTS.
%  CONDITIONS is a cell array of CACHE indices. It must have two columns
%  ("left" and "right") and N rows (or "backgrounds"). One d-prime value
%  is calculated for each of the N backgrounds and for each representation
%  unit separately.  Thus, the output matrix DPR has N rows and R columns,
%  where R is the length of each representation CACHE(_).rep(_,:).
%  Default CONDITIONS = {[1 3 5],[2 4 6];[7 9 11],[8 10 12]}.
%
%  The variance of the "left" and "right" conditions can be unequal.
%  The R/L ratio is reported in the optional second output SIGMA_RATIO.
%  D-primes are defined by  DPR=(mean(R)-mean(L))./((std(R)+std(L))/2).
%
%  See also CACHE_INPUTS, PLFRONTEND, AGGREG_CACHE.

%  Original coding by Alexander Petrov, UC Irvine.
%  $Revision: 1.0 $  $Date: 2002/07/25 19:07 $

if (nargin<2)
   conditions = {[1 3 5],[2 4 6] ; [7 9 11],[8 10 12]} ;
end

N_back = size(conditions,1) ;   % number of "backgrounds"
N_rep = size(cache(1).rep,2) ;  % number of values in each representation

dpr = zeros(N_back,N_rep) ;
sigma_ratio = zeros(N_back,N_rep) ;

for bk = 1:N_back
   L = cat(1,cache(conditions{bk,1}).rep) ;
   R = cat(1,cache(conditions{bk,2}).rep) ;
   stdL = std(L,1) ;
   stdR = std(R,1) ;
   sigma_ratio(bk,:) = stdR ./ stdL ;
   dpr(bk,:) = (mean(R)-mean(L)) ./ ((stdR+stdL)./2) ;
end

%--- Return DPR
%%%%% End of file RAW_DPRIME.M
