0001 function frequency = Frequency(timestamps,varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043 method = 'fixed';
0044 binSize = 0.05;
0045 smooth = 2;
0046 limits = [];
0047 show = 'off';
0048
0049
0050 if nargin < 1,
0051 error('Incorrect number of parameters (type ''help <a href="matlab:help Frequency">Frequency</a>'' for details).');
0052 end
0053
0054
0055 if size(timestamps,2) ~= 1,
0056 error('Parameter ''timestamps'' is not a vector (type ''help <a href="matlab:help Frequency">Frequency</a>'' for details).');
0057 end
0058
0059
0060 for i = 1:2:length(varargin),
0061 if ~ischar(varargin{i}),
0062 error(['Parameter ' num2str(i+2) ' is not a property (type ''help <a href="matlab:help Frequency">Frequency</a>'' for details).']);
0063 end
0064 switch(lower(varargin{i})),
0065
0066 case 'method',
0067 method = lower(varargin{i+1});
0068 if ~isastring(method,'fixed','adaptive','inverse','iisi'),
0069 error('Incorrect value for property ''method'' (type ''help <a href="matlab:help Frequency">Frequency</a>'' for details).');
0070 end
0071
0072 case 'binsize',
0073 binSize = varargin{i+1};
0074 if ~isdscalar(binSize,'>=0'),
0075 error('Incorrect value for property ''binSize'' (type ''help <a href="matlab:help Frequency">Frequency</a>'' for details).');
0076 end
0077
0078 case 'limits',
0079 limits = varargin{i+1};
0080 if ~isdvector(limits,'#2'),
0081 error('Incorrect value for property ''limits'' (type ''help <a href="matlab:help Frequency">Frequency</a>'' for details).');
0082 end
0083
0084 case 'smooth',
0085 smooth = varargin{i+1};
0086 if ~isdscalar(smooth,'>=0'),
0087 error('Incorrect value for property ''smooth'' (type ''help <a href="matlab:help Frequency">Frequency</a>'' for details).');
0088 end
0089
0090 case 'show',
0091 show = varargin{i+1};
0092 if ~isastring(show,'on','off'),
0093 error('Incorrect value for property ''show'' (type ''help <a href="matlab:help Frequency">Frequency</a>'' for details).');
0094 end
0095
0096 otherwise,
0097 error(['Unknown property ''' num2str(varargin{i}) ''' (type ''help <a href="matlab:help Frequency">Frequency</a>'' for details).']);
0098
0099 end
0100 end
0101
0102 if isempty(limits),
0103 limits = [timestamps(1)-10*binSize timestamps(end)+10*binSize];
0104 end
0105
0106 if strcmp(method,'inverse') | strcmp(method,'iisi'),
0107
0108 ds = diff(timestamps);
0109 f1 = 1./ds;
0110 t = timestamps(1:end-1) + ds/2;
0111 f2 = Interpolate([t f1],timestamps(2:end-1));
0112 f2 = [timestamps(1) 0;f2;timestamps(end) 0];
0113 frequency = f2;
0114 else
0115
0116
0117 t = (limits(1):binSize:limits(2))';
0118 T = Bin(timestamps,t);
0119 binned = Accumulate(T,1,size(t));
0120 f = Smooth(binned/binSize,smooth);
0121 frequency = [t f];
0122 if strcmp(method,'adaptive'),
0123
0124
0125 N = length(t);
0126 mu = exp(1/N*sum(log(f(f~=0))));
0127 lambda = sqrt(f/mu);
0128 sigma = (smooth*binSize)./lambda;
0129 sigma = Clip(sigma,0,N*binSize/3);
0130
0131 binned = [flipud(binned);binned;flipud(binned)];
0132 for i = 1:N,
0133
0134 x = (0:binSize:3*sigma(i))';
0135 x = [flipud(-x);x(2:end)];
0136 kernel = exp(-x.^2/sigma(i)^2);
0137 kernel = kernel/sum(kernel);
0138
0139 n = (length(kernel)-1)/2;
0140 bins = N + i + (-n:n);
0141 S2(i) = sum(binned(bins).*kernel)/binSize;
0142 end
0143 frequency = [t S2'];
0144 end
0145 end
0146
0147 if strcmp(lower(show),'on'),
0148 PlotXY(frequency);
0149 hold on;
0150 PlotTicks(timestamps,'size',10,'k');
0151 end