top of page

Generating White Noise in Python: A Step-by-Step Guide

Feb 15, 2024

White noise has numerous practical applications, from sound masking and sleep therapy to audio engineering and even machine learning. In this article, we'll guide you through the process of generating white noise using Python, a popular programming language known for its simplicity and versatility.

To create white noise, we'll be utilizing Python's built-in libraries, such as 'numpy' and 'scipy.' These libraries contain powerful tools for scientific computing and will help us generate the random audio signals that characterize white noise. If you don't already have these libraries installed, you can use the pip package manager to install them with the following commands:

pip install numpy
pip install scipy

Now that we have the required libraries installed, let's start generating our white noise. First, let's import the necessary libraries and define the parameters for the white noise:

import numpy as np
from import write

# Parameters
sample_rate = 44100 # Sample rate in Hz
duration = 5 # Duration of the white noise in seconds

Next, we'll generate the random audio signals, normalize them, and create a NumPy array of the white noise samples:

# Generate white noise
noise = np.random.normal(0, 1, sample_rate * duration)

# Normalize the white noise
noise = noise / np.max(np.abs(noise))

# Convert the white noise to a 16-bit format
noise = (noise * 2**15).astype(np.int16)

Finally, we'll save the white noise as an audio file using the 'write' function from the '' library:

# Save the white noise as a .wav file
write('white_noise.wav', sample_rate, noise)

Congratulations! You've successfully generated a white noise audio file using Python. You can now use this file for sound masking purposes, test its properties in your audio projects, or use it as input for your machine learning algorithms.

It's worth noting that you can easily modify the parameters we've defined earlier to create a more tailored white noise sound, like adjusting the sample rate or duration to suit your needs. Experiment with different settings and enjoy the limitless potential of Python's capabilities!

bottom of page