<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Guy and Seth on Simulink</title>
	
	<link>http://blogs.mathworks.com/seth</link>
	<description>This blog is about Simulink.</description>
	<lastBuildDate>Thu, 24 May 2012 03:27:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/SethOnSimulink" /><feedburner:info uri="sethonsimulink" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId>SethOnSimulink</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>How to make your own blocks with code! (Introduction to S-Functions)</title>
		<link>http://feedproxy.google.com/~r/SethOnSimulink/~3/4vUEptyidcA/</link>
		<comments>http://blogs.mathworks.com/seth/2012/05/23/how-to-make-your-own-blocks-with-code-introduction-to-s-functions/#comments</comments>
		<pubDate>Thu, 24 May 2012 03:27:35 +0000</pubDate>
		<dc:creator>Guy Rouleau</dc:creator>
				<category><![CDATA[Fundamentals]]></category>
		<category><![CDATA[S-functions]]></category>

		<guid isPermaLink="false">http://blogs.mathworks.com/seth/?p=865</guid>
		<description><![CDATA[I have to admit... before joining MathWorks, I was afraid of S-Functions. Now this makes me laugh, because I know that writing an S-Function is very powerful and not that complicated. In my opinion, understanding S-Functions is the best way to understand how Simulink works. When looking at the previous posts on this blog, I [...]]]></description>
			<content:encoded><![CDATA[<p>I have to admit... before joining MathWorks, I was afraid of S-Functions.</p>

<p>Now this makes me laugh, because I know that writing an S-Function is very powerful and not that complicated.</p>

<p>In my opinion, understanding S-Functions is the best way to understand how Simulink works.</p>

<p>When looking at the previous posts on this blog, I realized that I wrote a few posts about advanced usage of s-functions (<a href="http://blogs.mathworks.com/seth/2011/06/28/variable-size-signals-and-unit-delay/">Variable Size Signals and Unit Delay</a> and <a href="http://blogs.mathworks.com/seth/2012/01/22/advanced-s-function-techniques-scheduling-future-events//">Scheduling Future Events</a>), but I never covered the basic of how S-Functions work.</p>

<p><strong>Getting Started with S-Functions</strong></p>

<p>For most MATLAB users, I recommend starting by writing S-Functions in MATLAB (in opposite to C or C++). MATLAB S-Functions have some limitations compared to C or C++, but debugging is easier.</p>

<p>Personally, I like to start with examples. In MATLAB, type:</p>

<code>sfundemos</code>

<p>This will launch a library containing links to many examples. In this collection, the simplest model to start with is <tt>msfcndemo_timestwo.mdl</tt>.</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/sfundemos.png" alt="S-Functions demos"></p>

<p>In this model, click on the annotation to open the source code of the S-Function.</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/timestwo_model.png" alt="Simple times two s-function demo"></p>

<p><strong>Basic structure of an S-Function</strong></p>

<p>If you look at the code, you will find that the S-Function is in fact just a MATLAB function, taking as input a variable <tt>block</tt> and passing it to another function using the line <tt>setup(block)</tt>.</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/timestwo_setup.png" alt="Times two s-function code"></p>

<p><strong>The <tt>block</tt> variable</strong></p>

<p>This <tt>block</tt> variable is an instance of the <a href="http://www.mathworks.com/help/toolbox/simulink/slref/simulink.msfcnruntimeblock.html "><tt>Simulink.MSFcnRunTimeBlock</tt></a> class. We call it the S-Function run-time object.</p>

<p>Using the run-time object, the S-Function can exchange information with the Simulink engine.</p>

<p><strong>The <tt>setup(block)</tt> function</strong></p>

<p>In the <tt>setup</tt> function, the run-time object allows you to specify and obtain information about various characteristics of the block, including ports, parameters, states, work vectors, etc.</p>

<p>In our example <tt>msfcn_times_two.m</tt>, the properties of ports are set using lines like:</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/timestwo_setup2.png" alt="Setting properties of the block ports"></p>

<p>Also, the <tt>setup</tt> function is responsible for registering a set of <a href="http://www.mathworks.com/help/releases/R2012a/toolbox/simulink/sfg/f7-67622.html">callback methods</a> that the Simulink solver will call during different phases of the simulation. </p>

<p>In the times two example, we register only one method, the <tt>output</tt> function:</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/timestwo_setup3.png" alt="Registering s-function callback methods"></p>

<p>If you are interested to see the list of available methods and when they are called, I recommend going through the documentation section <a href="http://www.mathworks.com/help/toolbox/simulink/sfg/f8-37326.html">How the Simulink Engine Interacts with C S-Functions</a></p>


<p><strong>Registered Methods</strong></p>

<p>Every time the Simulink engine calls a method of an S-Function, it passes to it the <a href="http://www.mathworks.com/help/toolbox/simulink/slref/simulink.msfcnruntimeblock.html ">run-time object</a>.</p>

<p>In <tt>msfcn_times_two.m</tt>, we access the data of the input port and use it to compute the value that will be written to the output port:</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/timestwo_output.png" alt="Times two s-function output method"></p>

<p><strong>Conclusion</strong></p>

<p>This is it for this simple s-function example.</p>

<p>Of course, if you need to multiply a signal by two in Simulink, I recommend using a <a href="http://www.mathworks.com/help/toolbox/simulink/slref/gain.html">Gain</a> block, and not an S-Function. But hopefully this simple example gives you an idea of how MATLAB S-functions work.</p> 

<p><strong>Now it's your turn</strong></p>

<p>Do you use S-Functions? Are there S-Functions related topics you would like to be covered in a future post? Leave a <a href="http://blogs.mathworks.com/seth/?p=865&#comment">comment here</a>.</p><img src="http://feeds.feedburner.com/~r/SethOnSimulink/~4/4vUEptyidcA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.mathworks.com/seth/2012/05/23/how-to-make-your-own-blocks-with-code-introduction-to-s-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blogs.mathworks.com/seth/2012/05/23/how-to-make-your-own-blocks-with-code-introduction-to-s-functions/</feedburner:origLink></item>
		<item>
		<title>How to Load and Save Bus Signal Data</title>
		<link>http://feedproxy.google.com/~r/SethOnSimulink/~3/eZVMCFlf6Ck/</link>
		<comments>http://blogs.mathworks.com/seth/2012/05/14/how-to-load-and-save-bus-signal-data/#comments</comments>
		<pubDate>Tue, 15 May 2012 01:48:53 +0000</pubDate>
		<dc:creator>Guy Rouleau</dc:creator>
				<category><![CDATA[Signals]]></category>
		<category><![CDATA[What's new?]]></category>
		<category><![CDATA[loading buses]]></category>
		<category><![CDATA[Simulink bus signals]]></category>
		<category><![CDATA[Time Series]]></category>

		<guid isPermaLink="false">http://blogs.mathworks.com/seth/?p=682</guid>
		<description><![CDATA[In R2012a, the From Workspace, To Workspace, From File and To File blocks all support bus signals. This is an often requested capability, and I have been looking forward to it for a long time. MATLAB Timeseries In R2012a, most methods for importing and exporting data to and from a model have been standardized to [...]]]></description>
			<content:encoded><![CDATA[<p>In R2012a, the <a href="http://www.mathworks.com/help/releases/R2012a/toolbox/simulink/slref/fromworkspace.html">From Workspace</a>, <a href="http://www.mathworks.com/help/releases/R2012a/toolbox/simulink/slref/toworkspace.html">To Workspace</a>, <a href="http://www.mathworks.com/help/releases/R2012a/toolbox/simulink/slref/fromfile.html">From File</a> and <a href="http://www.mathworks.com/help/releases/R2012a/toolbox/simulink/slref/tofile.html">To File</a> blocks all support bus signals.  This is an often requested capability, and I have been looking forward to it for a long time.</p>

<p><strong>MATLAB Timeseries</strong></p>

<p>In R2012a, most methods for importing and exporting data to and from a model have been standardized to use <a href="http://www.mathworks.com/help/releases/R2012a/techdoc/ref/timeseriesclass.html">MATLAB Timeseries</a>.</p>

<p>For example, if you open the dialog of the To Workspace, you will see that timeseries is now the default save format:</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/ToWorkspaceDialog.png" alt="save formats for the To Workspace block"></p>

<p>Using MATLAB timeseries offers a lot of advantages as I described in a <a href="http://blogs.mathworks.com/seth/2011/08/22/the-dataset-logging-format/">previous post on the dataset logging format</a>.</p>

<p><strong>Importing Bus Data</strong></p>

<p>Let's say I want to import bus data, I create a structure of timeseries:</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/createBusData.png" alt="Creating bus data"></p>

<p>Then I specify the name of the variable or file, and the bus object in the From Workspace or From File dialog.</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/FromWSdialog.png" alt="Configuring the From Workspace block to import bus data"></p>

<p>and we have a model importing buses!</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/busLoadingModel.png" alt="Model importing bus data"></p>

<p><strong>Exporting Bus data</strong></p>

<p>To export bus data, all you need to do do is connect the bus signal to a To Workspace or To File block and ensure the block is configured to use timeseries as save format (the default).</p>

<p>Once the data is in the MATLAB Workspace, you can use all <a href="http://www.mathworks.com/help/releases/R2012a/techdoc/ref/f16-48518.html#f16-48710">timeseries methods</a> to process your data.</p>

<p>For example, I can quickly detrend, filter and plot a random signal "b", contained inside bus "busOut":</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/postProcess.png" alt="processing bus data using MATLAB timeseries"></p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/displayProcessedData.png" alt="Processed data"></p>

<p><strong>Now its your turn</strong></p>

<p>Are you going to include these new methods of loading and saving bus data in your workflow? Which is be best block pair for loading/saving buses?  Let us know by leaving a <a href="http://blogs.mathworks.com/seth/?p=682&amp;#comment">comment here</a></p>

<img src="http://feeds.feedburner.com/~r/SethOnSimulink/~4/eZVMCFlf6Ck" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.mathworks.com/seth/2012/05/14/how-to-load-and-save-bus-signal-data/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://blogs.mathworks.com/seth/2012/05/14/how-to-load-and-save-bus-signal-data/</feedburner:origLink></item>
		<item>
		<title>Signal Hierarchy Viewer</title>
		<link>http://feedproxy.google.com/~r/SethOnSimulink/~3/wR6wlfAnN6M/</link>
		<comments>http://blogs.mathworks.com/seth/2012/05/01/signal-hierarchy-viewer/#comments</comments>
		<pubDate>Tue, 01 May 2012 18:30:04 +0000</pubDate>
		<dc:creator>Guy Rouleau</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Signals]]></category>
		<category><![CDATA[What's new?]]></category>
		<category><![CDATA[bus signals]]></category>
		<category><![CDATA[nonvirtual buses]]></category>
		<category><![CDATA[Simulink bus signals]]></category>

		<guid isPermaLink="false">http://blogs.mathworks.com/seth/?p=730</guid>
		<description><![CDATA[If you are like me and often need to understand and edit new Simulink models, you probably already looked at a bus signal and wished you could easily know the hierarchy of signals inside it. What did you do? Before R2012a, my trick was to branch out the signal and connect it to a Bus [...]]]></description>
			<content:encoded><![CDATA[<p>If you are like me and often need to understand and edit new Simulink models, you probably already looked at a bus signal and wished you could easily know the hierarchy of signals inside it. What did you do? Before R2012a, my trick was to branch out the signal and connect it to a <a href="http://www.mathworks.com/help/releases/R2012a/toolbox/simulink/slref/busselector.html">Bus Selector</a>, just to inspect what is in the bus.</p>

<p>In R2012a, we made your life easier by introducing the <a href="http://www.mathworks.com/help/releases/R2012a/toolbox/simulink/ug/bq4h5ej-1.html#bs85t2i-1">Signal Hierarchy Viewer</a>.</p>

<p>When right-clicking on a signal in R2012a, you will notice a new <strong>Signal Hierarchy</strong> option.</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/signalHierarchyMenu.png" alt="Right-click menu"></p>

<p>This opens a nice small window listing the hierarchy of signals inside the bus.</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/signalHierarchyViewer.png" alt="Signal Hierarchy Viewer"></p>

<p>As you can see in the image above, the Signal Hierarchy Viewer allows you to filter the signals in the bus to quickly find what you are looking for.</p>


<p><strong>Now it's your turn</strong></p>

<p>What do you think of the Signal Hierarchy Viewer? Let us know by leaving a <a href="http://blogs.mathworks.com/seth/?p=730&amp;#comment">comment here</a>.</p><img src="http://feeds.feedburner.com/~r/SethOnSimulink/~4/wR6wlfAnN6M" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.mathworks.com/seth/2012/05/01/signal-hierarchy-viewer/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://blogs.mathworks.com/seth/2012/05/01/signal-hierarchy-viewer/</feedburner:origLink></item>
		<item>
		<title>Automatic Refresh of Links and Model Blocks</title>
		<link>http://feedproxy.google.com/~r/SethOnSimulink/~3/kxJiIoejZWs/</link>
		<comments>http://blogs.mathworks.com/seth/2012/04/24/automatic-refresh-of-links-and-model-blocks/#comments</comments>
		<pubDate>Tue, 24 Apr 2012 18:31:03 +0000</pubDate>
		<dc:creator>Guy Rouleau</dc:creator>
				<category><![CDATA[Libraries]]></category>
		<category><![CDATA[Model Reference]]></category>
		<category><![CDATA[What's new?]]></category>
		<category><![CDATA[normal mode model reference]]></category>
		<category><![CDATA[Simulink blocks]]></category>
		<category><![CDATA[simulink libraries]]></category>

		<guid isPermaLink="false">http://blogs.mathworks.com/seth/?p=694</guid>
		<description><![CDATA[The first time I tried editing models componentized with library blocks and referenced models in R2012a, I noticed something was different... ... and I really like that change! What's new In R2012a, Simulink automatically refreshes links and model blocks. Yes, you heard me, no longer need to manually select Edit > Links and Model Blocks [...]]]></description>
			<content:encoded><![CDATA[<p>The first time I tried editing models componentized with library blocks and referenced models in R2012a, I noticed something was different...</p>

<p>... and I really like that change!</p>

<p><strong>What's new</strong></p>

<p>In R2012a, Simulink <a href="http://www.mathworks.com/help/releases/R2012a/toolbox/simulink/rn/bs8esk9.html#bs8eszn">automatically refreshes links and model blocks</a>. Yes, you heard me, no longer need to manually select <strong>Edit > Links and Model Blocks > Refresh</strong> for your model to realize a change in a library or model block.</p>

<p>Here's how it works.</p>

<p><strong>Model Block</strong></p>

<p>Before R2012a, when changing the number of ports of a referenced model, you had to manually select <strong>Edit > Links and Model Blocks > Refresh</strong> for the Model block to update its ports. In R2012a, saving the referenced mode automatically updates the parent block:</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/LiveModelLink.gif" alt="Automatic update of model block"></p>

<p><strong>Library Blocks</strong></p>

<p>When you edit a library block, Simulink indicates stale links by displaying the linked blocks grayed out. When you click in the model, as soon as the window becomes active Simulink refreshes any stale links with what is in the library, even if you have not saved the library yet.</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/LiveLibLink.gif" alt="Automatic update of library blocks"></p>

<p><strong>Now it's your turn</strong></p>

<p>Try this new behavior and let us know if you are as happy as I am by leaving a <a href="http://blogs.mathworks.com/seth/?p=694&amp;#comment">comment here</a>.</p>
<img src="http://feeds.feedburner.com/~r/SethOnSimulink/~4/kxJiIoejZWs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.mathworks.com/seth/2012/04/24/automatic-refresh-of-links-and-model-blocks/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://blogs.mathworks.com/seth/2012/04/24/automatic-refresh-of-links-and-model-blocks/</feedburner:origLink></item>
		<item>
		<title>A scope for Simulink and MATLAB</title>
		<link>http://feedproxy.google.com/~r/SethOnSimulink/~3/t_0jVQlanoU/</link>
		<comments>http://blogs.mathworks.com/seth/2012/04/16/a-scope-for-simulink-and-matlab/#comments</comments>
		<pubDate>Mon, 16 Apr 2012 18:32:41 +0000</pubDate>
		<dc:creator>Guy Rouleau</dc:creator>
				<category><![CDATA[Signal Processing]]></category>
		<category><![CDATA[Signals]]></category>
		<category><![CDATA[What's new?]]></category>
		<category><![CDATA[Simulink blocks]]></category>
		<category><![CDATA[Simulink scopes]]></category>
		<category><![CDATA[Time Scope]]></category>

		<guid isPermaLink="false">http://blogs.mathworks.com/seth/?p=677</guid>
		<description><![CDATA[Do you know about the Time Scope available with the DSP System Toolbox? This visualization tool was introduced in release R2010a and has been improved upon every release thereafter. The novelty of this tool is that it ships as both a Simulink block and a System object (dsp.TimeScope) and therefore offers the same graphical user [...]]]></description>
			<content:encoded><![CDATA[<p>Do you know about the <a href="http://www.mathworks.com/help/releases/R2012a/toolbox/dsp/ref/timescope.html">Time Scope</a> available with the <a href="http://www.mathworks.com/products/dsp-system/">DSP System Toolbox</a>?</p>

<p>This visualization tool was introduced in release R2010a and has been improved upon every release thereafter.</p>

<p>The novelty of this tool is that it ships as both a Simulink <a href="http://www.mathworks.com/help/releases/R2012a/toolbox/dsp/ref/timescope.html">block</a> and a <a href="http://www.mathworks.com/help/releases/R2012a/toolbox/dsp/gs/br_xl0s.html">System object</a> (<a href="http://www.mathworks.com/help/releases/R2012a/toolbox/dsp/ref/dsp.timescopeclass.html">dsp.TimeScope</a>) and therefore offers the same graphical user interface, parameters, and tools in both the MATLAB and Simulink environments.

<p><strong>A Scope for Simulink</strong></p>

<p>To use it in a model, just drag the block from the Library Browser and connect the signal you want to visualize.</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/TimeScope2.gif" alt="Time Scope block"></p>

<p>As you can see, when used in Simulink, the Time Scope allows you to control the simulation directly on the scopes, for example, it has Start, Pause and Stop buttons. In addition, there is a button to highlight and quickly find the block corresponding to the Scope within the model.</p>

<p><strong>A Scope for MATLAB</strong></p>

<p>For MATLAB-based simulations, the Time Scope System object offers the standard System object methods, for example:</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/scopeFromMATLAB.png" alt="Time Scope System object"></p>

<p><strong>So, what is unique about the Time Scope?</strong></p>

<p>Both the block and System object offer many common features, for example, both include integrated measurement panels, which display Signal Statistics, peak detection and Bi-level measurements:</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/TSstatistics.png" alt="Integrated Measurement Panel"></p>

<p>The Time Scope has a configurable layout that lets you select the number of rows and columns used to display multiple axes:</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/TSLayout.png" alt="Customizable Layout"></p>

<p>Unique to this scope is its ability to accept input data as both sample-based and frame-based signals. Also, both block and System object accept complex data and automatically display the data as Magnitude and Phase, or real and imaginary parts.</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/TimeScopeRealImag.gif" alt="Visualizing Frames of complex data"></p>

<p><strong>Look at the demos</strong></p>

<p>Many of the Simulink and MATLAB based examples of the DSP System Toolbox have been updated to take advantage of the Time Scope features. From the Help Browser, look in the demo section of the DSP System Toolbox:</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/dsp_demos.png" alt="DSP System Toolbox Demos"></p>

<p>For MATLAB users, you will find demos like:</p>

<ul>
	<li>Envelope Detection</li>
	<li>GSM Digital Down Converter</li>
</ul>

<p>and for Simulink:</p>

<ul>
	<li>Audio Sample Rate Conversion</li>
	<li>Autoscaling and Curve Fitting</li>
</ul>

<p>Moreover, I strongly recommend going through the <a href="http://www.mathworks.com/help/releases/R2012a/toolbox/dsp/ref/timescope.html">block documentation</a> to see everything the Time Scope can do.</p>

<p><strong>Now it's your turn</strong></p>

<p>What do you think of the Time Scope? Let us know by leaving a <a href="http://blogs.mathworks.com/seth/?p=677&amp;#comment">comment here</a></p>

<br />
<img src="http://feeds.feedburner.com/~r/SethOnSimulink/~4/t_0jVQlanoU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.mathworks.com/seth/2012/04/16/a-scope-for-simulink-and-matlab/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blogs.mathworks.com/seth/2012/04/16/a-scope-for-simulink-and-matlab/</feedburner:origLink></item>
		<item>
		<title>Conditional Execution</title>
		<link>http://feedproxy.google.com/~r/SethOnSimulink/~3/vMoRp1t6O78/</link>
		<comments>http://blogs.mathworks.com/seth/2012/04/09/conditional-execution/#comments</comments>
		<pubDate>Mon, 09 Apr 2012 18:27:11 +0000</pubDate>
		<dc:creator>Guy Rouleau</dc:creator>
				<category><![CDATA[Modeling]]></category>
		<category><![CDATA[Signals]]></category>
		<category><![CDATA[Simulink Tips]]></category>
		<category><![CDATA[Stateflow]]></category>
		<category><![CDATA[Conditional Subsystems]]></category>

		<guid isPermaLink="false">http://blogs.mathworks.com/seth/?p=707</guid>
		<description><![CDATA[As we all know, in Simulink there are always multiple ways to implement a functionality. One situation where I often hesitate when choosing an implementation is conditional execution. I will share a few possible options I usually consider and would like to know how you choose one versus another. Enabled Subsystem Maybe it is because [...]]]></description>
			<content:encoded><![CDATA[<p>As we all know, in Simulink there are always multiple ways to implement a functionality. One situation where I often hesitate when choosing an implementation is conditional execution.</p>

<p>I will share a few possible options I usually consider and would like to know how you choose one versus another.</p>

<p><strong>Enabled Subsystem</strong></p>

<p>Maybe it is because it is the first method I learned when I started using Simulink long ago, but I like <a href="http://www.mathworks.com/help/releases/R2012a/toolbox/simulink/ug/f4-84107.html">Enabled Subsystems</a>.</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/enabledSubsystem.png" alt="Enabled Subsystem"></p>

<p>Using this method, you use Simulink blocks to generate one logical signal for each subsystem. This method is useful when you have a small number of mutually exclusive systems and the activation logic is relatively simple.</p>

<p><strong>Switch and Multiport Switch</strong></p>

<p>As explained by Seth in a <a href="http://blogs.mathworks.com/seth/2009/06/02/the-if-else-construct-in-models/">previous post</a>, the <a href="http://www.mathworks.com/help/releases/R2012a/toolbox/simulink/slref/switch.html">Switch</a> and <a href="http://www.mathworks.com/help/releases/R2012a/toolbox/simulink/slref/multiportswitch.html">Multiport Switch</a> blocks can be used to implement conditional execution.</p>

<p>If you enable the <a href="http://www.mathworks.com/help/releases/R2012a/toolbox/simulink/gui/bq7cqu2-1.html#bq7cq0i-1">Conditional branch execution</a> optimization, Simulink executes only the blocks required to compute the control input and the data input selected by the control input. </p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/switch.png" alt="Switch"></p>

<p>I typically use this method only when I need to switch between 2 very simple options, made of just a few directfeedthrough stateless blocks.</p>

<p><strong>Action Subsystems</strong></p>

<p><a href="http://www.mathworks.com/help/releases/R2012a/toolbox/simulink/slref/ifactionsubsystem.html">If Action Subsystem</a> and <a href="http://www.mathworks.com/help/releases/R2012a/toolbox/simulink/slref/switchcaseactionsubsystem.html">Switch Case Action Subsystem</a> are especially designed to model <a href="http://www.mathworks.com/help/releases/R2012a/toolbox/simulink/slref/if.html">if-else</a> and <a href="http://www.mathworks.com/help/releases/R2012a/toolbox/simulink/slref/switchcase.html">switch-case</a> control flows.</p>

<p>In this approach, you define the conditions in the dialog of the If-Else or Switch-Case block.</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/ifSubsystem.png" alt="If Action Subsystem"></p>

<p>One thing to note with this approach is that all blocks in an Action subsystem driven by an If or Switch Case block must run at the same rate as the driving block.</p>

<p><strong>Stateflow and Simulink Functions</strong></p>

<p>For complex activation logic, I recommend going with Stateflow. Inside the chart, you can place the algorithms to be conditionally executed inside Simulink Functions.</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/stateflowSLFunction.png" alt="Simulink function inside Stateflow"></p>

<p>I like to use this technique especially when the Simulink function is called from multiple places in the chart.</p>

<p><strong>Stafeflow and Function-Call Subsystems</strong></p>

<p>I have to admit, when I place a large or important algorithm inside a Stateflow Simulink Function, I feel like if I am hiding it. This is why sometimes I prefer to generate Function-Calls from the chart.</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/sf_fcn_call.png" alt="Stateflow and Function-Call Subsystems"></p>

<p><strong>Stafeflow, Enumeration and Switch-Case Action Subsystems</strong></p>

<p>One way to make it obvious that one and only one subsystem is active at a time is to generate an enumerated type from the Stateflow chart and use it in a Switch-Case construct.</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/stateflowEnum.png" alt="Stateflow and Function-Call Subsystems"></p>

<p><strong>Conclusion</strong></p>

<p>All these methods have advantages and disadvantages. I think it is important to read and understand the documentation to pick the one appropriate for your use case.</p>

<p>When in doubt, go for readability. Try picking one that you think will make it as easy as possible to understand the functionality of your model.</p>

<p><strong>Now it's your turn</strong></p>

<p>I would be very interested to hear what are your criteria when choosing one versus another.</p>

<p>Let us know by leaving a <a href="http://blogs.mathworks.com/seth/?p=707&amp;#comment">comment here</a>.</p><img src="http://feeds.feedburner.com/~r/SethOnSimulink/~4/vMoRp1t6O78" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.mathworks.com/seth/2012/04/09/conditional-execution/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		<feedburner:origLink>http://blogs.mathworks.com/seth/2012/04/09/conditional-execution/</feedburner:origLink></item>
		<item>
		<title>Fine-Tuning Model Advisor Analysis</title>
		<link>http://feedproxy.google.com/~r/SethOnSimulink/~3/NNf05GY6zOo/</link>
		<comments>http://blogs.mathworks.com/seth/2012/04/03/fine-tuning-model-advisor-analysis/#comments</comments>
		<pubDate>Tue, 03 Apr 2012 22:09:14 +0000</pubDate>
		<dc:creator>Guy Rouleau</dc:creator>
				<category><![CDATA[Analysis]]></category>
		<category><![CDATA[Standards and Guidelines]]></category>
		<category><![CDATA[Verification & Validation]]></category>
		<category><![CDATA[What's new?]]></category>
		<category><![CDATA[model advisor]]></category>
		<category><![CDATA[Model-Based Design]]></category>

		<guid isPermaLink="false">http://blogs.mathworks.com/seth/?p=646</guid>
		<description><![CDATA[Today I welcome my friend Nishaat Vasi to introduce a new feature of Simulink Verification and Validation: Model Advisor Exclusions. Nishaat: Hey Guy, did you notice the new Model Advisor Exclusions feature in R2012a? Guy: There are so many new features every release... I think I missed that one. What is it? Nishaat: When you [...]]]></description>
			<content:encoded><![CDATA[<p><em>Today I welcome my friend <a href="http://www.mathworks.com/matlabcentral/fileexchange/authors/65473">Nishaat Vasi</a> to introduce a new feature of <a href="http://www.mathworks.com/products/simverification/">Simulink Verification and Validation</a>: Model Advisor Exclusions.</em></p>

<p><strong>Nishaat:</strong> Hey Guy, did you notice the new <a href="http://www.mathworks.com/help/releases/R2012a/toolbox/slvnv/ug/bta0e56.html">Model Advisor Exclusions</a> feature in R2012a?</p>

<p><strong>Guy:</strong> There are so many new features every release... I think I missed that one. What is it?</p>

<p><strong>Nishaat:</strong> When you create models, I bet you sometimes use the <a href="http://www.mathworks.com/help/releases/R2012a/toolbox/simulink/ug/f4-141979.html">Model Advisor</a>.</p>

<p><strong>Guy:</strong> Of Course! Model Advisor checks are very useful to ensure design consistency in large modeling environments.</p>

<p><strong>Nishaat:</strong> Using exclusions, you will now be able to fine-tune the Model Advisor analysis. This should save you time if there are blocks or subsystems in your model that do not need to be checked.</p> 

<p><strong>Guy:</strong> Cool, how does that work?</p>

<p><strong>Nishaat:</strong>  First, identify and right-click on the model elements you want to exclude from Model Advisor analysis and select from which checks you want it excluded.</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/exclusion_menu.png" alt="Excluding a block from Model Advisor Analysis"></p>

<p><strong>Nishaat:</strong> This will launch the Model Advisor Exclusion Editor</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/exclusion_editor.png" alt="Model Advisor Exclusion Editor"></p>

<p><strong>Nishaat:</strong> Run the Model Advisor analysis and you will see in the report when an exclusion rule is used in a check.</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/exclusion_report.png" alt="Model Advisor report"></p>

<p><strong>Guy:</strong> Pretty cool, I'll definitely start using Model Advisor Exclusions soon!</p>

<p></p>

<p><strong>Now it's your turn</strong></p>

<p>Will exclusions be useful in your workflow? Leave us a <a href="http://blogs.mathworks.com/seth/?p=646&amp;#comment">comment here</a>.</p><img src="http://feeds.feedburner.com/~r/SethOnSimulink/~4/NNf05GY6zOo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.mathworks.com/seth/2012/04/03/fine-tuning-model-advisor-analysis/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://blogs.mathworks.com/seth/2012/04/03/fine-tuning-model-advisor-analysis/</feedburner:origLink></item>
		<item>
		<title>Estimating Continuous-Time Transfer Functions with System Identification Toolbox</title>
		<link>http://feedproxy.google.com/~r/SethOnSimulink/~3/TNjTSV6DoLA/</link>
		<comments>http://blogs.mathworks.com/seth/2012/03/28/estimating-continuous-time-transfer-functions-with-system-identification-toolbox/#comments</comments>
		<pubDate>Wed, 28 Mar 2012 16:22:56 +0000</pubDate>
		<dc:creator>Guy Rouleau</dc:creator>
				<category><![CDATA[Controls]]></category>
		<category><![CDATA[Modeling]]></category>
		<category><![CDATA[What's new?]]></category>
		<category><![CDATA[Control Design]]></category>
		<category><![CDATA[PID Control]]></category>
		<category><![CDATA[System Identification]]></category>

		<guid isPermaLink="false">http://blogs.mathworks.com/seth/?p=658</guid>
		<description><![CDATA[Today guest blogger Pravallika describes how she used new features of the System Identification Toolbox to design a controller for a DC motor Any controls engineer knows that the key to designing a good controller is having a good plant model. But what do you do if you don’t have the underlying equations? System identification [...]]]></description>
			<content:encoded><![CDATA[<em><p>Today guest blogger Pravallika describes how she used new features of the <a href="http://www.mathworks.com/products/sysid/">System Identification Toolbox</a> to design a controller for a DC motor</p></em>

<p><img src="http://blogs.mathworks.com/images/seth/pvinnako_tn_large.jpg" alt="Pravallikota Vinnakota, guest blogger" style="float: left; margin-right: 1em;">

Any controls engineer knows that the key to designing a good controller is having a good plant model. But what do you do if you don’t have the underlying equations?

<br /><br />

System identification can help in that case. In R2012a, the <a href="http://www.mathworks.com/products/sysid/">System Identification Toolbox</a> added support for estimating parameters of a model type that is most intuitive and easiest to work with for controls engineers – continuous-time transfer functions. You can specify how many poles and zeros you want your transfer function to have, and the toolbox determines locations of poles and zeros automatically. If the system you are trying to model has a delay, the toolbox can automatically determine its value.</p>

<p><strong>The Setup</strong></p>

<p>I have been working with the setup shown in the image below. It is very simple; it is a small DC motor connected to an Arduino board.</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/ArduinoDUT1.JPG" alt="DC Motor connected to an Arduino board."></p>

<p>I applied a voltage to the motor and recorded the resulting motion. I imported this data in MATLAB and <a href="http://www.mathworks.com/help/releases/R2012a/toolbox/ident/ug/bqt1uuy.html">constructed an iddata object</a>.</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/data_object.png" alt="iddata object"></p>

<p>Here is what the data look like, using the <a href="http://www.mathworks.com/help/releases/R2012a/toolbox/ident/ref/plot.html"> iddata plot</a> command.</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/experimental_data.png" alt="Experimental data"></p>

<p><strong>Estimation</strong></p>

<p>Let’s begin by estimating the simplest possible transfer function – first-order, with no zeros. For that, we use the new function <a href="http://www.mathworks.com/help/releases/R2012a/toolbox/ident/ref/tfest.html">tfest</a>. 
</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/estimation_unig_tfest.png" alt="Estimation"></p>

<p>After the estimation is completed, we can <a href="http://www.mathworks.com/help/releases/R2012a/toolbox/ident/ref/compare.html">compare</a> the output of the model with the measured shaft angle. The comparison shows that the model does a good job in capturing dynamics of the motor.</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/comparison1.png" alt="Comparing experimental and identified data"></p>

<p><strong>Validation</strong></p>

<p>To ensure that the estimated transfer function represents motor dynamics, we need to validate this transfer function against an independent data set.</p>

<p>I tried feeding other sets of data acquired experimentally through the identified transfer function and compared the result with the measured shaft angle. Even though the fit is not perfect, the identified transfer function does a pretty good job in capturing system dynamics.</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/comparison2.png" alt="Comparing experimental and identified data"></p>

<p><strong>Using the Estimated Transfer Function</strong></p>

<p>Now let's use the identified transfer function model to design a PID controller using <a href="http://www.mathworks.com/help/releases/R2012a/toolbox/control/ref/pidtune.html">pidtune</a>.</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/pidtune_code.png" alt="Designing a controller using PIDTUNE"></p>

<p>One notable quality of models obtained with System Identification Toolbox is that they not only contain the information about the “nominal” parameter values, but also carry information about parameter uncertainty.  We can check the effect of this <a href="http://www.mathworks.com/help/releases/R2012a/toolbox/ident/ug/bq1sjno.html#bs3z12f-1">parameter uncertainty</a> on the bode plot magnitude of the estimated transfer function.</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/confidence_region.png" alt="Displaying confidence region on a Bode plot"></p>

<p>We can also use the function <a href="http://www.mathworks.com/help/releases/R2012a/toolbox/ident/ref/rsample.html">rsample</a> to sample this parameter uncertainty to create an array of transfer functions to test our controller. </p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/pidtune.png" alt="Designing a controller using PIDTUNE"></p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/final_response.png" alt="Step response of the controlled system"></p>

<p>The plot above shows the closed-loop step response for the nominal plant as well as the additional ten step responses for the sampled parameter uncertainty. We can see that the PID controller we designed works well in the face of uncertainty in estimated transfer function parameters.
</p>

<p><strong>Estimating Other Model Types</strong></p>

<p>In addition to estimating continuous-time transfer functions, System Identification Toolbox lets you estimate continuous-time state-space models and process models (special, low-order transfer functions). You can quickly compare different models and pick the one that is most appropriate for your problem.</p>


<p><strong>Now it is your turn</strong></p>

<p>How do you develop plant models? Try using new capabilities for continuous-time transfer function estimation and let us know what you think by leaving us a <a href="http://blogs.mathworks.com/seth/?p=658&amp;#comment">comment here</a>.</p><img src="http://feeds.feedburner.com/~r/SethOnSimulink/~4/TNjTSV6DoLA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.mathworks.com/seth/2012/03/28/estimating-continuous-time-transfer-functions-with-system-identification-toolbox/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://blogs.mathworks.com/seth/2012/03/28/estimating-continuous-time-transfer-functions-with-system-identification-toolbox/</feedburner:origLink></item>
		<item>
		<title>Normal Mode Simulation in Real Time</title>
		<link>http://feedproxy.google.com/~r/SethOnSimulink/~3/6yl8fxXZry0/</link>
		<comments>http://blogs.mathworks.com/seth/2012/03/22/normal-mode-simulation-in-real-time/#comments</comments>
		<pubDate>Thu, 22 Mar 2012 20:05:53 +0000</pubDate>
		<dc:creator>Guy Rouleau</dc:creator>
				<category><![CDATA[Real-Time]]></category>
		<category><![CDATA[What's new?]]></category>
		<category><![CDATA[missed ticks]]></category>
		<category><![CDATA[Real Time Windows Target]]></category>
		<category><![CDATA[RTWIN]]></category>
		<category><![CDATA[RTWT]]></category>

		<guid isPermaLink="false">http://blogs.mathworks.com/seth/?p=586</guid>
		<description><![CDATA[In R2012a, Real-Time Windows Target offers two modes for executing Simulink models in real time: The classic External Mode and the new Normal Mode. External Mode vs. Normal Mode In external mode, Simulink Coder is used to dynamically link generated algorithm code with generated hardware driver code. The resulting executable runs in Windows kernel mode [...]]]></description>
			<content:encoded><![CDATA[<p>In R2012a, <a href="http://www.mathworks.com/products/rtwt/">Real-Time Windows Target</a> offers two modes for executing Simulink models in real time: The classic External Mode and the new <a href="http://www.mathworks.com/help/toolbox/rtwin/rn/bs8hqn2-1.html#bs8hqn2-2">Normal Mode</a>.

<p><strong>External Mode vs. Normal Mode</strong></p>

<p>In external mode, <a href="http://www.mathworks.com/products/simulink-coder/">Simulink Coder</a> is used to dynamically link generated algorithm code with generated hardware driver code. The resulting executable runs in <a href="http://www.mathworks.com/help/toolbox/rtwin/ug/f8790.html#btahzq9-1">Windows kernel</a> mode and communicates with Simulink via <a href="http://www.mathworks.com/help/toolbox/rtw/ug/f17854.html">External Mode</a>.</p>

<p>In normal mode, the algorithm runs in Simulink and the I/O drivers run in a separate Windows kernel mode process that maintains the real-time clock.</p>

<p>The following diagram illustrates the difference between these two modes:</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/RealTimeWindowsTargetModes.png" alt="Comparison between external and Normal Mode" /></p>

<p><strong>Advantages of Normal Mode</strong></p>

<p>As you can guess based on the above diagram, normal mode offers lower performance when compared to external mode. On the other hand, normal mode offers interesting advantages:</p>

<ul>
	<li><a href="http://www.mathworks.com/help/toolbox/rtwin/rn/bs8hqn2-1.html#btar9bi">Support for variable step solvers</a></li>
	<li>No need for Simulink Coder</li>
</ul>

<p>You can also use Normal Mode on Windows 64-bit, where External Mode is only supported in Windows 32-bit as of R2012a.</p>

<p><strong>How does that work?</strong></p>

<p>Most blocks in the Real-Time Windows Target library have at least 2 parameters: <strong>Sample Time</strong> and <strong>Maximum missed ticks</strong>.</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/RealTimeWindowsTargetBlockParameters.png" alt="Dialog of the Real Time Synchronization block" /></p>

<p>Since, in normal mode, only the I/O drivers are synchronized with the real-time clock, it is possible for the simulation to miss clock ticks. When this happens, the <strong>Maximum missed ticks</strong> parameter determines how your model should behave. Set it to a value of zero if you want your model to error out at the first missed tick. Use a larger value if your application can deal with a few missed ticks. Note that, if the model misses a few ticks, it will resynchronize with the real-time clock as soon as possible.</p>

<p><strong>Now it's your turn</strong></p>

<p>Do you think Real-Time Windows Target normal mode will be useful for you? Leave a <a href="http://blogs.mathworks.com/seth/?p=586&amp;#comment">comment here</a>.</p><img src="http://feeds.feedburner.com/~r/SethOnSimulink/~4/6yl8fxXZry0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.mathworks.com/seth/2012/03/22/normal-mode-simulation-in-real-time/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://blogs.mathworks.com/seth/2012/03/22/normal-mode-simulation-in-real-time/</feedburner:origLink></item>
		<item>
		<title>Legends for Simulink Scopes</title>
		<link>http://feedproxy.google.com/~r/SethOnSimulink/~3/i-yna31cJ2A/</link>
		<comments>http://blogs.mathworks.com/seth/2012/03/12/legends-for-simulink-scopes/#comments</comments>
		<pubDate>Tue, 13 Mar 2012 03:23:44 +0000</pubDate>
		<dc:creator>Guy Rouleau</dc:creator>
				<category><![CDATA[What's new?]]></category>
		<category><![CDATA[Simulink scopes]]></category>

		<guid isPermaLink="false">http://blogs.mathworks.com/seth/?p=576</guid>
		<description><![CDATA[Today my colleague Roshin Kadanna Pally is back to talk about additions he made to the Simulink Scope in R2012a. Legends for Simulink Scopes When opening the Scope parameters dialog in R2012a, you will notice a new option: When simulating the model, the legend will appear and you will be able to choose its position: [...]]]></description>
			<content:encoded><![CDATA[<p>Today my colleague <a href="http://blogs.mathworks.com/seth/2011/09/14/how-to-customize-the-simulink-scope-take-2/">Roshin Kadanna Pally</a> is back to talk about additions he made to the Simulink <a href="http://www.mathworks.com/help/toolbox/simulink/slref/scope.html">Scope</a> in R2012a.</p>

<p><strong>Legends for Simulink Scopes</strong></p>

<p>When opening the Scope parameters dialog in R2012a, you will notice a new option:</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/ScopeParameters.png" alt="Simulink Scope parameters"></p>

<p>When simulating the model, the legend will appear and you will be able to choose its position:</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/ScopeLegendPosition.png" alt="Setting the position of the Scope legend"></p>

<p>The selected position will be saved with model.</p>

<p>The information displayed in the legend can come from multiple sources in the model:</p>

<ul>
	<li>Signal name</li>
	<li>Block name if signal is not named</li>
	<li>Name of elements for composite signals</li>
	<li>Port number if source block has multiple ports</li>
        <li>Signal index for vectors</li>
</ul>

<p>I tried to illustrate all those in the following example:</p>

<p><img src="http://blogs.mathworks.com/images/seth/2012Q2/ScopeLegendDetails.png" alt="Scope Naming Convention"></p>

<p><strong>Now it's your turn</strong></p>

<p>Are you happy the legends are now available in the Simulink Scope? Leave a <a href="http://blogs.mathworks.com/seth/?p=576&amp;#comment">comment here</a>.</p><img src="http://feeds.feedburner.com/~r/SethOnSimulink/~4/i-yna31cJ2A" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.mathworks.com/seth/2012/03/12/legends-for-simulink-scopes/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://blogs.mathworks.com/seth/2012/03/12/legends-for-simulink-scopes/</feedburner:origLink></item>
	</channel>
</rss>

