File Exchange

## Generate spatial data

version 1.0.0.0 (2.63 KB) by
Generate 1/f noise spatial data.

Updated 31 Mar 2016

The script generates spatial data with a scale-invariant power spectrum (1/f noise) and a normal error distribution.
The spectral density of the data is proportional to f^BETA, where f is the frequency and BETA is the spectral exponent (BETA=0 is white noise, BETA=-2 is Brown noise).

To generate Brown noise on a 10x10 grid you type

spatialPattern([10,10],-2)

which gives a 10x10 matrix containihng the data.

1/f noise is not stationary, and so doesn't have a well defined variance. A variogram of the data show variance linearly increasing with increasing lag.

Time series data can also be generated. For example, a pink noise time series of lenth 1000 is produced by

spatialPattern([1000,1],-1)

### Cite As

Jon Yearsley (2020). Generate spatial data (https://www.mathworks.com/matlabcentral/fileexchange/5091-generate-spatial-data), MATLAB Central File Exchange. Retrieved .

Christopher

UPDATE:
Marcin is correct, the current version of the function (with BETA/2) gives a f^BETA power spectral density. To generate noise with a 1/f FFT *amplitude*, use BETA=-2.
However, I also agree with Marcin that the power spectral density looks very unusual, and is very noisy. For an alternative, see the generatepinknoise.m function from https://github.com/kendrickkay/knkutils

Marcin Konowalczyk

UPDATE:
It has been pointed out to me that I have done my analysis wrong (and, obviously, so did @Christopher). I believe we have both plotted the signal amplitude (fft(x)) vs. frequency as opposed to the signal power spectral density (fft(x).^2) vs. frequency on a log-log plot (see e.g. https://uk.mathworks.com/help/signal/ug/power-spectral-density-estimates-using-fft.html). Anyway, the line 'S_f = (u.^2 + v.^2).^(BETA/2)' is in fact correct. I have also corrected my FileExchange submission to reflect that (https://uk.mathworks.com/matlabcentral/fileexchange/59305-randnd).

I have however found some other problems with this function. The way noise is generated (flat power spectral density rotated by a random phase into imaginary) causes the power spectral density to look very unnatural. Furthermore, from an unbeknownst to me reason, the noise generated by this function does not have a unit standard deviation. You can see that by comparing with 'randnd':

beta = 0; N = 2^12;
A1 = spatialPattern([1 N],beta);
A2 = randnd(beta,[1 N]);

A1fft = abs(fft(A1)).^2; A1fft = 2*A1fft(2:N/2);
A2fft = abs(fft(A2)).^2; A2fft = 2*A2fft(2:N/2);
k = 1:length(A1fft);

loglog(k,A1fft,k,A2fft); grid on; legend('spacialPattern','randnd');
xlim([min(k) max(k)]); xlabel('Wavenumber'); ylabel('Power/Wavenumber');

Before I've rated this function with 4 stars. Now:
+1 since the beta scaling is in fact correct
-1 for the unnatural power spectral density
-1 for the unnatural standard deviation
+1 for me feeling bad for badly assessing the 1/beta scaling beforehand
therefore still 4 stars.

raynerhmc

May

Pavel

Thanks, it's very useful.

Marcin Konowalczyk

I agree with @Christopher. It should be just BETA. Also; I wrote an updated version which works in an arbitrary number of dimensions (see Acknowledgments).

Zhang Chuan

if I want generate a 2-D 1/f noise ,is it right ?spatialPattern([256,256],-1)?

Zhang Chuan

Uriah Peng

Christopher

I believe the line S_f = (u.^2 + v.^2).^(BETA/2); is not correct - since S_f is raised to the 0.5 power during the ifft, I believe this line should instead read S_f = (u.^2 + v.^2).^BETA. Double-checking the spectra by taking the fft of the resulting image seems to confirm that this gives the correct exponent.

Monica He

Marius

Nicholas Howe

rui

IT is great. Thanks so much！

Great, thank you very much.

Yuval Cohen

I use it for audio noise generation. It works great. It would be beneficial to eliminate non-audible components (below 20Hz) in order to improve the power density of the signal.

Terence Picton

fine program - easy to use for pink noise for auditory purposes, haven't tried the spatial noises.

Md Nurujjaman

Stanley Bileschi

fantastic, works just like you want it to. It's a quick edit to make the default parameters a bit more user friendly, like so that Dim can be one number, and that automatically BETA sets to -2, for 1/f noise

Its of great use.

Gomez Lopez
{"ft_lang_label":"__label__en","ft_lang_prob":0.83732533,"math_prob":0.9379582,"size":4448,"snap":"2020-34-2020-40","text_gpt3_token_len":1168,"char_repetition_ratio":0.107785776,"word_repetition_ratio":0.0,"special_character_ratio":0.26056653,"punctuation_ratio":0.15864332,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97475934,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-09-24T12:57:35Z\",\"WARC-Record-ID\":\"<urn:uuid:aae816d5-67dd-4464-bc84-24202275d3c1>\",\"Content-Length\":\"100146\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:94ed93aa-f846-40c5-8b43-290f006b0a0f>\",\"WARC-Concurrent-To\":\"<urn:uuid:8c4ce78a-5f18-4fe1-ae38-903ffc7930ab>\",\"WARC-IP-Address\":\"23.223.252.57\",\"WARC-Target-URI\":\"https://www.mathworks.com/matlabcentral/fileexchange/5091-generate-spatial-data\",\"WARC-Payload-Digest\":\"sha1:LIQPIWAFUDE3CA2EVYM6ZXDFXUUWL7MT\",\"WARC-Block-Digest\":\"sha1:CWTBPXJR7HR6L5OQR33UED3WMJT7TJMC\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-40/CC-MAIN-2020-40_segments_1600400217623.41_warc_CC-MAIN-20200924100829-20200924130829-00721.warc.gz\"}"}