view tvii/noise.py @ 84:0f3af15bb29a

add noise introduction function
author Jeff Hammel <k0scist@gmail.com>
date Sun, 17 Dec 2017 13:55:35 -0800
parents
children
line wrap: on
line source

"""
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]