Dynamic range gate
System object™ performs dynamic range gating independently across each input channel. Dynamic
range gating suppresses signals below a given threshold. It uses specified attack, release,
and hold times to achieve a smooth applied gain curve. Properties of the
System object specify the type of dynamic range gating.
To perform dynamic range gating:
noiseGateobject and set its properties.
Call the object with arguments, as if it were a function.
To learn more about how System objects work, see What Are System Objects?
dRG = noiseGate creates a System object,
dRG, that performs dynamic range gating independently
across each input channel.
dRG = noiseGate( sets the
dRG = noiseGate(___,
sets each property
Name to the specified
Unspecified properties have default values.
dRG = noiseGate('AttackTime',0.01,'SampleRate',16000)
creates a System object,
dRG, with a 10 ms attack time and a 16 kHz sample
Unless otherwise indicated, properties are nontunable, which means you cannot change their
values after calling the object. Objects lock when you call them, and the
release function unlocks them.
If a property is tunable, you can change its value at any time.
For more information on changing property values, see System Design in MATLAB Using System Objects.
Threshold — Operation threshold (dB)
–10 (default) | real scalar
Operation threshold in dB, specified as a real scalar.
Operation threshold is the level below which gain is applied to the input signal.
AttackTime — Attack time (s)
0.05 (default) | real scalar
Attack time in seconds, specified as a real scalar greater than or equal to 0.
Attack time is the time it takes the applied gain to rise from 10% to 90% of its final value when the input goes below the threshold.
ReleaseTime — Release time (s)
0.02 (default) | real scalar
Release time in seconds, specified as a real scalar greater than or equal to 0.
Release time is the time it takes the applied gain to drop from 90% to 10% of its final value when the input goes above the threshold.
HoldTime — Hold time (s)
0.05 (default) | real finite scalar
Hold time in seconds, specified as a real scalar greater than or equal to 0.
Hold time is the period for which the (negative) gain is held before starting to decrease towards its steady state value when the input level drops below the threshold.
SampleRate — Input sample rate (Hz)
44100 (default) | positive scalar
Input sample rate in Hz, specified as a positive scalar.
EnableSidechain — Enable sidechain input
false (default) |
Enable sidechain input, specified as
false. This property determines the number of available inputs on
The sidechain datatype and (frame) length must be the same as
The number of channels of the sidechain must be equal to the number of channels of
be equal to one. When the number of sidechain channels is one, the
computed based on this channel is applied to all channels of
When the number of sidechain channels is equal to the number of channels in
computed for each sidechain channel is applied to the corresponding channel of
audioIn — Audio input to noise gate
Audio input to the noise gate, specified as a matrix. The columns of the matrix are treated as independent audio channels.
audioOut — Audio output from noise gate
Audio output from the noise gate, returned as a matrix the same size as
gain — Gain applied by noise gate (dB)
Gain applied by noise gate, returned as a matrix the same size as
To use an object function, specify the
System object as the first input argument. For
example, to release system resources of a System object named
Specific to noiseGate
functions map tunable properties of the
System object to user-facing parameters:
Gate Audio Signal
Use dynamic range gating to attenuate background noise from an audio signal.
Set up the
audioDeviceWriter System objects™.
frameLength = 1024; fileReader = dsp.AudioFileReader( ... 'Filename','Counting-16-44p1-mono-15secs.wav', ... 'SamplesPerFrame',frameLength); deviceWriter = audioDeviceWriter( ... 'SampleRate',fileReader.SampleRate);
Corrupt the audio signal with Gaussian noise. Play the audio.
while ~isDone(fileReader) x = fileReader(); xCorrupted = x + (1e-2/4)*randn(frameLength,1); deviceWriter(xCorrupted); end release(fileReader)
Set up a dynamic range gate with a threshold of -25 dB, an attack time of 0.01 seconds, a release time of 0.02 seconds, and a hold time of 0 seconds. Use the sample rate of your audio file reader.
gate = noiseGate(-25, ... 'AttackTime',0.01, ... 'ReleaseTime',0.02, ... 'HoldTime',0, ... 'SampleRate',fileReader.SampleRate);
Set up a time scope to visualize the signal before and after dynamic range gating.
scope = timescope( ... 'SampleRate',fileReader.SampleRate, ... 'TimeSpanOverrunAction','Scroll', ... 'TimeSpanSource','property',... 'TimeSpan',16, ... 'BufferLength',1.5e6, ... 'YLimits',[-1 1], ... 'ShowGrid',true, ... 'ShowLegend',true, ... 'Title','Corrupted vs. Gated Audio');
Play the processed audio and visualize it on scope.
while ~isDone(fileReader) x = fileReader(); xCorrupted = x + (1e-2/4)*randn(frameLength,1); y = gate(xCorrupted); deviceWriter(y); scope([xCorrupted,y]); end release(fileReader) release(gate) release(deviceWriter) release(scope)
Tune Noise Gate Parameters
dsp.AudioFileReader to read in audio frame-by-frame. Create an
audioDeviceWriter to write audio to your sound card. Create a
noiseGate to process the audio data.
frameLength = 1024; fileReader = dsp.AudioFileReader('RockDrums-44p1-stereo-11secs.mp3', ... 'SamplesPerFrame',frameLength); deviceWriter = audioDeviceWriter('SampleRate',fileReader.SampleRate); dRG = noiseGate('SampleRate',fileReader.SampleRate);
parameterTuner to open a UI to tune parameters of the
noiseGate while streaming.
In an audio stream loop:
Read in a frame of audio from the file.
Apply dynamic range gating.
Write the frame of audio to your audio device for listening.
While streaming, tune parameters of the dynamic range gate and listen to the effect.
while ~isDone(fileReader) audioIn = fileReader(); audioOut = dRG(audioIn); deviceWriter(audioOut); drawnow limitrate % required to update parameter end
As a best practice, release your objects once done.
release(deviceWriter) release(fileReader) release(dRG)
Sidechain Dynamic Range Gating
Use the EnableSidechain input of a
noiseGate object to emulate an electronic drum controller, also known as a multipad. This technique is common in recording studio production and creates interesting changes to the timbre of an instrument. The sidechain signal controls the gating on the input signal. Sidechain gating decreases the amplitude of the input signal when the sidechain signal falls below the Threshold of the
noiseGate. A noise gate is essentially an
expander with an infinite Ratio.
Prepare Audio Files
Convert the sidechain signal from stereo to mono.
[expanderSideChainStereo,Fs] = audioread('FunkyDrums-44p1-stereo-25secs.mp3'); expanderSideChainMono = (expanderSideChainStereo(:,1) + expanderSideChainStereo(:,2)) / 2;
Write the converted sidechain signal to a file.
Construct Audio Objects
dsp.AudioFileReader object for the input and sidechain signals. To allow the script to run indefinitely, change the
playbackCount variable from
inputAudio = 'SoftGuitar-44p1_mono-10mins.ogg'; sidechainAudio = 'convertedSidechainSig.wav'; playbackCount = 1; inputAudioAFR = dsp.AudioFileReader(inputAudio,'PlayCount',playbackCount); sidechainAudioAFR = dsp.AudioFileReader(sidechainAudio,'PlayCount',playbackCount);
dRG = noiseGate('EnableSidechain',true,'Threshold',-15,'AttackTime',... 0.08,'ReleaseTime',0.0001,'HoldTime',0.00001); visualize(dRG)
audioDeviceWriter object to play the sidechain and input signals.
afw = audioDeviceWriter;
timescope object to view the input signal, the sidechain signal, as well as the gated input signal.
scope = timescope('NumInputPorts',3,... 'SampleRate',Fs,... 'TimeSpanSource','property',... 'TimeSpan',5,... 'TimeDisplayOffset',0,... 'LayoutDimensions',[3 1],... 'BufferLength',Fs*15,... 'TimeSpanOverrunAction','Scroll',... 'YLimits',[-1 1],... 'ShowGrid',true,... 'Title','Input Audio - Classical Guitar'); scope.ActiveDisplay = 2; scope.YLimits = [-1 1]; scope.Title = 'Sidechain Audio - Drums'; scope.ShowGrid = true; scope.ActiveDisplay = 3; scope.YLimits = [-1 1]; scope.ShowGrid = true; scope.Title = 'Gated Input Audio - Classical Guitar';
parameterTuner to open a UI to tune parameters of the gate while streaming. Adjust the property values and listen to the effect in real time.
Create Audio Streaming Loop
Read in a frame of audio from your input and sidechain signals. Process your input and sidechain signals with your
noiseGate object. Playback your processed audio signals and display the audio data using a
The top panel of your
timescope displays the input audio signal and the middle panel displays the sidechain audio signal. The bottom panel displays the gated input audio signal.
Substitute different audio files for your
inputAudio variable to create different textures and timbres in your drum mix.
while ~isDone(sidechainAudioAFR) inputAudioFrame = inputAudioAFR(); sideChainAudioFrame = sidechainAudioAFR(); noiseGateOutput = dRG(inputAudioFrame,sideChainAudioFrame); afw(sideChainAudioFrame+noiseGateOutput); scope(inputAudioFrame,sideChainAudioFrame,noiseGateOutput); drawnow limitrate; % required to update parameter settings from UI end
Release your objects.
release(inputAudioAFR) release(sidechainAudioAFR) release(dRG) release(afw) release(scope)
System object processes a signal frame by frame and element by element.
Convert Input Signal to Magnitude
The N-point signal, x[n], is converted to magnitude:
xa[n] passes through the gain computer. The gain computer uses the static characteristic properties of the dynamic range gate to determine a brick-wall gain for signal below the threshold:
Tlin is the threshold property converted to a linear domain:
The computed gain, gc[n], is smoothed using specified attack, release, and hold time properties:
The attack time coefficient, αA , is calculated as
The release time coefficient, αR , is calculated as
is the attack time period, specified by the
AttackTime property. TR
is the release time period, specified by the
ReleaseTime property. Fs is the input sampling rate, specified by the
CA is the hold counter for attack. The limit, TH
, is determined by the
The output of the dynamic range gate is given as
 Giannoulis, Dimitrios, Michael Massberg, and Joshua D. Reiss. "Digital Dynamic Range Compressor Design –– A Tutorial and Analysis." Journal of Audio Engineering Society. Vol. 60, Issue 6, 2012, pp. 399–408.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
System Objects in MATLAB Code Generation (MATLAB Coder)
Introduced in R2016a