Question

This may open a can of worms or will be very easily answered: I'm building a model of a system within Python: how do I quantitatively add noise? So far I have this (below code) -

i. Can I do this by broadcasting, even for unique noise added to each sample?

and

ii. Should noise be Gaussian or Uniform for electrical signal modelling? (Gaussian I think though I'm unsure)

import random
import numpy as np
import matplotlib.pyplot as plt

f   = 1e6
T   = 1/f
pi  = np.pi
t   = np.arange(0,20e-6,10e-9) 
# create signal and normalise  
y  = np.sin(2*pi*f*t) 
y /= max(y)

# add noise
for i in range(0, len(y)):
    noise = random.uniform(-1, 1) / 10    **#10% noise added** 
    y[i] += noise

plt.figure(1)
plt.plot(t*1e6,y,'r-')
plt.grid()
plt.show()

enter image description here

Was it helpful?

Solution

Judging by the signal you've generated it looks like your going for volts vs time. In which case you're wanting to add Gaussian noise.

You can generate Gaussian noise by exploiting the central limits theorem. Simply generate a bunch of random numbers (the distribution doesn't matter), add them together, store the result. Repeat that len(y) times and the list of results will be randomish but Gaussian distributed. Then just add that list to your y signal. But there's probably a predefined routine to give you Gaussian noise in the first place.

As for doing it in a more pythonic way, I expect numpy has a vector add routine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top