Top Banner
1 © 2012 The MathWorks, Inc. Signal Processing with MATLAB: System Simulation and Real-Time Implementation Gabriele Bunkheila Senior Application Engineer
43

Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

Mar 08, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

1 © 2012 The MathWorks, Inc.

Signal Processing with MATLAB:

System Simulation and Real-Time

Implementation

Gabriele Bunkheila

Senior Application Engineer

Page 2: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

2

Session Highlights

Efficient system simulations in MATLAB for DSP,

communications, computer vision, and radar systems

Latest capabilities to generate and customise

embeddable C code directly from MATLAB

Guidance and tools to aid preparation of MATLAB code

for implementation

FPGA/ASIC verification and implementation directly

from MATLAB

Page 3: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

3

MATLAB for Signal Processing – three key

requirement areas

Application-relevant algorithms

Faster simulations

Path to implementation on real-time targets

Page 4: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

4

Agenda

Workflow from MATLAB algorithms to embeddable C

and simulation acceleration

System simulation algorithms in the area of Signal

Processing

Workflow from algorithms to HDL

Page 5: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

5

Example – tracking an acoustic source

Simple example

MATLAB validation harness for

graphics and display, calling out

to compiled code

Algorithm coded in C

– Auto-generated from MATLAB

code

– Portable to virtually any software

platform

– Comfortably running live on a PC

Page 6: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

6

Application overview

delay

Sound source

delay

𝜃

ALGORITHM

d

d

Page 7: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

7

Applications

Sniper detection

Hands-free conferencing

systems

Speaker identification

Gaming applications

Other security applications

Page 8: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

8

From MATLAB algorithms to real-time code

Experiment with algorithm in MATLAB

Architect/review/optimize MATLAB code

Generate real-time source C/C++ code

Verify/validate generated code

Optimize generated code

Page 9: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

9

From MATLAB algorithms to real-time code

Experiment with algorithm in MATLAB

Architect/review/optimize MATLAB code

Generate real-time source C/C++ code

Verify/validate generated code

Optimize generated code

Page 10: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

10

Step 1 – Explore algorithm in MATLAB

Simplify:

– Get a static source

– Take a picture

– Record the stereo audio input

Understand your setup

– What distance between microphones?

stillimage.jpg

stereorecording.wav

Page 11: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

12

From MATLAB algorithms to real-time code

Experiment with algorithm in MATLAB

Architect/review/optimize MATLAB code

Generate real-time source C/C++ code

Verify/validate generated code

Optimize generated code

Page 12: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

13

Package algorithm as a function

Minimum requirement for

– Testability

– Re-usability

– C code generation

“script”

“code”

“program” test

ben

ch

function

Page 13: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

14

Reviewing our objectives – things to keep in mind

MATLAB

Function

Real-time

embeddable C

Accelerate execution

through C

Efficiency

How you write

MATLAB code

Performance and

quality of generated C Greatly impacts

Page 14: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

15

Interpolation in a nutshell

Includes

– Up-sampling input signal (Inserting zeros between samples)

– Filtering with a low-pass filter

Before After

Page 15: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

16

How many ways to do this in MATLAB?

All times Only once

Insert zeros

and filter

conventionally

interp

upsample

filter(b, a, x)

persistent

Use efficient

filtering structures

(e.g. polyphase)

filter(mfilt) step(dsp.FIRInterpolator)

How often to compute filter coefficients?

How optimized a filtering algorithm

… ?

Page 16: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

17

Investigate efficiency of MATLAB code

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

How long did it take? Where are the bottlenecks?

Page 17: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

18

Profile and review

Before After

Interpolation: 5x faster

Algorithm: > 2x faster

Page 18: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

19

Lesson learnt

Separate initialization and setup from recurring

execution

test

ben

ch

function

test

ben

ch

fun

cti

on

init/setup for / while

end

process

Page 19: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

20

From MATLAB algorithms to real-time code

Experiment with algorithm in MATLAB

Architect/review/optimize MATLAB code

Generate real-time source C/C++ code

Verify/validate generated code

Optimize generated code

Page 20: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

22

Translating MATLAB to C – Typical use cases

Hand-off code to software engineers for

implementation (e.g. on embedded processor)

Integrate MATLAB algorithms w/ existing C

environment

Deploy MATLAB algorithms on Windows/Linux

desktop PC

Accelerate MATLAB algorithms

Page 21: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

23

Same algorithm, different performance

tic/toc timing for different existing versions of the same

algorithm

What did the trick?

– Better-structured algorithms

– Automatic C code generation

----------------------------------------------------------------------------------------------

Versions of the Algorithm | Elapsed Time (sec)| Acceleration Ratio

1. Traditional use of MATLAB functions | 7.3740 | 1.0000

2. Separating setup and execution | 3.8605 | 1.9101

3. MATLAB Coder MEX version | 0.8062 | 9.1462

----------------------------------------------------------------------------------------------

Page 22: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

24

java

visualization

analysis

nested

functions

sparse

variable-sized data

arrays

struct

numeric

fixed-point

functions

complex

System objects global

persistent classes

MATLAB Language Support for Code Generation

cell arrays

varargin

• Functions supported for code generation

• System objects supported for code generation

Page 23: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

25

From MATLAB algorithms to real-time code

Experiment with algorithm in MATLAB

Architect/review/optimize MATLAB code

