%% Measure Pink Noise Power in Octave Bands
% Note: If you are using R2016a or an earlier release, replace each call to the object with the equivalent step syntax. For example, obj(x) becomes step(obj,x).
% The output from this example shows that pink noise has approximately equal power in octave bands.
% Generate a single-channel signal of pink noise that is 44,100 samples in length. Set the random number generator to the default settings for reproducible results.

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

%
% Set the sampling frequency to 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;
freqinterval = zeros(7,2);
Pwr = zeros(7,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)

% The pink noise has roughly equal power in octave bands.
%
% Rerun the preceding code 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.

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

%
% Set 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)

% White noise has approximately equal power per unit frequency, so octave bands have an unequal distribution of power. Because the width of an octave band increases with increasing frequency, the power per octave band increases for white noise.
% Copyright 2012 The MathWorks, Inc.