Exemple #1
0
 def _pvoc(self, X_hat, Phi_hat=None, R=None):
     """
     ::
       a phase vocoder - time-stretch
       inputs:
         X_hat - estimate of signal magnitude
         [Phi_hat] - estimate of signal phase
         [R] - resynthesis hop ratio
       output:
         updates self.X_hat with modified complex spectrum
     """
     N = self.nfft
     W = self.wfft
     H = self.nhop
     R = 1.0 if R is None else R
     dphi = (2 * P.pi * H * P.arange(N / 2 + 1)) / N
     print("Phase Vocoder Resynthesis...", N, W, H, R)
     A = P.angle(self.STFT) if Phi_hat is None else Phi_hat
     phs = A[:, 0]
     self.X_hat = []
     n_cols = X_hat.shape[1]
     t = 0
     while P.floor(t) < n_cols:
         tf = t - P.floor(t)
         idx = P.arange(2) + int(P.floor(t))
         idx[1] = n_cols - 1 if t >= n_cols - 1 else idx[1]
         Xh = X_hat[:, idx]
         Xh = (1 - tf) * Xh[:, 0] + tf * Xh[:, 1]
         self.X_hat.append(Xh * P.exp(1j * phs))
         U = A[:, idx[1]] - A[:, idx[0]] - dphi
         U = U - P.np.round(U / (2 * P.pi)) * 2 * P.pi
         phs += (U + dphi)
         t += P.randn() * P.sqrt(PVOC_VAR * R) + R  # 10% variance
     self.X_hat = P.np.array(self.X_hat).T
def ThreeD_Scatter(x, y, z, selected_checkboxes, panel, auto_rotate, axis_3d=True):

	ax = Axes3D(panel.fig)
	ax.grid(False)
	ax.set_xlabel(selected_checkboxes[0][1])
	ax.set_ylabel(selected_checkboxes[1][1])
	ax.set_zlabel(selected_checkboxes[2][1])
	ax.set_xlim(np.min(x), np.max(x))
	ax.set_ylim(np.min(y), np.max(y))
	ax.set_ylim(np.min(z), np.max(z))

	if not axis_3d:
		ax._axis3don = False
	
	ax.scatter3D(x, y, z,  s=15, c= plt.randn(len(x)))
#	ax.plot_surface(x, y, z,  s=1)
#	def onpick(event):
#		ind = event.ind[0]
#		x, y, z = event.artist._offsets3d
#		print x[ind], y[ind], z[ind]
#	panel.fig.canvas.mpl_connect('pick_event', onpick)	


	
	if auto_rotate:
		for i in xrange(0,360,5):
			ax._axis3don = axis_3d
			ax.view_init(elev=10. +i, azim=i)
			panel.canvas.draw()
		return 
	panel.canvas.draw()
	return
def TwoD_Scatter(x, y, selected_checkboxes, panel, s=None, c=None):
    """
	This method is used to plot a two dimensional graphs on the basis of the selected checkboxes from the pyscrolledwindow
	s is the size of the points being drawn
	c is the color of the points being drawn
	"""
    if c is None:
        c = "b"
    if s is None:
        s = 10

    plt.xlabel(selected_checkboxes[0][1])
    plt.ylabel(selected_checkboxes[1][1])
    try:

        panel.canvas.figure.clf()
        plt.xlim(np.min(x), np.max(x))
        plt.ylim(np.min(y), np.max(y))
        plt.grid(True)
        scatter = plt.scatter(x,
                              y,
                              marker="o",
                              s=s,
                              c=plt.randn(len(x)),
                              picker=True)
        plt.subplots_adjust(left=0.05, right=0.94, top=0.95, bottom=0.09)
    except:
        try:
            panel.canvas.figure.clf()
            ax = panel.canvas.figure.add_subplot(111)
            y_ticks = list(set(y))
            ax.set_yticks(range(len(y_ticks)))
            ax.set_yticklabels(y_ticks)
            for direction in ["left", "right", "top", "bottom"]:
                ax.spines[direction].set_color("none")
            y_data = [y_ticks.index(element) for element in y]
            plt.scatter(x, y_data)
        except:
            panel.canvas.figure.clf()
            ax = panel.canvas.figure.add_subplot(111)
            y_ticks = list(set(y))
            ax.set_yticks(range(len(y_ticks)))
            ax.set_yticklabels(y_ticks)
            for direction in ["left", "right", "top", "bottom"]:
                ax.spines[direction].set_color("none")
            y_data = [y_ticks.index(element) for element in y]
            x_ticks = list(set(x))
            ax.set_xticks(range(len(x_ticks)))
            ax.set_xticklabels(x_ticks)
            x_data = [x_ticks.index(element) for element in x]
            plt.scatter(x_data, y_data)

    panel.canvas.draw()
    return
def TwoD_Scatter(x, y, selected_checkboxes,  panel, s=None, c=None):
	"""
	This method is used to plot a two dimensional graphs on the basis of the selected checkboxes from the pyscrolledwindow
	s is the size of the points being drawn
	c is the color of the points being drawn
	"""
	if c is None:
		c = "b"
	if s is None:
		s= 10
	
	plt.xlabel(selected_checkboxes[0][1])
	plt.ylabel(selected_checkboxes[1][1])
	try:

		panel.canvas.figure.clf()
		plt.xlim(np.min(x), np.max(x))
		plt.ylim(np.min(y), np.max(y))
		plt.grid(True)
		scatter = plt.scatter(x, y,marker="o", s=s, c=plt.randn(len(x)), picker= True)
		plt.subplots_adjust(left=0.05, right=0.94, top=0.95, bottom=0.09)
	except:
		try:
			panel.canvas.figure.clf()
			ax = panel.canvas.figure.add_subplot(111)  
			y_ticks = list(set(y))
			ax.set_yticks(range(len(y_ticks)))
			ax.set_yticklabels(y_ticks)
			for direction in ["left", "right", "top", "bottom"]:
				ax.spines[direction].set_color("none")
			y_data= [y_ticks.index(element) for element in y]
			plt.scatter(x, y_data)
		except:
			panel.canvas.figure.clf()
			ax = panel.canvas.figure.add_subplot(111)  
			y_ticks = list(set(y))
			ax.set_yticks(range(len(y_ticks)))
			ax.set_yticklabels(y_ticks)
			for direction in ["left", "right", "top", "bottom"]:
				ax.spines[direction].set_color("none")
			y_data= [y_ticks.index(element) for element in y]
			x_ticks = list(set(x))
			ax.set_xticks(range(len(x_ticks)))
			ax.set_xticklabels(x_ticks)
			x_data= [x_ticks.index(element) for element in x]
			plt.scatter(x_data, y_data)
		
	panel.canvas.draw()
	return
#% 16 = rand3 17 = rand9 18 = rand10 19 = 0.8 20 = sineroot27
#% 21 = sineroot19 22 = sineroot50 23 = sineroot75
#% 24 = sineroot10 25 = sineroot110 26 = sineroot75tenth
#% 27 = sineroots20plus40  28 = sineroot75third
#% 29 = sineroot243  30 = sineroot150  31 = sineroot200
#% 32 = sine10pt587352723 33 = sine10pt10.387352723
#% 34 = rand7  35 = sine12  36 = rand5  37 = sine11
#% 38 = sine10pt17352723  39 = sine5 40 = sine6
#% 41 = sine7 42 = sine8  43 = sine9 44 = sine12
#% 45 = sine13  46 = sine14  47 = sine10.8342522
#% 48 = sine11.8522311  49 = sine12.5223223  50 = sine13.1900453
#% 51 = sine7.1900453  52 = sine7.9004531  53 = sine8.4900453
#% 54 = sine9.1900453 55 = sine5.19004  56 = sine5.8045
#% 57 = sine6.49004 58 = sine6.9004 59 = sine13.9004
#%%% Initializations
plt.randn('state', randstate)
np.random.rand('twister', randstate)
I = np.eye(Netsize)
Np = length(patts)
#% Create raw weights
if newNets:
    if Netsize<=20.:
        Netconnectivity = 1.
    else:
        Netconnectivity = 10./Netsize
        
    
    WRaw = generate_internal_weights(Netsize, Netconnectivity)
    WinRaw = plt.randn(Netsize, 1.)
    WbiasRaw = plt.randn(Netsize, 1.)
