"Find" block alternative that is HDL code compatible - simulink

I need to find the index of the minimum value in a vector using all HDL code compatible blocks in simulink. This can be done quite simply using the "Find" block, but sadly this block is not HDL code compatible. Can someone propose an alternative using blocks that are HDL compatible?
code working with "Find" block

You can use the minimum block, which directly returns the index. This block require the DSP Toolbox.

Related

MATLAB Coder and parfor in MATLAB R2014b

Does the MATLAB Coder in MATLAB R2014b support parfor?
If I check the documentation, it reports:
Treated as a for-loop in a MATLAB Function block.
Does that mean that there is no speed improvement?
The loop runs in a serial manner only in the context of a "MATLAB Function" block
If you check the MATLAB Coder parfor reference page:
http://www.mathworks.com/help/coder/ref/parfor.html
You can see this information:
parfor ... ... creates a loop in a generated MEX function or in C/C++ code that runs in parallel on shared-memory multicore platforms.
Does Matlab Coder in Matlab-r2014b support parfor?
Yes, the list provided in you reference tells you it is supported.
Does that mean that there is no speed reduction?
Yes. You literally quote "it is treated as a for loop".
So in the strict sense of the word parfor is supported, since it will not throw an error. However, it is treated the same way MATLAB would treat it when the parallellisation toolbox is not installed, as a regular for loop. Thus yes, you can compile MATLAB code containing parfor loops, but they will be treated as serial for loops.
Note that the above only holds true for function blocks; as #Edric pointed out:
parfor ... ... creates a loop in a generated MEX function or in C/C++ code that runs in parallel on shared-memory multicore platforms.

is there a faster version of fminbnd in matlab?

I am now using fminbnd in Matlab, and I find it relatively slow (I am using it inside a nested loop). The function itself, its interface and the values it returns are great, but when looking into the .m file I see it is not optimized. As a matter of fact, I was hoping for something like that to be written as a mex.
Anyone knows of an alternative to fminbnd that works much faster and does not have as much overhead?
It's written like that because it has to evaluate (feval) your user-defined function(s) on every iteration. Matlab's ODE solvers work in the same way. In current Matlab it's costly for a C/C++ code to call a user-defined Matlab function and read in its return values iteratively.
Make sure you're using the options correctly, that fminbnd is the correct tool (maybe a simpler scheme would be better or, since this in a loop, maybe a multi-dimensional method like fminsearch would be more appropriate), and have optimized your objective function. The next easiest thing would be to try compiling your Matlab code to C or C++ (see codgen). You'll likely need to compile in your objective function, and all of the options, as well in order to avoid the slowdown issues mentioned above. I've not tried this for fminbnd, but I did see mention of it working online. If your objective function itself is complicated, you could try just converting it to a mex function.
fminbnd is based on Brent's method. You can find C, C++, and FORTRAN code for that here. The GSL also has a version: gsl_min_fminimizer_brent.

Sorting an array in Simulink

I want to sort a given array in ascending order. Is there any block in Simulink which does this?
If no, how to do this most efficiently using common Simulink blocks? I understand this could be done using Stateflow but just wanted to know how to do in Simulink.
Thanks!
If you have DSP System toolbox, there is a Sort block available under statistics library. Otherwise you need to use MATLAB Function block as suggested by am304.
Use a MATLAB Function block in conjunction with the sort function. I think that block supports matrix inputs.

RMS not supported in Matlab function inside Simulink

Simulink has a module called "Matlab Function," which allows you to create a custom function in a Simulink flow diagram.
I implemented a simple function in a Simulink Matlab Function module. My function contains a call to Matlab's built-in rms(). When I run the Simulink model, I get the following error:
The function 'rms' not supported for standalone code generation
If I remove rms from my Matlab Function in the Simulink model, the error goes away and the model runs flawlessly.
Questions:
Is there a way to use Matlab's rms in Simulink?
Are there many other native Matlab calls that can't be used inside Simulink?
I just wanted to clarify and expand upon some points made in learnvst's answer.
Even if you are simply trying to simulate a model containing a MATLAB Function block and are not explicitly attempting to perform code generation, you will still get the not supported for standalone code generation error.
As learnvst indicated, there are multiple restrictions on functions that can be used with code generation. However, if you just want to simulate your model, Simulink allows you to do this if you signify these "black-listed" functions as being extrinsic. This lets Simulink know that the functions will be used for simulation purposes only and won't be part of code generation.
In your particular case, add the following line of code somewhere before your call to rms:
coder.extrinsic('rms');
Declaring a function as extrinsic in a MATLAB Function is often useful even when you are performing code generation. For example, you may want to visualize your data using the plot command during simulation, but obviously would not need the plot command to be part of generated code.
Refer to this doc for more info on declaring functions to be extrinsic.
The not supported for standalone code generation part of the error suggests to me that you are trying to use a product like Matlab Coder to make an executable or native code. If this is the case, there are many naive calls that cannot be used directly in both core Matlab and the toolboxes. The coder products only support a subset of the language. More information can be found here . . .
http://www.mathworks.co.uk/products/matlab-coder/description2.html
As for your call to rms, it is only calculating the root of the mean of the squares. Try creating an alternative using something like . . .
sqrt(mean(x.^2))
...where x is the signal.

How can I call an m file in Simulink and put it to a block in my model?

How can I call an m file in Simulink and put it to a block in my model (without using an S function)? Does anybody have an idea? I'd really appreciate it.
If you're trying to apply a user-defined MATLAB function to Simulink signals, there are a few different ways to do this, depending on your objective. All options are available within the User-Defined Functions section of the Simulink library.
Use the MATLAB function block if you intend to generate code from your model. This block does have restrictions, the entire gamut of built-in MATLAB functions is not available.
Use the Interpreted MATLAB function block if you don't care about code generation, this block can make use of any functions.
Use the Fcn block if your m-file is trivial and contains a simple expression operating on the inputs. In this case you can type the expression directly into the block dialog and reference the input / output signals as shown in the documentation.
MATLAB Fcn block is the best solution to embed M-function file into Simulink model. However, be cautious which version of MATLAB you are using, e.g., with later versions of MATLAB Function Block can be implemented with M-function file with %#codegen and C compiler need to be with your MATLAB package. Good luck