Smooth Random Noise

Version 1.0.0 (2.29 KB) by Morgan
N-dimensional array of locally smooth random noise
19 Downloads
Updated 14 Feb 2024

View License

Motivation
I was inspired by the famous Perlin Noise algorithm to create a locally-smooth random number generator with a few tweaks of my own. Below is a typical output of 2D Perlin noise (left) and my version of smooth noise (right):
This is not the Perlin noise algorithm, but it does behave similarly in that it generates smooth random data. Key properties of the rands() function include:
  1. The random numbers are in the interval (0,1).
  2. The random numbers are locally smooth. Meaning the relative change between any one number and its neighbors is not drastic.
  3. Due to the use of fftn() inside the rands() algorithm, the arrays have periodic boundary conditions.
How do I use the function?
The rands() function takes between 0 and 2 input arguments:
  1. In the case of 0 input arguments, it defaults to the built-in rand() function.
  2. Given 1 input argument ("sz"), it generates an array of smooth random noise of that size. In the case of a scalar size input, it returns a square matrix of that size.
  3. Given the 2nd optional input argument ("sig"), the function uses this value as the Gaussian width in the kernal generation step.
The output of the function is, in general, an n-dimensional array of smooth random data.
How does the function work?
The rands() function calls the rand() function and smooths it by convolving it with a Gaussian kernel.
Example Usage
In one-dimension, a rands() function call might look like:
% SIZE OF NOISE ARRAY
sz = [ 100 1 ];
% GENERATE SMOOTH NOISE
X = rands(sz);
% VISUALIZE SMOOTH NOISE
plot(X,'-b','LineWidth',2);
title('1D Smooth Noise');
In two-dimensions:
% SIZE OF NOISE ARRAY
sz = 100; % Equivalent to sz = [ 100 100 ];
% GENERATE SMOOTH NOISE
X = rands(sz);
% VISUALIZE SMOOTH NOISE
subplot(132);
imagesc(X);
title('2D Smooth Noise');
axis square tight
colormap('gray')
colorbar
In three-dimensions with custom Gaussian width:
% SIZE OF NOISE ARRAY
sz = [ 100 100 100 ];
% GAUSSIAN WIDTH (in pixels)
sig = 2;
% GENERATE SMOOTH NOISE
X = rands(sz,sig);
% CALCULATE CENTER INDICES
Nx0 = ceil(size(X,1)/2);
Ny0 = ceil(size(X,2)/2);
Nz0 = ceil(size(X,3)/2);
% VISUALIZE SMOOTH NOISE
subplot(133);
slice(X,Nx0,Ny0,Nz0);
title('3D Smooth Noise')
shading interp
axis square tight
colormap('gray')
And so and for higher dimensions, although that becomes more difficult to visualize.
Some parting advice
The dominant time complexity of this algorithm is on the order of , where n = prod(sz). Because of that, generating large N-dimenional random numbers can become prohibitively time consuming.

Cite As

Morgan (2024). Smooth Random Noise (https://www.mathworks.com/matlabcentral/fileexchange/159633-smooth-random-noise), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2023b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.0