How to apply a running window to a simulink signal using embedded matlab function? - matlab

I want to apply a moving window to the signal of my Simulink model using embedded Matlab function. But whenever I try to divide the signal in time frames, it is always showing to have 1 element. Please suggest how to apply a running window to such signals.

Using a MATLAB function block might not be the best choice. Here is a simple implementation of a moving average filter. Add, Sin and Noise on the left generate an example signal. The mux in the centre is used to insert the new element. The Variable Selector is used to drop the oldest element. In my case I used a workspace variable N for the window size. When really using this I recommend to wrap everything into a subsystem and make it a mask parameter.

Related

Paraview IntegrateVariables and Plot Selection Over Time

I am using the IntegrateVariables filter in Paraview to integrate an energy density rho over a cylindrical volume (obtained using the Clip filter). I need to plot this integrated rho over time, which should be possible using 'Plot Selection Over Time'. However, I can't find a way of applying the filter to the right variable and when I press Apply, Paraview just buffers until I force quit. Can anyone help?
There is no way to specify to a PlotXXX filter which array to process.
But there is the 'PassArray' filter which allows to extract the arrays you want, limiting the computation and the memory consumption.

How to take values generated from a MATLAB program and display them in a Simulink block?

I have a working MATLAB program hooked up to an Arduino and rotary sensor that displays the current angle. I would like to display this angle on a Simulink model, so I can control a motor based off the current angle. Is there any way to do this without creating an angle sensor in Simulink and just using my MATLAB code that already works?
Your question is not really clear and it depends if you are trying to do it before simulation or during simulation.
If you want to do it before the simulation, just create a constant block with a variable name that is taken from the workspace, and let your function to set that variable in the workspace.
If you want to do it during simulation, it is a little more difficult but still possible. Lets create a sample a MVCE. The simulink model mask.slx contains a constant the is set to 0, and this constant value is displayed in the display block on the right. The simulation time is set to inf, so when you play it, you must stop it manually.
It is possible to checge the value of the constant while the simulation is running using this simple Matlab call:
set_param('mask/Value_to_set', 'Value', '10')
you should also consider that the constanta must be a Tunable gain (it is by default).
(as you can see the simulation is running).
There are some additional (and surely better) solution you can use:
Include your Matlab function in a MATLAB User Defined Function block, and call it at each simulation iteration. If you have compilation issue you shall follow the coder.extrinsic way (here an example for fmincon)
Use the Simulink Support Package for Arduino Uno Hardware Add-on that is available in the Add on store.

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.

Reproducing a discrete filter block in simulink

I would like to make a discrete filter, where the sampling rate can be controlled by an input. I am trying to understand how the discrete filter block looks, "under its own mask." Is there anyway to retrieve the code behind this block so it can be modified for my use?
You can use an user-defined function as a filter, pick the filter transfer function, translate it into a difference equation (the discrete-time equivalant to the diferrential equation), implement that difference equation in a function and feed the sampling rate as an input (the sampling rate will appear as a constant in your difference equation).
The block is too complex and has too many options to simply be able to look under the mask. Your best option is to have a look at the documentation, which does show some detailed implementation of the block in some articular cases to get an idea and then try to recreate the discrete filter you want from basic building blocks, using a constant sample time at first, until you can validate your own implementation against the Simulink library block. Only then, start considering how you will change the sample time. Your main problem though is that the filter coefficients will change with the sample time, so you need to be able to re-calculate them on the fly. It's not an easy problem, I don't even know if it's possible.

How to deal with video frames in simulink

