Sampling time as an input-simulink - matlab

I am trying to make a library of functions that will allow me to parameterize filters and other function. In simulink standard blocks, I am only able to set a sampling time prior, rather than take an input. Is there any ideas on how I can create this? My first approach was to use conditionals and a clock to allow this parameter to exist, but the clock cannot be parameterized either.
Thanks in advance.

The only way to do this is to write each of your blocks as S-Functions.
If you are using m-code then within the setup method you'll need to define the block sample time as
block.SampleTimes = [-2 0];
then within the output method you'll need to set the next sample time (based on the value of the input signal) by assigning the new value into block.NextTimeHit.
An example of doing this can be found here: A Square Wave with Jitter
A similar thing can be done in a c-mex S-Function by using the mdlGetTimeOfNextVarHit method.

Related

Setting a Matlab function block's sample time in Simulink

Is it possible to explicitly set the sample time of a Matlab function block in a Simulink model? If yes, how? If not, how can you change the sample time of a signal coming from a Matlab function block?
For instance, how can one make sure that the sample time of a (possibly variable-sized) signal coming out of a block will be discrete?
Your best option is to use the Rate Transition block.
Also have a look at the Convert Signals Between Continuous Time and Discrete Time example to see how it's being used in practice.
Using the Matlab Function block you can Right click > Block Parameters > Sample time. Change from -1 to the sample time you want.
To make sure your simulation is using the correct sample time you can select the double arrows in the left panel and click on "Colors". Then, back in the double arrows, select "Sample Time Legend". Now, the different colors represents different rates in which Simulink is running.

import a continuous bitsream from workspace into simulink using "In" block and then buffer it using "buffer" block

how to import a bitsream form binary vector from workspace into simulink.Actually I have found that I can use simin block or In block but my binary vector is independant of time. I tried to use Const block and it works but afer that when I wanted to put my output in the Buffer block in simulink, it didn't work because the input is continuous and not discrete. So I am asking if it's a way to add time to my binary uni-dimensional without having any influence on the result?and how can I do it?
Or is there another way to import this date to avoid this problem with Buffer block?
Your screenshot shows your constant block to have a sample time of Inf. As the error message suggest, you need to change that to a discrete sample time. In addition, you should also:
check your model is using a fixed-step solver
check what time step you are using for your chosen fixed-step solver (ideally the same as your constant).
You can have multi-rate models, but you need to manage the rate transitions with Rate Transition blocks. For more details on sample times, see the documentation, in particular how to view sample time information in a Simulink model. You should probably also have a quick look at the Choose a Solver section.

Change simulink parameters at runtime from the code/block flow

My initial problem is that I have a continuous transfer function which coefficients change with time.
Currently the TF's coefficients are expressed in function of the block mask parameters. These parameters are tunable, and if I change the value in the mask parameters dialog during a simulation the response seems to react appropriately.
However how can I do just that in the code/block flow? Basically, I
have the block parameter 'maskParam' which is set using the mask
parameters dialog, and in the mask initialization commands:
'param=maskParam'. 'param' is used in the transfer function and I
would like to change it in real time (as param=maskParam*f(t)).
I have already looked around and found relevant solutions but either it's unbelievably complicated; or the only transfer function which we are allowed to modify at runtime is discrete and 1) I would like to avoid z-transforming my quite complex TF (I don't have the control toolbox) 2) The sampling time seems to be fixed.. None uses this "dirty" technique of updating parameters, maybe that's the way around?
To illustrate:
I am assuming that you want to change your sim parameters whilst the simulation is running?
A solution is that you run your simulation for inf period and use/change a workspace variable during the simulation period to make the changes take effect.
for Example:
If you look at the w block, you can set it's value in runtime, by doing this:
set_param('my_model_name/w', 'value', 100); % Will change to 100 immediately
You can do similar things with arrays (i.e. a list of coefficients in your case).
HINT FOR YOU
You are using discrete transfer function block. Try the following:
1) Give your block a name e.g. fcn_1
2) In your script, type set_param('your_model_name/fcn_1', 'numerator', '[1 2]'); This will set the numerator value to [1 2]. Do the same for denominator.
3) You should be able to understand, through this exercise, how to handle the property names etc. so that you can change/get them using set_param/get_param.
I leave you to investigate further.
The short answer is that Simulink blocks are not really designed to do this. By definition, a transfer function is Liner-Time Invariant, meaning its characteristics (read coefficients) do not vary with time.
Having said that, there are some workarounds, such as the ones you mentioned in your question. These are the correct way to approach the problem I'm afraid, other than the set_param method suggested by #ha9u63ar. See also this blog on the subject on the MathWorks web site.

Any Tic Toc function in Simulink for embedded blocks

I have a system with some embedded Matlab blocks where I'd like to perform some actions after a certain amount of time, in this case turn on lights and switches in an interface to which I send signals from Simulink.
The problem is that I thought I'd use "tic"-"toc" and "while" in a Matlab function block to perform these actions, say one parameter becoming 1 after 5 seconds, the following parameter becoming 1 after 12 seconds and so on, but I noticed that tic-toc apparently doesn't work in Simulink for embedded functions.
Is there any similar functions that could be used in Simulink for embedded functions or is there any other way to do this?
Edit: I've tried to get the clock's time as well, but it's a growing value. Is there any way to "lock" the time as a parameter when the block's function is executed?
You shouldn't be using absolute time in an embedded system, which is at least one of the reasons why tic-toc and clock from MATLAB don't work with Simulink Coder.
You should create your own counter, which you start and stop when you need to.
This is pretty easy to do using a Unit Delay and Summation block.
If you need to be able to enable and/or reset the counter then use the appropriate block from the Additional Discrete library.

Variable storage in simulink

I am working on a simulink model, in which i am getting a supply of a a variable at certain time intervals and i need to use the recent 10 values of that variable only.
how should i proceed with that?
It's a bit cheesy, but how about using a chain of Unit Delay blocks?