Simulink - Find computed index of multidimensional signal at specific time - matlab

I am trying to find which element(index) of a multidimensional signal is computed at a specific time in a Simulink model. Assume that there is a multidimensional input signal given to an add block together with a constant so Simulink calculates the sum with the constant for each element of the signal and gives the output as multidimensional signal. I want to know which index of the signal is computed at a time so I can do additional computation. How can I do that?

Do you want to do additional computation during simulation or after? In the latter case, you can save the multidimensional signal to the MATLAB Workspace as a "structure with time" variable. Then do your additional computations by finding the index of the time point of interest and the associated multidimensional data point. In the former case, you can probably a MATLAB function block with two inputs: the output of a clock block and the multidimensional signal. Then do your processing in there using MATLAB code.

Related

Input signal optimization

I have a system, described by a black-box, that takes as input a signal in time (let's say something similar to a sine wave) and returns a single scalar as output.
My goal is to find the optimum signal that maximizes/minimizes the output. As constraints, the time average of the signal must be kept constant and the minimum and maximum value of the signal must be within a specific range.
The parameters to be optimized are all the points describing my signal in time.
I want to use the scipy.optimize library to minimize the output of my black-box system however, everytime the parameters are modified by the optimizer, I need to smooth the input signal in order to avoid discontinuity.
My question is: is it possible to access the input variables after they are modified by the algorithm, smooth them, and substitute them at every iteration of the algorithm?

How does DiffEqFlux.sciml_train work for Neural ODEs in Julia?

I have a ganeral question about how Neural ODE Nets are trained in Julia. Are data points being sampled from the tspan on which the Nural ODE is defined and on them the parameter updates computed? In other words is there some shuffling and batching happening during training or is the loss computed over all data points in the tspan?
parameters are optimized according to minimizing the loss function. So it's up to you to define how the sampling occurs in the loss function. Typically one may be comparing the output to discrete data points, in whcih case those become your discrete points.
But neuralODE isn't handling this--- you are. It's the loss function
I found an answer about what Julia is doing here: https://github.com/JuliaDiffEq/DiffEqFlux.jl/blob/master/src/train.jl .
"Optimizes the `loss(θ,curdata...)` function with respect to the parameter vector
`θ` iterating over the `data`. By default the data iterator is empty, i.e.
`loss(θ)` is used. The first output of the loss function is considered the loss.
Extra outputs are passed to the callback."
I think that to do batching one needs to sample datapoints and then run Flux.train on them in a loop, giving as input the batch data points.

Simulink MATLAB function block with vector inputs

I'm new to Simulink and am working on a project.
The bigger picture is that I want to gather different sensor data, pick out an interval of that, and in my Matlab functions analyze it, a kind of adaption. It feels like I have a fundamental misconception since I thought my Matlab scripts could work directly in Simulink. I have a couple of Matlab function blocks which in Matlab takes vectors as inputs, but when I use a source such as "from workspace" I get the error that it cannot handle more than one value at the same time: Index expression out of bounds. Attempted to access element 2. The valid range is 1-1.
So I wonder if there is any neat way of collecting a given interval of a continuous flow of sensor data (as a beginning, the raw input will just be a vector which will represent sensor data) so that it could be analyzed by some Matlab functions?

Iterate over array from workspace at each sample time in Simulink MATLAB function block

I have a MATLAB function block embedded in a Simulink model. In my initFnc callback I set up some vectors which I need to use in my function block inside the simulink model. The vector is 1x10000 and contains setpoints for a robotic arm. The function block simply needs to read the next value at each sample iteration.
So far I've tried using "From Workspace" and "evalin()" but they all throw various errors when building the model (I'm using code generation which doesn't play nice all the time).
What would be a good way to read in that vector in Simulink and feed the cells one by one into my function block? Something like repeating sequence stair but without the repeating part.
I got it to work by reading in the vector into a constant block and feeding that into my embedded function. I then use a persistent iterator which is a 1x1 matrix (ones(1)) and incrementing it every time the value from the vector is read.

Storing signal as vector for input to Matlab Function Block - Simulink

I'm trying to build a Simulink model containing a "s-function block" simulating a continuous process with a "Matlab Function Block" that use the input and output from s-function.
But I need the input to the "Matlab Function Block" with differents values of the same signal over time. That is, a vector with different sampling times for each input to "Matlab Function Block". This will be needed for testing identification techniques.
How could I do this?
Thank you
Assuming you are using a fixed-step discrete solver, and that you don't have too many values of the same signal to hold, you could use Unit Delay blocks to get the value of the signal at previous time steps. You can then mux all these signals together to form your vector input. Obviously, the practicality of it is limited by how many values of the signals you need to have (and buffer).