Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import brian
- import brian.hears as hears
- def irn(dur, f, g, n, method='s'):
- """Generate IRN with a duration dur in seconds, a pitch at f (either a
- single value for a flat pitch, or an iterable for a contour) in Hz, a gain
- of g (-1 <= g <= 1) and n iterations. Since this is done in the time
- domain, processing time gets longer with larger n."""
- x = hears.whitenoise(dur * brian.second) #input signal
- y = np.ravel(x)
- y0 = np.ravel(x) #for irno
- if hasattr(f, '__iter__'):
- f = np.array(f)
- delay = np.array(np.round(1/f * x.samplerate), dtype=int)
- if len(delay) != len(y):
- raise Exception('len(f) should be %i, not %i' %(len(y),len(delay)))
- else:
- delay = [int(1/float(f) * float(x.samplerate))] * len(y)
- for i in xrange(n):
- if method == 's':
- y = [y[t] + g*y[t-delay[t]] for t in xrange(len(y))]
- elif method == 'o':
- y = [y0[t] + g*y[t-delay[t]] for t in xrange(len(y))]
- else:
- raise Exception("IRN method not understood: should be 's' or 'o'")
- return hears.Sound(np.array(y) / np.max(y))
- def main():
- dur, f, g, N = 1, 200, 1, [0, 4, 8, 16, 32, 64]
- for n in N:
- if not n:
- sound = irn(dur, f, g, n, 's')
- else:
- sound = hears.sequence(sound, irn(dur, f, g, n, 's'))
- sound.play(sleep=True)
- sound.save('IRN_increasing_n.wav', normalise=True)
- dur, f = 2, np.linspace(100, 400, 88200)
- sound = irn(dur, f, g, n, 's')
- sound.play(sleep=True)
- sound.save('IRN_increasing_f.wav', normalise=True)
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment