image thumbnail

CalculatePSTH(Spike​Times,start,varargi​n)

version 1.0.0.0 (8.88 KB) by Jordan Sorokin
calculates peri-stimulus time histogram (PSTH) + variance of neural spike times

351 Downloads

Updated 19 Feb 2016

View License

% ---------[psth] = CalculatePSTH(SpikeTimes,EventTimes,varargin)-------------
%
% Calculates peri-stimulus time histograms (PSTHs) from a matrix or cell
% array of spiketimes (in seconds), given a user-defined bin-width. Plots
% PSTHs and saves in current directory.
%
% >>> INPUTS >>>
% Required:
% SpikeTimes = matrix or cell of spiketimes in SECONDS
% If matrix, will assume columns are trials, rows are spiketimes.
% If cell, will assume each cell is a trial, and in each cell
% columns are channels, rows are spiketimes.
% start = n-element vector containing times of events (in seconds)...if
% spiketimes are relative (i.e. blocks of spikeitmes, each block relative
% to stim onset), then user should define "start" as the time of the
% onset of the stim relative to the start of the block. For instance, if
% stim is 6 seconds into the start of each block, then set "start" = 6.
% * if spikes NOT relative, start = nx1 array of starting times.
% Optional:
% pre_time = time (in SECONDS) to subtract from starting time...
% makes plots relative to stim onset (default = 1s);
% post_time = time (in SECONDS) to add to starting time...
% (default = 1s);
% bin_width = bin (ms) for PSTH calculation. Default = 10ms.
% name = name to save figure (default = "psth.pdf")
% saving = 0 or 1 (default 1). If 1, saves figures to current directory.
%
% <<< OUTPUTS <<<
% psth = bin-counts of spiketimes occuring within specified time range.
% If SpikeTimes is a cell array, psth is an nxtrialsxchan matrix.
% If SpikeTimes is a matrix, psth is an nxtrials matrix
% psthTrialAvg = average of psth across trials per channel
% varTrialAvg = average variance of psth across trials per channel
%
% Example:
% SpikeTimes{1} = sort(rand(100)); % fake spiketimes for ch1
% SpikeTimes{2} = sort(rand(100)); % fake spiketimes for ch2
% [psth,trialAvg,varAvg] = CalculatePSTH(SpikeTimes,.5,.2,.5,10,'control',1)
% % plots histogram and variances for each channel, from -0.2s : 0.5s
% % around the starting point, (here 0.5s into the SpikeTimes). Save
% % the figures and appends "control" to the figure name
%
% By JMS, 11/13/2015
%-------------------------------------------------------

Cite As

Jordan Sorokin (2021). CalculatePSTH(SpikeTimes,start,varargin) (https://www.mathworks.com/matlabcentral/fileexchange/55460-calculatepsth-spiketimes-start-varargin), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2013a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!