27

I need to draw samples from a white noise process in order to implement a particular integral numerically.

How do I generate this with Python (i.e., numpy, scipy, etc.)?

Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
abcd
  • 10,215
  • 15
  • 51
  • 85

3 Answers3

37

You can achieve this through the numpy.random.normal function, which draws a given number of samples from a Gaussian distribution.

import numpy
import matplotlib.pyplot as plt

mean = 0
std = 1 
num_samples = 1000
samples = numpy.random.normal(mean, std, size=num_samples)

plt.plot(samples)
plt.show()

1000 random samples drawn from a Gaussian distribution of mean=0, std=1

Sam
  • 2,398
  • 4
  • 25
  • 37
  • 3
    `numpy.random.standard_normal(size=num_samples)` can also be used when mean=0, and std=1 – papahabla Mar 04 '16 at 00:29
  • 2
    You can achieve this with any kind of distribution as long as there are no autocorrelations in the signal. "numpy.random.uniform(low=0.0, high=1.0, size=1000)", "np.random.triangular(-3, 0, 8, 100000)" will also get white noise. You can also have a correlated signal process and randomize it using "numpy.random.shuffle" for getting white noise. – ivangtorre May 09 '18 at 09:54
  • 1
    this is not white noise – Alex Monras Jun 08 '22 at 11:51
7

Short answer is numpy.random.random(). Numpy site description

But since I find more and more answers to similar questions written as numpy.random.normal, I suspect a little description is needed. If I do understand Wikipedia (and a few lessons at the University) correctly, Gauss and White Noise are two separate things. White noise has Uniform distribution, not Normal (Gaussian).

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

num_samples = 10000
num_bins = 200

samples = numpy.random.random(size=num_samples)

plt.hist(samples, num_bins)
plt.show()

Image: Result

This is my first answer, so if you correct mistakes possibly made by me here, I'll gladly update it. Thanks =)

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
user8866568
  • 95
  • 1
  • 2
  • 6
    `White noise has Uniform distribution, not Normal (Gaussian).` White noise must have Uniform distribution over *frequencies* but it can have any distribution over *time* (for instance Normal). – Gluttton Dec 26 '17 at 10:42
  • 2
    As Wikipedia says: " white noise is a random signal having equal intensity at different frequencies". This means that you can have any kind of PDF for the signal as long as there are no temporary correlations. So White noise can have Uniform distribution, Normal distribution or other kind of distributions – ivangtorre May 09 '18 at 09:46
  • 5
    This answer is incorrect. White noise is a continuous process from *any* uncorrelated random process, like uniform or normal. However, if you digitize it, you must apply a bandpass filter at the Nyquist frequency, otherwise your approximation of the continuous process contains aliasing. It turns out that bandpassing white noise results in a discrete random process where each sample is picked from a Gaussian/normal distribution. This is the result of your confusion. Gaussian and white noise are the same thing in discrete processes. Gaussian is a *subset* of continuous white noise processes. – Vortico Jul 23 '18 at 19:04
  • 1
    @Vortico Interesting comment! In an attempt to understand what you are saying I have opened a [follow up question](https://dsp.stackexchange.com/q/70514/24353) :). – bluenote10 Sep 24 '20 at 20:39
1

Create random samples with normal distribution (Gaussian) with numpy.random.normal:

import numpy as np
import seaborn as sns

mu, sigma = 0, 1 # mean and standard deviation
s = np.random.normal(mu, sigma, size=1000) # 1000 samples with normal distribution

# seaborn histogram with Kernel Density Estimation
sns.distplot(s, bins=40, hist_kws={'edgecolor':'black'})

enter image description here