changeset 84:0f3af15bb29a

add noise introduction function
author Jeff Hammel <k0scist@gmail.com>
date Sun, 17 Dec 2017 13:55:35 -0800
parents 1b61ce99ee82
children d705f6384e8b
files tvii/noise.py
diffstat 1 files changed, 20 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tvii/noise.py	Sun Dec 17 13:55:35 2017 -0800
@@ -0,0 +1,20 @@
+"""
+functions for noise addition
+"""
+
+import random
+
+def add_noise(points, noise=None, fraction=0.01):
+    """
+    add `noise` to `points` vector
+
+    noise -- absolute magnitude of noise to add
+    fraction -- fraction of range of points to add, if `noise` not given
+    """
+
+    if noise is None:
+        span = max(points) - min(points)
+        noise = fraction*span
+
+    return [x+noise*random.random()
+            for x in points]