I am working on videos in simulink.As we know that multimedia file block reads one frame at a time so when I will attach it with matlab function block it should read one frame at a time with imread command. If I double click the matlab function block (as shown at http://tinypic.com/view.php?pic=dggujd&s=6) then you will see that I have to give the name of the function as written in matlab mfile along with the input; which in this case is video(named as convid.avi). I am reading one frame at a time but I have given the whole video as an argument to the matlab function.This is the problem how can I resolve it.What should be given as an argument in matlab function block rather than the whole video.I have also uploaded my model at http://tinypic.com/view.php?pic=55jggw&s=6. The code which I am using for vidfunc is:
function h=vidfunc(u)
a=imread(u); % read frame
BW = edge(a,'sobel'); %sobel edge detection
[H,thetaa,rhoo]=hough(BW); % Hough Transform
P = houghpeaks(H,6,'threshold',ceil(0.5*max(H(:))));
lines=houghlines(BW,thetaa,rhoo,P,'FillGap',15,'Minlength',15)
figure,imshow(I),hold on
for k = 1:length(lines) % Draw lines
xy = [lines(k).point1; lines(k).point2];
z(k)=lines(k).point2(2);
plot(xy(:,1),xy(:,2),'LineWidth',1,'Color','green');
end
h=z(k)
It looks like the meat of the work done in this "Simulink" model is actually being performed by your MATLAB function. So the answer to this question is going to largely rely on what that function is actually doing. Specifically, what is the expected input to vidfunc, and what is this function's output? I suspect that this function may need to be revised to fit into your model.
To debug your model, it's useful to think about what the signal output is from each block. At each time step, your From Multimedia File block will output a single image frame, which according to the doc looks to be structured as an
M-by-N-by-P color video signal where P is the number of color planes.
Moving downstream, we next come to the Color Space Conversion block, which in this case looks like it will most likely output an image frame in the form of an M-by-N matrix (where each element of the matrix corresponds to the image's intensity at that pixel).
Now we come to the interesting part -- the MATLAB Fcn block. As we just saw, the input to this block will be an M-by-N matrix representing a single image frame. When you look in the parameter dialog box for the MATLAB Fcn block, the input to this block is represented by the variable u. Therefore, to execute the vidfunc function on the image frame being input to this block, you would simply enter vidfunc(u) for your MATLAB function.
Now, based on the input going to the MATLAB Fcn block, and the fact that you have a Video Viewer block connected to the output, vidfunc should be structured such that it operates on a single image frame as its input and outputs another single image frame. If vidfunc is not structured in this way, you will need to edit it (or just re-implement the same functionality using Simulink blocks).
That said, let's assume that vidfunc is also returning an M-by-N matrix representing a processed image frame. You will want to set the Output Dimensions parameter for your MATLAB Fcn block to -1 to indicate that the output will have the same dimensions as the input. Also, (as indicated in the doc) you will want to make sure Collapse 2-D results to 1-D is unchecked or else your image output will be in the form of one long vector rather than an M-by-N matrix.
Provided that vidfunc is structured correctly, this should solve your problem.
NOTE: To make your life a lot easier, I would strongly suggest displaying the signal data type and dimensions in your Simulink model. This can help to avoid a lot of confusion. This doc describes exactly how to do this.
--UPDATE--
After looking at your code, this confirms my suspicion that the inputs/outputs of vidfunc are inconsistent with what your Simulink model expects. The way that you proceed is highly dependent on what your own design constraints are and what you actually want out of this system. Basically, your Simulink model and MATLAB function disagree... which one is right? I'll give some general thoughts based on my best guesses at what you're aiming for.
First, Simulink is passing an image (in the form of an M-by-N matrix) into vidfunc. This means that vidfunc no longer needs to load an image at the start of the code. So I believe that you could update the first few lines of code to be as such:
function h=vidfunc(a)
BW = edge(a,'sobel'); %sobel edge detection
See that now vidfunc is taking the actual image (not the filename that contains the image) as its input. Basically, you are removing the a=imread(u); line and jumping right in to processing a.
The other issue is the output of vidfunc. Simulink is expecting the output to be an image, but it is not. I'm not 100% sure what h is supposed to be in this code (when I first glanced at your code I thought these were handles to line objects but that doesn't seem to be the case). It looks to possibly be the y coordinate of the endpoint of one of the houghlines. Nevertheless, it's not what your Simulink model is expecting. This one is not as straight-forward to fix. Possibly you could try to use getframe to grab an image from the plot of your lines.
I actually feel that the best advice that I can give to you is to just scrap the MATLAB function and implement everything in Simulink. I think this will be a lot easier than trying to get vidfunc to play nicely with your model. vidfunc does not actually contain that much code, so this should not be too difficult of a task for you. Another benefit is that at the end of this process you'll have a nice Simulink model that explicitly shows all of the image processing steps that you are taking.
I believe that all of the image processing that you are doing with MATLAB functions can also be done with Simulink blocks (see the Simulink Blocks section of this doc).
Good luck.