Isra
Isra

Reputation: 155

Sampling frequency from Simulink to Matlab

I'm running a simulink model from simulink using matlab. My system is mainly in matlab, but I run the slx file and export the outputs to be used in matlab. The simulation is run for 48 seconds (1 second representing an hour). When I get the outputs, I'm expecting it to be the same quality as when I view it in simulink, but it's not. Here is an example of what my data looks like in simulink:

enter image description here

Here is how it looks like when I plot it in matlab (the number of samples becomes 307 when exported)

enter image description here

I tried to change the step size in simulink or change the solver, but this distorted my simulink output as the following.

enter image description here

My solver is ode45, how do I control the sampling frequency of my data so that I don't get different resolution after exporting it to matlab. P.S Once I export it, I will interpolate the data so that I get samples in between the hours (a sample every minute instead of every hours). If I can do it at once by changing the step size then that will be perfect.

following your advice, I got this plot when I plot it vs time instead of samples

enter image description here

Thank you

Upvotes: 0

Views: 2666

Answers (1)

Naib
Naib

Reputation: 1029

You are using a variable-step solver (ODE45) and thus there is a very high chance you won't get a consistent sampling frequency.

The only way to ensure/control the sampling frequency is to use a fixed-step solver (ode4 for instance).

However, as to why the data looks different between the Simulink scope and the plotted data, for variable timestep solvers there is refine factor (configuration parameters -> Data Import/Export -> Additional Parameters). This is by default set to 1. Set this to 100 and you should get a more consistent-looking sample density.

What should be known about the refine factor?

  • To get smoother output and have a better time resolution, it is much faster to change the refine factor instead of reducing the step size.
  • When the refine factor is changed, the solvers generate additional points by evaluating a continuous extension formula at those points.
  • The refine factor applies to variable-step solvers and is most useful when you are using ode45.
  • Usually a value of 4 produces much smoother results.

https://blogs.mathworks.com/simulink/2009/07/14/refining-the-output-of-a-simulation/ https://uk.mathworks.com/help/simulink/gui/refine-factor.html

Upvotes: 2

Related Questions