Exemple #6
0
import matplotlib.pyplot as plt
import matplotlib.pylab as pl
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.savefig('pyplot01.png', format='png')

plt.hist(pl.randn(100000), 50)
plt.savefig('pyplot_randn_hist.png', format='png')


Exemple #7
0
def make_lor9d(tau_sec, numtrials, siglen_pre, kerlen_ratio, fs_Hz_pre, downsample):

    # Local Variables: siglen_pre, xlim, kerlen_ratio, psd_from_fft, psd_fft, sc_d_0, sc_rep, downsample, s2, tt, sc_d, var, kerlen, tau_sec, df, i0, c, psd, varinfo, numtrials, name, fs_Hz_post, siglen_post, VERBOSE, f, i, signal, freqs, kern, fs_Hz_pre, s, sc, x
    # Function calls: autocorrel, figure, warning, fprintf, make_lor9d, diff, find, size, plot, randn, conv, floor, reshape, sum, nargout, zeros, length, pi, sort, fftpsd, atan, squeeze, mod, decimate, exp, clf, mean
    #%9d: downsamples as well
    #%
    #%tau_sec in seconds. (can be big)
    #%siglen in samples.
    #%kerlen_ratio is about 2,3,4,...
    #%
    #%returns:
    #%    signal: trials x siglen
    #%    psd and varinfo are generated only if demanded.
    #%    only if varinfo is mentioned, the 8 variances are printed.
    #%main method imported from simulate_lorentzian8.m
    #%known problems:
    #%   1-bad at low fs_Hz (?)
    #%   2- bad at low tau_sec
    #%   But ok for natural ranges (e.g., A1 cortex data)
    VERBOSE = True
    #%for debuging
    if VERBOSE:
        print 'tau=%g(ms), trials=%d, len=%d (before downsampling), ker x=%g'%(tau_sec*1000., numtrials, siglen_pre, kerlen_ratio)
    
    
    if tau_sec > siglen_pre / fs_Hz_pre:
        matcompat.warning('tau_sec > T')
        #% although the result will be correct
    
    
    #%making the kernel
    kerlen = np.dot(kerlen_ratio, tau_sec)
    tt = np.arange(0., (kerlen)+(1./fs_Hz_pre), 1./fs_Hz_pre)
    kern = np.exp((-tt / tau_sec))
    if VERBOSE:
        #%this should be close to 1.0 :
        print('%g ?= 1.0\n'%( ((np.sum(kern) / tau_sec) / fs_Hz_pre)))
    
    #%pre = before downsampling
    #%post = after downsampling
    siglen_post = (siglen_pre / downsample)
    #%making the signal.
    signal = np.zeros([numtrials, siglen_post])
    for i in np.arange(1., (numtrials)+1):
        #%notes in simulate_lorentzian6.m



        #%notes in simulate_lorentzian6.m
        #s = plt.randn(1., (siglen_pre+len(kern)*2.+4.))
        s = plt.randn( (siglen_pre+len(kern)*2.+4.))
        #print s.shape
        #%+2!!
        xlim = len(kern)+np.array(np.hstack((1., siglen_pre)))-1.+1.+2.
        #%throwing away a few extra samples (for very long correlation lengths)
        #s2 = np.dot(plt.conv(s, matdiv(kern, fs_Hz_pre)), fs_Hz_pre)
        #print s.shape
        #print (kern / fs_Hz_pre).shape
        s2 = np.convolve(s, kern / fs_Hz_pre)* fs_Hz_pre
        #%integration
        sc = s2[int(xlim[0])-1:xlim[1]]
        #%cut the useful portion
        #%signal(i,:) = sc; %s2
        #%downsampling
        #%assert(mod(siglen_pre,downsample)==0);
        #%if 1
        #%sc_rep=reshape(sc,[1,siglen_pre/downsample,downsample]); %1 trial
        #sc_rep = np.reshape(sc, np.array(np.hstack((1., downsample, matdiv(siglen_pre, downsample)))))
        sc_rep = np.reshape(sc, [1, downsample, siglen_pre/ downsample])
        #%1 trial
        #%assert(floor(siglen_pre/downsample)==siglen_post)
        #%sc_d_0=mean(sc_rep,3);
        sc_d_0 = np.mean(sc_rep, axis=2-1)
        #%siglen_b=siglen/downsample;
        #%end

        #print signal.shape
        #print i,

        #sc_d = matcompat.decimate(sc, downsample)
        sc_d = sc[0::downsample]
        #signal[int(i)-1,:] = sc_d
        #print sc_d.shape

        signal[int(i)-1][:] = sc_d
        

        #%s2
        #%A=0
        #%if A
        if False:
            plt.figure(1.)
            plt.clf
            plt.plot((np.arange(1., (siglen_post)+1)-1.), np.squeeze(sc_d_0))
            plt.hold(on)
            plt.plot((np.arange(1., (siglen_post)+1)-1.), sc_d, 'r')
            #%plot((1:length(sc))/4/5+1,squeeze(sc),'k')
            plt.plot(matdiv(np.arange(1., (siglen_pre)+1)-1., downsample), np.squeeze(sc), 'k')
            #%end
        #%end

        
    #fs_Hz_post = matdiv(fs_Hz_pre, downsample)
    fs_Hz_post = fs_Hz_pre / downsample
    #%making & testing the fft
    #%FFT is done on DOWNSAMPLED signals (after downsampling)
    #% Only if fft psd is demanded. If not, only the signal is generated

    nargout = 1000 #extra_output +1

    if nargout > 1.:
        [psd_from_fft, freqs] = fftpsd(signal, fs_Hz_post)
        #psd_fft = np.array([])
        psd_fft_psd = psd_from_fft
        psd_fft_f = freqs
        psd_fft = (psd_fft_psd,psd_fft_f)
        if nargout > 2:
            varinfo = [{},{},{},{},{},{},{}] #np.array([])
            #%there are eight (!!) variance measures that can be used for normalization!!
            #%please note that the variance rate (i.e., var(:)/fs_Hz_post) is multiplied by fs_Hz_post.
            #% to get the approximation for var(signal(:))
            df = np.mean(np.diff(np.sort(freqs)))
            #%=1.0!
            varinfo[0]['_name'] = 'fft'
            varinfo[0]['_var'] = ((np.dot(np.sum(psd_from_fft), df) / fs_Hz_post)* fs_Hz_post)
            varinfo[1]['_name'] = 'vertical variance'
            #%var_v; % var_1;%varinfo(2)['_name']='mean(var(dim1))';
            #varinfo(2).var = mean(var(signal,[0],1))/fs_Hz_post * fs_Hz_post;
            varinfo[1]['_var'] = np.mean(np.var(signal,axis=0).flatten()) / fs_Hz_post* fs_Hz_post
            varinfo[2]['_name'] = 'var(:)'
            #%var_2;
            #varinfo(3).var = var(signal(:))/fs_Hz_post * fs_Hz_post;
            varinfo[2]['_var'] =  np.var(signal.flatten()) / fs_Hz_post * fs_Hz_post

            varinfo[3]['_name'] = 'horizontal variance'
            #%var_h;
            #varinfo(4).var = mean(var(signal,[0],2))/fs_Hz_post * fs_Hz_post;
            varinfo[3]['_var'] = np.mean(np.var(signal,axis=2-1).flatten()) / fs_Hz_post * fs_Hz_post

            #%can be very low if tau_sec is very big compared to siglen/fs_Hz
            varinfo[4]['_name'] = 'analytical varinace (nyquist)'
            #%var_a;
            #varinfo(5).var = 2*tau_sec*atan(2*pi*fs_Hz_post*tau_sec)/(2*pi) * fs_Hz_post;
            varinfo[4]['_var'] = 2.*tau_sec * np.arctan(2.*np.pi * fs_Hz_post * tau_sec) /  (2.*np.pi) * fs_Hz_post
            varinfo[5]['_name'] = 'ideal variance'
            #%var_a_inf;
            varinfo[5]['_var'] = tau_sec/2. * fs_Hz_post
            varinfo[6]['_name'] = 'kernel norm'
            varinfo[6]['_var'] = np.sum(kern) / tau_sec / fs_Hz_post
            #%this is not a 'variance'. This should be 1.0.
            #%slow
            if False:
                #autocorrel not converted into python
                [x, c] = autocorrel(signal, fs_Hz_post)
                i0 = nonzero((x >= 0.), 1., 'first') #find(,'first')
                print "Warning: nonzero()"
                #%plot(x*1000,c)
                varinfo[7]['_name'] = 'autocorrel(0) var'
                #varinfo(8).var=c(i0) * fs_Hz_post;
                varinfo[7]['_var'] = np.dot(c[int(i0)-1], fs_Hz_post)
                #%not very reliable.

            
            
            #if VERBOSE:
            #    fprintf('var*fs=')
            #    fprintf(' %g, ', np.array(np.hstack((varinfo[:]['_var']))))
            #    fprintf('.\n')

            return signal, psd_fft, fs_Hz_post, varinfo
            
    #return signal, psd_fft, fs_Hz_post
            
    #return signal #, psd_fft, fs_Hz_post, varinfo
    raise Exception("Bad usage")
