Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

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.

    Note:   Starting in R2016b, instead of using the step method to perform the operation defined by the System object™, you can call the object with arguments, as if it were a function. For example, y = step(obj) and y = obj() perform equivalent operations.

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

Color

Noise color

Specify the color of the noise as one of 'pink' | 'white' | 'brown' | 'blue' | 'purple' | 'custom'. Each color is associated with an inverse frequency power of the generated noise sequence.

  • For pink noise, set Color to 'pink'. The inverse frequency power of pink noise is equal to 1.

  • For white noise, set Color to 'white'. The inverse frequency power of white noise is equal to 0.

  • For brown noise (also known as red or Brownian noise), set Color to 'brown'. The inverse frequency power of brown noise is equal to 2.

  • For blue noise (also known as azure noise), set Color to 'blue'. The inverse frequency power of blue noise is equal to -1.

  • For purple noise (also known as violet noise), set Color to 'purple'. The inverse frequency power of purple noise is equal to -2.

  • For noise with a custom inverse frequency power, set Color to 'custom'. In this case, the inverse frequency power is equal to the value specified in InverseFrequencyPower. This is the default value.

    InverseFrequencyPower can be any value in the interval [-2,2]. InverseFrequencyPower is applicable only when Color is set to 'custom'.

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 -1 generates a blue noise process.

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

This property is applicable only when Color is set to 'custom'.

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 property 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 property 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
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

Note: This example runs only in R2016b or later. If you are using an earlier release, replace each call to the function with the equivalent step syntax. For example, myObject() becomes step(myObject).

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 = 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 = 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.

Note: This example runs only in R2016b or later. If you are using an earlier release, replace each call to the function with the equivalent step syntax. For example, myObject() becomes step(myObject).

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('Color','pink','SamplesPerFrame',2048);
x = 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');

Note: This example runs only in R2016b or later. If you are using an earlier release, replace each call to the function with the equivalent step syntax. For example, myObject() becomes step(myObject).

Generate two channels of Brownian noise by setting Color to brown and NumChannels to 2.

hcn = dsp.ColoredNoise('Color','brown','SamplesPerFrame',2048,...
    'NumChannels',2);
x = 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');

Note: This example runs only in R2016b or later. If you are using an earlier release, replace each call to the function with the equivalent step syntax. For dsp.ColoredNoise System object™, myObject() becomes step(myObject). For all other objects, myObject(x) becomes step(myObject,x).

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 Color to pink to generate pink noise with a $1/|f|$ power spectral density.

N = 1024;
hafr = dsp.AudioFileReader('Filename','speech_dft.mp3','SamplesPerFrame',N);
hap = audioDeviceWriter('SampleRate',hafr.SampleRate);
hcn = dsp.ColoredNoise('Color','pink','SamplesPerFrame',N);
hrms = dsp.RMS;

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 = hafr();
    speechRMS = hrms(audio);
    noise = hcn();
    noiseRMS = hrms(noise);
    noise = noise*(speechRMS/noiseRMS);
    sigPlusNoise = audio+noise;
    hap(sigPlusNoise);
end

release(hafr);
release(hap);

Note: This example runs only in R2016b or later. If you are using an earlier release, replace each call to the function with the equivalent step syntax. For dsp.ColoredNoise System object™, myObject() becomes step(myObject). For all other objects, myObject(x) becomes step(myObject,x).

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', ...
    'OverlapPercent',50,'Window','Hamming', ...
    'SpectralAverages',50,'PlotAsTwoSidedSpectrum',false, ...
    'FrequencyScale','log','YLimits',[-50 20]);

Run the simulation for 30 seconds.

tic
while toc < 30
    pink = Hpink();
    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

|

Introduced in R2014a

Was this topic helpful?