Top Banner
© 2003 The MathWorks, Inc. 1 Importing C code into Simulink for Signal Processing Applications © 2003 The MathWorks, Inc. Colin Warwick [email protected] #2 in the webinar series “Today’s Simulink for Wireless Design” MATLAB and Simulink are registered trademarks of The MathWorks, Inc.
21

© 2003 The MathWorks, Inc. 1 Importing C code into Simulink for Signal Processing Applications © 2003 The MathWorks, Inc. Colin Warwick [email protected].

Jan 14, 2016

Download

Documents

Verity Banks
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: © 2003 The MathWorks, Inc. 1 Importing C code into Simulink for Signal Processing Applications © 2003 The MathWorks, Inc. Colin Warwick cwarwick@mathworks.com.

© 2

00

3 T

he M

ath

Work

s, Inc.

1

Importing C code into Simulinkfor Signal Processing Applications

© 2

00

3 T

he M

ath

Work

s, Inc.

Colin Warwick

[email protected]

#2 in the webinar series “Today’s Simulink for Wireless Design”

MATLAB and Simulink are registered trademarks of The MathWorks, Inc.

Page 2: © 2003 The MathWorks, Inc. 1 Importing C code into Simulink for Signal Processing Applications © 2003 The MathWorks, Inc. Colin Warwick cwarwick@mathworks.com.

© 2

00

3 T

he M

ath

Work

s, Inc.

2

Agenda

What's available? Interoperability between MATLAB, Simulink, and C

Why call C from Simulink? The MathWorks’ System-Level Design Flow

How? S-function Builder block – new in R13 (August 2002) C-code S-function – full API

Same “full strength” API as we use to build our blocks

Webex live Q&A

Page 3: © 2003 The MathWorks, Inc. 1 Importing C code into Simulink for Signal Processing Applications © 2003 The MathWorks, Inc. Colin Warwick cwarwick@mathworks.com.

© 2

00

3 T

he M

ath

Work

s, Inc.

3

Flexible usage model: Six permutations

C

SimulinkMATLAB

From Simulink,

call C

Page 4: © 2003 The MathWorks, Inc. 1 Importing C code into Simulink for Signal Processing Applications © 2003 The MathWorks, Inc. Colin Warwick cwarwick@mathworks.com.

© 2

00

3 T

he M

ath

Work

s, Inc.

4

Flexible usage model: Six permutations

S-function BuilderC-code S-function

CSimulink

M-code S-functionCaution: interpreted, not compiledMATLABSimulink

>> sim(‘mymodel’)SimulinkMATLAB

MEXActiveX/COM & DDE

CMATLAB

Engine or COM[x,y,t] = sim(‘mymodel’,u0)SimulinkC

MATLAB EngineActiveX/COM & DDEMATLAB Compiler & runtime

MATLABC

How?Call…From…

Page 5: © 2003 The MathWorks, Inc. 1 Importing C code into Simulink for Signal Processing Applications © 2003 The MathWorks, Inc. Colin Warwick cwarwick@mathworks.com.

© 2

00

3 T

he M

ath

Work

s, Inc.

5

MATLAB

Simulink

Why use Simulink as a framework for your C-code?

Capture and validate an executable specification before component-level implementation

Use our blocks as a first cut and as a test harness

Capture your IP in C using Simulink as a framework

Validated Executable Specification

DSP/MCUSoftware Tools

EDA Tools

Analog & DigitalHardware

DSP & MCUSoftware

IMPLEMENTATION TOOLS

C

Page 6: © 2003 The MathWorks, Inc. 1 Importing C code into Simulink for Signal Processing Applications © 2003 The MathWorks, Inc. Colin Warwick cwarwick@mathworks.com.

© 2

00

3 T

he M

ath

Work

s, Inc.

6

Why use Simulink as a framework for your C-code? Jump start your project

Prebuilt Blocksets and application examples MathWorks, 3rd parties

Test bench surrounding your product Stimulus generation

From a simple sin wave to a complete transmitter & channel model

Response instrumentation, visualization Time (eye), I-Q (constellation), frequency (spectra)

M-file scripting (test executive), BER versus Eb/No plot Block diagram paradigm

