{"id":7883,"date":"2018-09-11T17:10:52","date_gmt":"2018-09-11T22:10:52","guid":{"rendered":"https:\/\/blogs.mathworks.com\/simulink\/?p=7883"},"modified":"2020-06-19T09:16:53","modified_gmt":"2020-06-19T13:16:53","slug":"optimizing-data-types-with-the-fixed-point-tool","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2018\/09\/11\/optimizing-data-types-with-the-fixed-point-tool\/","title":{"rendered":"Optimizing Data Types with the Fixed-Point Tool"},"content":{"rendered":"<p>Have you ever had one of those days... ?<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2018Q3\/twoMinusOne.png\" alt=\"Two minus one\" \/><\/p>\n<p><strong>The Explanation<\/strong><\/p>\n<p>When I see things like that, my first reflex is usually to turn on most of the items in the Display menu. In this case, displaying the port data types helps a bit with understanding what is happening:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2018Q3\/withDataType.png\" alt=\"Show Port Data Type\" \/><\/p>\n<p>In this case, the data type of the output signal is <strong>sfix16_E1<\/strong>. This means that it is a 16 bits integer with a fraction length of -1. See the documentation for <a href=\"http:\/\/www.mathworks.com\/help\/simulink\/slref\/fixdt.html\">fixdt<\/a> for more information on the possible ways to specify fixed-point data types in Simulink.<\/p>\n<p>This data type, sfix16_E1, can only represent 0 and 2, not 1. This is why the output is zero.<\/p>\n<p><strong>Why?<\/strong><\/p>\n<p>Now... you are probably wondering why the data type of the Sum block is sfix16_E1.<\/p>\n<p>First, let's look at its block parameters:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2018Q3\/sumParameters.png\" alt=\"Sum block dialog\" \/><\/p>\n<p>As you can see, its output data type is set to \"Inherit via internal rule\". When the time comes to automatically choose a data type, Simulink has to make a trade-off between range and precision. In most cases, by default Simulink will tend to favor range over precision. In this case, the fraction length of -1 is the most precise option allowing the Sum block to cover the entire potential range of the sum of the input signals data type.<\/p>\n<p>If you want us to find a better trade off, here are two options using the Fixed-Point Tool, a very useful tool included with <a href=\"https:\/\/www.mathworks.com\/products\/fixed-point-designer.html\">Fixed-Point Designer<\/a>. <\/p>\n<p><strong>Fixed-Point Tool - Simulation Workflow<\/strong><\/p>\n<p>Right-clicking in the model window gives you the option to launch the Fixed-Point Tool. If you click on the <strong>Collect Ranges<\/strong>, it will simulate the model and collect data about the ranges of signals in the model.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2018Q3\/collectRange.png\" alt=\"Collect Ranges\" \/><\/p>\n<p>Note that you have many options when collecting ranges, like aggregating results from multiple simulations and using double data type during the collection.<\/p>\n<p>Once this is done, click <strong>Propose Data Types<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2018Q3\/proposeTypes.png\" alt=\"Propose Types\" \/><\/p>\n<p>And now magic happens!<\/p>\n<p>You can inspect the proposed types and decide whether to accept each one individually. In my case, I accepted the data type proposed for the Sum block output, which was ufix16_En15.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2018Q3\/applyTypes.png\" alt=\"Apply Types\" \/><\/p>\n<p>After I apply the results, I simulate the model again and get the desired result: 2-1=1.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2018Q3\/twoMinusOneFixed.png\" alt=\"Fixed model\" \/><\/p>\n<p><strong>Fixed-Point Tool - Range Analysis Workflow<\/strong><\/p>\n<p>If you know the ranges that the signals in your model are expected to take, you can specify them in block dialogs and those ranges will be used:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2018Q3\/specifyRanges.png\" alt=\"Specify Ranges\" \/><\/p>\n<p>In the Fixed-Point Tool, click on Derived Ranges to use those values.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2018Q3\/derivedRanges.png\" alt=\"Derived Ranges\" \/><\/p>\n<p>Once this is done, follow the same workflow as above: Collect, Propose, Apply. We'll take care of the rest!<\/p>\n<p><strong>Now it's your turn<\/strong><\/p>\n<p>Are you taking advantage of simulation results and specified ranges to optimize fixed-point properties in your models? Let us know in the comments below.<\/p>\n<p>If you are not using it already, I recommend reading more about <a href=\"https:\/\/www.mathworks.com\/products\/fixed-point-designer.html\">Fixed-Point Designer<\/a>. What I showed in this post is only a small example of what it can do for you.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2018Q3\/derivedRanges.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>Have you ever had one of those days... ?<\/p>\n<p>The Explanation<br \/>\nWhen I see things like that, my first reflex is usually to turn on most of the items in the Display menu. In this case, displaying the port... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2018\/09\/11\/optimizing-data-types-with-the-fixed-point-tool\/\">read more >><\/a><\/p>\n","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[76,11],"tags":[68],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/7883"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=7883"}],"version-history":[{"count":18,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/7883\/revisions"}],"predecessor-version":[{"id":9613,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/7883\/revisions\/9613"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=7883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=7883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=7883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}