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:
- The random numbers are in the interval (0,1).
- The random numbers are locally smooth. Meaning the relative change between any one number and its neighbors is not drastic.
- 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:
- In the case of 0 input arguments, it defaults to the built-in rand() function.
- 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.
- 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.
引用格式
Morgan (2024). Smooth Random Noise (https://www.mathworks.com/matlabcentral/fileexchange/159633-smooth-random-noise), MATLAB Central File Exchange. 检索时间: .
MATLAB 版本兼容性
创建方式
R2023b
兼容任何版本
平台兼容性
Windows macOS Linux标签
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!版本 | 已发布 | 发行说明 | |
---|---|---|---|
1.0.0 |