From 1516cb765d6a4a85e759fde6bb9a9f3a77e96b60 Mon Sep 17 00:00:00 2001 From: Dominik Roth Date: Mon, 26 Jun 2023 16:18:35 +0200 Subject: [PATCH] Implemented Harmonic Perlin Noise --- priorConditionedAnnealing/noise.py | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/priorConditionedAnnealing/noise.py b/priorConditionedAnnealing/noise.py index d0d8a09..2e9d0de 100644 --- a/priorConditionedAnnealing/noise.py +++ b/priorConditionedAnnealing/noise.py @@ -90,10 +90,10 @@ class SDE_Noise(): class Perlin_Noise(): - def __init__(self, known_shape=None, scale=0.1, octaves=1): + def __init__(self, known_shape=None, scale=0.1, octave=1): self.known_shape = known_shape self.scale = scale - self.octaves = octaves + self.octave = octave self.magic = 3.141592653589 # Axis offset, should be (kinda) irrational # We want to genrate samples, that approx ~N(0,1) self.normal_factor = 14/99 @@ -102,9 +102,30 @@ class Perlin_Noise(): def __call__(self, shape): self.index += 1 noise = [self.noise([self.index*self.scale, self.magic*a]) / self.normal_factor - for a in range(self.known_shape[-1])] + for a in range(shape[-1])] return th.Tensor(noise) def reset(self): self.index = 0 - self.noise = PerlinNoise(octaves=self.octaves) + self.noise = PerlinNoise(octaves=self.octave) + + +class Harmonic_Perlin_Noise(): + def __init__(self, known_shape=None, scale=0.1, octaves=8): + self.known_shape = known_shape + self.scale = scale + if type(octaves) == int: + octaves = [1/(i+1) for i in range(octaves)] + octaves = np.array(octaves) + self.octaves = octaves / np.linalg.norm(octaves) + self.reset() + + def __call__(self, shape): + harmonics = [noise(shape)*self.octaves[i] for i, noise in enumerate(self.noises)] + return sum(harmonics) + + def reset(self): + self.index = 0 + self.noises = [] + for octave, amplitude in enumerate(self.octaves): + self.noises += [Perlin_Noise(known_shape=self.known_shape, scale=self.scale, octave=(octave+1))]