Generate real-time source C/C++ code

Optimize generated code

Verify/validate generated code

Page 24: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

26

Calling out to existing C code from MATLAB

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

Use cases:

Optimized C/C++ code

Legacy code

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

Interacting with larger C/C++ application

Verifying C/C++ using existing MATLAB testbench

Page 25: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

27

Tools for target-specific C code optimization

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 26: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

29

From MATLAB algorithms to real-time code

Experiment with algorithm in MATLAB

Architect/review/optimize MATLAB code

Generate real/time source C/C++ code

Verify/validate generated code

Optimize generated code

Page 28: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

31

Agenda

Workflow from MATLAB algorithms to embeddable C

and simulation acceleration

System simulation algorithms in the area of Signal

Processing

Workflow from algorithms to HDL

Page 29: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

33

System Toolboxes for Signal Processing

System Design & Implementation for MATLAB and Simulink

Page 30: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

34

Lesson learnt

Separate initialization and setup from recurring

execution

test

ben

ch

function

test

ben

ch

fun

cti

on

init/setup for / while

end

process

Page 31: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

35

Two entry points for MATLAB programming

Data analysis

Algorithm investigation

[Functions]

System simulation

Efficient C implementation

[System objects]

% instatiate first

filt2D = vision.ImageFilter(...

'Coefficients', [1 -1; 0 1]);

% iteratively process streamed data

while(…)

% process

outImage = filt2D.step(inImage);

end

% All happens in one place

outImage = ...

imfilter(inImage, [1 -1; 0 1])

Page 32: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

36

Using System objects on our example

% Process audio data in a loop

for k = 1:nFrames

inInt = step(Interpolator, in);

xc = step(Correlator, inInt(:,1), inInt(:,2));

[~,maxIdx] = max(xc);

end

• Create objects and configure parameters

• Call step on each object to perform its computation

% Create and initialize objects

Interpolator = dsp.FIRInterpolator('Numerator',b);

Correlator = dsp.Crosscorrelator; init/setup

for / while

end

process

Page 33: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

38

Phased Array System Toolbox

Tools and algorithms to model and simulate every aspect

of a phased array signal processing system:

Array design and analysis

Waveform design and analysis

Transmitter and receiver modeling

Target and environment modeling

Temporal processing

Spatial processing

Space-time adaptive processing

10-10

10-8

10-6

10-4

10-2

100

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

SNR=0dB

SNR=3dB

SNR=10dB

SNR=13dB

NonfluctuatingCoherent Receiver Operating Characteristic (ROC) Curves

Pfa

Pd

0 0.005 0.01 0.015 0.02 0.025 0.03

-300

-280

-260

-240

-220

-200

-180

-160

-140

-120

Time (ms)

Pow

er

(dB

w)

Target Range Estimation

Page 34: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

39

Phased Array Systems –

Example of a System-Level Block Diagram

Waveform

Generator Transmitter

Transmit

Array

Signal

Processing Receiver

Receive

Array

Environment,

Targets, and

Interference

Page 35: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

40

Example – modelling a beamscan radar

Monostatic radar

30 x 30 rectangular array

Beamscan for angle

resoution

Detect two moving

targets

Page 36: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

41

System Toolboxes

System Design & Implementation for MATLAB and Simulink

• Libraries of algorithms for system implementation

• Support both MATLAB and Simulink

- Functions, System objects, Blocks

• Support Coder products

• Execution acceleration

• Embedded deployment

• Optionally include analysis tools for

design-space exploration, verification

Page 37: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

42

Desktop Acceleration: Growing number of

GPU implementations

Addressing application-specific bottlenecks

Page 38: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

43

Agenda

Workflow from MATLAB algorithms to embeddable C

and simulation acceleration

System simulation algorithms in the area of Signal

Processing

Workflow from algorithms to HDL

Page 39: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

44

From models to FPGAs and ASICs

Further challenges and desirable capabilities

Fixed-point design, resource optimization

Auto HDL code generation

Auto elaboration of high-level design choices

Re-using models for HDL verification

Page 40: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

45

HDL Code Generation from

MATLAB and Simulink

Generate VHDL and Verilog

code for FPGA and ASIC

design

One HDL code generation

product for both MATLAB

and Simulink

Generates portable HDL

code for ASIC, FPGA, and

RP/HIL

HDL

Coder

MATLAB Simulink

HDL Code

Page 41: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

46

HDL Coder (R2012a and R2012b)

Floating-point to fixed-point

conversion and HDL code

generation

HDL optimizations for

MATLAB designs

Integration with simulation

and synthesis tools

Inference, traceability, and

resource reports

MATLAB to HDL

Over 20 new optimization,

modeling, and HDL code

generation enhancements

Floating-point library support

for Xilinx and Altera

Xilinx System Generator

Subsystem

Simulink to HDL

Page 42: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

49

(Generated) Fixed-Point MATLAB Code

Page 43: Signal Processing with MATLAB: System Simulation and Real-Time … · 3 MATLAB for Signal Processing – three key requirement areas Application-relevant algorithms Faster simulations

53

Summary

Flow from MATLAB to C/C++

– Acceleration

– Embeddable source code

Signal Processing – new System Toolboxes

– Application-specific

– Efficient simulation

– Implementation-ready

New flow from MATLAB to HDL

– Auto code generation

– Verification