Main Content

noiseGate

Dynamic range gate

Description

The noiseGate 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 noiseGate System object specify the type of dynamic range gating.

To perform dynamic range gating:

  1. Create the noiseGate object and set its properties.

  2. Call the object with arguments, as if it were a function.

To learn more about how System objects work, see What Are System Objects?

Creation

Description

dRG = noiseGate creates a System object, dRG, that performs dynamic range gating independently across each input channel.

dRG = noiseGate(thresholdValue) sets the Threshold property to thresholdValue.

example

dRG = noiseGate(___,Name,Value) sets each property Name to the specified Value. Unspecified properties have default values.

Example: dRG = noiseGate('AttackTime',0.01,'SampleRate',16000) creates a System object, dRG, with a 10 ms attack time and a 16 kHz sample rate.

Properties

expand all

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.

Operation threshold in dB, specified as a real scalar.

Operation threshold is the level below which gain is applied to the input signal.

Tunable: Yes

Data Types: single | double

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.

Tunable: Yes

Data Types: single | double

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.

Tunable: Yes

Data Types: single | double

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.

Tunable: Yes

Data Types: single | double

Input sample rate in Hz, specified as a positive scalar.

Tunable: Yes

Data Types: single | double

Enable sidechain input, specified as true or false. This property determines the number of available inputs on the noiseGate object.

  • false –– Sidechain input is disabled and the noiseGate object accepts one input: the audioIn data to be gated.

  • true –– Sidechain input is enabled and the noiseGate object accepts two inputs: the audioIn data to be gated and the sidechain input used to compute the gain applied by noiseGate.

The sidechain datatype and (frame) length must be the same as audioIn.

The number of channels of the sidechain must be equal to the number of channels of audioIn or be equal to one. When the number of sidechain channels is one, the gain computed based on this channel is applied to all channels of audioIn. When the number of sidechain channels is equal to the number of channels in audioIn, the gain computed for each sidechain channel is applied to the corresponding channel of audioIn.

Tunable: No

Usage

Description

example

audioOut = dRG(audioIn) performs dynamic range gating on the input signal, audioIn, and returns the gated signal, audioOut. The type of dynamic range gating is specified by the algorithm and properties of the noiseGate System object, dRG.

[audioOut,gain] = dRG(audioIn) also returns the applied gain, in dB, at each input sample.

Input Arguments

expand all

Audio input to the noise gate, specified as a matrix. The columns of the matrix are treated as independent audio channels.

Data Types: single | double

Output Arguments

expand all

Audio output from the noise gate, returned as a matrix the same size as audioIn.

Data Types: single | double

Gain applied by noise gate, returned as a matrix the same size as audioIn.

Data Types: single | double

Object Functions

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 obj, use this syntax:

release(obj)

expand all

visualizeVisualize static characteristic of dynamic range controller
createAudioPluginClassCreate audio plugin class that implements functionality of System object
parameterTunerTune object parameters while streaming
configureMIDIConfigure MIDI connections between audio object and MIDI controller
disconnectMIDIDisconnect MIDI controls from audio object
getMIDIConnectionsGet MIDI connections of audio object
cloneCreate duplicate System object
isLockedDetermine if System object is in use
releaseRelease resources and allow changes to System object property values and input characteristics
resetReset internal states of System object
stepRun System object algorithm

The createAudioPluginClass and configureMIDI functions map tunable properties of the noiseGate System object to user-facing parameters:

PropertyRangeMappingUnit
Threshold[–140, 0]lineardB
AttackTime[0, 4]linearseconds
ReleaseTime[0, 4]linearseconds
HoldTime[0, 4]linearseconds

Examples

collapse all

Use dynamic range gating to attenuate background noise from an audio signal.

Set up the dsp.AudioFileReader and 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)

Create a 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);

Call parameterTuner to open a UI to tune parameters of the noiseGate while streaming.

parameterTuner(dRG)

In an audio stream loop:

  1. Read in a frame of audio from the file.

  2. Apply dynamic range gating.

  3. 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)

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.

audiowrite('convertedSidechainSig.wav',expanderSideChainMono,Fs);

Construct Audio Objects

Construct a dsp.AudioFileReader object for the input and sidechain signals. To allow the script to run indefinitely, change the playbackCount variable from 1 to Inf.

inputAudio = 'SoftGuitar-44p1_mono-10mins.ogg';
sidechainAudio  = 'convertedSidechainSig.wav';
playbackCount = 1;
inputAudioAFR = dsp.AudioFileReader(inputAudio,'PlayCount',playbackCount);
sidechainAudioAFR = dsp.AudioFileReader(sidechainAudio,'PlayCount',playbackCount);

Construct and visualize a noiseGate object. Use fast AttackTime and ReleaseTime, and a short HoldTime.

dRG = noiseGate('EnableSidechain',true,'Threshold',-15,'AttackTime',...
    0.08,'ReleaseTime',0.0001,'HoldTime',0.00001);
visualize(dRG)

Construct an audioDeviceWriter object to play the sidechain and input signals.

afw = audioDeviceWriter;

Construct a 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';

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

parameterTuner(dRG)

Figure Audio Parameter Tuner: noiseGate [dRG] contains an object of type uigridlayout.

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 timescope object.

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)

Algorithms

expand all

The noiseGate System object processes a signal frame by frame and element by element.

References

[1] 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.

Extended Capabilities

Introduced in R2016a