#% 21 = sineroot19 22 = sineroot50 23 = sineroot75
#% 24 = sineroot10 25 = sineroot110 26 = sineroot75tenth
#% 27 = sineroots20plus40  28 = sineroot75third
#% 29 = sineroot243  30 = sineroot150  31 = sineroot200
#% 32 = sine10.587352723 33 = sine10.387352723
#% 34 = rand7  35 = sine12  36 = 10+perturb  37 = sine11
#% 38 = sine10.17352723  39 = sine5 40 = sine6
#% 41 = sine7 42 = sine8  43 = sine9 44 = sine12
#% 45 = sine13  46 = sine14  47 = sine10.8342522
#% 48 = sine11.8342522  49 = sine12.8342522  50 = sine13.1900453
#% 51 = sine7.1900453  52 = sine7.8342522  53 = sine8.8342522
#% 54 = sine9.8342522 55 = sine5.19004  56 = sine5.8045
#% 57 = sine6.49004 58 = sine6.9004 59 = sine13.9004
#% 60 = 18+perturb
#%%% Initializations
plt.randn('state', randstate)
np.random.rand('twister', randstate)
#% Create raw weights
if newNets:
    if Netsize<=20.:
        Netconnectivity = 1.
    else:
        Netconnectivity = 10./Netsize
        
    
    WstarRaw = generate_internal_weights(Netsize, Netconnectivity)
    WinRaw = plt.randn(Netsize, 1.)
    WbiasRaw = plt.randn(Netsize, 1.)


#% Scale raw weights and initialize weights
#% 21 = sineroot19 22 = sineroot50 23 = sineroot75
#% 24 = sineroot10 25 = sineroot110 26 = sineroot75tenth
#% 27 = sineroots20plus40  28 = sineroot75third
#% 29 = sineroot243  30 = sineroot150  31 = sineroot200
#% 32 = sine10.587352723 33 = sine10.387352723
#% 34 = rand7  35 = sine12  36 = 10+perturb  37 = sine11
#% 38 = sine10.17352723  39 = sine5 40 = sine6
#% 41 = sine7 42 = sine8  43 = sine9 44 = sine12
#% 45 = sine13  46 = sine14  47 = sine10.8342522
#% 48 = sine11.8342522  49 = sine12.8342522  50 = sine13.1900453
#% 51 = sine7.1900453  52 = sine7.8342522  53 = sine8.8342522
#% 54 = sine9.8342522 55 = sine5.19004  56 = sine5.8045
#% 57 = sine6.49004 58 = sine6.9004 59 = sine13.9004
#% 60 = 18+perturb
#%%% Initializations
plt.randn('state', randstate)
np.random.rand('twister', randstate)
#% Create raw weights
if newNets:
    if Netsize<=20.:
        Netconnectivity = 1.
    else:
        Netconnectivity = 10./Netsize
        
    
    WstarRaw = generate_internal_weights(Netsize, Netconnectivity)
    WinRaw = plt.randn(Netsize, 1.)
    WbiasRaw = plt.randn(Netsize, 1.)


#% Scale raw weights and initialize weights
Exemple #10
0
import scipy as sp
import scipy.signal as sg
import simpleSTC as sstc
import compiSTAC as ci
import matplotlib.pylab as plt

nt = 20 # number of temporal elements of filter
tvec = np.vstack(np.arange(-nt+1., 1)) # time vector
filt1 = np.exp(-(((tvec+4.5)/1.5)**2)/2) - .2*np.exp((-((tvec+nt/2)/3)**2)/2)  #1st filter
filt1 = filt1/np.linalg.norm(filt1) #normalize
filt2 = np.vstack((np.diff(filt1, 1, 0), 0)) # 2nd filter
filt2 = filt2 - filt1*np.sum(filt1*filt2) # orthogonalize to 1st filter
filt2 = (filt2)/np.linalg.norm(filt2) # normalize

slen = 2000 # Stimulus length
Stim = Stim = plt.randn(slen, 1.)
RefreshRate = 100. # refresh rate

linresp = (sg.convolve2d(np.concatenate((np.zeros((len(filt1)-1, Stim.shape[1])), Stim), 0), np.rot90(filt1, 2), 'valid')) # filter output
r = np.maximum(linresp, 0.)*50      # instantaneous spike rate
r = np.vstack(r)
spikes = np.random.poisson(r/RefreshRate)   # generate spikes
[sta, stc, rawmu, rawcov] = sstc.simpleSTC(Stim, spikes, nt) #% Compute STA and STC
u, s, v = np.linalg.svd(stc, 'full_matrices') # Compute eigenvectors of STC matrix
s = np.diag(s)
v = v.T

ndims = 1
vecs, vals, mu1, mu0, v1, v2, _ = ci.compiSTAC(sta, stc, rawmu, rawcov, ndims)

