dsp.ColoredNoise System object

Package: dsp

Colored noise generator

Description

The ColoredNoise object generates pink noise and other colored noise signals. The form of the PSD is 1/|f|α with the exponent, α, a real number in the interval [-2,2].

To generate a colored noise signal:

  1. Define and set up your colored noise generator. See Construction.

  2. Call step to generate the colored noise signal according to the properties of dsp.ColoredNoise. The signal is an array with the number of rows given by the SamplesPerFrame property. The number of columns is given by the NumChannels property.

Construction

H = dsp.ColoredNoise returns a colored noise generator, H, with default settings. Calling step with the default property settings generates a pink noise signal.

H = dsp.ColoredNoise('PropertyName',PropertyValue, ...) returns a colored noise generator, H, with each specified property set to the specified value.

H = dsp.ColoredNoise(POW,SAMP,CHAN,'PropertyName',PropertyValue) returns a colored noise generator, H, with InverseFrequencyPower equal to POW, SamplesPerFrame equal to SAMP, and NumChannels equal to CHAN. Other specified properties are set to the specified values.

Properties

InverseFrequencyPower

Inverse PSD exponent

Specify the inverse PSD exponent, α, as a real number in the interval [-2,2]. The inverse exponent defines the PSD of the random process by 1/|f|α. The default value of this property is 1. Values of InverseFrequencyPower greater than 0 generate lowpass noise with a singularity (pole) at f=0. These processes exhibit long memory. Values of InverseFrequencyPower less than 0 generate highpass noise with increments that are negatively correlated. These processes are referred to as anti-persistent. Special cases include:

  • An InverseFrequencyPower value of 1 generates a pink noise. This is the default value.

  • An InverseFrequencyPower value of 2 generates a Brownian process.

  • An InverseFrequencyPower value of 0 generates a white noise process with a flat PSD.

  • An InverseFrequencyPower value of -2 generates a violet (purple) noise process.

In a log-log plot of power as a function of frequency, processes generated by dsp.ColoredNoise exhibit an approximate linear relationship with slope equal to -α.

NumChannels

Number of output channels

Specify the number of output channels as a positive integer. This determines the number of columns of the signal. The default value of this property is 1.

SamplesPerFrame

Samples per frame

Specify the number of samples per frame as a positive integer. This determines the number of rows of the signal. The default value of this property is 1024.

RandomStream

Source of random number stream

Specify the source of the random number stream as 'Global Stream' or 'mt19937ar with seed'. If you set RandomStream to 'Global Stream', the current random number generator settings are used. The current settings of the random number generator are returned by rng. The default value of this property is 'Global Stream'.

Seed

Initial seed

Specify the initial seed of the mt19937ar random number generator as a nonnegative integer. This property only applies when RandomStream is 'mt19937ar with seed'. The default value of this property is 67.

OutputDataType

Output data type

Specify the output data type as 'double' or 'single'. The default value of this property is 'double'.

Methods

cloneCreate colored noise generator with same property values
getNumInputsNumber of expected inputs to step method
getNumOutputsNumber of outputs of step method
isLockedLocked status for input attributes and nontunable properties
releaseAllow property value and input characteristics changes
resetReset random number generator seed
stepGenerate colored noise signal

Definitions

Colored Noise Processes

Many phenomena in diverse fields such as hydrology and finance produce time series with power spectral density (PSD) functions that follow a power law of the form

S(f)=L(f)|f|α

where α is a real number in the interval [-2,2] and L(f) is a positive slowly-varying or constant function. Plotting the power spectral density of such processes on a log-log plot displays an approximate linear relationship between the log frequency and log PSD with slope equal to -α

lnS(f)=αln|f|+lnL(f).

It is often convenient to plot the PSD in dB as function of the log frequency to base 2 in order to characterize the slope in dB/octave. Rewriting the preceding equation, you obtain

10logS(f)=10αln(2)log2(f)ln(10)+10ln(L(f))ln(10)

with the slope in dB/octave given by

10αln(2)log2(f)ln(10)

If α>0, S(f) goes to infinity as the frequency, f, approaches 0. Stochastic processes with power spectral densities of this form exhibit long memory. Long-memory processes have autocorrelations that persist for a long time as opposed to decaying exponentially like many common time-series models. If α<0, the process is antipersistent and exhibits negative correlation between increments [1].

