CODE_DATA transforms between input data and formatted spike times Description: Function to return an input spike time cell array or desired output spike time cell array depending on Type. The type of coding performed on the inputs is determined by the Coding structure and is user expandable. Syntax: CodedData=CODE_DATA(PutNeurons, Type, Puts, Coding); Input Parameters: o PutNeurons: Array of input or output neuron indices, depending on whether inputs or outputs are being coded. o Type: String with possible values 'Encode' or 'Decode' indicating whether Puts should be encoded into input spike times or decoded into desired output spike times. o Puts: System inputs or outputs before encoding into input spike times or before decoding from output spike times. For Spike coding, the format is a cell array, with each cell containing a input or output pattern. Each pattern is a qx2 array, where the first column ranges from 1 up to the number of input or output neurons, and the second column is the actual input or outputs. For linear coding, the format is n by m array where n is the number of input patterns and m is the number of input or output neurons o Coding: A structure containing fields specific for the type of encoding or decoding to be performed. All methods require EncodeMethod or DecodeMethod. Linear coding requires InputRange and InputSpikeRange or OutputRange and OutputSpikeRange. Output Parameters: o CodedData: Formatted input or desired output spike times ready for use in other functions, such as train. Examples: >> ThNN=theta_neuron_network; >> Coding.EncodeMethod='Spikes'; >> Coding.DecodeMethod='Spikes'; >> InputSpikeTimes=code_data(ThNN,'Encode',{[1 2], [1 4]},Coding) >> DesiredSpikeTimes=code_data(ThNN,'Decode',{[1 23], [1 20]},Coding) >> ThNN=theta_neuron_network('StructureFormat',{'LayerArray',[2 3 2]}); >> Coding=struct(... 'EncodeMethod','Linear',... 'DecodeMethod','Linear',... 'InputSpikeRange',[2 8],... 'OutputSpikeRange',[20 30],... 'InputRange',[0 1],... 'OutputRange',[0 1]); >> Inputs=rand(5,2); %5 patterns and 2 inputs (not including the ref input) >> InputSpikeTimes=code_data(ThNN,'Encode',Inputs,Coding) >> DesiredSpikeTimes=code_data(ThNN,'Decode',Inputs.^2,Coding) See also theta_neuron_network
0001 function CodedData=code_data(PutNeurons, Type, Puts, Coding) 0002 %CODE_DATA transforms between input data and formatted spike times 0003 % 0004 %Description: 0005 %Function to return an input spike time cell array or desired output spike 0006 %time cell array depending on Type. The type of coding performed on the 0007 %inputs is determined by the Coding structure and is user expandable. 0008 % 0009 %Syntax: 0010 %CodedData=CODE_DATA(PutNeurons, Type, Puts, Coding); 0011 % 0012 %Input Parameters: 0013 %o PutNeurons: Array of input or output neuron indices, depending on 0014 % whether inputs or outputs are being coded. 0015 %o Type: String with possible values 'Encode' or 'Decode' indicating 0016 % whether Puts should be encoded into input spike times or decoded into 0017 % desired output spike times. 0018 %o Puts: System inputs or outputs before encoding into input spike times or 0019 % before decoding from output spike times. For Spike coding, the format 0020 % is a cell array, with each cell containing a input or output pattern. 0021 % Each pattern is a qx2 array, where the first column ranges from 1 up 0022 % to the number of input or output neurons, and the second column is the 0023 % actual input or outputs. For linear coding, the format is n by m array 0024 % where n is the number of input patterns and m is the number of input 0025 % or output neurons 0026 %o Coding: A structure containing fields specific for the type of encoding 0027 % or decoding to be performed. All methods require EncodeMethod or 0028 % DecodeMethod. Linear coding requires InputRange and InputSpikeRange or 0029 % OutputRange and OutputSpikeRange. 0030 % 0031 %Output Parameters: 0032 %o CodedData: Formatted input or desired output spike times ready for use 0033 % in other functions, such as train. 0034 % 0035 %Examples: 0036 %>> ThNN=theta_neuron_network; 0037 %>> Coding.EncodeMethod='Spikes'; 0038 %>> Coding.DecodeMethod='Spikes'; 0039 %>> InputSpikeTimes=code_data(ThNN,'Encode',{[1 2], [1 4]},Coding) 0040 %>> DesiredSpikeTimes=code_data(ThNN,'Decode',{[1 23], [1 20]},Coding) 0041 % 0042 %>> ThNN=theta_neuron_network('StructureFormat',{'LayerArray',[2 3 2]}); 0043 %>> Coding=struct(... 0044 % 'EncodeMethod','Linear',... 0045 % 'DecodeMethod','Linear',... 0046 % 'InputSpikeRange',[2 8],... 0047 % 'OutputSpikeRange',[20 30],... 0048 % 'InputRange',[0 1],... 0049 % 'OutputRange',[0 1]); 0050 %>> Inputs=rand(5,2); %5 patterns and 2 inputs (not including the ref input) 0051 %>> InputSpikeTimes=code_data(ThNN,'Encode',Inputs,Coding) 0052 %>> DesiredSpikeTimes=code_data(ThNN,'Decode',Inputs.^2,Coding) 0053 % 0054 %See also theta_neuron_network 0055 0056 %Copyright (C) 2008 Sam McKennoch <Samuel.McKennoch@loria.fr> 0057 0058 0059 if strcmpi(Type,'EncodeInputs') 0060 Inputs=Puts; 0061 InputNeurons=PutNeurons;%get_input_neurons(ThNN); 0062 0063 switch lower(Coding.EncodeMethod) 0064 case ('spikes') 0065 CodedData=cell(1,length(Inputs)); 0066 for j=1:length(Inputs) %For multiple patterns 0067 for k=1:size(Inputs{j},1) %If there are multiple inputs 0068 CodedData{j}(k,:)=[InputNeurons(Inputs{j}(k,1)) Inputs{j}(k,2)]; 0069 end 0070 end 0071 0072 case ('linear') 0073 NormalInputs=(Inputs-Coding.InputRange(1))/(Coding.InputRange(2)-Coding.InputRange(1)); 0074 tiInputs=NormalInputs*(Coding.InputSpikeRange(2)-Coding.InputSpikeRange(1))+Coding.InputSpikeRange(1); 0075 0076 if length(InputNeurons)<size(tiInputs,2) 0077 disp('Error in code_date: Not enough input neurons'); 0078 CodedData=-1; 0079 return; 0080 end 0081 0082 CodedData=cell(1,size(tiInputs,2)); 0083 for j=1:size(tiInputs,1) %For multiple patterns 0084 for k=1:size(tiInputs,2) %If there are multiple inputs 0085 CodedData{j}(k,:)=[InputNeurons(k) tiInputs(j,k)]; 0086 end 0087 end 0088 0089 %Bad coding method 0090 otherwise 0091 disp('Error: Invalid Encoding Method in code_data function'); 0092 return; 0093 end 0094 0095 0096 elseif strcmpi(Type,'DecodeOutputs') 0097 Outputs=Puts; 0098 OutputNeurons=PutNeurons;%get_output_neurons(ThNN); 0099 0100 switch lower(Coding.DecodeMethod) 0101 case ('spikes') 0102 CodedData=cell(1,length(Outputs)); 0103 for j=1:length(Outputs) %For multiple patterns 0104 for k=1:size(Outputs{j},1) %If there are multiple inputs 0105 CodedData{j}(k,:)=[OutputNeurons(Outputs{j}(k,1)) Outputs{j}(k,2)]; 0106 end 0107 end 0108 0109 case ('linear') 0110 NormalOutputs=(Outputs-Coding.OutputRange(1))/(Coding.OutputRange(2)-Coding.OutputRange(1)); 0111 tdOutputs=NormalOutputs*(Coding.OutputSpikeRange(2)-Coding.OutputSpikeRange(1))+Coding.OutputSpikeRange(1); 0112 if length(OutputNeurons)<size(tdOutputs,2) 0113 disp('Error in code_date: Not enough output neurons'); 0114 CodedData=-1; 0115 return; 0116 end 0117 0118 CodedData=cell(1,length(tdOutputs)); 0119 for j=1:size(tdOutputs,1) %For multiple patterns 0120 for k=1:size(tdOutputs,2) %If there are multiple inputs 0121 CodedData{j}(k,:)=[OutputNeurons(k) tdOutputs(j,k)]; 0122 end 0123 end 0124 0125 %Bad coding method 0126 otherwise 0127 disp('Invalid Decoding Method in code_spike_times function!'); 0128 end 0129 0130 elseif strcmpi(Type,'EncodeOutputs') 0131 %Change output spikes into dataset outputs 0132 OutputSpikes=Puts; 0133 OutputNeurons=PutNeurons;%get_output_neurons(ThNN); 0134 0135 switch lower(Coding.DecodeMethod) 0136 case ('spikes') 0137 for j=1:length(OutputSpikes) %For multiple patterns 0138 for k=1:length(OutputNeurons) %If there are multiple outputs 0139 CodedData{j}(k,:)=[k OutputSpikes{j}(k,2)]; 0140 end 0141 end 0142 0143 case ('linear') %Assumes SISO 0144 for j=1:length(OutputSpikes) %For multiple patterns 0145 for k=1:length(OutputNeurons) %If there are multiple outputs 0146 Temp=(OutputSpikes{j}{OutputNeurons(k)}-Coding.OutputSpikeRange(1))/(Coding.OutputSpikeRange(2)-Coding.OutputSpikeRange(1)); 0147 CodedData(j,k)=Temp*(Coding.OutputRange(2)-Coding.OutputRange(1))+Coding.OutputRange(1); 0148 end 0149 end 0150 0151 %Bad coding method 0152 otherwise 0153 disp('Invalid Decoding Method in code_spike_times function!'); 0154 end 0155 elseif strcmpi(Type,'DecodeInputs') 0156 %Change input spikes into dataset inputs 0157 InputSpikes=Puts; 0158 InputNeurons=PutNeurons; 0159 switch lower(Coding.EncodeMethod) 0160 case ('spikes') 0161 CodedData=cell(1,length(InputSpikes)); 0162 for j=1:length(InputSpikes) %For multiple patterns 0163 for k=1:size(InputSpikes{j},1) %If there are multiple inputs 0164 CodedData{j}(k,:)=[k InputSpikes{j}(k,2)]; 0165 end 0166 end 0167 case ('linear') %Assumes SISO 0168 for j=1:length(InputSpikes) %For multiple patterns 0169 for k=1:size(InputSpikes{j},1) %If there are multiple inputs, use this instead of InputNeurons to avoid figuring out ReferenceTime here 0170 Temp=(InputSpikes{j}(k,2)-Coding.InputSpikeRange(1))/(Coding.InputSpikeRange(2)-Coding.InputSpikeRange(1)); 0171 CodedData(j,k)=Temp*(Coding.InputRange(2)-Coding.InputRange(1))+Coding.InputRange(1); 0172 end 0173 end 0174 0175 %Bad coding method 0176 otherwise 0177 disp('Invalid Decoding Method in code_spike_times function!'); 0178 end 0179 else 0180 disp('Error: Invalid Coding Type in code_data function'); 0181 SpikeTimes=-1; 0182 return; 0183 end