Skip to content

Commit a22c6b8

Browse files
committed
new noises, update of steps
1 parent 959d539 commit a22c6b8

File tree

10 files changed

+216
-49
lines changed

10 files changed

+216
-49
lines changed

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def readme():
1111
setup(
1212
name = 'signalz',
1313
packages = find_packages(exclude=("tests",)),
14-
version = '0.3',
14+
version = '0.4',
1515
description = 'Data generators in Python',
1616
long_description = readme(),
1717
author = 'Matous Cejnek',
@@ -20,7 +20,7 @@ def readme():
2020
license = 'MIT',
2121
url = 'http://matousc89.github.io/signalz/',
2222
download_url = 'https://github.com/matousc89/signalz/',
23-
keywords = ['signals', 'data', 'time-series', 'generators', 'models'],
23+
keywords = ['signals', 'data', 'time-series', 'generators', 'models', 'noise'],
2424
install_requires=[
2525
'numpy',
2626
],

signalz/__init__.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
.. cssclass:: functag
3636
3737
:ref:`tags-goniometric`
38+
3839
3940
.. cssclass:: funcitem
4041
@@ -43,6 +44,7 @@
4344
.. cssclass:: functag
4445
4546
:ref:`tags-goniometric`
47+
4648
4749
.. cssclass:: funcitem
4850
@@ -51,6 +53,7 @@
5153
.. cssclass:: functag
5254
5355
:ref:`tags-steps`
56+
5457
5558
.. cssclass:: funcitem
5659
@@ -59,6 +62,7 @@
5962
.. cssclass:: functag
6063
6164
:ref:`tags-steps`, :ref:`tags-random`
65+
6266
6367
.. cssclass:: funcitem
6468
@@ -67,14 +71,34 @@
6771
.. cssclass:: functag
6872
6973
:ref:`tags-chaotic`
74+
7075
7176
.. cssclass:: funcitem
7277
73-
* :ref:`generators-white_noise`
78+
* :ref:`generators-gaussian_white_noise`
7479
7580
.. cssclass:: functag
7681
77-
:ref:`tags-random`
82+
:ref:`tags-random`, :ref:`tags-noise`
83+
84+
85+
.. cssclass:: funcitem
86+
87+
* :ref:`generators-uniform_white_noise`
88+
89+
.. cssclass:: functag
90+
91+
:ref:`tags-random`, :ref:`tags-noise`
92+
93+
94+
.. cssclass:: funcitem
95+
96+
* :ref:`generators-brownian_noise`
97+
98+
.. cssclass:: functag
99+
100+
:ref:`tags-random`, :ref:`tags-noise`
101+
78102
79103
.. cssclass:: funcitem
80104
@@ -83,6 +107,7 @@
83107
.. cssclass:: functag
84108
85109
:ref:`tags-autoregressive`
110+
86111
87112
.. cssclass:: funcitem
88113

signalz/generators/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from signalz.generators.autoregressive_model import autoregressive_model
2-
from signalz.generators.white_noise import white_noise
2+
from signalz.generators.gaussian_white_noise import gaussian_white_noise
3+
from signalz.generators.brownian_noise import brownian_noise
4+
from signalz.generators.uniform_white_noise import uniform_white_noise
35
from signalz.generators.mackey_glass import mackey_glass
46
from signalz.generators.sinus import sinus
57
from signalz.generators.cosinus import cosinus

signalz/generators/autoregressive_model.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
======================================
6363
"""
6464
import numpy as np
65-
from signalz.generators.white_noise import white_noise
65+
from signalz.generators.gaussian_white_noise import gaussian_white_noise
6666

6767
import signalz
6868

@@ -105,7 +105,7 @@ def autoregressive_model(n, a, const=0, noise="white", initials="none"):
105105
if noise == "none":
106106
noise = np.zeros(n)
107107
elif noise == "white":
108-
noise = white_noise(n)
108+
noise = gaussian_white_noise(n)
109109
else:
110110
try:
111111
noise = np.array(noise)
@@ -116,7 +116,7 @@ def autoregressive_model(n, a, const=0, noise="white", initials="none"):
116116
x = np.zeros(n)
117117
# handle initials
118118
if initials == "random":
119-
x[:taps] = white_noise(taps)
119+
x[:taps] = gaussian_white_noise(taps)
120120
elif initials == "none":
121121
pass
122122
else:

signalz/generators/brownian_noise.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""
2+
.. versionadded:: 0.4
3+
4+
This function generates Brownian noise series. The noise is produced by
5+
integration of white noise (gaussian or uniform).
6+
7+
This function uses
8+
`numpy.random.normal` and `numpy.random.uniform`
9+
10+
Example Usage
11+
==================
12+
13+
The following example produce 1000 samples of brownian noise starting in
14+
value 10 (`start=10`). The noise is produced by integration of white gaussian
15+
noise (`source="gaussian"`) with standard deviation of 1 (`std=1`).
16+
To keep in noise in some reasonable range,
17+
it is used 10% leaky integration (`leak=0.1`).
18+
19+
.. code-block:: python
20+
21+
import signalz
22+
x = signalz.brownian_noise(1000, leak=0.1, start=10, std=1, source="gaussian")
23+
24+
Function Documentation
25+
======================================
26+
"""
27+
import numpy as np
28+
29+
from signalz.misc import check_type_or_raise
30+
from signalz.generators.gaussian_white_noise import gaussian_white_noise
31+
from signalz.generators.uniform_white_noise import uniform_white_noise
32+
33+
def brownian_noise(n, leak=0., start=0, std=1., source="gaussian"):
34+
"""
35+
Random values with uniform distribution.
36+
37+
**Args:**
38+
39+
* `n` - length of the output data (int) - how many samples will be on output
40+
41+
**Kwargs:**
42+
43+
* `leak` - leakage during integration (float), this should prevent signal
44+
from wander off. Possible value is `0 <= leak < 1`
45+
46+
* `start` - offset on the start (float)
47+
48+
* `std` - standard deviation of source white noise (float), in case of
49+
uniform distribution it is the difference between min and max value.
50+
51+
* `source` - distribution of source white noise (str). Options are
52+
`gaussian` or `uniform`
53+
54+
**Returns:**
55+
56+
* vector of values representing the noise (1d array)
57+
"""
58+
# check inputs
59+
check_type_or_raise(n, int, "n")
60+
check_type_or_raise(leak, float, "leak")
61+
if not 0. <= leak < 1:
62+
raise ValueError("Leak must be between 0. and 1.")
63+
# generate white noise
64+
if source == "gaussian":
65+
x = np.random.normal(0, std, n)
66+
elif source == "uniform":
67+
x = np.random.uniform(-std/2., std/2., n)
68+
else:
69+
raise ValueError("Source must be gaussian or uniform")
70+
# add offset
71+
x[0] = start
72+
# integrate the white noise
73+
for i in range(1,n):
74+
x[i] += x[i-1]*(1-leak)
75+
return x

signalz/generators/white_noise.py renamed to signalz/generators/gaussian_white_noise.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
"""
22
.. versionadded:: 0.1
3+
.. versionchanged:: 0.4
34
4-
This function generates white noise series. This function uses
5+
This function generates Gaussian white noise series. This function uses
56
`numpy.random.normal`.
67
78
Function Documentation
89
======================================
910
"""
1011
import numpy as np
1112

12-
def white_noise(n, offset=0, std=1):
13+
def gaussian_white_noise(n, offset=0, std=1):
1314
"""
1415
Random values with normal distribution.
1516
@@ -25,6 +26,6 @@ def white_noise(n, offset=0, std=1):
2526
2627
**Returns:**
2728
28-
* vector of random values (1d array)
29+
* vector of values representing the noise (1d array)
2930
"""
3031
return np.random.normal(offset, std, n)

signalz/generators/random_steps.py

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""
22
.. versionadded:: 0.2
3+
.. versionchanged:: 0.4
34
45
This function generates random steps according to given width of steps and
56
desired number of steps.
@@ -21,39 +22,43 @@
2122
.. code-block:: python
2223
2324
import signalz
24-
x = signalz.random_steps(50, 10, distribution="normal", std=30, mean=-10)
25+
x = signalz.random_steps(50, steps_count=10, distribution="normal", std=30, mean=-10)
2526
26-
Another example, this time with uniform distribution - values in range from 10
27-
to 20.
27+
Another example, this time the size of the data is not requested by number of
28+
steps, but by number of samples (`size=500`).
2829
2930
.. code-block:: python
3031
3132
import signalz
32-
x = signalz.random_steps(50, 10, distribution="uniform", minimum=10, maximum=20)
33+
x = signalz.random_steps(50, size=500)
3334
3435
Function Documentation
3536
======================================
3637
"""
3738
import numpy as np
3839
import matplotlib.pylab as plt
3940

41+
from signalz.misc import check_type_or_raise
4042
import signalz
4143

42-
def random_steps(steps_count, step_width, distribution="normal", maximum=1,
43-
minimum=0, std=1, mean=0):
44+
def random_steps(step_width, steps_count=10, size=None, distribution="normal",
45+
maximum=1, minimum=0, std=1, mean=0):
4446
"""
4547
This function generates random steps.
4648
4749
**Args:**
4850
49-
* `steps_count` - desired number steps (int)
50-
5151
* `step_width` - desired width of every step (int)
52-
52+
5353
**Kwargs:**
54-
55-
* `repeat` - number of step sequence repetions (int)
5654
55+
* `steps_count` - desired number steps (int), this variable is used,
56+
if the `size` is not defined
57+
58+
* `size` - lenght of desired output in samples (int),
59+
if this variable is defined,
60+
it determines the size of data instead of `steps_count`
61+
5762
* `distribution` - distribution of random numbers (str), Options are
5863
`normal` and `uniform`.
5964
@@ -64,22 +69,32 @@ def random_steps(steps_count, step_width, distribution="normal", maximum=1,
6469
in case of uniform distribution.
6570
6671
* `std` - standard deviation of random variable (float), this value
67-
is used in case of normal distribution.
72+
is used in case of gaussian (normal) distribution.
6873
6974
* `mean` - mean value of random variable (float), this value
70-
is used in case of normal distribution.
75+
is used in case of gaussian (normal) distribution.
7176
7277
**Returns:**
7378
74-
* `x` - array of values describing desired steps (1d array)
79+
* vector of values representing desired steps (1d array)
7580
"""
76-
# generate values
77-
values = False
78-
if distribution == "normal":
79-
values = np.random.normal(mean, std, 10)
81+
# check values
82+
check_type_or_raise(steps_count, int, "steps count")
83+
check_type_or_raise(step_width, int, "step width")
84+
# get correct number of steps if size is defined
85+
if not size is None:
86+
check_type_or_raise(size, int, "size")
87+
steps_count = int(np.ceil(size / float(step_width)))
88+
# generate random values
89+
if distribution in ["normal", "gaussian"]:
90+
values = signalz.gaussian_white_noise(steps_count, offset=mean, std=std)
8091
elif distribution == "uniform":
81-
values = np.random.random(steps_count) * float(maximum-minimum)
82-
values = values + minimum
92+
values = signalz.uniform_white_noise(steps_count, minimum=minimum,
93+
maximum=maximum)
8394
# generate steps
8495
x = signalz.steps(step_width, values)
85-
return x
96+
if size is None:
97+
return x
98+
else:
99+
return x[:size]
100+

0 commit comments

Comments
 (0)