Special examples of 1|f|α processes include:

  • α=0 — white noise where L(f) is a constant proportional to the process variance.

  • α=1 — pink, or flicker noise. Pink noise has equal energy per octave. See Measure Pink Noise Power in Octave Bands for a demonstration. Accordingly, the power spectral density of pink noise decreases 3 dB per octave.

  • α=2 — brown noise, or Brownian motion. Brownian motion is a nonstationary process with stationary increments. You can think of Brownian motion as the integral of a white noise process. Even though Brownian motion is nonstationary, you can still define a generalized power spectrum, which behaves like 1|f|2. Accordingly, power in a brown noise decreases 6 dB per octave.

  • α= -1 — blue noise. The power spectral density of blue noise increases 3 dB per octave.

  • α= -2 — violet, or purple noise. The power spectral density of violet noise increases 6 dB per octave. You can think of violet noise as the derivative of white noise process.

Examples

expand all

Measure Pink Noise Power in Octave Bands

This example shows how pink noise has approximately equal power in octave bands.

Generate a single-channel signal of pink noise that is 44100 samples in length. Set the random number generator to the default settings for reproducible results.

hpink = dsp.ColoredNoise(1,44.1e3,1);
rng default;
x = step(hpink);

Assume the sampling frequency is 44.1 kHz. Measure the power in octave bands beginning with 100-200 Hz and ending with 6.400-12.8 kHz. Display the results in a table.

beginfreq = 100;
endfreq = 200;
count = 1;
while(endfreq<=44.1e3/2)
    freqinterval(count,:) = [beginfreq endfreq];
    Pwr(count) = bandpower(x,44.1e3,[beginfreq endfreq]);
    beginfreq = endfreq;
    endfreq = 2*endfreq;
    count = count+1;
end
Pwr = Pwr(:);
table(freqinterval,Pwr)
ans = 

    freqinterval       Pwr  
    _____________    _______

     100      200    0.19436
     200      400    0.18472
     400      800    0.20873
     800     1600     0.2177
    1600     3200    0.21887
    3200     6400    0.23617
    6400    12800    0.23526

The pink noise has roughly equal power in octave bands. Repeat the preceding example with 'InverseFrequencyPower' equal to 0, which generates a white noise signal. A white noise signal has a flat power spectral density, or equal power per unit frequency. Set the random number generator to the default settings for reproducible results.

hwhite = dsp.ColoredNoise(0,44.1e3,1);
rng default;
x = step(hwhite);

Assume the sampling frequency is 44.1 kHz. Measure the power in octave bands beginning with 100-200 Hz and ending with 6.400-12.8 kHz. Display the results in a table.

beginfreq = 100;
endfreq = 200;
count = 1;
while(endfreq<=44.1e3/2)
    freqinterval(count,:) = [beginfreq endfreq];
    Pwr(count) = bandpower(x,44.1e3,[beginfreq endfreq]);
    beginfreq = endfreq;
    endfreq = 2*endfreq;
    count = count+1;
end
Pwr = Pwr(:);
table(freqinterval,Pwr)
ans = 

    freqinterval        Pwr   
    _____________    _________

     100      200    0.0031417
     200      400    0.0073833
     400      800     0.017421
     800     1600     0.035926
    1600     3200     0.071139
    3200     6400      0.15183
    6400    12800      0.28611

White noise has approximately equal power per unit frequency. Therefore, you expect octave bands to have an unequal distribution of power. Because the width of an octave band increases with increasing frequency, the power per octave band increases for a white noise.

PSD of Pink Noise Realization

Generate a pink noise signal 2048 samples in length. Assume a sampling rate of 1 Hz. Obtain an estimate of the power spectral density using Welch's overlapped segment averaging.

hcn = dsp.ColoredNoise('InverseFrequencyPower',1,'SamplesPerFrame',2048);
x = step(hcn);
Fs = 1;
[Pxx,F] = pwelch(x,hamming(128),[],[],Fs,'psd');

Construct the theoretical PSD of the pink noise process.

PSDPink = 1./F(2:end);

Display the Welch PSD estimate of the noise along with the theoretical PSD on a log-log plot. Plot the frequency axis as the logarithm to the base 2 to clearly show the octaves. Plot the PSD estimate in dB, $10\log_{10}$.

plot(log2(F(2:end)),10*log10(Pxx(2:end)));
hold on;
plot(log2(F(2:end)),10*log10(PSDPink),'r','linewidth',2)
xlabel('log_2(Hz)'); ylabel('dB');
title('Pink Noise');
grid on;
legend('PSD Estimate','Theoretical Pink Noise PSD');

Two-Channel Brownian Noise

Generate two channels of Brownian noise by setting alpha and 'NumChannels' equal to 2. Set the random number generator to its default settings for reproducible results.

hcn = dsp.ColoredNoise('InverseFrequencyPower',2,'SamplesPerFrame',2048,...
    'NumChannels',2);
