Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "© 2003 The MathWorks, Inc. 1 Importing C code into Simulink for Signal Processing Applications © 2003 The MathWorks, Inc. Colin Warwick"— Presentation transcript:

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 in the webinar series “Today’s Simulink for Wireless Design” MATLAB and Simulink are registered trademarks of The MathWorks, Inc.

2 © 2003 The MathWorks, 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

3 © 2003 The MathWorks, Inc. 3 Flexible usage model: Six permutations C SimulinkMATLAB From Simulink, call C

4 © 2003 The MathWorks, Inc. 4 Flexible usage model: Six permutations S-function Builder C-code S-function CSimulink M-code S-function Caution: interpreted, not compiled MATLABSimulink >> sim(‘mymodel’) SimulinkMATLAB MEX ActiveX/COM & DDE CMATLAB Engine or COM [x,y,t] = sim(‘mymodel’,u0) SimulinkC MATLAB Engine ActiveX/COM & DDE MATLAB Compiler & runtime MATLABC How?Call…From…

5 © 2003 The MathWorks, Inc. 5 M ATLAB S imulink 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/MCU Software Tools EDA Tools Analog & Digital Hardware DSP & MCU Software IMPLEMENTATION TOOLS C

6 © 2003 The MathWorks, Inc. 6 Why use Simulink as a framework for your C-code? Jump start your project Prebuilt Blocksets and application examples  MathWorks, 3 rd 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 E b /N o plot Block diagram paradigm Intuitive capture of concurrent simulation time Divide and conquer Self-documenting  Algorithm visualization  Library of reusable blocks

7 © 2003 The MathWorks, Inc. 7 Structure of code to be imported: `core and leaves´ test_slicer.exe //test_slicer.c main() { for(){ slicer(); } //slicer.c slicer() { return; } t

8 © 2003 The MathWorks, Inc. 8 Try it! Quick demo. Today’s demos use… Simulink (simulink.dll & matlab.exe) Optional: Symbolic Debugger See 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

9 © 2003 The MathWorks, 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.c mdlOutputs(S) { slicer(); } //slicer.c //definition // of //slicer() UNIX is a registered trademark of The Open Group t

10 © 2003 The MathWorks, Inc. 10 The “S-function” DLL contains several methods mdlInitializeSizes() mdlInitializeSampleTimes() mdlOutputs() mdlTerminate() y t = f(t, u t ) y = outputs, t = time u = inputs Done? n y

11 © 2003 The MathWorks, 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; }

12 © 2003 The MathWorks, Inc. 12 Two main types of state available in Simulink Discrete states, x d  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 states x d, w input u output y=f(t,x d,w,u)

13 © 2003 The MathWorks, 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() y t = f(t, x t, u t ), x t+  t = g(t, x t, u t ) y = outputs, t = time, x = states, u = inputs [x t+  t, y t ]= f(t, x t, u t )

14 © 2003 The MathWorks, Inc. 14 1) “Output-update” leaf structure mdlInitializeSizes() mdlInitializeSampleTimes() mdlOutputs() mdlTerminate() x t+  t = g(t, x t, u t ) Done? n y mdlUpdate() y t = f(t, x t, u t ) y = outputs, t = time x = states, u = inputs “Division of labor is preferred”

15 © 2003 The MathWorks, Inc. 15 2) General leaf structure mdlInitializeSizes() mdlInitializeSampleTimes() mdlOutputs() mdlTerminate() y t = f(t, w t, u t ) w t+  t = g(t, w t, u t ) y = outputs t = time w = work vec u = inputs Done? n y

16 © 2003 The MathWorks, 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

17 © 2002 The MathWorks, Inc. 17 Summary

18 © 2003 The MathWorks, Inc. 18 M ATLAB S imulink 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/MCU Software Tools EDA Tools Analog & Digital Hardware DSP & MCU Software IMPLEMENTATION TOOLS C

19 © 2003 The MathWorks, 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 Thank you!

20 © 2002 The MathWorks, Inc. 20 Webex Q&A session

21 © 2002 The MathWorks, Inc. 21 Webinar End Thank You


Download ppt "© 2003 The MathWorks, Inc. 1 Importing C code into Simulink for Signal Processing Applications © 2003 The MathWorks, Inc. Colin Warwick"

Similar presentations


Ads by Google