annotate aim-mat/tools/@signal/sim_spikes.asv @ 4:537f939baef0 tip

various bug fixes and changed copyright message
author Stefan Bleeck <bleeck@gmail.com>
date Tue, 16 Aug 2011 14:37:17 +0100
parents 74dedb26614d
children
rev   line source
tomwalters@0 1 function ret_spikes=sim_spikes(sig,nr_sweeps,params)
tomwalters@0 2 % simulates the response to a signal
tomwalters@0 3
tomwalters@0 4 % % other parameters
tomwalters@0 5 if isfield(params,'latency')
tomwalters@0 6 latency=params.latency;
tomwalters@0 7 else
tomwalters@0 8 latency=4;
tomwalters@0 9 end
tomwalters@0 10 if isfield(params,'first_spike_boost')
tomwalters@0 11 first_spike_boost=params.first_spike_boost;
tomwalters@0 12 else
tomwalters@0 13 first_spike_boost=2;
tomwalters@0 14 end
tomwalters@0 15 if isfield(params,'threshold')
tomwalters@0 16 threshold=params.threshold;
tomwalters@0 17 else
tomwalters@0 18 threshold=0; % in dB
tomwalters@0 19 end
tomwalters@0 20 if isfield(params,'dynamic_range')
tomwalters@0 21 dynamic_range=params.dynamic_range;
tomwalters@0 22 else
tomwalters@0 23 dynamic_range=20; % in dB
tomwalters@0 24 end
tomwalters@0 25 % if isfield(params,'start_boost_beta_length')
tomwalters@0 26 % start_boost_beta_length=params.start_boost_beta_length;
tomwalters@0 27 % else
tomwalters@0 28 % start_boost_beta_length=15;
tomwalters@0 29 % end
tomwalters@0 30 % if isfield(params,'start_boost_beta_val')
tomwalters@0 31 % start_boost_beta_val=params.start_boost_beta_val;
tomwalters@0 32 % else
tomwalters@0 33 % start_boost_beta_val=2;
tomwalters@0 34 % end
tomwalters@0 35 if isfield(params,'jitter_time')
tomwalters@0 36 jitter_time=params.jitter_time;
tomwalters@0 37 else
tomwalters@0 38 jitter_time=0.1;
tomwalters@0 39 end
tomwalters@0 40
tomwalters@0 41 % calcualates an artificail spiketrain with so many sweeps
tomwalters@0 42 % the important parameter are mu (the location) and beta (the scale) of the
tomwalters@0 43 % distribution
tomwalters@0 44
tomwalters@0 45 % changing some parameters so that the result looks good
tomwalters@0 46 %jitter to reduce some effect that the odd (or even?) multiples
tomwalters@0 47 %of the sr have significant and repeatable higher nr intervals
tomwalters@0 48 %then the others. Cant work out why. solution: let the interval
tomwalters@0 49 %jitter around the peak a bit
tomwalters@0 50
tomwalters@0 51 % calculate the x values of the distribution
tomwalters@0 52 % take a length of the distribution up to the point where it it 99.9 %
tomwalters@0 53 % that saves a lot of time
tomwalters@0 54 % maxpoint=0.999;
tomwalters@0 55 % tlength=mu-beta*log(-log(maxpoint));
tomwalters@0 56
tomwalters@0 57 % set the random number generator to a new value
tomwalters@0 58 % rand('state', sum(100*clock));
tomwalters@0 59 % dont do that because the values are too close to each other and actually
tomwalters@0 60 % not random at all!
tomwalters@0 61
tomwalters@0 62 binwidth=1/getsr(sig)*1000;
tomwalters@0 63 sig_len=getlength(sig);
tomwalters@0 64 nr_steps=getnrpoints(sig);
tomwalters@0 65
tomwalters@0 66
tomwalters@0 67 % % x2=binwidth:binwidth:tlength;
tomwalters@0 68 %
tomwalters@0 69 % % spike_prob_function=hazard_function(x2,mu,beta); % for the full signal length
tomwalters@0 70 % % spike_prob_function2=hazard_function(x2,mu,beta/start_boost_beta_val); % for the first 20 ms
tomwalters@0 71 %
tomwalters@0 72 % test_sig=getvalues(sig);
tomwalters@0 73 % test_sig=test_sig+60; % shift it upwards
tomwalters@0 74 %
tomwalters@0 75 %
tomwalters@0 76 % for i=1:nr_sweeps
tomwalters@0 77 % spikes=[];
tomwalters@0 78 % last_spike=-1; % inital condition: how long ago was the last spike
tomwalters@0 79 % spikecounter=1; % spikecounter
tomwalters@0 80 % for j=1:nr_steps
tomwalters@0 81 % time_now=j*binwidth; % thats the global time counter
tomwalters@0 82 % difft=time_now-last_spike; % how long ago is the last spike?
tomwalters@0 83 % if difft<1, continue, end
tomwalters@0 84 %
tomwalters@0 85 % % % modify eta by the amplitude:
tomwalters@0 86 % % cur_amp=test_sig(j)-threshold; % in dB above threshold
tomwalters@0 87 % % if cur_amp>dynamic_range % in saturation
tomwalters@0 88 % % cur_eta=eta;
tomwalters@0 89 % % else
tomwalters@0 90 % % cur_eta=(eta-eta_null)/dynamic_range*cur_amp+eta_null;
tomwalters@0 91 % % % cur_eta=f2f(cur_amp,0,dynamic_range,eta_null,eta);
tomwalters@0 92 % % end
tomwalters@0 93 %
tomwalters@0 94 % % z = (log(difft) - p1) ./ p2;
tomwalters@0 95 % % spike_prob = exp(p3 - exp(p3)) ./ p2;
tomwalters@0 96 % spike_prob = gevpdf(log(difft),p3,p2,p1);
tomwalters@0 97 %
tomwalters@0 98 % % weibull function
tomwalters@0 99 % % spike_prob=(beta/cur_eta)*power(log(difft)/cur_eta,beta-1);
tomwalters@0 100 %
tomwalters@0 101 % % spike_prob=spike_prob*test_sig(j)+spont_rate; % % modulate the probability with the height of the signal
tomwalters@0 102 % % spike_prob=spike_prob/(1.2+binwidth/2); %correction factor
tomwalters@0 103 % if rand<spike_prob % if a random number is smaller, then ...
tomwalters@0 104 % % jitter=randfloat(-jitter_time,jitter_time);
tomwalters@0 105 % % last_spike=time_now+jitter; % yes, a spike has happend now!
tomwalters@0 106 % last_spike=time_now; % yes, a spike has happend now!
tomwalters@0 107 % spikes(spikecounter)=last_spike+latency; % save and add the latency
tomwalters@0 108 % spikecounter=spikecounter+1; %remember the spike, when it happens
tomwalters@0 109 % end
tomwalters@0 110 % end
tomwalters@0 111 % % end
tomwalters@0 112 % ret_spikes{i}=spikes;
tomwalters@0 113 % end
tomwalters@0 114
tomwalters@0 115 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 116 %% the version with 2 different prob functions
tomwalters@0 117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 118
tomwalters@0 119 x2=getxvalues(sig).*1000;
tomwalters@0 120 x2=x2(x2>0);
tomwalters@0 121 x2=[x2; x2(end)+binwidth];
tomwalters@0 122 % figure(43)
tomwalters@0 123 % cla
tomwalters@0 124 % hold on
tomwalters@0 125 for i=1:2
tomwalters@0 126 p1=params.recovery_parameter.fit_p(1);
tomwalters@0 127
tomwalters@0 128 p2=log(p{10}.x);
tomwalters@0 129 p3=p{10}.y;
tomwalters@0 130 if i==1
tomwalters@0 131 p2=p2/2;
tomwalters@0 132 end
tomwalters@0 133 % plot3(p1,p2,p3,'o')
tomwalters@0 134 pdf=gevpdf(log(x2),p1,p2,p3);
tomwalters@0 135 cdf=gevcdf(log(x2),p1,p2,p3);
tomwalters@0 136 spike_prob_function{i}=ones(size(x2))*inf;
tomwalters@0 137 for j=1:length(x2)
tomwalters@0 138 if cdf(j)<1
tomwalters@0 139 spike_prob_function{i}(j)=pdf(j)/(1-cdf(j))/params.acc_fac;
tomwalters@0 140 end
tomwalters@0 141 end
tomwalters@0 142 end
tomwalters@0 143
tomwalters@0 144
tomwalters@0 145 times=[20 250];
tomwalters@0 146
tomwalters@0 147
tomwalters@0 148 % spike_prob_function2=gevpdf(x2,p3,p2/start_boost_beta_val,p1); % for the first 20 ms
tomwalters@0 149
tomwalters@0 150 % shift the signal by the latency to simulate the latency:
tomwalters@0 151
tomwalters@0 152
tomwalters@0 153
tomwalters@0 154 % spont_rate=0.015;
tomwalters@0 155
tomwalters@0 156 for i=1:nr_sweeps
tomwalters@0 157 spikes=[];
tomwalters@0 158 last_spike=-1; % inital condition: how long ago was the last spike
tomwalters@0 159 spikecounter=1; % spikecounter
tomwalters@0 160 swapc=1;
tomwalters@0 161 next_swap=times(swapc);
tomwalters@0 162 c_function=spike_prob_function{swapc};
tomwalters@0 163
tomwalters@0 164 still_in_latency=1;
tomwalters@0 165 for j=1:nr_steps
tomwalters@0 166 time_now=j*binwidth; % thats the global time counter
tomwalters@0 167
tomwalters@0 168 if still_in_latency && time_now> latency
tomwalters@0 169 if rand<binwidth*first_spike_boost % if a random number is smaller, then ...
tomwalters@0 170 jitter=randfloat(-jitter_time,jitter_time);
tomwalters@0 171 last_spike=time_now+jitter; % yes, a spike has happend now!
tomwalters@0 172 spikes(spikecounter)=last_spike; % save and add the latency
tomwalters@0 173 spikecounter=spikecounter+1; %remember the spike, when it happens
tomwalters@0 174 still_in_latency=0;
tomwalters@0 175 end
tomwalters@0 176
tomwalters@0 177
tomwalters@0 178 else % its a follow up spike
tomwalters@0 179 difft=time_now-last_spike; % how long ago is the last spike?
tomwalters@0 180 sindx=round(difft/binwidth); sindx=max(1,sindx); sindx=min(350,sindx);
tomwalters@0 181
tomwalters@0 182 spike_prob=c_function(sindx);
tomwalters@0 183 %timefound=find(difft<times,1,'first');
tomwalters@0 184 %spike_prob=spike_prob_function{timefound}(sindx);
tomwalters@0 185
tomwalters@0 186 spike_prob=spike_prob*sig(j)+spont_rate; % % modulate the probability with the height of the signal
tomwalters@0 187 % spike_prob=spike_prob/(1.2+binwidth/2); %correction factor
tomwalters@0 188 if rand<spike_prob % if a random number is smaller, then ...
tomwalters@0 189 jitter=randfloat(-jitter_time,jitter_time);
tomwalters@0 190 last_spike=time_now+jitter; % yes, a spike has happend now!
tomwalters@0 191 % make sure that it is not too close to the last one (as a result of the jitter)
tomwalters@0 192 % if last_spike<spikes(spikecounter-1)+0.1;
tomwalters@0 193 % last_spike=time_now;
tomwalters@0 194 % end
tomwalters@0 195 spikes(spikecounter)=last_spike; % save and add the latency
tomwalters@0 196 spikecounter=spikecounter+1; %remember the spike, when it happens
tomwalters@0 197 end
tomwalters@0 198 end
tomwalters@0 199 end
tomwalters@0 200 ret_spikes{i}=spikes;
tomwalters@0 201 end
tomwalters@0 202
tomwalters@0 203 return
tomwalters@0 204 %
tomwalters@0 205 % %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 206 % %% the version with 10 different prob functions
tomwalters@0 207 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 208 %
tomwalters@0 209 % x2=getxvalues(sig).*1000;
tomwalters@0 210 % x2=x2(x2>0);
tomwalters@0 211 % x2=[x2; x2(end)+binwidth];
tomwalters@0 212 % % figure(43)
tomwalters@0 213 % % cla
tomwalters@0 214 % % hold on
tomwalters@0 215 % for i=10:-1:1
tomwalters@0 216 % if isfield(p{i},'k') % otherwise no spikes
tomwalters@0 217 % p1=p{i}.k;
tomwalters@0 218 % p2=log(p{i}.x);
tomwalters@0 219 % p3=p{i}.y;
tomwalters@0 220 % % plot3(p1,p2,p3,'o')
tomwalters@0 221 % pdf=gevpdf(log(x2),p1,p2,p3);
tomwalters@0 222 % cdf=gevcdf(log(x2),p1,p2,p3);
tomwalters@0 223 % spike_prob_function{i}=ones(size(x2))*inf;
tomwalters@0 224 % for j=1:length(x2)
tomwalters@0 225 % if cdf(j)<1
tomwalters@0 226 % spike_prob_function{i}(j)=pdf(j)/(1-cdf(j))/params.acc_fac;
tomwalters@0 227 % end
tomwalters@0 228 % end
tomwalters@0 229 % else
tomwalters@0 230 % % otherwise take the "default" one
tomwalters@0 231 % spike_prob_function{i}=spike_prob_function{10};
tomwalters@0 232 % end
tomwalters@0 233 % end
tomwalters@0 234 %
tomwalters@0 235 % times=0:10:60;
tomwalters@0 236 % times=[times 100 250];
tomwalters@0 237 %
tomwalters@0 238 %
tomwalters@0 239 % % spike_prob_function2=gevpdf(x2,p3,p2/start_boost_beta_val,p1); % for the first 20 ms
tomwalters@0 240 %
tomwalters@0 241 % jitter_time=0;
tomwalters@0 242 % latency=0;
tomwalters@0 243 %
tomwalters@0 244 % start_boost_beta_length=20;
tomwalters@0 245 % test_sig=zeros(length(x2),1);
tomwalters@0 246 % sigend=53;
tomwalters@0 247 % test_sig(1:sigend/binwidth)=1;
tomwalters@0 248 % % build a ramp...
tomwalters@0 249 % test_sig(1:2/binwidth)=linspace(0,1,2/binwidth);
tomwalters@0 250 % test_sig(sigend/binwidth-2/binwidth:sigend/binwidth)=linspace(1,0,2/binwidth+1);
tomwalters@0 251 %
tomwalters@0 252 % spont_rate=0;
tomwalters@0 253 %
tomwalters@0 254 % for i=1:nr_sweeps
tomwalters@0 255 % spikes=[];
tomwalters@0 256 % last_spike=-1; % inital condition: how long ago was the last spike
tomwalters@0 257 % spikecounter=1; % spikecounter
tomwalters@0 258 % swapc=2;
tomwalters@0 259 % next_swap=times(swapc);
tomwalters@0 260 % c_function=spike_prob_function{swapc};
tomwalters@0 261 %
tomwalters@0 262 % for j=1:nr_steps
tomwalters@0 263 % time_now=j*binwidth; % thats the global time counter
tomwalters@0 264 % if time_now>next_swap
tomwalters@0 265 % swapc=swapc+1;
tomwalters@0 266 % next_swap=times(swapc);
tomwalters@0 267 % c_function=spike_prob_function{swapc};
tomwalters@0 268 % end
tomwalters@0 269 % % implementation of a simple solution for the first spike problem: if the spike is the first then assume a very high probability
tomwalters@0 270 % if spikecounter==1 % yes, its the first
tomwalters@0 271 % % if rand<binwidth*first_spike_boost % if a random number is smaller, then ...
tomwalters@0 272 % % jitter=randfloat(-jitter_time,jitter_time);
tomwalters@0 273 % % last_spike=time_now+jitter; % yes, a spike has happend now!
tomwalters@0 274 % % spikes(spikecounter)=last_spike+latency; % save and add the latency
tomwalters@0 275 % % spikecounter=spikecounter+1; %remember the spike, when it happens
tomwalters@0 276 % % end
tomwalters@0 277 %
tomwalters@0 278 % % follow the first spike prob
tomwalters@0 279 % spike_prob=spike_prob_function{1}(j);
tomwalters@0 280 % if rand<spike_prob% if a random number is smaller, then ...
tomwalters@0 281 % jitter=randfloat(-jitter_time,jitter_time);
tomwalters@0 282 % last_spike=time_now+jitter; % yes, a spike has happend now!
tomwalters@0 283 % spikes(spikecounter)=last_spike+latency; % save and add the latency
tomwalters@0 284 % spikecounter=spikecounter+1; %remember the spike, when it happens
tomwalters@0 285 % end
tomwalters@0 286 %
tomwalters@0 287 %
tomwalters@0 288 % else % its a follow up spike
tomwalters@0 289 % difft=time_now-last_spike; % how long ago is the last spike?
tomwalters@0 290 % sindx=round(difft/binwidth); sindx=max(1,sindx); sindx=min(350,sindx);
tomwalters@0 291 %
tomwalters@0 292 % spike_prob=c_function(sindx);
tomwalters@0 293 % %timefound=find(difft<times,1,'first');
tomwalters@0 294 % %spike_prob=spike_prob_function{timefound}(sindx);
tomwalters@0 295 %
tomwalters@0 296 % spike_prob=spike_prob*test_sig(j)+spont_rate; % % modulate the probability with the height of the signal
tomwalters@0 297 % % spike_prob=spike_prob/(1.2+binwidth/2); %correction factor
tomwalters@0 298 % if rand<spike_prob % if a random number is smaller, then ...
tomwalters@0 299 % jitter=randfloat(-jitter_time,jitter_time);
tomwalters@0 300 % last_spike=time_now+jitter; % yes, a spike has happend now!
tomwalters@0 301 % % make sure that it is not too close to the last one (as a result of the jitter)
tomwalters@0 302 % if last_spike<spikes(spikecounter-1)+0.1;
tomwalters@0 303 % last_spike=time_now;
tomwalters@0 304 % end
tomwalters@0 305 % spikes(spikecounter)=last_spike+latency; % save and add the latency
tomwalters@0 306 % spikecounter=spikecounter+1; %remember the spike, when it happens
tomwalters@0 307 % end
tomwalters@0 308 % end
tomwalters@0 309 % end
tomwalters@0 310 % ret_spikes{i}=spikes;
tomwalters@0 311 % end
tomwalters@0 312 %