Matlab Function inside simulink - matlab

I need to use a Matlab function inside of a Simulink model. I know how to use a Matlab function to do simple stuff. But what I need now is a little bit more complicated. Let me give you a basic example.
Assume that I need to have a block to generate a sine wave to be viewed directly on the scope (I know that there is already a sine-wave block, I'm just taking that as an example). If I'm writing in Matlab NOT in Simulink, I would do something like:
t = [0:1/30000:0.2];
A = 1;
f =10000;
y = A*sin(2*pi*f*t);
plot(t(1:100),y(1:100))
How can I build the same function inside a Simulink matlab-function block and see the results directly on the Scope?
Remember: The Matlab Function Block has two ports, u and y. Which represents input and output respectively. In the above-given example, a sine-wave generator doesn't need an input.

Perhaps it's just that you haven't chosen a very good example, but there are several things to be aware of when translating the code you've given into Simulink.
The easiest way to get the simulation time into a MATLAB Function block is by feeding a Clock block into an input port (which as #Daniel indicates, are optional, but in this case I suggest would be used for t). So I think you do want an inport in this example.
Your use of plot in your example only plots the first 100 points, where as a Simulink Scope rolls through the data being displayed. There's no concept of only displaying the first X points in a Scope when the simulation runs for longer than that.
You need to remember that Simulink generates data one simulation time step at a time, so you can't generate them all (as per your MATLAB code) and then plot them all. (Well... you can if you want to use frame based signals, but I assume that's not what you're asking here.)
So, to implement the equivalent of what you have would involve doing the following.
write a MATLAB Function block containing the following code (although you might want to make A and f input parameters rather than hard coding them)
function y = myCustomSineWave(t)
A = 1;
f =10000;
y = A*sin(2*pi*f*t);
Feed a Clock block into the above block, and have a Scope block on its output

Related

Sample sequence of points from continuous signal simulink

I have a Matlab function (created by me) that must be evaluated only at a given rate. I would like to sample the value of a signal, give to this function (discrete values) and then, the calculated output must be hold until the next value is available. Is there a way in simulink to do this? All answers I have found use quantizer + ZOH but in this case I still get "a continuum" (or almost it) of points to be evaluated by thsi function which is really slow. Changing the rate of simulink's solver is also not an option as the result of this function will be given for a continuous time system.
Any help will be highly appreciatted!
Thank you
Assuming by Matlab function you mean a MATLAB Function block, then it sounds as if all you need to do is make the block discrete. Do that by right-clicking on the block, going down to Block Properties and then in the resulting dialog enter your required sample time.
The block will then sample its input and generate an output (which is held between sample times) at each sample time.

AC coupling in MATLAB / Simulink

I've been trying to use Simulink for modelling an oscilloscope (including DSP). I am still very novice so please forgive me if the following question is way too easy: How do I implement AC coupling (remove DC component) in Simulink while using analog signals? I know there is a DC blocker, but it only accepts discrete signals and I want to discretize my signal later with an imperfect ADC.
I'm trying to use a MATLAB function for the AC coupling, but I cannot find a way to use the existing analog signal gained from the function declaration. Instead, all MATLAB documentary seems to want me to use an analog device on my computer (https://mathworks.com/help/daq/analog-data-acquisition.html).
Thank you for any efforts and/or suggestions! :)
I don't think using the Matlab Function Block will work since you need to hold a state. To simulate AC coupling, I believe that you need to create a high pass filter. This removes DC from the signal so that the waveform is centered around zero. The corner frequency generally needs to be very low so that there is no distortion of the waveform being measured. You can build this without any special toolboxes. You just need an integrator block, gain block and some summation blocks.
An example is provided below:
This example applies a DC step to the waveform at 1 second. The figure below provides the output
In the upper figure (input) the offset remains, but in the lower figure (output) the offset is removed.
If you want using the matlab AC Coupling function, try using the Matlab Function Block : https://fr.mathworks.com/help/simulink/ug/creating-an-example-model-that-uses-a-matlab-function-block.html . You can use it like any matlab function, i.e. function [out1, out2] = my_function(in1) but it will be defined in a simulink block.

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.

Generating square wave using Matlab embeded function

I intend to generate a square wave which is applied on a DSP.
I have written these codes and put them in an embeded Matlab function.
function y = fcn(u)
%#eml
t=0:0.001:1
h = sign(sin(125600*t+u));
y= (h+1)/2
where, u is a constant value of 0.582 which is used for shifting the square wave.
The problem is at the output in the simulation, instead of getting a square wave, I see only two straight lines of y=o and y=1.
Please let me know where is the problem that I can not get the square wave?
Note that the frequency of square wave must be 20 kHz. Therefore, I adjust the sampling time as 1e-7 s. And also its amplitude is between 0 and 1 In addition, due to this signal must be transferred to a DSP board, in the "solver option" I chose the type: " Fixed-step" and for the Solver: "Discrete (no continues state)".
Thanks a lot.
This is wrong on many levels.
First of all, you never define the time vector inside a MATLAB Function, that's what the Simulink engine does. Pass time as an input to your MATLAB Function block and use a Clock block to generate the time input.
Second, the above is fine for simulation, but it sounds like you are generating C code from the Simulink model to run it (in real-time) on your DSP. This is not my area of expertise, but from memory, I think you need to enable "absolute time" or something similar for the above to work with code generation. However, I think this is target-dependent and so I'm not sure whether this will work on your DSP.
In you function type plot(t,y) at the end. You are generating a 20khz square wave (assuming you are sampling at 1e-7). Essentially your generating it is working.
Now, what is the DSP board you are using/any information that is relevant to your problem?
I don't know what you are referring to when you say "Solver" either.
Is the "simulation" an oscilloscope or a program? Either way perhaps it is not triggering correctly? Is there an edge trigger option?

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.