Guest User

Untitled

a guest
Nov 22nd, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. import numpy as np
  2. import brian
  3. import brian.hears as hears
  4.  
  5. def irn(dur, f, g, n, method='s'):
  6. """Generate IRN with a duration dur in seconds, a pitch at f (either a
  7. single value for a flat pitch, or an iterable for a contour) in Hz, a gain
  8. of g (-1 <= g <= 1) and n iterations. Since this is done in the time
  9. domain, processing time gets longer with larger n."""
  10. x = hears.whitenoise(dur * brian.second) #input signal
  11. y = np.ravel(x)
  12. y0 = np.ravel(x) #for irno
  13. if hasattr(f, '__iter__'):
  14. f = np.array(f)
  15. delay = np.array(np.round(1/f * x.samplerate), dtype=int)
  16. if len(delay) != len(y):
  17. raise Exception('len(f) should be %i, not %i' %(len(y),len(delay)))
  18. else:
  19. delay = [int(1/float(f) * float(x.samplerate))] * len(y)
  20. for i in xrange(n):
  21. if method == 's':
  22. y = [y[t] + g*y[t-delay[t]] for t in xrange(len(y))]
  23. elif method == 'o':
  24. y = [y0[t] + g*y[t-delay[t]] for t in xrange(len(y))]
  25. else:
  26. raise Exception("IRN method not understood: should be 's' or 'o'")
  27. return hears.Sound(np.array(y) / np.max(y))
  28.  
  29. def main():
  30. dur, f, g, N = 1, 200, 1, [0, 4, 8, 16, 32, 64]
  31. for n in N:
  32. if not n:
  33. sound = irn(dur, f, g, n, 's')
  34. else:
  35. sound = hears.sequence(sound, irn(dur, f, g, n, 's'))
  36. sound.play(sleep=True)
  37. sound.save('IRN_increasing_n.wav', normalise=True)
  38. dur, f = 2, np.linspace(100, 400, 88200)
  39. sound = irn(dur, f, g, n, 's')
  40. sound.play(sleep=True)
  41. sound.save('IRN_increasing_f.wav', normalise=True)
  42.  
  43. if __name__ == '__main__':
  44. main()
Add Comment
Please, Sign In to add comment