def lambda_interp(lambdau, s):
# lambda is the index sequence that is produced by the model
# s is the new vector at which evaluations are required.
# the value is a vector of left and right indices, and a vector of fractions.
# the new values are interpolated bewteen the two using the fraction
# Note: lambda decreases. you take:
# sfrac*left+(1-sfrac*right)
if len(lambdau) == 1:
nums = len(s)
left = scipy.zeros([nums, 1], dtype = scipy.integer)
right = left
sfrac = scipy.zeros([nums, 1], dtype = scipy.float64)
else:
s[s > scipy.amax(lambdau)] = scipy.amax(lambdau)
s[s < scipy.amin(lambdau)] = scipy.amin(lambdau)
k = len(lambdau)
sfrac = (lambdau[0] - s)/(lambdau[0] - lambdau[k - 1])
lambdau = (lambdau[0] - lambdau)/(lambdau[0] - lambdau[k - 1])
coord = scipy.interpolate.interp1d(lambdau, range(k))(sfrac)
left = scipy.floor(coord).astype(scipy.integer, copy = False)
right = scipy.ceil(coord).astype(scipy.integer, copy = False)
#
tf = left != right
sfrac[tf] = (sfrac[tf] - lambdau[right[tf]])/(lambdau[left[tf]] - lambdau[right[tf]])
sfrac[~tf] = 1.0
#if left != right:
# sfrac = (sfrac - lambdau[right])/(lambdau[left] - lambdau[right])
#else:
# sfrac[left == right] = 1.0
result = dict()
result['left'] = left
result['right'] = right
result['frac'] = sfrac
return(result)
# end of lambda_interp
# =========================================
python类ceil()的实例源码
poisson_disk_sampling.py 文件源码
项目:nodebox1-generative-tools
作者: x-raizor
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
def __init__( self, w, h, r, n ):
# w and h are the width and height of the field
self.w = w
self.h = h
# n is the number of test points
self.n = n
self.r2 = r**2.0
self.A = 3.0*self.r2
# cs is the cell size
self.cs = r / scipy.sqrt(2)
# gw and gh are the number of grid cells
self.gw = int( scipy.ceil( self.w/self.cs ) )
self.gh = int( scipy.ceil( self.h/self.cs ) )
# create a grid and a queue
self.grid = [ None ] * self.gw * self.gh
self.queue = list()
# set the queue size and sample size to zero
self.qs, self.ss = 0, 0
def lambda_interp(lambdau, s):
# lambda is the index sequence that is produced by the model
# s is the new vector at which evaluations are required.
# the value is a vector of left and right indices, and a vector of fractions.
# the new values are interpolated bewteen the two using the fraction
# Note: lambda decreases. you take:
# sfrac*left+(1-sfrac*right)
if len(lambdau) == 1:
nums = len(s)
left = scipy.zeros([nums, 1], dtype = scipy.integer)
right = left
sfrac = scipy.zeros([nums, 1], dtype = scipy.float64)
else:
s[s > scipy.amax(lambdau)] = scipy.amax(lambdau)
s[s < scipy.amin(lambdau)] = scipy.amin(lambdau)
k = len(lambdau)
sfrac = (lambdau[0] - s)/(lambdau[0] - lambdau[k - 1])
lambdau = (lambdau[0] - lambdau)/(lambdau[0] - lambdau[k - 1])
coord = scipy.interpolate.interp1d(lambdau, range(k))(sfrac)
left = scipy.floor(coord).astype(scipy.integer, copy = False)
right = scipy.ceil(coord).astype(scipy.integer, copy = False)
#
tf = left != right
sfrac[tf] = (sfrac[tf] - lambdau[right[tf]])/(lambdau[left[tf]] - lambdau[right[tf]])
sfrac[~tf] = 1.0
#if left != right:
# sfrac = (sfrac - lambdau[right])/(lambdau[left] - lambdau[right])
#else:
# sfrac[left == right] = 1.0
result = dict()
result['left'] = left
result['right'] = right
result['frac'] = sfrac
return(result)
# end of lambda_interp
# =========================================
def lambda_interp(lambdau, s):
# lambda is the index sequence that is produced by the model
# s is the new vector at which evaluations are required.
# the value is a vector of left and right indices, and a vector of fractions.
# the new values are interpolated bewteen the two using the fraction
# Note: lambda decreases. you take:
# sfrac*left+(1-sfrac*right)
if len(lambdau) == 1:
nums = len(s)
left = scipy.zeros([nums, 1], dtype = scipy.integer)
right = left
sfrac = scipy.zeros([nums, 1], dtype = scipy.float64)
else:
s[s > scipy.amax(lambdau)] = scipy.amax(lambdau)
s[s < scipy.amin(lambdau)] = scipy.amin(lambdau)
k = len(lambdau)
sfrac = (lambdau[0] - s)/(lambdau[0] - lambdau[k - 1])
lambdau = (lambdau[0] - lambdau)/(lambdau[0] - lambdau[k - 1])
coord = scipy.interpolate.interp1d(lambdau, range(k))(sfrac)
left = scipy.floor(coord).astype(scipy.integer, copy = False)
right = scipy.ceil(coord).astype(scipy.integer, copy = False)
#
tf = left != right
sfrac[tf] = (sfrac[tf] - lambdau[right[tf]])/(lambdau[left[tf]] - lambdau[right[tf]])
sfrac[~tf] = 1.0
#if left != right:
# sfrac = (sfrac - lambdau[right])/(lambdau[left] - lambdau[right])
#else:
# sfrac[left == right] = 1.0
result = dict()
result['left'] = left
result['right'] = right
result['frac'] = sfrac
return(result)
# end of lambda_interp
# =========================================
def lambda_interp(lambdau, s):
# lambda is the index sequence that is produced by the model
# s is the new vector at which evaluations are required.
# the value is a vector of left and right indices, and a vector of fractions.
# the new values are interpolated bewteen the two using the fraction
# Note: lambda decreases. you take:
# sfrac*left+(1-sfrac*right)
if len(lambdau) == 1:
nums = len(s)
left = scipy.zeros([nums, 1], dtype = scipy.integer)
right = left
sfrac = scipy.zeros([nums, 1], dtype = scipy.float64)
else:
s[s > scipy.amax(lambdau)] = scipy.amax(lambdau)
s[s < scipy.amin(lambdau)] = scipy.amin(lambdau)
k = len(lambdau)
sfrac = (lambdau[0] - s)/(lambdau[0] - lambdau[k - 1])
lambdau = (lambdau[0] - lambdau)/(lambdau[0] - lambdau[k - 1])
coord = scipy.interpolate.interp1d(lambdau, range(k))(sfrac)
left = scipy.floor(coord).astype(scipy.integer, copy = False)
right = scipy.ceil(coord).astype(scipy.integer, copy = False)
#
tf = left != right
sfrac[tf] = (sfrac[tf] - lambdau[right[tf]])/(lambdau[left[tf]] - lambdau[right[tf]])
sfrac[~tf] = 1.0
#if left != right:
# sfrac = (sfrac - lambdau[right])/(lambdau[left] - lambdau[right])
#else:
# sfrac[left == right] = 1.0
result = dict()
result['left'] = left
result['right'] = right
result['frac'] = sfrac
return(result)
# end of lambda_interp
# =========================================
def kde(data, N=None, MIN=None, MAX=None):
# Parameters to set up the mesh on which to calculate
N = 2**14 if N is None else int(2**sci.ceil(sci.log2(N)))
if MIN is None or MAX is None:
minimum = min(data)
maximum = max(data)
Range = maximum - minimum
MIN = minimum - Range/10 if MIN is None else MIN
MAX = maximum + Range/10 if MAX is None else MAX
# Range of the data
R = MAX-MIN
# Histogram the data to get a crude first approximation of the density
M = len(data)
DataHist, bins = sci.histogram(data, bins=N, range=(MIN,MAX))
DataHist = DataHist/M
DCTData = scipy.fftpack.dct(DataHist, norm=None)
I = [iN*iN for iN in xrange(1, N)]
SqDCTData = (DCTData[1:]/2)**2
# The fixed point calculation finds the bandwidth = t_star
guess = 0.1
try:
t_star = scipy.optimize.brentq(fixed_point, 0, guess,
args=(M, I, SqDCTData))
except ValueError:
print 'Oops!'
return None
# Smooth the DCTransformed data using t_star
SmDCTData = DCTData*sci.exp(-sci.arange(N)**2*sci.pi**2*t_star/2)
# Inverse DCT to get density
density = scipy.fftpack.idct(SmDCTData, norm=None)*N/R
mesh = [(bins[i]+bins[i+1])/2 for i in xrange(N)]
bandwidth = sci.sqrt(t_star)*R
density = density/sci.trapz(density, mesh)
return bandwidth, mesh, density
def minmax(arr):
minv = arr[0]
maxv = arr[0]
for val in arr:
if val > maxv:
maxv = val
elif val < minv:
minv = val
return int(sp.floor(minv)), int(sp.ceil(maxv))
def ellipse2bbox(a, b, angle, cx, cy):
a, b = max(a, b), min(a, b)
ca = sp.cos(angle)
sa = sp.sin(angle)
if sa == 0.0:
cta = 2.0 / sp.pi
else:
cta = ca / sa
if ca == 0.0:
ta = sp.pi / 2.0
else:
ta = sa / ca
x = lambda t: cx + a * sp.cos(t) * ca - b * sp.sin(t) * sa
y = lambda t: cy + b * sp.sin(t) * ca + a * sp.cos(t) * sa
# x = cx + a * cos(t) * cos(angle) - b * sin(t) * sin(angle)
# tan(t) = -b * tan(angle) / a
tx1 = sp.arctan(-b * ta / a)
tx2 = tx1 - sp.pi
x1, y1 = x(tx1), y(tx1)
x2, y2 = x(tx2), y(tx2)
# y = cy + b * sin(t) * cos(angle) + a * cos(t) * sin(angle)
# tan(t) = b * cot(angle) / a
ty1 = sp.arctan(b * cta / a)
ty2 = ty1 - sp.pi
x3, y3 = x(ty1), y(ty1)
x4, y4 = x(ty2), y(ty2)
minx, maxx = Util.minmax([x1, x2, x3, x4])
miny, maxy = Util.minmax([y1, y2, y3, y4])
return sp.floor(minx), sp.floor(miny), sp.ceil(maxx), sp.ceil(maxy)
def stft_mc(x,N=1024,hop=None,window='hann'):
# N=1024
if hop is None:
hop=N/2
S=x.shape
if len(S)==1:
nch=1
nsampl=len(x)
x=np.reshape(x,(1,nsampl))
else:
nch=S[0]
nsampl=S[1]
xdtype=x.dtype
nfram=int(scipy.ceil(float(nsampl)/float(hop)))
npad=int(nfram)*hop-nsampl
pad=np.zeros((nch,npad)).astype(xdtype)
x=np.concatenate((x,pad),axis=1)
#pad the edges to avoid window taper effects
pad=np.zeros((nch,N)).astype(xdtype)
x=np.concatenate((pad,x,pad),axis=1)
for ich in range(0,nch):
x0=x[ich,:]
if not x0.flags.c_contiguous:
x0=x0.copy(order='C')
X0=librosa.core.stft(x0,n_fft=N,hop_length=hop,window=window,center=False,dtype=np.complex64)
if ich==0:
X=np.zeros((N/2+1,X0.shape[1],nch)).astype(np.complex64)
X[:,:,0]=X0
else:
X[:,:,ich]=X0
return X