How to add random noise to a signal using NumPy?

10,507

To generate a single number, use the 2-argument form of np.random.normal:

In [47]: np.random.normal(0, 0.5)
Out[47]: 0.6138972867165546

You may need to scale this number (multiply it by a small number epsilon) so the noise is small compared to self.x.

Share:
10,507
Barry
Author by

Barry

Updated on June 05, 2022

Comments

  • Barry
    Barry almost 2 years

    I want to add Gaussian random noise to a variable in my model for each separate time-step and not to generate a noise array and add it to my signal afterwards. In this way I want to examine a standard dynamic effect of my system.

    So, I want to generate each time-step a random noise (i.e. a single value) and it to my signal (e.g. add noise then it calculates the next state, add noise it calculates the next state, etc.). I thought to do this via NumPy using the following in the dynamic section of my model over a set of time-steps:

    self.x = self.x + self.a * ((d-f)/100)
    
    self.x = self.x + np.random.normal(0, 0.5, None)`
    

    the second line is drawing random samples from a normal distribution and adds it to my variable.

    0 is the mean of the normal distribution I am choosing from, 0.5 is the standard deviation of the normal distribution and the third argument is the size.

    I am wondering if numpy.random.normal is the correct way to do it and, if so, what parameter I should use for the size argument?