# binspikes

## PURPOSE

bin spikes at a specified frequency sampling i.e. sampling rate 1/sampling

## SYNOPSIS

function [dN,t]=binspikes(data,Fs,t)

## DESCRIPTION

     bin spikes at a specified frequency sampling i.e. sampling rate 1/sampling
     eg: 1ms accuracy use sampling = 1000
     Usage: [dN,t]=binspikes(data,Fs,t)
     Inputs:
     data   (data as a structure array of spike times; or as a single
            vector of spike times)
     Fs     (binning frequency)
     t      (the minimum and maximum times to be used to form the bins - [mint maxt]
                - optional. Default use the spike times themselves to
                  determine the location of the bins.
     Note: the times in data can be in any units. However, it is important
     that all units are chosen consistently. So, if spike times are in secs,
     Fs and t (if present) have to be in Hz and secs respectively. If spike
     times are in number of samples, Fs has to be 1, and t has to be in number
     of samples.
     Outputs:
     dN     (output binned spike counts as a matrix defined on bins starting with the
             earliest spike across all channels and ending with the latest spike)
     t      (lower limit of each bin)

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

- testscript function testscript(pname,direction,movingwin,segave,params,fscorr)

## SOURCE CODE

    0001 function [dN,t]=binspikes(data,Fs,t)
    0002 % bin spikes at a specified frequency sampling i.e. sampling rate 1/sampling
    0003 % eg: 1ms accuracy use sampling = 1000
    0004 % Usage: [dN,t]=binspikes(data,Fs,t)
    0005 % Inputs:
    0006 % data   (data as a structure array of spike times; or as a single
    0007 %        vector of spike times)
    0008 % Fs     (binning frequency)
    0009 % t      (the minimum and maximum times to be used to form the bins - [mint maxt]
    0010 %            - optional. Default use the spike times themselves to
    0011 %              determine the location of the bins.
    0012 % Note: the times in data can be in any units. However, it is important
    0013 % that all units are chosen consistently. So, if spike times are in secs,
    0014 % Fs and t (if present) have to be in Hz and secs respectively. If spike
    0015 % times are in number of samples, Fs has to be 1, and t has to be in number
    0016 % of samples.
    0017 % Outputs:
    0018 % dN     (output binned spike counts as a matrix defined on bins starting with the
    0019 %         earliest spike across all channels and ending with the latest spike)
    0020 % t      (lower limit of each bin)
    0021 if nargin < 2; error('Need at least two input arguments'); end;
    0022 dt=1/Fs;
    0023 dtmp='';
    0024 if isstruct(data);
    0025    C=length(data);
    0026    fnames=fieldnames(data);
    0027    if nargin <3 || isempty(t);
    0028        mintime=zeros(1,C);
    0029        maxtime=zeros(1,C);
    0030        for ch=1:C
    0031          eval(['dtmp=data(ch).' fnames{1} ';'])
    0032          mintime(ch)=min(dtmp);
    0033          maxtime(ch)=max(dtmp);
    0034        end
    0035        mintime=min(mintime);
    0036        maxtime=max(maxtime);
    0037    else
    0038 %        maxtimech=zeros(1,C);
    0039 %        for ch=1:C
    0040 %          eval(['dtmp=data(ch).' fnames{1} ';'])
    0041 % %          mintimech(ch)=min(dtmp);
    0042 %          maxtimech(ch)=max(dtmp);
    0043 %        end
    0044        mintime=t(1);
    0045        maxtime=t(end);
    0046 %        mintimech=min(mintimech);
    0047 %        maxtimech=max(maxtimech);
    0048 %        if maxtimech > max(t); t=[t maxtimech+dt]; end;
    0049    end
    0050    t=linspace(mintime,maxtime,1+(maxtime-mintime)/dt);
    0051    for ch=1:C;
    0052        eval(['dtmp=data(ch).' fnames{1} ';'])
    0053        x=histc(dtmp,t);
    0054        dN(:,ch)=x(:);
    0055    end
    0056 else
    0057    dtmp=data;
    0058    if nargin < 3;
    0059       mintime=min(dtmp);
    0060       maxtime=max(dtmp);
    0061    else
    0062       mintime=t(1);
    0063       maxtime=t(end);
    0064    end
    0065    t=linspace(mintime,maxtime,1+(maxtime-mintime)/dt);
    0066    if max(dtmp)>max(t); t=[t maxtime+dt]; end;
    0067    x=histc(dtmp,t);
    0068    dN=x(:);
    0069 end