rng default;
x = step(hcn);
subplot(2,1,1)
plot(x(:,1)); title('Channel 1'); axis tight;
subplot(2,1,2)
plot(x(:,2)); title('Channel 2'); axis tight;

Assume a sampling frequency of 1 Hz. Obtain Welch PSD estimates for both channels.

Fs = 1;
for nn = 1:size(x,2)
[Pxx(:,nn),F] = pwelch(x(:,nn),hamming(128),[],[],Fs,'psd');
end

Construct the theoretical PSD of a Brownian process. Plot the theoretical PSD along with both realizations on a log-log plot. Use the logarithm to the base 2 for the frequency axis and plot the power spectral densities in dB.

PSDBrownian = 1./F(2:end).^2;
figure;
plot(log2(F(2:end)),10*log10(PSDBrownian),'k-.','linewidth',2);
hold on;
plot(log2(F(2:end)),10*log10(Pxx(2:end,:)));
xlabel('log_2(Hz)'); ylabel('dB');
grid on;
legend('Theoretical PSD','Channel 1', 'Channel 2');

Add Pink Noise at 0 dB SNR

This example shows how to stream in an audio file and add pink noise at a 0 dB SNR. The example reads in frames of an audio file 1024 samples in length, measures the RMS value of the audio frame, and adds pink noise with the same RMS value as the audio frame.

Set up the System objects. Set the number of 'SamplesPerFrame' for both the file reader and the colored noise generator to 1024 samples. Set 'InverseFrequency' to 1 to generate pink noise with a $1/|f|$ power spectral density.

N = 1024;
hafr = dsp.AudioFileReader('Filename','speech_dft.mp3','SamplesPerFrame',N);
hap = dsp.AudioPlayer('SampleRate',hafr.SampleRate);
hcn = dsp.ColoredNoise('InverseFrequencyPower',1,'SamplesPerFrame',N);
hrms = dsp.RMS;

Test whether the default sound output device is ready. A 1 indicates that the device is ready.

~isempty(dspAudioDeviceInfo('defaultOutput'))
ans =

     1

Stream the audio file in 1024 samples at a time. Measure the signal RMS value for each frame, generate a frame of pink noise equal in length, and scale the RMS value of the pink noise to match the signal. Add the scaled noise to the signal and play the output.

while ~isDone(hafr)
    audio = step(hafr);
    speechRMS = step(hrms,audio);
    noise = step(hcn);
    noiseRMS = step(hrms,noise);
    noise = noise*(speechRMS/noiseRMS);
    sigPlusNoise = audio+noise;
    step(hap,sigPlusNoise);
end

pause(hap.QueueDuration);
release(hafr);
release(hap);

Averaged Power Spectrum of Pink Noise

This example shows how to generate two-channels of pink noise and compute the power spectrum based on a running average of 50 PSD estimates.

Set up the colored noise generator to generate two-channels of pink noise with 1024 samples. Set up the spectrum analyzer to compute modified periodograms using a Hamming window and 50% overlap. Obtain a running average of the PSD using 50 spectral averages.

Hpink = dsp.ColoredNoise(1, 1024, 2);
Hsa   = dsp.SpectrumAnalyzer('SpectrumType', 'Power density',...
                            'PlotAsTwoSidedSpectrum', false,...
                            'FrequencyScale', 'Log',...
                            'OverlapPercent', 50,...
                            'Window', 'Hamming',...
                            'YLimits', [-50,20],...
                            'SpectralAverages',50);

Run the simulation for 30 seconds.

     tic,
     while toc < 30

         pink = step(Hpink);
         step(Hsa, pink);
     end

Algorithms

AR Generation Method

dsp.ColoredNoise generates colored noise using a white noise input in an autoregressive model (AR) of order 63.

k=063aky(nk)=w(n)

where a0=1 and w(n) is a zero-mean white noise process.

The AR coefficients for k≥1 are generated according to the following recursive formula with α the inverse PSD exponent

ak=(k1α2)ak1kk=1,2,

The AR method used in dsp.ColoredNoise is detailed on pp. 820–822 in [2].

References

[1] Beran, J., Feng, Y., Ghosh, S., and Kulik, R. Long-Memory Processes: Probabilistic Properties and Statistical Methods, Springer, 2013.

[2] Kasdin, N.J. "Discrete Simulation of Colored Noise and Stochastic Processes and 1/fα Power Law Noise Generation", Proceedings of the IEEE®, Vol. 83, No. 5, 1995, pp. 802-827.

See Also

Was this topic helpful?