fig1 = plt.figure()
Exemple #11
0
def x():

    dithering = False
    #% from test_lor3c2
    #% 2) down sampling changes SNR
    tau_s = 25./1.
    std_s = 3.53
    tau_n = 12./1.
    std_n = 5.55
    methid = 0
    #%NTA=2.^[4:5];
    #%NTA=2.^[4:8,9];

    #NTA = np.power(2, np.array(np.hstack((np.arange(4, 7), 8, 10, 12))))
    #NTA = np.power(2, np.array([4, 6, 8])) #12
    NTA = np.power(2, np.array([4])) #12
    nt_generate = np.max(NTA)
    b_msec = 7.  #3,5,1
    fs_Hz = 1000.
    siglen_msec = 10000.
    #%kerlenratio=5;
    siglen_msec = math.floor(siglen_msec / b_msec)* b_msec
    #%uses fs_Hz, and then downsamples. The downsampling is not quite correct.
    downsample_b = int(b_msec/1000. * fs_Hz + 0.00001)
    #print downsample_b #7
    assert downsample_b == 7
    #%happens to be equal to b, if fs=1000
    if False:
        r2_raw = makelorresponse(tau_s, std_s, tau_n, std_n, nt_generate, siglen_msec, fs_Hz, [], downsample_b)
    else:
        #test only
        r2_raw=np.random.randn(nt_generate, int((siglen_msec / b_msec))) #temporary
    if False:
        print    r2_raw.shape #1428x256
        print nt_generate
        print nt_generate
        print nt_generate
    del nt_generate
    
    siglen_b = (siglen_msec / b_msec)
    del siglen_msec
    #%M=4;
    #%M=8;
    #%M=4;
    M = 5
    #%M=8;
    #%M=15;
    #%M=3;
    [resp_dig, dithinfo1] = discr(r2_raw, M)
    #%not dithered
    #assert is_any_int_type(r2_raw[0,0])
    #r2_raw ois float

    [resp_dith, dithinfo2] = dith_unsure(r2_raw, (dithinfo1['centres']))
    #%not dithered
    
    resp_dig = resp_dith
    #del dithinfo

    resp_dig = resp_dig-1
    assert is_any_int_type(resp_dig[0,0])
    resp_cont = r2_raw
    sz=resp_cont.shape
    #print sz #1428 * 256
    resp_cont=resp_cont.flatten(1)
    resp_cont = resp_cont-np.mean(resp_cont.flatten(1))
    #%resp_cont=resp_cont/std(resp_cont(:))/2;
    #%resp_cont=resp_cont/std(resp_cont(:))/2*1.5*4*4;
    #resp_cont = matdiv(np.dot(matdiv(resp_cont, np.std(resp_cont.flatten(1)))/2.*1., M), M-1.)
    #resp_cont=resp_cont/std(resp_cont(:))/2*1*M/(M-1);
    resp_cont =  resp_cont / np.std(resp_cont.flatten()) /2.* M / (M-1.)
    #% [-1,+1]
    resp_cont = (resp_cont+1.)/2.
    #% [0,1]
    resp_cont = resp_cont * (M-1.)
    if 1:
        resp_cont[(resp_cont<0.)] = 0
        resp_cont[(resp_cont > M-1.)] = M-1
    resp_cont = resp_cont.reshape(sz)
    



    #print "HERE"
    #exit(0)
    
    #%clear r2_raw;
    #% analytical formula
    #%fs_Hz is used for Nyquist frequency
    #%ami=analytical_lor(tau_s,std_s, tau_n, std_n, fs_Hz, 0.0001);
    #%ami_perbin=mi/fs_Hz*b_msec;
    #% %WHY?!
    #%a=analytical_lor(tau_s/b_msec,std_s, tau_n/b_msec, std_n, fs_Hz/b_msec, 0.0001);
    #%ami_perbin=a / (fs_Hz/b_msec);
    #% (tau_s_msec,  sigma_s,  tau_n_msec,  sigma_n, fs_Hz, d_freq)
    #%Ia_bits_per_sec
    #% Ia_bps --> ami_bps = analytical mi
    ami_bps = analytical_lor(tau_s, std_s, tau_n, std_n, (1./b_msec/1000.), 0.0001)
    #%ami_perbin=ami_bps* (b_msec/1000);
    #% 1/ (fs_Hz/b_msec) = b_msec / fs = above (bcoz, fs=1000)
    #% previous: 0.2362
    #% new: 2.4512e-04
    binlen_sec = b_msec/1000.
    #%used later for normalization
    
    #del a
    del b_msec

    #%clearvars -except   resp_cont resp_dig M NTA binlen_sec siglen_b tau_s tau_n ami_bps ...
    #%    r2_raw std_* methid;
    don_plot_this = False
    #%size(respcut_dig)
    if don_plot_this:
            #%%
            #%mean(mean(resp_dig,1))
            [c, d] = plt.xcorr((matdiv(np.mean(resp_dig, 1.)-1.-(M-1.)/2.+1., M)*2.), 'unbiased')
            plt.figure(10.)
            plt.clf
            #% / sqrt(std_s^2+std_n^2)
            h = plt.plot((np.dot(d, binlen_sec)*1000.), matdiv(c, std_s**2.), 'k.-')
            set(h, 'linewidth', 2.)
            set(h, 'displayname', 'signal')
            plt.xlim((np.array(np.hstack((-1., 1.)))*100.*1.))
            #%set(gca,'xtick',-200:10:200);
            plt.title('\\tau_s, \\tau_n = %g,%g [msec]'%( tau_s, tau_n))
            #nn = matcompat.size(resp_dig, 1.)
            nn = resp_dig.shape[1-1]
            c1 = 0.
            for tri in np.arange(1., (nn)+1):
                #%mean(resp_dig(tri,:))
                #MISSING CODE!!!
                [c,d]=np.xcorr((resp_dig[tri,:]-1-(M-1)/2+1)/M*2,'unbiased');
                c1=c1+c/nn;
                pass

            del nn
            plt.hold(on)
            h = plt.plot((np.dot(d, binlen_sec)*1000.), matdiv(c1, std_n**2.), 'r.-')
            set(h, 'linewidth', 2.)
            set(h, 'displayname', 'noise')
            tt = np.dot(d, binlen_sec)
            h = plt.plot((tt*1000.), np.dot(binlen_sec, np.exp(matdiv(-np.abs(tt), tau_s/1000.))), 'k--')
            set(h, 'displayname', 'signal analytical')
            h = plt.plot((tt*1000.), np.dot(binlen_sec, np.exp(matdiv(-np.abs(tt), tau_n/1000.))), 'r--')
            set(h, 'displayname', 'noise analytical')
            h = plt.legend('show')
            set(h, 'box', 'off')
            set(h, 'location', 'NorthWest')
            plt.xlabel('Time [msec]')
            plt.title('cross correlation')
            #% my_save_png('test_lor4f1---signal-xcorr-v12-temp',{},[4,3]);
            
    begintime = time.time()
    #tic=time.time()

    #%estims=cell(4,4);
    #%LARR=[1,4,8,10];
    #%LARR=[1,2:2:10];
    #%LARR=[1,2:2:8];
    #%LARR=[1,2,3,6];
    #%LARR=[1,2,3,6,7];
    #%LARR=[1]; %
    #LARR = np.array(np.hstack((1., 3., 6., 7.)))
    #LARR = np.array(np.hstack((1+1, 3, 6, 7)))
    #LARR = [1+1, 3, 6, 7]
    LARR = [1,1+1, 4]
    MAX_L_DIRECT = np.Inf

    #estims_cell=[[],[],[],[]]
    #estims_cell=[[{}],[{}],[{}],[{}]]
    #estims_cell=[[{}]]
    estims_cell=[]
    #len(NTA)
    #print len(LARR),len(NTA), "******************"
    for i in range(0,len(LARR)):
            estims_cell.append([])
            for j in range(0,len(NTA)):
                print i,j
                estims_cell[i].append({})


    #%for nti=10:len(NTA)
    for nti in range(1, len(NTA)+1):
        nt = int(NTA[int(nti)-1])
        for li in range(1, len(LARR)+1):
            L = LARR[int(li)-1]
            #%lenL=floor(siglen_msec/b_msec/L)*L;
            #%lenL=floor(siglen_msec/L/b_msec)*L*b_msec;
            lenL = (int((siglen_b/ L)) * L)
            #%respcut=resp(:,1:lenL)';
            #%  respcut=resp(1:nt,1:lenL)';
            if dithering:
                respcut_cont = resp_cont[0:nt,0:lenL].conj().T


            #print "HERE"
            #exit(0)
            
            #print nt, max(NTA) #16,256
            #print resp_cont.shape  #1428x256
            #print resp_dig.shape
            #print "000"
            respcut_dig = resp_dig[0:nt,0:lenL].conj().T
            #% len x trials
            #%spk0 = zeros(1,LNum,size(signal,1), newlen );
            #%spk=reshape(respcut,  [1,L,size(resp)]);
            #% spk__=reshape(respcut,  1,L,lenL/L,[]);
            #%dithering = False
            if dithering:
                print "Using dithering"
                #spk__cont = np.reshape(respcut_cont, 1., L, matdiv(lenL, L), np.array([]))
                spk__cont = np.reshape(respcut_cont, [1, L, int(lenL/ L), -1])
                #% 1 x L x len x trials
                #spk2_cont = permute(spk__cont, np.array(np.hstack((1., 2., 4., 3.))))
                spk2_cont = spk__cont.transpose([1-1, 2-1, 4-1, 3-1]) #, np.array(np.hstack((1., 2., 4., 3.))))
                assert spk2_cont.shape == (1, L, nt, int(lenL/ L))

                #clear(spk__c)
                # #%slow
            else:
                print "Not using dithering"
            
            
            #% 1 x L x trials x len
            #print respcut_dig.shape, "*1"  #256 x 16  
            #print (lenL,L,nt), "*2" #1428.0, 1.0, 16
            spk__dig = np.reshape(respcut_dig, [1, L, int(lenL/ L), -1])
            spk2_dig = spk__dig.transpose([1-1, 2-1, 4-1, 3-1]) #permute(spk__dig, np.array(np.hstack((1., 2., 4., 3.))))
            assert spk2_dig.shape == (1, L, nt, int(lenL/ L))
            assert is_any_int_type(spk2_dig[0,0,0,0])

            del spk__dig

            #%slow
            
            assert type(nt) is int
            #nts = np.dot(np.ones(1., matdiv(lenL, L)), nt)
            #nts = np.ones([(lenL/ L)]) * nt
            nts = np.array([nt]*(lenL/ L), dtype=int) #np.ones([(lenL/ L)]) * nt
            #[nt]*int(lenL/ L)

            #print nts

            #assert type(nts[0]) is int
            #print type(nts), type(nts[0])
             

            
            print 'spk2: %dx%dx%dx%d ['%spk2_dig.shape
            if L<=MAX_L_DIRECT:
                ns = int(lenL/ L)
                #nta = np.arange(1., (ns)+1)*0.+nt
                nta = np.array([nt]*ns)
                if dithering:
                    #%[prs3,ps3]=my_prs_dith1(spk2, nta);
                    #%   [prs3_dith,ps3]=my_prs_dith1(spk2_c-1, nta);
                    #%[prs3_dith,ps3]=my_prs_dith1(spk2_cont-0, nta);
                    (prs_dith, ps_dith) = my_prs_dith1((spk2_cont-0.), nta)
                    #%    [prs3_dig,ps3]=my_prs(spk1, nta);
                    #%    sum(abs(prs3_dith(:)-prs3_dig(:)))
                    #%   if 0
                    #%       %[prs1,ps1]=my_prs(spk2, nta);
                    #%       [prs1,ps1]=my_prs(spk2_dig, nta);
                    #%   end
                    (prs_dig, ps_dig) = my_prs(spk2_dig, nta)
                    #print(repr((prs_dig, ps_dig)))
                    #%prs=prs3_dith;
                    #%ps=ps3;
                    fprintf('p')
                    hrs0_th = my_hrs(prs_dith, ps_dith, methid)
                    pr_dith = my_pr(prs_dith, ps_dith)
                    hr0_th = my_hr(pr_dith, methid)
                    #%hr1=hr(spk2_dig,nts,methid);
                    #%hrs1=hrs(spk2_dig,nts,methid);
                    hrs_dig = my_hrs(prs_dig, ps_dig, methid)
                    pr_dig = my_pr(prs_dig, ps_dig)
                    hr_dig = my_hr(pr_dig, methid)
                    #%[ my_hr(ps_dith, methid),my_hr(ps_dig, methid)]
                    #%m22= [...
                    #%    my_hr(pr_dith, methid),my_hr(pr_dig, methid);
                    #%    my_hrs(prs_dith,ps_dith, methid),my_hrs(prs_dig,ps_dig, methid)];
                    #%
                    vv1 = my_hr(pr_dith, methid)
                    my_hr(pr_dig, methid)
                    vv2 = my_hrs(prs_dith, ps_dith, methid)
                    my_hrs(prs_dig, ps_dig, methid)
                    m22 = vertcat(vv1, vv2)
                    #m22

                    #print li, nti
                    #%myminmax(spk2_dig(:))  % 1--4
                    #%myminmax(spk2_cont(:)) % 0--3
                    #estims.cell[int(li)-1,int(nti)-1]['hr_d'] = hr_dig
                    estims_cell[int(li)-1][int(nti)-1]['hr_d'] = hr_dig
                    #%hr1;
                    estims_cell[int(li)-1][int(nti)-1]['hrs_d'] = hrs_dig
                    #%hrs1;
                    estims_cell[int(li)-1][int(nti)-1]['hr_th'] = hr0_th
                    estims_cell[int(li)-1][int(nti)-1]['hrs_th'] = hrs0_th


                #print type(nts), type(nts[0])
                hr1_dig = hr(spk2_dig, nts, methid)
                hrs1_dig = hrs(spk2_dig, nts, methid)
                #print li, nti #1.0, 1.0
                #print int(li)-1,int(nti)-1
                #print estims_cell[int(li)-1][int(nti)-1]
                zz=estims_cell[int(li)-1][int(nti)-1]
                zz['hr_d'] = 1
                #print hr1_dig
                estims_cell[int(li)-1][int(nti)-1]['hr_d'] = hr1_dig
                #%hr1;
                estims_cell[int(li)-1][int(nti)-1]['hrs_d'] = hrs1_dig
                #%hrs1;
            
    
                if 1:

                    #print "***********"
                    #print nts
                    #%no dithering
                    estims_cell[int(li)-1][int(nti)-1]['hrs_sh'] = hrs_shuff(spk2_dig, nts, methid)

                    estims_cell[int(li)-1][int(nti)-1]['hrs_ind'] = hrsind(spk2_dig, nts, methid)
                    estims_cell[int(li)-1][int(nti)-1]['_xi'] = xi(spk2_dig, nts, methid)

                    #    
    
            else:
                estims_cell[int(li)-1][int(nti)-1]['_hr'] = 0.
                estims_cell[int(li)-1][int(nti)-1]['_hrs'] = 0.
                estims_cell[int(li)-1][int(nti)-1]['hrs_sh'] = 0.
                estims_cell[int(li)-1][int(nti)-1]['hrs_ind'] = 0.
                estims_cell[int(li)-1][int(nti)-1]['_xi'] = 0.
                
            
            #%we need: q-model in which , up to q0(=0) is shuffled: model=q, shuffle=0
            #%shuffle across:R (not S)
            #%if 1
            #print "HERE2"
            #exit(0)


            q = 1
            estims_cell[int(li)-1][int(nti)-1]['_hqrs'] = hqrs(spk2_dig, nts, q, methid)
            estims_cell[int(li)-1][int(nti)-1]['_hqr'] = hqr(spk2_dig, nts, q, methid)
            estims_cell[int(li)-1][int(nti)-1]['_xiq'] = xiq(spk2_dig, nts, q, methid)
            #%spk_q=q_shuffle(spk,nts,q);

            spk_q = q_shuffle(spk2_dig, nts, q)
            #%estims{li,nti}['hrs_q']=hrs(spk_q,nts,methid);
            estims_cell[int(li)-1][int(nti)-1]['hrs_q_sh'] = hrs(spk_q, nts, methid)
            #%estims{li,nti}.hqrs0=hqrs(spk,nts,q,meth);
            #%estims{li,nti}.hrss0=hrs_shuff(spk,nts,meth);
            #%estims{li,nti}.spks=q_shuffle(spk,nts,q);
            #%estims{li,nti}.hqrss0=hrs(spks,nts,meth);
            #%estims{li,nti}.hr0=hr(spk,nts,meth);
            #%estims{li,nti}.hqr0=hqr(spk,nts,q,meth);
            #%estims{li,nti}.hrs0=hrs(spk,nts,meth);
            #%estims{li,nti}.hrsi0=hrsind(spk,nmiu,meth);
            #%estims{li,nti}.xiq0=xiq(spk,nmiu,q,meth);
            #%estims{li,nti}.xi0=xi(spk,nmiu,meth);
            #%end
            estims_cell[int(li)-1][int(nti)-1]['L'] = L
            estims_cell[int(li)-1][int(nti)-1]['len'] = lenL/ L
            estims_cell[int(li)-1][int(nti)-1]['nt'] = nt
            print(']'),
            #print' (%g sec.)'%(np.floor(toc),)
            print('\n')
            #%estims{li,nti}

        #Takes long up to here. Cleaned up.
        #print "HERE"
        #exit(0)

        #%% -
        #%a2d1_th=[];
        #a2d1_d = [] #np.array([])
        a2d1_d = [] #np.array([])
        a2d2_qd = [] #np.array([])
        a2d3_dsh = [] #np.array([])
        nta1d = [] #np.array([])
        for ntj in range(1, nti+1):
            a2d1_d.append([])
            assert len(a2d1_d)-1 == ntj-1 #assert index number
            a2d3_dsh.append([])
            assert len(a2d3_dsh)-1 == ntj-1 #assert index number
            a2d2_qd.append([])
            assert len(a2d2_qd)-1 == ntj-1 #assert index number

            #ee=estims_cell[li-1][ntj-1]
            #nta1d.append( ee['nt'] )

            nta1d.append( estims_cell[0][ntj-1]['nt'] )
            assert len(nta1d)-1 == ntj-1

            for li in range(1, len(LARR)+1 ):
                #%L=LARR(li);
                #ee=estims{li,ntj};
                print estims_cell  #list of list of dict
                #print estims_cell.shape
                print estims_cell[0][0]
                print li,ntj
                ee=estims_cell[li-1][ntj-1]
                #%if  L<=MAX_L_DIRECT
                if 'hrs_d' in ee: #haskey(ee, 'hrs_d'):
                    #% mi1=(ee['_hr']-ee['_hrs'] -ee['hrs_ind'] + ee['hrs_sh']  )/ee['L'];
                    #%mi1=(ee['hr_th']-ee['hrs_th']  )/ee['L'];
                    #%mi1=(ee['hr_d']-ee['hrs_d'] -ee['hrs_ind'] + ee['hrs_sh']  )/ee['L'];
                    #%mi2=(ee['hr_d']-ee['hrs_d'])/ee['L'];
                    assert not type(ee['hr_d']) is list
                    #mi1_plugin = matdiv(ee['hr_d']-ee['hrs_d'], ee['L'])
                    #mi1_sh = matdiv(ee['hr_d']-ee['hrs_d']-ee['hrs_ind']+ee['hrs_sh'], ee['L'])
                    mi1_plugin = (ee['hr_d']-ee['hrs_d']) / ee['L']
                    mi1_sh = (ee['hr_d']-ee['hrs_d']-ee['hrs_ind']+ee['hrs_sh']) /  ee['L']
                else:
                    mi1_plugin = 0.
                    #%NaN;
                    mi1_sh = 0.
                    
                
                print mi1_plugin
                #%a2d1_th(ntj,li)=mi1;
                #a2d1_d[int(ntj)-1,int(li)-1] = mi1_plugin
                a2d1_d[ntj-1].append(mi1_plugin)
                assert len(a2d1_d[ntj-1])==li-1+1
                #a2d3_dsh[int(ntj)-1,int(li)-1] = mi1_sh
                a2d3_dsh[ntj-1].append( mi1_sh )
                assert len(a2d3_dsh[ntj-1])==li-1+1
                
                #%mi2=(ee['_xi']-ee['hrs_ind'])/ee['L'];
                #%mi2=(ee['_hqr']-ee['_hrs'] -ee['_hqrs'] + ee['hrs_q']  )/ee['L'];
                #%mi2=(ee['_xiq']-ee['hrs_q'] +ee['hrs_sh'] - ee['hrs_ind']  )/ee['L']; %DID NOT
                #%WORK
                #%mi2=(ee['_hqr']-ee['hrs_q'])/ee['L']; %+ee['hrs_sh'] - ee['hrs_ind']  )/ee['L'];
                #%slightly biased (uses hrs_q-sh)
                #% ************ CHECK THIS *************
                #mi2 = matdiv(ee['_xiq']-ee['_hqrs'], ee['L'])
                mi2 = (ee['_xiq']-ee['_hqrs']) /  ee['L']
                #%mi2=(ee['_hqr']-ee['_hqrs'])/ee['L'];
                #% ... %+ee['hrs_sh'] - ee['hrs_ind']  )/ee['L'];
                #%mi2=(ee['_xi']-ee['hrs_ind'])/ee['L'];
                #%mi2=(ee.hr1-ee.hrs1)/ee['L'];
                #%mi2=(ee['hr_d']-ee['hrs_d'])/ee['L'];
                #a2d2_qd[int(ntj)-1,int(li)-1] = mi2
                a2d2_qd[ntj-1].append( mi2 )
                assert len(a2d2_qd[ntj-1]) -1 == li-1
                #%mi3=(ee['_hqr']-ee['hrs_q'])/ee['L']; %+ee['hrs_sh'] - ee['hrs_ind']  )/ee['L'];
                #%mi3=(ee['_xiq']-ee['hrs_q'])/ee['L'];
                #nta1d[int(ntj)-1] = ee['nt']
                #nta1d.append( ee['nt'] )
                #print len(nta1d),ntj
                #print nta1d
                #assert len(nta1d)-1 == ntj-1
                #assert len(nta1d)-1 == ntj-1  ##COMPILE_TIME:CHECK_PATTERN: MATCH LAST_INDEX(nta1d) == ntj-1   #PLANGNOTE
                assert nta1d[ntj-1] == ee['nt']
                #%    end
                #%end
               
        if False:
            plt.figure(5.)
            plt.clf
            ax = np.array([])
            #%ax(1)=subplot(1,3,1:2);
            ax[0] = plt.subplot(2., 3., 1.)
            plt.hold(on)
            #%box on;
            lha = np.array([])
            #%h = plot(nta1d,a2d1_th/binlen_sec);
            h = plt.plot(nta1d, matdiv(a2d1_d, binlen_sec))
            #%if isempty(h)
            #%    h=plot(0,0,'w.');
            #%end
            set(h, 'linewidth', 2.)
            set(h, 'Displayname', 'L-direct')
            #%L-bin
            #%set(h,'Displayname','dithered'); %L-bin
            #%lha(1)=h(1);
            plt.ylabel('Information rate [bits/sec]')
            #% set(gca,'xscale','log');
            plt.title('direct plug-in')
            #%MARKOV
            ax[2] = plt.subplot(2., 3., 3.)
            h = plt.plot(nta1d, matdiv(a2d2_qd, binlen_sec), '-')
            #%if isempty(h)
            #%    h=plot(0,0,'w.');
            #%end
            #%plot(nta1d,a2d3,':');
            set(h, 'Displayname', 'q-Markov')
            #%L-bin
            #%set(h,'Displayname','L-direct');
            set(h, 'linewidth', 2.)
            #%lha(2)=h(1);
            #%  q=NaN;
            #%title('q=1');
            #%plot((ee['nt']),mi,'.');
            #%text((ee['nt']),mi, sprintf(' L=%d',ee['L']));
            #%   set(gca,'xscale','log');
            #%end
            #%  end
            #%hold on;
            #%plot(nta1d, ami_perbin + nta1d*0, 'k:');
            #%nta1d+100
            #%     nta1d_temp=myminmax(nta1d);  nta1d_temp(end)=nta1d(end)+100; nta1d_temp(1)=90;
            #%     h=plot(nta1d_temp, ami_bps + nta1d_temp*0, 'k-.');
            #%     set(h,'Displayname','analytical');
            #%lha(3)=h;
            plt.hold(on)
            #%    plot(nta1d, nta1d*0, 'k:');
            #%    xlabel('Trials');
            #%ylabel('Information [bits/bin]');
            #%xlim([10,(max(NTA)+100)*1.1]);
            #%xlim([min(NTA)-2,(max(NTA)+100)*1.1]);
            plt.title('q-Markov')
            lta=[]
            for li in np.arange(1., (len(LARR))+1):
                #%   text(nta1d(1),a2d1_d(1,li)/binlen_sec, sprintf(' L=%d',LARR(li)));
                lta[li]=' L=%d'%(LARR(li),);
                pass
 
            if False:
                lh = plt.legend(lta)
                set(lh, 'box', 'off')
                set(lh, 'Location', 'SouthWest')
                set(lh, 'FontSize', 7.)
            #% PANEL 2
            #%    h=legend(lha,'location','SouthEast'); set(h,'box','off');
            ax[1] = plt.subplot(2., 3., 2.)
            h = plt.plot(nta1d, matdiv(a2d3_dsh, binlen_sec), '-')
            #%if isempty(h)
            #%    h=plot(0,0,'w.');
            #%end
            set(h, 'Displayname', 'direct-sh')
            #%L-bin
            lha[2] = h[0]
            #% set(gca,'xscale','log');
            set(h, 'linewidth', 2.)
            plt.title('direct-sh')
            for pani in np.arange(1., 4.0):
                set(plt.gcf, 'CurrentAxes', ax[int(pani)-1])
                plt.hold(on)
                nta1d_temp = myminmax(nta1d)
                nta1d_temp[int(0)-1] = nta1d[int(0)-1]+100.
                nta1d_temp[0] = 20.
                #%90;
                #%h=plot(ax(pani),
                h = plt.plot(nta1d_temp, (ami_bps+nta1d_temp*0.), 'k--')
                set(h, 'Displayname', 'analytical')
                set(plt.gca, 'xscale', 'log')
                plt.plot(nta1d, (nta1d*0.), 'k:')
                plt.xlabel('Trials')
                set(plt.gca, 'xtick', np.unique(np.array(np.hstack(((2 ** np.array(np.hstack((5., 7., 9., np.log2(matcompat.max(NTA)))))))))))
                plt.ylim(np.array(np.hstack((-50., 100.))))
                plt.box(off)
                
            linkaxes(ax, 'y')
            linkaxes(ax, 'x')
            nta1d_temp = myminmax(nta1d)
            nta1d_temp[int(0)-1] = nta1d[int(0)-1]+1000.
            nta1d_temp[0] = nta1d_temp[0]-4.
            plt.xlim(nta1d_temp)
            #%a=[a2d3_dsh(:)/binlen_sec ; a2d1_d(:)/binlen_sec ;  a2d2_qd(:)/binlen_sec ];
            a = vertcat(matdiv(a2d3_dsh.flatten(1), binlen_sec), matdiv(a2d1_d.flatten(1), binlen_sec), matdiv(a2d2_qd.flatten(1), binlen_sec))
            #%ylim(myminmax(a)*1.5);
            #%ax(4)=
            plt.subplot(2., 3., 4.)
            plt.hold(on)
            WARR = np.dot(LARR, binlen_sec)*1000.
            #%bar(LARR,a2d2_qd(end,:) /binlen_sec);
            plt.plot(WARR, matdiv(a2d2_qd[int(0)-1,:], binlen_sec), 'ko-', 'displayname', 'q-Markov')
            plt.plot(WARR, matdiv(a2d1_d[int(0)-1,:], binlen_sec), 'r--', 'displayname', 'direct-sh')
            LA2 = np.array(np.hstack((WARR, WARR[int(0)-1]+1.)))
            h = plt.plot(LA2, (ami_bps+LA2*0.), 'k--')
            set(h, 'Displayname', 'analytical')
            #%xlabel('L');
            plt.xlabel('W [msec]')
            lh = plt.legend('show')
            set(lh, 'box', 'off')
            set(lh, 'Location', 'NorthEast')
            #%set(lh,'FontSize',7);
            #%linkaxes(ax, 'y')
            #%ylabel('Information rate [bits/sec]');
            plt.xlim(np.array(np.hstack((0.1, matcompat.max(LA2)+1.))))
            plt.ylabel('Information rate [bits/sec]')
            title_on_top2(sprintf('q=%d, DITHER, QE=%d, W=%g[ms]        \\tau_s,\\tau_n=%g,%g [ms]', q, methid, (1000.*binlen_sec), tau_s, tau_n), 0.)
            #%darbareye hichi be ghatiyat nemirese. va in kheili Pernicious, va
            #%PErvesaive, va daaem va hart dafe hastesh. bayad MM javab bekhad ta
            #%begam.
            #%save test_lor4???_all
            #% my_save_png('test_lor4f1---dither-v2',{},[7,5]);
        time.sleep(0.2)
        print("sleep")
        
    finishtime = time.time() #now()
    #toc=time.time()
    print('.')


    print estims_cell
    maxc = 0
    collect_keys={}
    for i in range(0,len(estims_cell)):
        for j in range(0,len(estims_cell[i])):
            for k in estims_cell[i][j]:
               collect_keys[k]=0
               if maxc < j+1:
                    maxc = j+1
    print collect_keys.keys()
    print
    ka={}
    for k in collect_keys:
       ka[k]=np.zeros((len(estims_cell),maxc))

    for i in range(0,len(estims_cell)):
        for j in range(0,len(estims_cell[i])):
            for k in collect_keys:
               ka[k][i,j]=estims_cell[i][j][k]
    for k in collect_keys:
        print k+":",
        print ka[k].T
    return

    dont_plot_this = False
    #%% - plots
    if dont_plot_this:
        DOUBLEPANEL = 0.
        plt.figure(6.)
        plt.clf
        if DOUBLEPANEL:
            plt.subplot(1., 2., 1.)
        
        
        #plt.hold(on)
        #%box on;
        styc = 'rgbmkckkk'
        for li in np.arange(1., (len(LARR))+1):
            lnt = np.array([])
            mi = np.array([])
            for ntj in np.arange(1., (nti)+1):
                ee = estims_cell[int(li)-1,int(ntj)-1]
                #%mi(ntj)=(ee['_hr']-ee['_hrs'])/ee['L'];
                #%mi(ntj)=(ee['_hr']-ee['_hrs'] -ee['hrs_ind'] + ee['hrs_sh']  )/ee['L'];
                #%mi(ntj)=(ee.hr1-ee.hrs1 -ee['hrs_ind'] + ee['hrs_sh']  )/ee['L'];
                mi[int(ntj)-1] = matdiv(ee['hr_d']-ee['hrs_d']-ee['hrs_ind']+ee['hrs_sh'], ee['L'])
                #%lnt(ntj)=log2(ee['nt']);
                lnt[int(ntj)-1] = matdiv(1., ee['nt'])
                plt.plot(lnt[int(ntj)-1], mi[int(ntj)-1], np.array(np.hstack((styc[int(li)-1], 'o'))))
                plt.text(lnt[int(ntj)-1], mi[int(ntj)-1], sprintf(' L=%d', (ee['L'])))
                
            h = plt.plot(lnt, mi, np.array(np.hstack((styc[int(li)-1], '--'))))
            set(h, 'LineWidth', 2.)
            h1[int(li)-1] = h
            
        #%set(gca,'xlim',[4,1+log2(max(NTA))]);
        ami_perbin = np.dot(ami_bps, binlen_sec)
        plt.plot(lnt, (ami_perbin+lnt*0.), 'k--')
        plt.xlabel('log_2(trials)')
        plt.ylabel('information (bits)')
        ylim = plt.get(plt.gca, 'ylim')
        #%set(gca,'ylim',[0,ylim(2)]);
        #%set(gca,'ylim',[0,0.5]);
        plt.title('direct method')
        #%  legend(h1, cell_sprintf('L=%d',LARR));
    
    
    #%%
    #%prs_dig(1,:)=[];
    #%%
    plt.figure(4.)
    plt.clf
    plt.plot(resp_cont[0,:])
    #plt.hold(on)
    plt.plot((resp_dig[0,:]-0.), 'r')
    plt.figure(10.)
    plt.clf
    mypcolor(prs_dig[0:,199:220.])
    plt.title('trunc')
    plt.figure(11.)
    plt.clf
    mypcolor(prs_dith[:,199:220.])
    plt.title('dith')
    plt.figure(12.)
    plt.clf
    plt.hist(np.array(np.hstack((prs_dith.flatten(1), prs_dig.flatten(1)))))
    #%%
    plt.figure(13.)
    plt.clf
    a = resp_cont[0,:]
    b = resp_dig[0,:]-0.
    c = r2_raw[0,:]
    plt.plot(a, (b+np.dot(plt.randn(matcompat.size(b)), 0.1)), 'k.')
    mm = myminmax((c.flatten(1)+np.dot(plt.randn(matcompat.size(a.flatten(1))), 0.01)))
    cx = np.arange(mm[0], (mm[1])+(np.diff(mm)/40.), np.diff(mm)/40.)
    h1 = histc(c.flatten(1), cx)
    h1 = matdiv(h1, np.sum(h1))
    #% *mean(diff(cx))
    plt.figure(14.)
    plt.clf
    plt.subplot(1., 2., 1.)
    plt.plot(c, (a+np.dot(plt.randn(matcompat.size(a)), 0.05)), 'k.', 'markersize', 1.)
    plt.hold(on)
    plt.plot(cx, (h1*20.+0.5), 'r')
    plt.subplot(1., 2., 2.)
    plt.plot(c, (b+np.dot(plt.randn(matcompat.size(a)), 0.05)), 'k.', 'markersize', 1.)
    plt.hold(on)
    plt.plot(cx, (h1*20.+0.5), 'r')
    for i in np.arange(1., 3.0):
        plt.subplot(1., 2., i)
        for x0 in np.array(np.hstack((-4., 4.))):
            plt.plot((x0+np.array(np.hstack((0., 0.)))), np.array(np.hstack((0., M))), 'b--', 'linewidth', 1.)
            
        
    #% my_save_png('test_lor4e2-v13-meth=1',{},[5,4]);
    #%%
    #%close all
    plt.figure(101.)
    plt.clf
    plt.plot(pr_dig, 'displayname', 'discrete')
    plt.hold(on)
    plt.plot(pr_dith, 'r.-', 'displayname', 'dith')
    np.array(np.hstack((np.sum(pr_dith), np.sum(pr_dig))))
    plt.legend(show)

    return estims_cell
