| | 24 | |
| | 25 | |
| | 26 | def white_noise(dt, duration): |
| | 27 | n = int(duration/dt) |
| | 28 | noise = randn(n) |
| | 29 | return noise |
| | 30 | |
| | 31 | def colored_noise(tau, dt, duration): |
| | 32 | noise = white_noise(dt, duration) |
| | 33 | a = [1., -exp(-dt/tau)] |
| | 34 | b = [1.] |
| | 35 | fnoise = sqrt(2*dt/tau)*lfilter(b, a, noise) |
| | 36 | return fnoise |
| | 37 | |
| | 38 | if __name__ == '__main__': |
| | 39 | tau = 10*ms |
| | 40 | dt = .1*ms |
| | 41 | duration = 1000*ms |
| | 42 | noise = white_noise(dt, duration) |
| | 43 | cnoise = colored_noise(tau, dt, duration) |
| | 44 | from numpy import linspace |
| | 45 | t = linspace(0*ms, duration, len(noise)) |
| | 46 | |
| | 47 | from pylab import acorr, show, subplot, plot, ylabel, xlim |
| | 48 | |
| | 49 | subplot(221) |
| | 50 | plot(t, noise) |
| | 51 | ylabel('white noise') |
| | 52 | |
| | 53 | subplot(222) |
| | 54 | acorr(noise, maxlags=200) |
| | 55 | xlim(-200,200) |
| | 56 | |
| | 57 | subplot(223) |
| | 58 | plot(t, cnoise) |
| | 59 | ylabel('colored noise') |
| | 60 | |
| | 61 | subplot(224) |
| | 62 | acorr(cnoise, maxlags=200) |
| | 63 | xlim(-200,200) |
| | 64 | |
| | 65 | show() |