Intuitive capture of concurrent simulation time Divide and conquer Self-documenting

Algorithm visualization Library of reusable blocks

Page 7: © 2003 The MathWorks, Inc. 1 Importing C code into Simulink for Signal Processing Applications © 2003 The MathWorks, Inc. Colin Warwick cwarwick@mathworks.com.

© 2

00

3 T

he M

ath

Work

s, Inc.

7

Structure of code to be imported: `core and leaves´

test_slicer.exe

//test_slicer.cmain(){ for(){ slicer(); }}

//slicer.cslicer(){ return;}

t

Page 8: © 2003 The MathWorks, Inc. 1 Importing C code into Simulink for Signal Processing Applications © 2003 The MathWorks, Inc. Colin Warwick cwarwick@mathworks.com.

© 2

00

3 T

he M

ath

Work

s, Inc.

8

Try it! Quick demo. Today’s demos use…

Simulink(simulink.dll & matlab.exe)

Optional: Symbolic DebuggerSee Tech Note 1601 for a list

User-defined Blocks (“S-function .dll”)in a block diagram (“Model” .mdl)

MATLAB Central

.c .h

Compiler: see Tech Note 1601 for a list

Page 9: © 2003 The MathWorks, Inc. 1 Importing C code into Simulink for Signal Processing Applications © 2003 The MathWorks, Inc. Colin Warwick cwarwick@mathworks.com.

© 2

00

3 T

he M

ath

Work

s, Inc.

9

Structure of code after import: `wrapped leaves´ Call your `leaf´ code from a wrapper to interface with

Simulink Compile both and link .dll Simulink calls the DLL using several functions

(“methods”) wrap_slicer.dll

Simulink //wrap_slicer.cmdlOutputs(S){ slicer();}

//slicer.c//definition// of//slicer()

UNIX is a registered trademark of The Open Group

t

Page 10: © 2003 The MathWorks, Inc. 1 Importing C code into Simulink for Signal Processing Applications © 2003 The MathWorks, Inc. Colin Warwick cwarwick@mathworks.com.

© 2

00

3 T

he M

ath

Work

s, Inc.

10

The “S-function” DLL contains several methods

mdlInitializeSizes()

mdlInitializeSampleTimes()

mdlOutputs()

mdlTerminate()

yt = f(t, ut)y = outputs, t = timeu = inputs

Done?n

y

Page 11: © 2003 The MathWorks, Inc. 1 Importing C code into Simulink for Signal Processing Applications © 2003 The MathWorks, Inc. Colin Warwick cwarwick@mathworks.com.

© 2

00

3 T

he M

ath

Work

s, Inc.

11

What about leaf functions with states?

States preserved across time steps Independent set of states per instance

FIR, IIR, coders, decoders Also known as...

Computer science: “Re-entrant persistent storage” Object-oriented: “property” C++: “member variable”

Different from… static variables in C are persistent but shared, not re-

entrant If a leaf contains static data it can only be instantiated

once

int always_one(void){ int y = 0; y = y + 1; return y;}

int up(void){ static int y = 0; y = y + 1; return y;}

int counter(int *z){ *z = *z + 1; return *z;}

Page 12: © 2003 The MathWorks, Inc. 1 Importing C code into Simulink for Signal Processing Applications © 2003 The MathWorks, Inc. Colin Warwick cwarwick@mathworks.com.

© 2

00

3 T

he M

ath

Work

s, Inc.

12

Two main types of state available in Simulink

Discrete states, xd

Read-only (const) in the mdlOutput() method Available in S-Function Builder block and full S-function

Work vectors, w Read-write in mdlOutput() Presently only accessible from full S-function API

statesxd, w

input u output y=f(t,xd,w,u)

Page 13: © 2003 The MathWorks, Inc. 1 Importing C code into Simulink for Signal Processing Applications © 2003 The MathWorks, Inc. Colin Warwick cwarwick@mathworks.com.

© 2

00

3 T

he M

ath

Work

s, Inc.

13

Approach depends on code structure

1) “Output-update” structure – use S-function Builder block Leaf with separate output and update functions Use “discrete states”, update them in mdlUpdate()

2) General structure or advanced needs – use full S-function Leaf with output and update functions intermixed Use “work vectors” , update them in mdlOutputs()

yt = f(t, xt, ut), xt+ t = g(t, xt, ut)y = outputs, t = time, x = states, u = inputs

[xt+ t , yt]= f(t, xt, ut)

Page 14: © 2003 The MathWorks, Inc. 1 Importing C code into Simulink for Signal Processing Applications © 2003 The MathWorks, Inc. Colin Warwick cwarwick@mathworks.com.

© 2

00

3 T

he M

ath

Work

s, Inc.

14

1) “Output-update” leaf structure

mdlInitializeSizes()

mdlInitializeSampleTimes()

mdlOutputs()

mdlTerminate()

xt+t = g(t, xt, ut)Done?

n

y

mdlUpdate()

yt = f(t, xt, ut)y = outputs, t = timex = states, u = inputs

“Division of labor is preferred”

Page 15: © 2003 The MathWorks, Inc. 1 Importing C code into Simulink for Signal Processing Applications © 2003 The MathWorks, Inc. Colin Warwick cwarwick@mathworks.com.

© 2

00

3 T

he M

ath

Work

s, Inc.

15

2) General leaf structure

mdlInitializeSizes()

mdlInitializeSampleTimes()

mdlOutputs()

mdlTerminate()

yt = f(t, wt, ut)wt+t = g(t, wt, ut)y = outputst = timew = work vecu = inputs

Done?n

y

Page 16: © 2003 The MathWorks, Inc. 1 Importing C code into Simulink for Signal Processing Applications © 2003 The MathWorks, Inc. Colin Warwick cwarwick@mathworks.com.

© 2

00

3 T

he M

ath

Work

s, Inc.

16

Demonstrations

S-function Builder block features Slicer: a memoryless block IIR Filter: a block with memory

Output-update structure S-function Builder A Discrete state Output in mdlOutputs()and update in mdlUpdate()

General structure Full S-function 1-element Work vector Output and update in mdlOutputs()

Using the sample code as a resource Tour of the S-function reference material

Page 17: © 2003 The MathWorks, Inc. 1 Importing C code into Simulink for Signal Processing Applications © 2003 The MathWorks, Inc. Colin Warwick cwarwick@mathworks.com.

© 2

00

2 T

he M

ath

Work

s, Inc.

17

Summary

Page 18: © 2003 The MathWorks, Inc. 1 Importing C code into Simulink for Signal Processing Applications © 2003 The MathWorks, Inc. Colin Warwick cwarwick@mathworks.com.

© 2

00

3 T

he M

ath

Work

s, Inc.

18

MATLAB

Simulink

Why use Simulink as a framework for your C-code?

Capture and validate an executable specification before component-level implementation

Use our blocks as a first cut and as a test harness

Capture your IP in C using Simulink as a framework

Validated Executable Specification

DSP/MCUSoftware Tools

EDA Tools

Analog & DigitalHardware

DSP & MCUSoftware

IMPLEMENTATION TOOLS

C

Page 19: © 2003 The MathWorks, Inc. 1 Importing C code into Simulink for Signal Processing Applications © 2003 The MathWorks, Inc. Colin Warwick cwarwick@mathworks.com.

© 2

00

3 T

he M

ath

Work

s, Inc.

19

Further Information on Products and Services

Twelve compilers across nine platforms - Tech Note 1601 mathworks.com/support/tech-notes/v5/1600/1601.shtml

Documentation, including templates and sample code Writing S-functions

File Download for this webinar matlabcentral.com

Consulting – Jump Start mathworks.com/consulting

Thankyou!

Page 20: © 2003 The MathWorks, Inc. 1 Importing C code into Simulink for Signal Processing Applications © 2003 The MathWorks, Inc. Colin Warwick cwarwick@mathworks.com.

© 2

00

2 T

he M

ath

Work

s, Inc.

20

Webex Q&A session

Page 21: © 2003 The MathWorks, Inc. 1 Importing C code into Simulink for Signal Processing Applications © 2003 The MathWorks, Inc. Colin Warwick cwarwick@mathworks.com.

© 2

00

2 T

he M

ath

Work

s, Inc.

21

Webinar End

Thank You