% COMPUTE LATENCY function written by Demetrio Ferro <demetrio.ferro@upf.edu>
% Based on (Roelfsema, Tolboom, Khayat, Neuron 2007).
% developed for (Ferro, van Kempen, Boyd, Panzeri, Thiele, PNAS 2021)
% The function computes Latency indices base don MUAes fitted by cumulative
% gaussian function with 5 (degrees of freedom) parameters (mu, sigma, alpha, c, d).
% The best fit is assigned based on Least Squares (LS) of iterative estimation.
% Inputs:
% MuaeData [NxT] Multi-channel (N channels) MUAes in time (T points)
% MuaeTimes [1xT] Timestamps of MUAe recordings (T points)
% lsqTimeRes [1x1] Resolution of time series for Latency estimation
% Outputs:
% latTimes [1xN] Latency indices at t*:={f(t*)=1/3*max(f(t)),t in MuaeTimes}
% lsqCurveTimes [1x(T*lsqTimeRes)] Timestamps of fitted curves
% lsqFittedCurve [Nx(T*lsqTimeRes)] Fitted curves at LS parameters
% lsqFittedParam [Nx5] LS parameters
% lsqFitErr [1xN] LS error of the fit
(correspondance contact might not be updated)