Commits

Jason S  committed 09910d1

update with different approach

  • Participants
  • Parent commits 447e173

Comments (0)

Files changed (1)

File sigmadeltatest.py

   x2 = (2*(x-0.5) for x in xit)
   feedback = Deferred()
   (feedback0, feedback1) = itertools.tee(buffer(feedback))
-  i1 = integrator(scale(subtract(x2,feedback0), k), i1_0, immediate=True, limits=[-1,1])
+  i1 = integrator(scale(subtract(x2,feedback0), k), i1_0, limits=[-1,1])
   (i1, i1out) = itertools.tee(i1)
-  i2 = integrator(subtract(i1,feedback1), i2_0, immediate=True)
+  i2 = integrator(subtract(i1,feedback1), i2_0)
   (i2, i2out) = itertools.tee(i2)
-  (y,yfb) = itertools.tee(comparator(i2,0))
-  feedback.set(unitdelay(switch(yfb,-1,1)))
+  (y,yfb) = itertools.tee(unitdelay(comparator(i2,0)))
+  feedback.set(switch(yfb,-1,1))
   return (y, i1out, i2out)
 
+def sigmadeltamod1a(xit, u0=0):
+  feedback = Deferred()
+  fwd = add(xit, unitdelay(feedback,u0))
+  (fwd0, fwd1) = itertools.tee(fwd)
+  out = comparator(fwd0)
+  feedback_in = (x-1 if x > 0 else x for x in fwd1) 
+  (fbin1, fbin2) = itertools.tee(feedback_in)
+  feedback.set(fbin1)
+  return (out, fbin2)
+
+
+def sigmadeltamod2a(xit, u_0=0, u2_0=0):
+  feedback = Deferred()
+  u = unitdelay(feedback, u_0)
+  (u1,u2) = itertools.tee(u)
+  xcancel = subtract(scale(u1,2.0),unitdelay(u2,u2_0))
+  (xcancel1, xcancel2) = itertools.tee(xcancel)
+  fwd = add(xit, xcancel1)
+  (fwd0, fwd1) = itertools.tee(fwd)
+  out = comparator(fwd0)
+  feedback_in = (x-1 if x > 0 else x for x in fwd1) 
+  (fbin1, fbin2) = itertools.tee(feedback_in)
+  feedback.set(fbin1)
+  return (out, fbin2, xcancel2)
+
+
 def atruncater(n):
   def f(x):
     return numpy.array(list(truncate(x,n)))
   return f
 
-def filter1pk(x,alpha,k):
+def filter1pk(x,alpha,k,x0=0):
   y = x
   for i in range(k):
-    y = filter1p(y,alpha)
+    y = filter1p(y,alpha,x0)
   return y
+  
+def repdelay(xit,xinit):
+  for xi in xinit:
+    yield xi
+  for x in xit:
+    yield x
+  
 
-dt = 100e-6
-t = numpy.arange(0,1,dt)
-Tchop = 1.0/11
+dt = 50e-6
+tend = 2.0
+t0 = 0.1
+t1 = 0.2
+i0 = int(t0/dt)
+t = numpy.arange(0,tend,dt)
+Tchop = tend/21
 n = len(t)
-x = ((t*(numpy.mod(t/Tchop,2)<1))-0.5)*0.9+0.5
+envelopetau = 0.7
+envelope=1-numpy.exp(numpy.minimum(-0.000001,-(t-t1)/envelopetau))
+x = envelope*((numpy.mod(t/Tchop,2)<1)-0.5)*0.999+0.5
 
-(d,di1) = sigmadeltamod1(x)
-k2 = 0.1
-(d2,di2a,di2b) = sigmadeltamod2(x,k2)
+filtpoles=3
+(d,di1) = sigmadeltamod1a(x)
+nk=16
+k2 = 1.0/nk
+k2p = 1-numpy.exp(-k2)
+nk = int(1/k2p)
+(d2,di2a,di2b) = sigmadeltamod2a(x,k2)
 snip = atruncater(n) 
-tau = 0.005
+tau = dt*100
 alpha = dt/tau
 #xfilt1 = snip(filter1pk(x),alpha,2))
-xfilt1 = snip(filter1pk(x,alpha,2))
+xfilt1 = snip(filter1pk(x,alpha,filtpoles,x[0]))
 di1 = snip(di1)
-y1 = snip(filter1pk(d,alpha,2))
+y1 = snip(filter1pk(d,alpha,filtpoles,x[0]))
 
-xfilt2 = snip(filter1pk(x,alpha,2))
+xfilt2 = snip(filter1pk(x,alpha,filtpoles,x[0]))
 di2a = snip(di2a)
 di2b = snip(di2b)
-y2 = snip(filter1pk(d2,alpha,2))
+y2 = snip(filter1pk(d2,alpha,filtpoles,x[0]))
 
-for (i, data) in enumerate([[xfilt1,y1,di1],[xfilt2,y2,di2a,di2b]]):
+for (i, data) in enumerate([[xfilt1,y1],[xfilt2,y2]]):
   xfilt = data[0]
   y = data[1]
   n = len(data)
   fig = plt.figure(i)
   ax=fig.add_subplot(n,1,1)
   ax.plot(t,x,t,xfilt,t,y)
+  ax.set_xlim(t0,tend)
   for j in range(n-2):
     ax=fig.add_subplot(n,1,j+2)
     ax.plot(t,data[j+2])
+    ax.set_xlim(t0,tend)
   ax=fig.add_subplot(n,1,n)
-  ax.plot(t,xfilt-y)
-  break
+  err = xfilt-y
+  ax.plot(t[i0:],err[i0:])
+  ax.set_xlim(t0,tend)
 plt.show()