signal processing with matlab: system simulation and real-time implementation · 2 why are we here...

62
1 © 2014 The MathWorks, Inc. Signal Processing with MATLAB: System Simulation and Real-Time Implementation Jonas Rutström Senior Application Engineer

Upload: others

Post on 08-Mar-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

1© 2014 The MathWorks, Inc.

Signal Processing with MATLAB: System Simulation

and Real-Time Implementation

Jonas Rutström

Senior Application Engineer

Page 2: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

2

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!

Page 3: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

3

Algorithm Development

ALGORITHM

Hardware Implementation

Page 4: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

4

DEMO(Kinect)

Page 5: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

5

From MATLAB Script to Real-Time C Code

Page 6: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

6

C/C++

MATLAB1. Experiment with algorithm in MATLAB

2. Customize the MATLAB code

3. Generate C/C++ code

4. Verify/validate generated code

5. Optimize generated code

Page 7: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

7

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++

MATLAB

Page 8: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

8

A closer look at the algorithm…

Page 9: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

9

delay

Sound source

delay

𝜃

ALGORITHM

d

d

Page 10: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

10

How to calculate the delay?

Page 11: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

11

Cross-correlation Interpolation

Delay

Page 12: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

12

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

Page 13: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

13

DEMO(MATLAB Script)

Page 14: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

14

1. Experiment with algorithm in MATLAB

Page 15: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

15

What is the next step?

Page 16: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

16

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++

MATLAB

Page 17: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

17

How do you intend to use the algorithm?

Page 18: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

18

Page 19: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

19

“Real-Time”

Page 20: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

20

Function + Test bench

Page 21: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

21

2. Customize the MATLAB code

Testability

Re-usability

C code generation

Algorithm

Page 22: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

22

2. Customize the MATLAB code

Testability

Re-usability

C code generation

AlgorithmAlgorithm

AlgorithmAlgorithm

Algorithm

Page 23: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

23

2. Customize the MATLAB code

Testability

Re-usability

C code generation

Page 24: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

24

DEMO(Algorithm + Test Bench + Code Generation)

Page 25: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

25

2. Customize the MATLAB Code

Page 26: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

26

Performance?

Page 27: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

27

tic/toc

% start timer

tic

% execute code

out = myFunction(in);

% stop timer (and store

% elapsed time)

et = toc;

profile

% turn on profiler

profile on

% execute code

out = myFunction(in);

% turn off profiler

profile off

% open html report

profile report

Where are the bottlenecks?

2. Customize the MATLAB Code

How long did it take?

Page 28: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

28

Before After

2. Architect/review/optimize MATLAB code

Let´s profile two versions of the interpolate function!

Page 29: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

29

DEMO(Profiling)

Page 30: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

30

Alternative 1 Alternative 2

2. Architect/review/optimize MATLAB code

Page 31: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

31

Alternative 1 Alternative 2

Interpolation: 5x faster Algorithm: > 2x faster

2. Architect/review/optimize MATLAB code

Page 32: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

32

Lesson learned from profiling?

– Separate initialization and setup from recurring execution

2. Architect/review/optimize MATLAB code

Page 33: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

33

Can we make it execute faster?

Page 34: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

34

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++

MATLAB

Page 35: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

35

DEMO(MATLAB to C MEX)

Page 36: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

36

~8x faster

3. Generate C/C++ Code

Page 37: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

37

Are there other implementation options?

Page 38: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

38

System Objects

Page 39: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

39

2. Architect/review/optimize MATLAB code

init/setup

process

Page 40: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

40

2. Architect/review/optimize MATLAB code

~ 2X faster in MATLAB

Page 41: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

41

~3x faster in C

3. Generate C/C++ Code

Page 42: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

42

3. Generate C/C++ Code

Page 43: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

43

3. Generate C/C++ Code

Page 44: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

44

Page 45: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

45

Page 46: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

46

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++

MATLAB

Page 47: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

47

4. Verify/Validate Generated Code

Software-in-the-Loop Verification

Page 48: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

48

DEMO(Software-in-the-Loop)

Page 49: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

49

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++

MATLAB

Page 50: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

50

4. Optimize Generated Code

Operator level (e.g. fixed-point arithmetic)

c = a + b

*c = _sadd(a, b)

Routine level

out = matlabFcn(in)

optimFcn(*in, *out)

Solution: Code Replacement Libraries (CRL)(requires Embedded Coder)

coder.replace

Page 51: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

51

4. Optimize Generated Code

coder.ceval('cfun_name', arg1, arg2, …)

Calling legacy code

Non-algorithmic code (e.g. peripherals, profiling)

Interacting with larger C/C++ application

Verifying C/C++ using existing MATLAB testbench

Page 52: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

52

What if software is not fast enough?

Page 53: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

53

HW/SW Co-Design

= +SoftwareHardwareSystem

Page 54: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

54

HW/SW Co-DesignThe Zynq Platform - Example

FPGAARM

Page 55: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

55

HDL MATLAB

Generating HDL Code

Page 56: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

56

DEMO(MATLAB HDL Code)

Page 57: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

57

HDL

Manual Translation

Manual Verification

MATLAB

Verifying Handwritten HDL Code

Page 58: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

58

DEMO(Verification of Handwritten HDL Code)

Page 59: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

59

Algorithm Development

ALGORITHM

Hardware Implementation

Page 60: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

60

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++

MATLAB

Summary | Algorithm Development

Page 61: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

61

Summary | Hardware Implementation

= +SoftwareHardwareSystem

“Integrated Workflow for both SW and HW System Design”

Page 62: Signal Processing with MATLAB: System Simulation and Real-Time Implementation · 2 Why are we here today? Learn more about algorithm and system design in MATLAB and Simulink –The

62

Contact us for more information:

www.mathworks.com

Wrap Up and Q & AMore information…