File size: 1,201 Bytes
987ed1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import numpy as np
import pdb

class NoiseGenerator:
    def __init__(self, noise_strength, correlation_factor=0.9):
        self.noise_strength = noise_strength
        self.correlation_factor = correlation_factor
        self.previous_noise = None

    def step(self, pred):
        # Generate random noise
        # noise_seed = np.random.randn(*pred) * self.noise_strength
        noise_seed = (np.random.rand(pred.shape[0], 1, pred.shape[2]) + 0.5) * np.random.choice([-1, 1], size=(pred.shape[0], 1, pred.shape[2]))
        action_step = (pred[:, 1:] - pred[:, :-1])
        noise_step = noise_seed.repeat(action_step.shape[1], axis=1) * action_step * self.noise_strength

        # If it's the first time step, there's no previous noise, so use the seed directly
        if self.previous_noise is None:
            self.previous_noise = noise_step
        else:
            # Combine the previous noise with new noise to create temporally correlated noise
            noise_step = self.correlation_factor * self.previous_noise + (1 - self.correlation_factor) * noise_step
            self.previous_noise = noise_step

        noise_cum = np.cumsum(noise_step, axis=1)

        return noise_cum