function [eta, taxis] = event_trig_ave(sig, trigtimes, pretime, posttime, SR) %outputs: eta is the event-triggered average, taxis is the time axis %inputs: sig is the LFP signal, trigtimes are the timestamps of spikes, % pretime is how much prior to the spike time to average, % posttime is how much after the spike time to average, SR is % the sampling rate of the LFP signal. trigtimes(trigtimeslength(sig)/SR - posttime) = []; trialsigs = nan(length(trigtimes), length(round(-pretime*SR):round(posttime*SR))); for n = 1:length(trigtimes) trialsigs(n,:) = sig(round((trigtimes(n)-pretime)*SR) : round((trigtimes(n)-pretime)*SR) + size(trialsigs,2) -1); end eta = mean(trialsigs,1); taxis = linspace(-pretime, posttime, size(trialsigs, 2));