www.gusucode.com > dsp 案例源码程序 matlab代码 > dsp/AddPinkNoiseAt0DBSNRExample.m

    %% Add Pink Noise at 0 dB SNR
%%
% *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(TM), 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);