Signal Processing with MATLAB: System Simulation
and Real-Time Implementation
Jonas Rutström
Senior Application Engineer
© 2014 The MathWorks, Inc.
1
Why are we here today?
Learn more about algorithm and system design in MATLAB and Simulink
– The why, how, and what…
Hopefully get some new ideas that makes you work easier
– There is always someting new to learn…
Give you the possibility to talk with MathWorks representatives
– Share your thoughts, give us feedback – We are here for you!
2
Algorithm Development Hardware Implementation
ALGORITHM
3
DEMO
(Kinect)
4
From MATLAB Script to Real-Time C Code
5
MATLAB
1. Experiment with algorithm in MATLAB
2. Customize the MATLAB code
3. Generate C/C++ code
4. Verify/validate generated code
5. Optimize generated code
C/C++
6
MATLAB
1. Experiment with algorithm in MATLAB
2. Customize the MATLAB code
3. Generate C/C++ code
4. Verify/validate generated code
5. Optimize generated code
C/C++
7
A closer look at the algorithm…
8
Sound source
ALGORITHM
delay
𝜃 d
delay
9
How to calculate the delay?
10
Delay
Cross-correlation Interpolation
11
1. Experiment with algorithm in MATLAB
Simplify:
– Get a static source
– Take a picture
– Record the stereo audio input
Understand your setup
– Distance between microphones?
– Resonable delay?
Implement according to the masterplan
– verify the result
12
DEMO
(MATLAB Script)
13
1. Experiment with algorithm in MATLAB
14
What is the next step?
15
MATLAB
1. Experiment with algorithm in MATLAB
2. Customize the MATLAB code
3. Generate C/C++ code
4. Verify/validate generated code
5. Optimize generated code
C/C++
16
How do you intend to use the algorithm?
17
18
“Real-Time”
19
Function + Test bench
20
2. Customize the MATLAB code
Testability
Algorithm Re-usability
C code generation
21
2. Customize the MATLAB code
Testability
Algorithm
Algorithm
Re-usability
Algorithm
Algorithm
Algorithm
C code generation
22
2. Customize the MATLAB code
Testability
Re-usability
C code generation
23
DEMO
(Algorithm + Test Bench + Code Generation)
24
2. Customize the MATLAB Code
25
Performance?
26
2. Customize the MATLAB Code
How long did it take? Where are the bottlenecks?
tic/toc profile
% start timer % turn on profiler
tic profile on
% execute code % execute code
out = myFunction(in); out = myFunction(in);
% stop timer (and store % turn off profiler
% elapsed time) profile off
et = toc; % open html report
profile report
27
2. Architect/review/optimize MATLAB code
Before After
Let´s profile two versions of the interpolate function!
28
DEMO
(Profiling)
29
2. Architect/review/optimize MATLAB code
Alternative 1 Alternative 2
30
2. Architect/review/optimize MATLAB code
Alternative 1 Alternative 2
Interpolation: 5x faster Algorithm: > 2x faster
31
2. Architect/review/optimize MATLAB code
Lesson learned from profiling?
– Separate initialization and setup from recurring execution
32
Can we make it execute faster?
33
MATLAB
1. Experiment with algorithm in MATLAB
2. Customize the MATLAB code
3. Generate C/C++ code
4. Verify/validate generated code
5. Optimize generated code
C/C++
34
DEMO
(MATLAB to C MEX)
35
3. Generate C/C++ Code
~8x faster
36
Are there other implementation options?
37
System Objects
38
2. Architect/review/optimize MATLAB code
init/setup
process
39
2. Architect/review/optimize MATLAB code
~ 2X faster in MATLAB
40
3. Generate C/C++ Code
~3x faster in C
41
3. Generate C/C++ Code
42
3. Generate C/C++ Code
43
44
45
MATLAB
1. Experiment with algorithm in MATLAB
2. Customize the MATLAB code
3. Generate C/C++ code
4. Verify/validate generated code
5. Optimize generated code
C/C++
46
4. Verify/Validate Generated Code
Software-in-the-Loop Verification
47
DEMO
(Software-in-the-Loop)
48
MATLAB
1. Experiment with algorithm in MATLAB
2. Customize the MATLAB code
3. Generate C/C++ code
4. Verify/validate generated code
5. Optimize generated code
C/C++
49
4. Optimize Generated Code
Operator level (e.g. fixed-point arithmetic)
c = a + b
*c = _sadd(a, b)
Routine level coder.replace
out = matlabFcn(in)
optimFcn(*in, *out)
Solution: Code Replacement Libraries (CRL)
(requires Embedded Coder)
50
4. Optimize Generated Code
Calling legacy code
Non-algorithmic code (e.g. peripherals, profiling)
Interacting with larger C/C++ application
Verifying C/C++ using existing MATLAB testbench
coder.ceval('cfun_name', arg1, arg2, …)
51
What if software is not fast enough?
52
HW/SW Co-Design
= +
System Hardware Software
53
HW/SW Co-Design
The Zynq Platform - Example
ARM FPGA
54
Generating HDL Code
MATLAB HDL
55
DEMO
(MATLAB HDL Code)
56
Verifying Handwritten HDL Code
Manual Translation
HDL
MATLAB
Manual Verification
57
DEMO
(Verification of Handwritten HDL Code)
58
Algorithm Development Hardware Implementation
ALGORITHM
59
Summary | Algorithm Development
MATLAB
1. Experiment with algorithm in MATLAB
2. Customize the MATLAB code
3. Generate C/C++ code
4. Verify/validate generated code
5. Optimize generated code
C/C++
60
Summary | Hardware Implementation
= +
System Hardware Software
“Integrated Workflow for both SW and HW System Design”
61
Wrap Up and Q & A
More information…
Contact us for more information:
www.mathworks.com
62