28

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.)?

3 Answers 3

38

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

3
  • 3
    numpy.random.standard_normal(size=num_samples) can also be used when mean=0, and std=1
    – papahabla
    Mar 4, 2016 at 0: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 9, 2018 at 9:54
  • 2
    this is not white noise Jun 8, 2022 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 =)

4
  • 8
    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, 2017 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 9, 2018 at 9:46
  • 6
    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, 2018 at 19:04
  • 1
    @Vortico Interesting comment! In an attempt to understand what you are saying I have opened a follow up question :).
    – bluenote10
    Sep 24, 2020 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.