function f = constructstimulusmatrices(m,prenumlag,postnumlag,wantwrap) <m> is a 2D matrix, each row of which is a stimulus sequence (i.e. a vector that is all zeros except for ones indicating the onset of a given stimulus (fractional values are also okay)) <prenumlag> is the number of stimulus points in the past <postnumlag> is the number of stimulus points in the future <wantwrap> (optional) is whether to wrap around. default: 0. return a stimulus matrix of dimensions size(m,2) x ((prenumlag+postnumlag+1)*size(m,1)). this is a horizontal concatenation of the stimulus matrix for the first stimulus sequence, the stimulus matrix for the second stimulus sequence, and so on. this function is useful for fitting finite impulse response (FIR) models. history: 2013/05/12 - update doc to indicate fractional values are okay. example: imagesc(constructstimulusmatrices([0 1 0 0 0 0 0 0 0; 0 0 1 0 0 0 0 0 0],0,3));
0001 function f = constructstimulusmatrices(m,prenumlag,postnumlag,wantwrap) 0002 0003 % function f = constructstimulusmatrices(m,prenumlag,postnumlag,wantwrap) 0004 % 0005 % <m> is a 2D matrix, each row of which is a stimulus sequence (i.e. 0006 % a vector that is all zeros except for ones indicating the onset 0007 % of a given stimulus (fractional values are also okay)) 0008 % <prenumlag> is the number of stimulus points in the past 0009 % <postnumlag> is the number of stimulus points in the future 0010 % <wantwrap> (optional) is whether to wrap around. default: 0. 0011 % 0012 % return a stimulus matrix of dimensions 0013 % size(m,2) x ((prenumlag+postnumlag+1)*size(m,1)). 0014 % this is a horizontal concatenation of the stimulus 0015 % matrix for the first stimulus sequence, the stimulus 0016 % matrix for the second stimulus sequence, and so on. 0017 % this function is useful for fitting finite impulse response (FIR) models. 0018 % 0019 % history: 0020 % 2013/05/12 - update doc to indicate fractional values are okay. 0021 % 0022 % example: 0023 % imagesc(constructstimulusmatrices([0 1 0 0 0 0 0 0 0; 0 0 1 0 0 0 0 0 0],0,3)); 0024 0025 % input 0026 if ~exist('wantwrap','var') || isempty(wantwrap) 0027 wantwrap = 0; 0028 end 0029 0030 % get out early 0031 if prenumlag==0 && postnumlag==0 0032 f = m'; 0033 return; 0034 end 0035 0036 % do it 0037 num = prenumlag + postnumlag + 1; 0038 f = zeros([size(m,2) num*size(m,1)]); 0039 for p=1:size(m,1) 0040 f(:,(p-1)*num+(1:num)) = constructstimulusmatrix(m(p,:),prenumlag,postnumlag,wantwrap); 0041 end 0042 0043 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% HELPER FUNCTION 0044 0045 function f = constructstimulusmatrix(v,prenumlag,postnumlag,wantwrap) 0046 0047 % function f = constructstimulusmatrix(v,prenumlag,postnumlag,wantwrap) 0048 % 0049 % <v> is the stimulus sequence represented as a vector 0050 % <prenumlag> is the number of stimulus points in the past 0051 % <postnumlag> is the number of stimulus points in the future 0052 % <wantwrap> (optional) is whether to wrap around. default: 0. 0053 % 0054 % return a stimulus matrix of dimensions 0055 % length(v) x (prenumlag+postnumlag+1) 0056 % where each column represents the stimulus at 0057 % a particular time lag. 0058 0059 % input 0060 if ~exist('wantwrap','var') || isempty(wantwrap) 0061 wantwrap = 0; 0062 end 0063 0064 % do it 0065 total = prenumlag + postnumlag + 1; 0066 f = zeros([length(v) total]); 0067 for p=1:total 0068 if wantwrap 0069 f(:,p) = circshift(v,[0 -prenumlag+(p-1)]).'; 0070 else 0071 temp = -prenumlag+(p-1); 0072 if temp < 0 0073 f(1:end+temp,p) = v(1-temp:end); 0074 else 0075 f(temp+1:end,p) = v(1:end-temp); 0076 end 0077 end 0078 end