Changeset 2260 for trunk/brian/library/random_processes.py

Show
Ignore:
Timestamp:
11/29/10 13:04:18 (2 years ago)
Message:

added white_noise and colored_noise functions in brian.library.random_processes

Files:
1 modified

Unmodified
Removed
• trunk/brian/library/random_processes.py

r2020 r2260
44from brian.equations import Equations
55from brian.units import get_unit
6from scipy.signal import lfilter
7from numpy import exp, sqrt
8from numpy.random import randn
9from brian.stdunits import ms
10
11__all__ = ['OrnsteinUhlenbeck', 'white_noise', 'colored_noise']
612
713def OrnsteinUhlenbeck(x, mu, sigma, tau):

1622    return Equations('dx/dt=(mu-x)*invtau+sigma*((2.*invtau)**.5)*xi : unit', \
1723                     x=x, mu=mu, sigma=sigma, invtau=1. / tau, unit=get_unit(mu))
24
25
26def white_noise(dt, duration):
27    n = int(duration/dt)
28    noise = randn(n)
29    return noise
30
31def 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
38if __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()