Audio Signal Processing in MATLAB
Youssef Abdelilah
Senior Product Manager
© 2014 The MathWorks, Inc.
1
Agenda
Tunable parametric equalizer example
Audio tone removal example
How to create a streaming test bench for audio processing
1 in MATLAB
How to develop algorithms and incorporate them into the
2 test bench
How to accelerate simulation for real-time performance
3
2
Stream processing of audio is everywhere!
Tablet/MP3 Player & Smart Phone Gaming System
Automotive Infotainment
Professional Audio & Music
Medical Devices-Hearing Aids
3
Tunable parameter equalizer example
MIDI Control
Tune parameters
in real-time
See it
Tune it
Audio Input Array Plot
Guitar10min.ogg Parameter
a 44.1Khz stereo Equalizer
audio Filters
Audio Output Visualize audio
Play it Custom Audio Algorithm waveforms in real-time
Create it
Speaker
Hear it
4
Challenges in audio system design
Framework for real-time simulations
“I have to process large data and test my simulations with streaming signals. I need a simulation
testbench that can keep up with real-time data.”
Quick Innovation & modeling
“I need to find innovative algorithms and create and model a
working system very quickly.”
Rapid prototyping & simulation acceleration
“I need to optimize my high-level MATLAB algorithm for speed. I then need to verify that the optimized code
works the same way as the original MATLAB code.”
5
What DSP System Toolbox offers in MATLAB
Framework for real-time simulations
Stream
“I have processing
to process techniques
large data and andwith
test my simulations hardware
streaming peripheral
signals. I need aaccess
simulationthat
speed
testbench that can upwith
keep up simulation and reduce memory footprint
real-time data.”
Quick Innovation & modeling
Pre-defined algorithms as functions and System objects for
“I need to find innovative algorithms and create and model a
quick prototyping
working system very quickly.”
Rapid prototyping & simulation acceleration
Support for C/C++ code generation
“I need to optimize my high-level MATLAB algorithm for speed.
that Ienables design
then need to continuity
verify that the optimizedand faster simulation
code works
the same way as the original MATLAB code.”
6
Part 1: Test bench and peripheral access
How to create a streaming test bench for audio processing
1 in MATLAB
How to develop algorithms and incorporate them into the
2 test bench
How to accelerate simulation for real-time performance
3
7
Stream processing in MATLAB
Streaming techniques* process continuous data from a captured signal or large file by
dividing it into “frames” and fully processes each frame before the next one arrives
Memory efficient
Streaming algorithms in DSP System Toolbox provide
Implicit data buffering, state management and indexing
Simulation speed-up by reducing overhead
MATLAB Memory
Stream Stream
Source Processing
*http://www.mathworks.com/discovery/stream-processing.html
8
How to create a streaming test bench
Sources Sinks Spectrum Analyzer
Spectrum
Analyzer
Algorithm
under test
Microphone Audio Output
Audio Input Visualize sound in real-time
Speaker
9
How to create test bench in MATLAB
%% Create and Initialize
SamplesPerFrame = 1024;
Fs = 44100; Initialize
Microphone=dsp.AudioRecorder('SamplesPerFrame',SamplesPerFrame);
Spectra=dsp.SpectrumAnalyzer ('SampleRate',Fs);
%% Stream processing loop
tic;
while toc < 20 Process
% Read frame from microphone
audioIn = step(Microphone); in-the-loop
% View audio spectrum
step(Spectra,audioIn);
End
%% Terminate Terminate
release(Microphone)
release(Spectra)
10
How to automatically create test benches from
“Generate DSP Testbench” App
>>HelperGenDSPTestbenchUI
11
Part 2: Algorithms
How to create a streaming test bench for audio processing
1 in MATLAB
How to develop algorithms and incorporate them into the
2 test bench
How to accelerate simulation for real-time performance
3
12
Example 1: Audio Tone Removal
MIDI Control
Tune parameter equalizer in real-time
Tune it
View it
Y Spectrum
Play it Analyzer
Guitar file X Notch-Peak Y
@ 44.1Khz + Filter
Hear it
stereo audio
Audio Input Visualize audio waveform
Y
spectra in real-time
Custom Audio Algorithm Speaker
250Hz
Tone Audio Output
Noise Create it
13
How to incorporate algorithm into test bench
%% Create & Initialize
Fs = 44.1e3;
SamplesPerFrame = 1024;
h = dsp.AudioFileReader('guitar10min.ogg');
hp = dsp.AudioPlayer;
% Interfering tone
ftone = 250;
initialize
hw = dsp.SineWave('Amplitude',0.8,'SampleRate',Fs,’Frequency',[ftone ftone],'SamplesPerFrame',SamplesPerFrame);
hs = dsp.SpectrumAnalyzer('SampleRate',Fs,'SpectralAverages',5,'ShowLegend',true,’Title',titlestr);
%% Stream processing loop
while ~isDone(h)
% Read one frame from audio file and add a tone to input audio
x = step(h) + step(hw);
% Audio Tone Removal algorithm
Process
[y,pauseSim,stopSim]HelperAudioToneRemovalProcessing(x,args,param);
% View audio waveform
in-the-loop
step(hs,[x(:,1),y(:,1),y(:,3)]);
% Start playing audio
step(hp, y(:,1:2));
end
%% Terminate
release(h)
release(hs) Terminate
release(hp)
14
Example 2: Tunable audio parametric equalizer
Tune parameter equalizer
in real-time
MIDI Control
Tune it See it
X H
H=Y/X
Array Plot
Y Transfer Function
Play it
Estimator
Guitar file Parameter Y
Hear it
@ 44.1Khz Equalizer
stereo audio
X Filters Y
Audio Input
Speaker
Custom Audio Algorithm Audio Output
Visualize audio waveform
in real-time
Create it
15
DSP System Toolbox audio related components
(supported on Apple/Windows/Linux)
– Multichannel audio I/O (Number of channels depends on hardware)
Audio Player/Recorder
(Supports multiple devices, one sound driver per MATLAB session)
Audio File Reader/Writer
ASIO low latency driver support on Windows(R)
Custom channel mapping
– Audio signal analysis
Scopes: time, spectrum analyzer, Array plot
Transfer function estimator
Measurements: SNR, THD, Average power, PeaktoRMS ratio, mean, variance, ...
– Signal processing algorithms
FIR, Biquad, Multirate FIR, FFT, LMS, ...
– Connectivity
UDP, MIDI (simultaneous support for multiple controls on multiple devices)
16
Part 3: Acceleration of simulation
How to create a streaming test bench for audio processing
1 in MATLAB
How to develop algorithms and incorporate them into the test
2 bench
How to accelerate simulation for real-time performance
3
17
Stream processing in real-time
Data acquisition & algorithm times
Frame 1 Frame 2 Frame 3 Frame n
Data acquisition Algorithm processing time
As long as
Data acquisition + Algorithm processing <= Frame time
We have
Real-time signal processing
18
Stream processing in real-time
Data acquisition & algorithm times
Frame 1 Frame 2 Frame 3 Frame n
Data acquisition Algorithm processing time
As long as
Data acquisition + Algorithm processing <= Frame time
We have
Real-time signal processing
19
Accelerating algorithm execution*
Optimize MATLAB Pre-allocation and vectorization
Code
User’s Code Pre-defined efficient implementations of algorithms
System Objects
MATLAB to C Generate MEX files automatically with MATLAB Coder
Parallel Computing Parallel computations on multicore computers,
GPUs, and clusters
*Webinar: Accelerating Simulation for Communications Applications
https://www.mathworks.com/company/events/webinars/wbnr78473.html 20
MATLAB to C code generation* MATLAB Coder
function y = audio_algorithm_peqso(u,tunedparams)
% Copyright 2014 The MathWorks, Inc.
persistent PE1 PE2
if isempty(PE1)
PE1 = parametricEQFilter('Bandwidth',2000,…
'CenterFrequency',3000,'PeakGaindB',6.02);
PE2 = ParametricEQFilter('Bandwidth',2000,…
'CenterFrequency',1000,'PeakGaindB',-6.02);
end
[PE1,PE2] = processtunedparams(tunedparams,PE1,PE2);
v = step(PE1,u);
y = step(PE2,v);
%-------------------------------------
function [PE1,PE2] = processtunedparams(tunedparams,PE1,PE2)
if ~isnan(tunedparams.CenterFrequency)
PE1.CenterFrequency = tunedparams.CenterFrequency;
end
if ~isnan(tunedparams.Bandwidth)
PE1.Bandwidth = tunedparams.Bandwidth;
end
if ~isnan(tunedparams.Gain)
PE1.PeakGaindB = tunedparams.Gain;
end
if ~isnan(tunedparams.CenterFrequency2)
PE2.CenterFrequency = tunedparams.CenterFrequency2;
end
if ~isnan(tunedparams.Bandwidth2)
PE2.Bandwidth = tunedparams.Bandwidth2;
end
if ~isnan(tunedparams.Gain2)
PE2.PeakGaindB = tunedparams.Gain2;
end
(*) Design and Prototype Real-Time DSP Systems with MATLAB (Conference Presentation):
http://www.mathworks.com/company/events/conferences/matlab-virtual-conference/2013/proceedings/design-and-prototype-real-time-dsp-systems-with-matlab.html
21
Simulation acceleration benchmarks
2-band parametric equalizer algorithm Processing time
MATLAB code 23.37 seconds
MEX code 2.84 seconds
22
DSP System Toolbox *
Over 300 algorithms for modeling, designing, implementing and deploying dynamic
system applications
●Advanced Filter Design, Adaptive, Multistage and Multi-rate Filters ●Visualization in Time and Frequency-domain
●FFT, DCT & other Transforms ●System objects and functions in MATLAB
●Signal processing blocks for Simulink ●Stream signal Processing
●Support for Fixed-Point, C/C++ code generation and HDL ● ARM Cortex-M support for hardware prototype
Algorithm libraries in MATLAB Algorithm libraries in Simulink
*http://www.mathworks.com/
products/dsp-system/index.html
23
Summary
Create a test bench for audio algorithms with various sources/sinks
AudioRecorder, AudioPlayer, AudioFileReader, Sine wave, white noise, …
Spectrum analyzer, time scope, transfer function estimator, …
Use DSP System Toolbox components (System objects) for algorithms
FIR, FIR Decimation/Interpolation/Rate Conversion, …
Biquad, Allpass, Allpole, …
FFT, DCT, Auto/Cross Correlation, …
Mean, variance, RMS, PeaktoRMS, …
Perform real-time audio signal processing
Accelerate critical components by code-generation (Generating MEX files using MATLAB Coder)
24
THANK YOU!
© 2014The MathWorks, Inc. MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See
ww.mathworks.com/trademarks for a list of additional trademarks. Other product or brand names may be trademarks
25
or registered trademarks of their respective holders.