{"id":83,"date":"2010-02-26T21:59:03","date_gmt":"2010-02-26T21:59:03","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/2010\/02\/26\/how-to-estimate-the-frequency-response-of-a-simulink-model\/"},"modified":"2016-12-20T14:57:31","modified_gmt":"2016-12-20T19:57:31","slug":"how-to-estimate-the-frequency-response-of-a-simulink-model","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2010\/02\/26\/how-to-estimate-the-frequency-response-of-a-simulink-model\/","title":{"rendered":"How to Estimate the Frequency Response of a Simulink Model"},"content":{"rendered":"A core theme in <a href=\"https:\/\/www.mathworks.com\/solutions\/model-based-design.html\">Model-Based Design<\/a> is\r\nreusing your intellectual property for different tasks.\u00a0 Analysis for the purpose of design is a\r\ncritical benefit.\u00a0 Today I introduce <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/87056\">Erman\r\nKorkut<\/a> to show how to estimate the frequency response of your Simulink\r\nmodel.\r\n\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2010Q1\/ermankorkut.png\" alt=\"Erman Korkut, frequency estimator\" \/>\r\n<ul>\r\n \t<li>Have you ever estimated frequency response of your Simulink\r\nmodels?<\/li>\r\n \t<li>Have you ever tried to create \u201cgood\u201d input signals for this\r\npurpose?<\/li>\r\n \t<li>Have you ever tried to automate this process by writing scripts to\r\nmodify your model for simulation using the designed input, and to process the\r\nresults for estimation?<\/li>\r\n<\/ul>\r\nIn R2009b, released this past September, <a href=\"https:\/\/www.mathworks.com\/products\/simcontrol\/\">Simulink Control Design<\/a>\r\nadded a set of commands that streamline this whole process.\u00a0 Now you can design\r\ninput signals, simulate, estimate the response and diagnosing results, without\r\nmodifying your model in the process!\r\n\r\n<strong>What is frequency response?<\/strong>\r\n\r\nFrequency response is basically the change in amplitude and\r\nphase of a system\u2019s output at steady state with respect to a sinusoidal excitation.\r\nBy analyzing the system\u2019s frequency response, you can gain insight into the\r\nsystem dynamics and robustness to noise. You can also design a <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/control\/rn\/br5mf0z-1.html#br6p87d\">controller<\/a>\r\nfor your system using the estimated frequency response data as a plant.\r\n\r\nOne way to estimate frequency response of a Simulink model\r\nis to <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/slcontrol\/ug\/linearize.html\">linearize<\/a>\r\nthe model. Linearization works for many models, but does not support models\r\nwith certain <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/slcontrol\/ug\/briir1h-16.html#brikx7z\">blocks<\/a>.\r\nEven if your model is linearizable, you might still want to validate your\r\nlinearization result by independently computing the frequency response from\r\nsimulated data.\r\n\r\nLet\u2019s go through the process of frequency response\r\nestimation using a sample engine model, where the input is the throttle angle\r\nand the output is the engine speed.\r\n<p style=\"margin-bottom: .0001pt; line-height: normal; text-autospace: none;\"><span style=\"font-size: 10.0pt; font-family: Consolas; color: black;\">&gt;&gt; scdengine\r\n&gt;&gt; mdl = <\/span><span style=\"font-size: 10.0pt; font-family: Consolas; color: #a020f0;\">'scdengine'<\/span><span style=\"font-size: 10.0pt; font-family: Consolas; color: black;\">;<\/span><\/p>\r\n&nbsp;\r\n\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2010Q1\/engine_model_small.png\" alt=\"Engine model, throttle angle input, engine speed output.\" \/>\r\n\r\nThe first step is to create a \u201cgood\u201d input signal for\r\nfrequency response estimation:\r\n\r\n<strong>Creating input signals for frequency response estimation<\/strong>\r\n\r\nBy definition, frequency response estimation requires a\r\nsinusoidal input to excite your system. <a href=\"https:\/\/www.mathworks.com\/products\/simcontrol\/\">Simulink Control Design<\/a>\r\nprovides the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/slcontrol\/ug\/frest.sinestream.html\">frest.Sinestream<\/a>\r\ncommand to create such a signal.\r\n\r\nLet\u2019s create a set of sinusoids with 30 frequencies, varying\r\nbetween 0.1 and 10 rad\/s. These are the frequencies that we want to compute the\r\nmodel\u2019s response for:\r\n<p style=\"margin-bottom: .0001pt; line-height: normal; text-autospace: none;\"><span style=\"font-size: 10.0pt; font-family: Consolas; color: black;\">&gt;&gt; in = frest.Sinestream(<\/span><span style=\"font-size: 10.0pt; font-family: Consolas; color: #a020f0;\">'Frequency'<\/span><span style=\"font-size: 10.0pt; font-family: Consolas; color: black;\">,linspace(0.1,10,30))<\/span><\/p>\r\nThe sinestream input signal:\r\n\r\nFrequency\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : [0.1 0.44138 0.78276 1.1241 ...] (rad\/s)\r\nAmplitude\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 1e-005\r\nSamplesPerPeriod\u00a0\u00a0\u00a0 : 40\r\nNumPeriods\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 4\r\nRampPeriods\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 0\r\nFreqUnits (rad\/s,Hz): rad\/s\r\nSettlingPeriods\u00a0\u00a0\u00a0\u00a0 : 1\r\nApplyFilteringInFRESTIMATE (on\/off)\u00a0\u00a0\u00a0 : on\r\nSimulationOrder (Sequential\/OneAtATime): Sequential\r\n\r\n&nbsp;\r\n\r\nThe resulting <em>sinestream<\/em>\r\ninput signal has several adjustable parameters; including the frequencies, amplitude,\r\nthe number of periods etc.\r\n\r\nThe <em>SettlingPeriods<\/em>\r\nparameter is especially important. This parameter specifies the period of your\r\nresponse where after the system is assumed to be at steady state. This parameter\r\nhelps you ensure that the frequency response estimation focuses on the steady\r\nstate portion of the response. The <em>Sinestream<\/em>\r\nsignal is the best input signal for frequency response estimation, because with\r\nthis signal you can isolate steady state portion of the response. However, you\r\ncan also design and use other types of input signals. For example, you can\r\ncreate a frequency sweep (chirp) using the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/slcontrol\/ug\/frest.chirp.html\">frest.Chirp<\/a>\r\ncommand or create a random signal with the command <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/slcontrol\/ug\/frest.random.html\">frest.Random<\/a>.\r\nYou can also use a <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/timeseries.html\">timeseries<\/a>\r\nobject to specify a custom input signal.\r\n\r\n<strong>Estimating the frequency response<\/strong>\r\n\r\nAfter you create an input signal for frequency response\r\nestimation, the next step is to run simulation using this input to obtain the output\r\nsignals and use this data to estimate the frequency response. <a href=\"https:\/\/www.mathworks.com\/products\/simcontrol\/\">Simulink Control Design<\/a>\r\ncombines these steps in the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/slcontrol\/ug\/frestimate.html\">frestimate<\/a>\r\ncommand:\r\n<p style=\"margin-bottom: .0001pt; line-height: normal; text-autospace: none;\"><span style=\"font-size: 10.0pt; font-family: Consolas; color: black;\">&gt;&gt;\r\n[sysest,simout] = frestimate(mdl,getlinio(mdl),in);<\/span><\/p>\r\n&nbsp;\r\n\r\nThe syntax of <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/slcontrol\/ug\/frestimate.html\">frestimate<\/a>\r\nis very similar to <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/slcontrol\/ug\/linearize.html\">linearize<\/a>.\r\nYou must specify the model, the linearization input\/output points and the input\r\nsignal you designed. In our model, the linearization input and output points are\r\nalready set at the throttle angle and the engine speed signals and you get them\r\nusing <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/slcontrol\/ug\/getlinio.html\">getlinio<\/a>.\r\n<a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/slcontrol\/ug\/frestimate.html\">frestimate<\/a>\r\nautomatically performs simulation(s) where designed input signal is inserted\r\nfrom the specified input point(s) and the specified output signal(s) are logged.\r\nIt uses the obtained output signals to estimate the frequency response. It\r\nreturns the estimated response as a frequency response data <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/control\/ref\/frd.html\">object<\/a><u>,\r\n<\/u>\u00a0<em><span style=\"font-family: 'Calibri','sans-serif';\">sysest<\/span><\/em>, \u00a0and\r\nthe simulation outputs in <em><span style=\"font-family: 'Calibri','sans-serif';\">simout<\/span><\/em>.\r\n\r\nThe nice thing about the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/slcontrol\/ug\/frestimate.html\">frestimate<\/a>\r\nis that it injects the input signal and runs the simulation logging the outputs\r\nwithout requiring you to change your model. You specify input and output points\r\nto be anywhere in your model and <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/slcontrol\/ug\/frestimate.html\">frestimate<\/a>\r\ntakes care of the rest!\r\n\r\nLet\u2019s look at the Bode plot of the estimated frequency\r\nresponse.\r\n<p style=\"margin-bottom: .0001pt; line-height: normal; text-autospace: none;\"><span style=\"font-size: 10.0pt; font-family: Consolas; color: black;\">&gt;&gt; bode(sysest)<\/span><\/p>\r\n&nbsp;\r\n\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2010Q1\/bode_plot.png\" alt=\"Bode plot of the estimated frequency response.\" \/>\r\n\r\n<strong>Confirming whether your frequency response estimation is\r\ngood<\/strong>\r\n\r\nThe <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/slcontrol\/ug\/frest.simview.html\">frest.simView<\/a>\r\ncommand lets you analyze your frequency response estimation results.\r\n<p style=\"margin-bottom: .0001pt; line-height: normal; text-autospace: none;\"><span style=\"font-size: 10.0pt; font-family: Consolas; color: black;\">&gt;&gt; frest.simView(simout,in,sysest);<\/span><\/p>\r\n&nbsp;\r\n\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2010Q1\/simview_figure.png\" alt=\"Plot of the computed FFT given a frequency input.\" \/>\r\n\r\nThe frequency response is the\r\nplot at the bottom where the frequency points are color-coded. You can use the\r\nslider on the frequency response plot to interactively explore the time\r\nresponse and the FFT at each frequency of the input. In the time response plot,\r\nthe steady state portion of the output signal is the thick line. The FFT plot\r\nshows the spectrum corresponding to this output signal. At the selected\r\nfrequency of 0.1 rad\/s, the time response includes transients during the first\r\nperiod but reaches steady state afterwards.\r\n\r\nSometimes, it takes a couple of tries to get good estimation\r\nresults. For example, it is possible that you need an input with different\r\namplitude, or you need to increase number of periods to drive the system to\r\nsteady state. The time and FFT plots in simulations results viewer help you\r\nidentify such issues which you can address by changing the parameters of your\r\ninput signal.\r\n\r\nYou can learn more about frequency response estimation in\r\nour <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/slcontrol\/ug\/br5s3tf.html\">documentation<\/a><u>\r\n<\/u>and demos.\r\n\r\n<strong>Now it is your turn<\/strong>\r\n\r\nDo you estimate the frequency response of your Simulink\r\nmodels? Try estimating the frequency response of your Simulink model and <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=83&amp;#comment\">share how it goes<\/a>.","protected":false},"excerpt":{"rendered":"<p>A core theme in Model-Based Design is\r\nreusing your intellectual property for different tasks.\u00a0 Analysis for the purpose of design is a\r\ncritical benefit.\u00a0 Today I introduce Erman\r\nKorkut to show how... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2010\/02\/26\/how-to-estimate-the-frequency-response-of-a-simulink-model\/\">read more >><\/a><\/p>","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[121,115,65,16],"tags":[122,116],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/83"}],"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\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=83"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/83\/revisions"}],"predecessor-version":[{"id":6118,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/83\/revisions\/6118"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=83"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=83"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=83"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}