how to generate gaussian distributed noise based on a data set including cartisian coordinates in python

StackOverflow https://stackoverflow.com/questions/22380169

  •  14-06-2023
  •  | 
  •  

Question

I am looking for any script (preferably Python) to generate Gaussian distributed noise. I have an 3D array including the x,y and z coordinates of a data set in 3D space. Now, I want to generate gaussian noise from this data set. I know that I can generate points with "random.gauss(mu, sigma) function" but I dont know how can I do it for 3D data? Do I have to do it for each direction? if so, at the end, how I have to correlate them? I am really thankful for any help or hint!!

Thank You

Était-ce utile?

La solution

Your question is kind of ambiguous. But let's assume that what you have is an Nx3 array of positions in cartesian (x,y,z) coordinates, and you want to add an error to those positions.

You can use numpy, and in particular numpy.random.normal(loc=0.0, scale=1.0, size=None). You want to generate a size=(n,3) array of "errors", and add them to your position array.

If you want isotropic errors, then you want ⟨|δr|²⟩ = ⟨σ²⟩ = ⟨σ_x² + σ_y² + σ_z²⟩ = 3⟨σ_1²⟩, so the one-dimensional variance is (1/3) the three-dimensional variance, which in turn means that the scale parameter is 1/sqrt(3) the 3D sigma.

Hence you want:

noise_plus_signal = signal_in + \
      numpy.random.normal(scale=sigma/numpy.sqrt(3.0), size=(n,3))

Or, I've completely misunderstood your question.

Updates:

  • if the data are in spherical coordinates, it's probably easiest to convert to and from cartesian and use the algorithm as above

  • if you want a different sigma for each point, you can multiply the entire array of random variates (which, if you do it as above, is the same size as your array of points), by another array using the rules of numpy. In particular, if your array has shape (n,3), you can use numpy.random.normal(scale=1,size=(n,3))*sigma_array where sigma_array has size (n) so that each point gets its own variance, but the (x,y,z) coordinates all get the same variance for a given point.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top