Exemple #12
0
#!/usr/bin/python
# -*- coding: utf-8 -*-

from scipy.signal.ltisys import lti, lsim
from matplotlib.pylab import save, randn
from Numeric import sqrt, array, arange

n = 128
Q = 1.
R = 1.
w = 0.3 * sqrt(Q) * randn(n)
v = 0.2 * sqrt(R) * randn(n)
ureq = array([[-1.743] * n])

t = arange(0, 0.9999, 1. / 128)
#Generator().generateSin(n, 3, 33) #-0.37727

u = ureq + w

#A, B, C, D = [[-6.,-25.], [1.,0.]], [[1.],[0.]], [[0., 1.]], [[0.]]
#sys=lti(A, B, C, D)
#y = lsim(sys, u, t)
yv = u + v

##save('Q.txt', Q)
##save('R.txt', R)
save('w.txt', w)
save('v.txt', v)
save('yv.txt', yv)
save('u.txt', u)
save('ureq.txt', ureq)
Spart3 = Spart3.reshape(int(nt), 200)
Spart3 = np.transpose(Spart3)

S = Spart1 - Spart2 * Spart3

# In[3]:

#%%Further definitions
M = B + cg_tau
aw = a0 - a2 * x**2
kLf = k * Lf
#%%noise timeseries
sig_noise = sig / np.sqrt(dt)
print 'sig_noise'
print sig_noise
noise = sig_noise * plt.randn(1, int(dur * nt))
lp = 1. / 52.  #%1 week 'decorrelation' time
alpha = np.exp(-dt / lp)  # this alpha differs from the one below
nalpha = np.sqrt(1. - alpha**2)
N_red = noise * 0.
N_red[0] = noise[0]
for i in np.arange(1, (len(noise)) - 1):
    N_red[i] = alpha * N_red[i - 1] + nalpha * noise[i]
