此代码应生成与小插图相似的图:
def ASD_envelope( nSamps, tAttack, tRelease, susPlateau, kA, kS, kD ):
# number of samples for each stage
sA = int( nSamps * tAttack )
sD = int( nSamps * (1.-tRelease) )
sS = nSamps - sA - sD
# 0 to 1 over N samples, weighted with w
def weighted_exp( N, w ):
t = np.linspace( 0, 1, N )
E = np.exp( w * t ) - 1
E /= max(E)
return E
A = weighted_exp( sA, kA )
S = weighted_exp( sS, kS )
D = weighted_exp( sD, kD )
A = A[::-1]
A = 1.-A
S = S[::-1]
S *= 1-susPlateau
S += susPlateau
D = D[::-1]
D *= susPlateau
env = np.concatenate( [A,S,D] )
# plot
tEnv = np.linspace( 0, nSamps, len(env) )
plt.plot( tEnv, env )
plt.savefig( "OUT/EnvASD.png" )
plt.close()
return env
我很感激任何改进,有一件事可能是一个好主意,那就是允许最后三个参数(确定三个阶段中每个阶段的陡度)在0和1之间变化,其中0.5是一条直线。但我看不到如何做到这一点。
另外,我还没有彻底测试所有用例,例如,如果一个阶段的长度为零。