multiband dynamic range compression -凯发k8网页登录
this example shows how to simulate a digital audio multiband dynamic range compression system.
introduction
dynamic range compression reduces the dynamic range of a signal by attenuating the level of strong peaks, while leaving weaker peaks unchanged. compression has applications in audio recording, mixing, and in broadcasting.
multiband compression compresses different audio frequency bands separately, by first splitting the audio signal into multiple bands and then passing each band through its own independently adjustable compressor. multiband compression is widely used in audio production and is often included in audio workstations.
the multiband compressor in this example first splits an audio signal into different bands using a multiband crossover filter. linkwitz-riley crossover filters are used to obtain an overall allpass frequency response. each band is then compressed using a separate dynamic range compressor. key compressor characteristics, such as the compression ratio, the attack and release time, the threshold and the knee width, are independently tunable for each band. the effect of compression on the dynamic range of the signal is showcased.
linkwitz-riley crossover filters
a linkwitz-riley crossover filter consists of a combination of a lowpass and highpass filter, each formed by cascading two lowpass or highpass butterworth filters. summing the response of the two filters yields a gain of 0 db at the crossover frequency, so that the crossover acts like an allpass filter (and therefore introducing no distortion in the audio signal).
may be used to implement a linkwitz-riley system object. since a linkwitz-riley crossover filter is formed by cascading two butterworth filters, its order is always even. a butterworth filter's slope is equal to 6*n db/octave, where n is the filter order. when the crossoverslopes
property of crossoverfilter
is divisible by 12
(i.e. the filter is even-ordered), the object implements a linkwitz-riley crossover. otherwise, the object implements a butterworth crossover, where the lowpass and highpass sections are each implemented using a single butterworth filter of order crossoverslopes/6
.
here is an example where an fourth-order linkwitz-riley crossover is used to filter a signal. notice that the lowpass and highpass sections each have a -6 db gain at the crossover frequency. the sum of the lowpass and highpass sections is allpass.
fs = 44100; % linkwitz-riley filter crossover = crossoverfilter(1,5000,4*6,fs); % transfer function estimator transferfuncestimator = dsp.transferfunctionestimator( ... 'frequencyrange','onesided','spectralaverages',20); framelength = 1024; scope = dsp.arrayplot( ... 'plottype','line', ... 'ylimits',[-40 1], ... 'ylabel','magnitude (db)', ... 'xscale','log', ... 'sampleincrement',(fs/2)/(framelength/2 1), ... 'xlabel','frequency (hz)', ... 'title','eighth order linkwitz-riley crossover filter', ... 'showlegend',true, ... 'channelnames',{'band 1','band 2','sum'}); tic while toc < 10 in = randn(framelength,1); % return lowpass and highpass responses of the crossover filter [ylp,yhp] = crossover(in); % sum the responses y = ylp yhp; v = transferfuncestimator(repmat(in,1,3),[ylp yhp y]); scope(20*log10(abs(v))); end
multiband crossover filters
may also be used to implement a multiband crossover filter by combining two-band crossover filters and allpass filters in a tree-like structure. the filter divides the spectrum into multiple bands such that their sum is a perfect allpass filter.
the example below shows a four-band crossover filter formed of fourth order linkwitz-riley crossover filters. notice the allpass response of the sum of the four bands.
fs = 44100; crossover = crossoverfilter(3,[2e3 5e3 10e3],[24 24 24],44100); transferfuncestimator = dsp.transferfunctionestimator('frequencyrange','onesided','spectralaverages',20); l = 2^14; scope = dsp.arrayplot( ... 'plottype','line', ... 'xoffset',0, ... 'ylimits',[-120 5], ... 'xscale','log', ... 'sampleincrement', .5 * fs/(l/2 1 ), ... 'ylabel','frequency response (db)', ... 'xlabel','frequency (hz)', ... 'title','four-band crossover filter', ... 'showlegend',true, ... 'channelnames',{'band 1','band 2','band 3','band 4','sum'}); tic; while toc < 10 in = randn(l,1); % split the signal into four bands [ylp,ybp1,ybp2,yhp] = crossover(in); y = ylp ybp1 ybp2 yhp; z = transferfuncestimator(repmat(in,1,5),[ylp,ybp1,ybp2,yhp,y]); scope(20*log10(abs(z))) end
dynamic range compression
is a dynamic range compressor system object. the input signal is compressed when it exceeds the specified threshold. the amount of compression is controlled by the specified compression ratio. the attack and release times determine how quickly the compressor starts or stops compressing. the knee width provides a smooth transition for the compressor gain around the threshold. finally, a make-up gain can be applied at the output of the compressor. this make-up gain amplifies both strong and weak peaks equally.
the static compression characteristic of the compressor depends on the compression ratio, the threshold and the knee width. the example below illustrates the static compression characteristic for a hard knee:
drc = compressor(-15,5); visualize(drc);
in order to view the effect of threshold, ratio and knee width on the compressor's static characteristic, change the values of the threshold
, ratio
and kneewidth
properties. the static characteristic plot should change accordingly.
the compressor's attack time is defined as the time (in msec) it takes for the compressor's gain to rise from 10% to 90% of its final value when the signal level exceeds the threshold. the compressor's release time is defined as the time (in seconds) it takes the compressor's gain to drop from 90% to 10% of its value when the signal level drops below the threshold. the example below illustrates the signal envelope for different release and attack times:
fs = 44100; drc = compressor(-10,5, ... 'samplerate',fs, ... 'attacktime',0.050, ... 'releasetime',0.200, ... 'makeupgainmode','property'); x = [ones(fs,1);0.1*ones(fs,1)]; [y,g] = drc(x); t = (1/fs) * (0: 2*fs - 1); figure subplot(211) plot(t,x); hold on grid on plot(t,y,'r') ylabel('amplitude') legend('input','compressed output') subplot(212) plot(t,g) grid on legend('compressor gain (db)') xlabel('time (sec)') ylabel('gain (db)')
the input maximum level is 0 db, which is above the specified -10 db threshold. the steady-state compressor output for a 0 db input is -10 10/5 = -8 db. the gain is therefore -8 db. the attack time is defined as the time it takes the compressor gain to rise from 10% to 90% of its final value when the input level goes above the threshold, or in this case, from -0.8 db to -7.2 db. let's find the times at which the gains in the compression stage are equal to -0.8 db and -7.2 db, respectively:
[~,t1] = min(abs(g(1:fs) 0.1 * 8));
[~,t2] = min(abs(g(1:fs) 0.9 * 8));
tattack = (t2 - t1) / fs;
fprintf('attack time is %d s\n',tattack)
attack time is 5.000000e-02 s
the input signal then drops back down to 0, where there is no compression. the release time is defined as the time it takes the gain to drop from 90% to 10% of its absolute value when the input goes below the threshold, or in this case, -7.2 db to -0.8 db. let's find the times at which the gains in the no-compression stage are equal to -7.2 db and -0.8 db, respectively:
[~,t1] = min(abs(g(fs:end) 0.9 * 8));
[~,t2] = min(abs(g(fs:end) 0.1 * 8));
trelease = (t2 - t1) / fs;
fprintf('release time is %d s\n',trelease)
release time is 2.000000e-01 s
the example below illustrates the effect of dynamic range compression on an audio signal. the compression threshold is set to -15 db, and the compression ratio is 7.
framelength = 1024; reader = dsp.audiofilereader('filename', ... 'rockguitar-16-44p1-stereo-72secs.wav', ... 'samplesperframe',framelength); % compressor. threshold = -15 db, ratio = 7 drc = compressor(-15,7, ... 'samplerate',reader.samplerate, ... 'makeupgainmode','property', ... 'kneewidth',5); scope = timescope('samplerate',reader.samplerate, ... 'timespansource','property',... 'timespan',1,'bufferlength',fs*4, ... 'showgrid',true, ... 'layoutdimensions',[2 1], ... 'numinputports',2, ... 'timespanoverrunaction','scroll'); scope.activedisplay = 1; scope.ylimits = [-1 1]; scope.showlegend = true; scope.channelnames = {'original versus compressed audio'}; scope.activedisplay = 2; scope.ylimits = [-6 0]; scope.ylabel = 'gain (db)'; scope.showlegend = true; scope.channelnames = {'compressor gain in db'}; while ~isdone(reader) x = reader(); [y,g] = drc(x); x1 = x(:,1); y1 = y(:,1); scope([x1,y1],g(:,1)) end
simulink version of the multiband dynamic range compression example
the following model implements the multiband dynamic range compression example:
model = 'audiomultibanddynamiccompression';
open_system(model)
in this example, the audio signal is first divided into four bands using a multiband crossover filter. each band is compressed using a separate compressor. the four bands are then recombined to form the audio output. the dynamic range of the uncompressed and compressed signals (defined as the ratio of the largest absolute value of the signal to the signal rms) is computed. to hear the difference between the original and compressed audio signals, toggle the switch on the top level.
the model integrates a user interface (ui) designed to interact with the simulation. the ui allows you to tune parameters and the results are reflected in the simulation instantly. to launch the ui that controls the simulation, click the 'launch parameter tuning ui' link on the model.
set_param(model,'stoptime','(1/44100) * 8192 * 20'); sim(model);
close the model:
bdclose(model)
matlab version of the multiband dynamic range compression example
helpermultibandcompressionsim
is the matlab function containing the multiband dynamic range compression example's implementation. it instantiates, initializes and steps through the objects forming the algorithm.
the function multibandaudiocompressionexampleapp
wraps around helpermultibandcompressionsim
and iteratively calls it. it also plots the uncompressed versus compressed audio signals. plotting occurs when the plotresults
input to the function is 'true'.
execute multibandaudiocompressionexampleapp
to run the simulation and plot the results on scopes. note that the simulation runs for as long as the user does not explicitly stop it.
multibandaudiocompressionexampleapp
launches a ui designed to interact with the simulation. the ui allows you to tune parameters and the results are reflected in the simulation instantly. for more information on the ui, please refer to helpercreateparamtuningui
.
matlab coder can be used to generate c code for the function helpermultibandcompressionsim
. in order to generate a mex-file for your platform, execute helpermultibandcompressioncodegeneration
.
by calling the wrapper function multibandaudiocompressionexampleapp
with 'true'
as an argument, the generated mex-file can be used instead of helpermultibandcompressionsim
for the simulation. in this scenario, the ui is still running inside the matlab environment, but the main processing algorithm is being performed by a mex-file. performance is improved in this mode without compromising the ability to tune parameters.
call multibandaudiocompressionexampleapp(true)
to use the mex-file for simulation. again, the simulation runs till the user explicitly stops it from the ui.