print 'N_red'
print N_red[0:10]
#%%Set up output arrays, saving 100 timesteps/year
E100 = np.zeros((int(n), int(dur * 100.)))
T100 = np.zeros((int(n), int(dur * 100.)))

# In[4]:
#% 16 = rand3 17 = rand9 18 = rand10 19 = 0.8 20 = sineroot27
#% 21 = sineroot19 22 = sineroot50 23 = sineroot75
#% 24 = sineroot10 25 = sineroot110 26 = sineroot75tenth
#% 27 = sineroots20plus40  28 = sineroot75third
#% 29 = sineroot243  30 = sineroot150  31 = sineroot200
#% 32 = sine10pt587352723 33 = sine10pt10.387352723
#% 34 = rand7  35 = sine12  36 = rand5  37 = sine11
#% 38 = sine10pt17352723  39 = sine5 40 = sine6
#% 41 = sine7 42 = sine8  43 = sine9 44 = sine12
#% 45 = sine13  46 = sine14  47 = sine10.8342522
#% 48 = sine11.8522311  49 = sine12.5223223  50 = sine13.1900453
#% 51 = sine7.1900453  52 = sine7.9004531  53 = sine8.4900453
#% 54 = sine9.1900453 55 = sine5.19004  56 = sine5.8045
#% 57 = sine6.49004 58 = sine6.9004 59 = sine13.9004
#%%% Initializations
plt.randn("state", randstate)
np.random.rand("twister", randstate)
I = np.eye(Netsize)
Np = length(patts)
#% Create raw weights
if newNets:
    if Netsize <= 20.0:
        Netconnectivity = 1.0
    else:
        Netconnectivity = 10.0 / Netsize

    WRaw = generate_internal_weights(Netsize, Netconnectivity)
    WinRaw = plt.randn(Netsize, 1.0)
    WbiasRaw = plt.randn(Netsize, 1.0)

 arm = P.arange(m).reshape(-1, 1)
 arsy_i = P.arange(seq_length + samps).reshape(1, -1)
 arsy_o = P.arange(samps).reshape(1, -1)
 arsx = P.arange(2 * seq_length + 1 + samps).reshape(1, -1)
 PERM = P.permutation(C * m) * stride + offset
 for c in range(C):
     # if ep==0 and c<C//2:
     #   continue
     print("On chunk {0}/{1}".format(c + 1, C))
     # I = c*m*stride + offset
     x_c = P.vstack([
         x[PERM[c * m + fn]:(PERM[c * m + fn] +
                             (2 * seq_length + 1 + samps))]
         for fn in range(m)
     ])
     x_c = x_c + P.randn(*x_c.shape) * vx * iSNR  # add light noise
     x_c[x_c > XMAX] = XMAX
     x_c[x_c < -XMAX] = -XMAX
     x_inds = mu_law(x_c, mu, XMAX)
     x_c = P.zeros((m, 2 * seq_length + 1 + samps, mu))
     x_c[arm, arsx, x_inds] = True
     y_c_i = P.vstack([
         y[PERM[c * m + fn]:(PERM[c * m + fn] + seq_length + samps)]
         for fn in range(m)
     ])
     y_c_i = y_c_i + P.randn(*y_c_i.shape) * vy * iSNR  # add light noise
     y_c_i[y_c_i > YMAX] = YMAX
     y_c_i[y_c_i < -YMAX] = -YMAX
     y_inds = mu_law(y_c_i, mu, YMAX)
     y_c_i = P.zeros((m, seq_length + samps, mu))
     y_c_i[arm, arsy_i, y_inds] = True