{"id":113,"date":"2011-06-20T18:36:48","date_gmt":"2011-06-20T18:36:48","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/2011\/06\/20\/importing-variable-step-signals-into-simulink\/"},"modified":"2011-06-20T18:36:48","modified_gmt":"2011-06-20T18:36:48","slug":"importing-variable-step-signals-into-simulink","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2011\/06\/20\/importing-variable-step-signals-into-simulink\/","title":{"rendered":"Importing variable-step signals into Simulink"},"content":{"rendered":"<p>This week I received the following question from a Simulink user:<\/p>\r\n\r\n<p><em>I have a data acquisition system which saves signals at variable intervals. I want to import this data into a variable-step simulation. When I use the From Workspace block, the Simulink variable-step solver seems to take large steps and skip some of my points. How can I force the solver to take steps at the times specified in my input data?<\/em><\/p>\r\n\r\n<p><strong>Generating a variable-step signal<\/strong><\/p>\r\n\r\n<p>To illustrate this problem, I will generate data using one variable-step simulation and try to input it into another. To begin I simulate the following model using the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/toolbox\/simulink\/gui\/bq7cmsp-1.html#bq9mhyj-1\">ode15s<\/a> stiff solver:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2010Q4\/ContinuousModel.png\" alt=\"Example Simulink model using a variable-step solver\" \/><\/p>\r\n\r\n<p>When inspecting the logged data, we can see that the step size is not constant:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2010Q4\/TheSignal.png\" alt=\"logged variable-step signal\" \/><\/p>\r\n\r\n<p><strong>Importing the signal into a model<\/strong><\/p>\r\n\r\n<p>To import this data in a simulation, I use a root-level <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/toolbox\/simulink\/slref\/inport.html\">Inport<\/a> block.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2010Q4\/SecondModel.png\" alt=\"Model where the data is imported\" \/><\/p>\r\n\r\n<p>Since the data to import is continuous, I enable the Interpolate option of the Inport block:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2010Q4\/InportConfig.png\" alt=\"Enable Interpolation\" \/><\/p>\r\n\r\n<p>Then I configure the model to use the saved data as <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/toolbox\/simulink\/gui\/bq7cpr_.html\">input<\/a>:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2010Q4\/LoadFromWorkspace.png\" alt=\"Load From Workspace\" \/><\/p>\r\n\r\n<p><strong>Reproducing the problem<\/strong><\/p>\r\n\r\n<p>The Simulink variable-step solvers <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/toolbox\/simulink\/ug\/f11-69449.html#f11-41989\">adjusts the step size<\/a> during the simulation to achieve the specified tolerance for state error as defined in the model configuration. The variable-step solver does not have to take steps at the times specified in the imported data. In my example model, when I run the simulation, the imported signal looks like:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2010Q4\/WrongAnswer.png\" alt=\"Signal read by default\" \/><\/p>\r\n\r\n<p><strong>The solution<\/strong><\/p>\r\n\r\n<p>We need to instruct the Simulink variable-step solver to execute at the times specified by the imported data. To do this, open the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/toolbox\/simulink\/gui\/bq7cpr_.html\">Data Import\/Export<\/a> configuration pane and look at the Save options section. Set the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/toolbox\/simulink\/gui\/bq7cpr_.html#bq9_fhw-1\">Output options<\/a> to \u201cProduce additional output\u201d, and specify the time points of the imported data as the Output times. <\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2010Q4\/OutputOptions.png\" alt=\"Output options: Produce Additional outputs\" \/><\/p>\r\n\r\n<p>Now the imported data looks as expected.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2010Q4\/CorrectAnswer.png\" alt=\"Signal reads correctly\" \/><\/p>\r\n\r\n<p><strong>Additional Note<\/strong><\/p>\r\n\r\n<p>For optimal performance, I recommend pre-processing the data before importing it. For example if your signal contains points very close in time with similar values, you can filter them out. This will make your simulation run faster. Let me know if you would like to see a follow up post on that topic.<\/p>\r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\n<p>Did you ever encountered problems when importing data into Simulink? Do you have tricks to recommend? Leave a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=113&amp;#comment\">comment here<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>This week I received the following question from a Simulink user:\r\n\r\nI have a data acquisition system which saves signals at variable intervals. I want to import this data into a variable-step... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2011\/06\/20\/importing-variable-step-signals-into-simulink\/\">read more >><\/a><\/p>","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,10,33],"tags":[204,441,203],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/113"}],"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=113"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/113\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}