function a = cochleagram(r, winLength,winShift)
% Generate a cochleagram from responses of a Gammatone filterbank.
% It gives the log energy of T-F units
% The first variable is required.
% winLength: window (frame) length in samples
% Written by ZZ Jin, and adapted by DLW in Jan'07

if nargin < 2
    winLength = 256;      % default window length in sample points which is 20 ms for 16 KHz sampling frequency
    %winShift =0.5*winLength;%0.5*winLength;            % frame shift (default is half frame)
end
if nargin <3  
    winShift =0.5*winLength;%0.5*winLength;            % frame shift (default is half frame)
end
[numChan,sigLength] = size(r);     % number of channels and input signal length

M = floor((sigLength-winLength+winShift)/winShift);     % number of time frames
% calculate energy for each frame in each channel
a = zeros(numChan,M);
for m = 1:M      
    for i = 1:numChan
        startpoint = (m-1)*winShift;
        a(i,m) = r(i,startpoint+1:startpoint+winLength)*r(i,startpoint+1:startpoint+winLength)';
    end
end
end
