MATLAB: How to generate colored gaussian noise of a certain length

coloredgaussiannoise

Hello, I am trying to generate colored gaussian noise in order to mimic neural noise.

Best Answer

It depends on what ‘color’ you want the noise to have. There are several ways to do it.
To create ‘1/f’ noise for example, this works:
fv = linspace(0, 1, 20); % Normalised Frequencies
a = 1./(1 + fv*2); % Amplitudes Of ‘1/f’
b = firls(42, fv, a); % Filter Numerator Coefficients
figure(1)
freqz(b, 1, 2^17) % Filter Bode Plot
N = 1E+6;
ns = randn(1, N);
invfn = filtfilt(b, 1, ns); % Create ‘1/f’ Noise
figure(2)
plot([0:N-1], invfn) % Plot Noise In Time Domain
grid
FTn = fft(invfn-mean(invfn))/N; % Fourier Transform
figure(3)
plot([0:N/2], abs(FTn(1:N/2+1))*2) % Plot Fourier Transform Of Noise
grid
See the documentation on the firls (link) function for details on how to use the function to shape the noise spectrum to meet your requirements.