Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. def triangle(length, amplitude):
  2. section = length // 4
  3. for direction in (1, -1):
  4. for i in range(section):
  5. yield i * (amplitude / section) * direction
  6. for i in range(section):
  7. yield (amplitude - (i * (amplitude / section))) * direction
  8.  
  9. >>> list(triangle(100, 0.5))
  10. [0.0, 0.02, 0.04, 0.06, 0.08, 0.1, 0.12, 0.14, 0.16, 0.18, 0.2, 0.22, 0.24, 0.26, 0.28, 0.3, 0.32, 0.34, 0.36, 0.38, 0.4, 0.42, 0.44, 0.46, 0.48, 0.5, 0.48, 0.46, 0.44, 0.42, 0.4, 0.38, 0.36, 0.33999999999999997, 0.32, 0.3, 0.28, 0.26, 0.24, 0.21999999999999997, 0.2, 0.18, 0.15999999999999998, 0.14, 0.12, 0.09999999999999998, 0.08000000000000002, 0.06, 0.03999999999999998, 0.020000000000000018, -0.0, -0.02, -0.04, -0.06, -0.08, -0.1, -0.12, -0.14, -0.16, -0.18, -0.2, -0.22, -0.24, -0.26, -0.28, -0.3, -0.32, -0.34, -0.36, -0.38, -0.4, -0.42, -0.44, -0.46, -0.48, -0.5, -0.48, -0.46, -0.44, -0.42, -0.4, -0.38, -0.36, -0.33999999999999997, -0.32, -0.3, -0.28, -0.26, -0.24, -0.21999999999999997, -0.2, -0.18, -0.15999999999999998, -0.14, -0.12, -0.09999999999999998, -0.08000000000000002, -0.06, -0.03999999999999998, -0.020000000000000018]
  11.  
  12. def triangle2(length, amplitude):
  13. section = length // 4
  14. x = np.linspace(0, amplitude, section+1)
  15. mx = -x
  16. return np.r_[x, x[-2::-1], mx[1:], mx[-2:0:-1]]
  17.  
  18. import numpy
  19.  
  20. def trigen(n, amp):
  21. y = 0
  22. x = 0
  23. s = amp / (n/4)
  24. while x < n:
  25. yield y
  26. y += s
  27. if abs(y) > amp:
  28. s *= -1
  29. x += 1
  30.  
  31. a = numpy.fromiter(trigen(100, 0.5), "d")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement