Is there anything I can do with code in MATLAB for digital image processing that I can't do with Simulink's digital image processing toolbox or vice versa?
What difference is there between the two?
At the code level, there's not much difference between MATLAB and Simulink. Simulink provides an environment for constructing programs at a higher level of abstraction than text; the relationships that hold between code blocks is represented as arrows that pass between flowchart objects. You can then visualize the system as it operates at runtime, as data objects are passed visibly between the flowchart blocks. Simulink also lets you alter a part of the program in real time, so that you can see the effects of the local change without having to stop everything else.
Given how much image processing work is done in block-level dataflow languages such as LabVIEW, I would recommend starting with Simulink. Most industrial or creative image processing (like Apple's Shake) is done via a node-based interface. That's not to say that you couldn't use MATLAB code as well; it's just a bit more obtuse (this is a good example of a powerful library that was made with MATLAB code).
Related
I was wondering if it is possible to use simlink's PID architecture in matlab to actually control hardware - rather than modelling it.
I have been playing around a little with the PID tuning in matlab - and have worked out the correct gains for my system (or at least good enough).
Now I want to implement the actual control loop in matlab using the real hardware. In this case I am tying to stabilise a laser using a measurement from a wavemeter - although from the point of view of matlab essentially I have two functions - one which returns the current wavelength, and another which alters the wavelength with an input from 0-100.
How do I get the fancy PID objects to work in a loop where for example I input data each loop, and get the required output to stabilise things given my gains - or should I just hardcode the equations in?
I have tried looking through some of the simlink examples - but there doesn't seem to me much actual implementation - mostly just modelling.
Thanks in advance for any advice.
MATLAB (without Simulink, or with Simulink in purely a simulation mode) is not a real-time environment. Trying to use it to control hardware in real-time won't work unless you have very slow sample rates.
If you do have slow sample rates, then you'd connect to your hardware to MATLAB using device drivers such as those in the Data Acquisition Toolbox
You haven't found any info on real-time implementation in the Simulink documentation because from Simulink the mechanism for real-time implementation is via Simulink Coder. You would need to use it in conjunction with a real-time environment such as Simulink Desktop Real-Time (if your sample rates are relatively slow), Simulink Real-Time, or one of many other 3rd party real-time form factors.
I want to make a biometric identification system of the ECG/EKG.
Provided that Matlab does not perform Data Acquisition in Real Time (for monitoring), is there any way to make the monitoring and data acquisition in LabVIEW and then work simultaneously with Matlab for signal processing?
You could just get a matlab compatible daq and run everything in matlab. http://www.mathworks.com/products/daq/
You can indeed do some data acquisition with LabView and work simultaneously with Matlab for signal processing by calling the Matlab script node, which executes some Matlab code during vi execution.
You may have some performance issues, though, because both Labview and Matlab have to run on your machine simultaneously.
Question:
is there any way to make the monitoring and data acquisition on
LabView and then work simultaneously with Matlab for signal processing
Answers:
LabVIEW has "MathScript" node which is basic MatLab built into
an add-on. It is not the MatLab toolboxes. It runs native MatLab
code. It also runs slightly faster LabVIEW updates to the code. If
your code runs there, then LabVIEW will pass data natively
to your code. This box does not have direct MatLab toolbox access, so if
you use any special calls then that can cause a problem.
If you have MatLab on the box, then you can call the external MatLab
function/code using mathscript (link), and the MatLab will run
the function.
Clarification:
Real time just means "bounded time" (link), not "instant". If your idea of bounds are loose enough then many systems can work for them. You do not state it in your question - but what do you consider acceptable response time?
I've worked a lot with LabVIEW and Matlab. Personally, I would not use the Math Scripting node and would opt for using the Matlab Automation Server. You can call Matlab from LabVIEW using the ActiveX palette in LabVIEW (See Functions>>Connectivity>>ActiveX>>Automation Open) A couple reasons why I'd go for ActiveX and NOT the MathScript node:
The Math Script node does not allow you to change code dynamically. You must hardcode your data into the Math Script node and any future changes would require a change to LabVIEW's G code and therefore a recompile of your EXE
The Math Script node does not support all functions when compiled to an executable. Most notably graphing functions. See the help file here to read more on this.
Calling Matlab from ActiveX is going to give you a lot more flexibility in regards to how data is passed and processed.
I wonder if there's some experience out there, how much performance can be gained from using the Simulink Coder to generate models over running the model in (rapid) accelerator mode?
For instance, how does it compare to the quite dramatic improvements from not-accelerated to accelerated models?
I know this is kind of vague and probably depends on the specifics of a model, but I'd be happy with examplary numbers.
I don't know if you will really find speed improvement anecdotes that helpful, as your results are going to be highly dependent on your own implementation.
That said, the following reference on How Acceleration Modes Work might be useful to you.
As described in this link...
The Accelerator mode generates and links code into a
C-MEX S-function. Simulink uses this acceleration target code to
perform the simulation...
...the model methods are separate from the Simulink
software and are part of the Acceleration target code. A C-MEX
S-function API communicates with the Simulink software, and a MEX API
communicates with MATLAB. The target code executes in the same process
as MATLAB and Simulink.
The link goes on to describe Rapid Acceleration, as such:
The Rapid Accelerator mode creates a Rapid Accelerator standalone
executable from your model. This executable includes the solver and
model methods, but it resides outside of MATLAB and Simulink. It uses
External mode (see Host/Target Communication) to communicate with
Simulink.
(Note that the actual link has some nice diagrams that help to illustrate this architecture).
I think that there are a few important things to takeaway from these descriptions. First, in Accelerator Mode, code is only generated for the model methods; however, in Rapid Accelerator Mode, code is also generated for the solver. In either case there will be some overhead due to communication between the generated code and Simulink (though in general that overhead would be greater in non-rapid accelerator mode).
Now, whether you are going to see significant differences between these acceleration modes and full-on code generation will most likely depend on how you would be using the generated code. You had stated that your application is intended to essentially analyze/visualize the outputs of simulink models. Do you intend for you application, itself, to be a Simulink model? If so, I imagine your workflow would be that you generate code for the "plug-in" model and then incorporate this into your "parent" model as an s-function. Based on the descriptions given above, my gut feeling is that you would not see much of a performance improvement compared to accelerator mode.
However, if your application is to be implemented outside of Simulink, then code generation would definitely be the way to go. In this case, you've completely eliminated the overhead of communicating with Simulink. In this workflow, you could, for example, generate a stand-alone executable from the "plug-in" model, execute that model to get the outputs, and then simply load those outputs for analysis and visualization into your application. Your application could be written in any language you want. In fact, you could even still write your application in MATLAB -- the key point, though, being that your application runs once the stand-alone executable (generated from the "plug-in" model) has finished executing, rather than interfacing with the plug-in model while it is executing.
This may be the wrong place to ask this, but I can't find a better place on the SE network.
I've briefly worked with both Matlab and Ansys, and from what I have learnt/can gather, Matlab is a programming environment that has functions that perform common math, visualization and analysis operations. You primarily write programs in a textual fashion (.m files) or use Simulink to generate flow graphs (model-based development). Ansys on the other hand is primary a simulation environment where quite a lot can be done simply with the GUI (3D models, physics domains, configuration, display settings), and you can add equations at various points in the simulation engine in order to modify the simulation flow.
Whatever I understand is cursory and only serves as an overview. Can anyone give me a suitable real-world comparison between Matlab and Ansys (or any other simulation product such as COMSOL) that would allow us to understand when to use which, and the weaknesses of each system.
I haven't used Ansys, but Ansys is often compared with Comsol, and I've used Comsol and Matlab for years.
Matlab:
Programming language and environment that runs it. Which means it can do anything (that any other programming language can do). What are its highlights, compared to other languages?
Hundreds of built-in functions to work with Matrices. For example, in one project I needed to do simple matrix algebra (add, multiply, scale matrices), and also needed singular value decomposition. SVD is not something you could write in 50 lines of code, so I needed a ready-made library. At the time I used a library for Java, and wrote my own code for representing matrices and doing matrix algebra on them. That's a few hundreds of lines of code. Had I used Matlab, it would have been about ten lines of code, because all of it is there. I would have needed only to type help svd to find out how to use it. However, if you don't need any of that, stay away from Matlab at all costs! There are much better languages that are free.
Great to use as a calculator that is always open on the desktop, and can do back-of-the-envelope style calculations.
Plotting graphs. Many academics recommend Matlab as the tool of choice for producing publication-quality graphics. These can be exported as PDF and imported into Inkscape for further editing. The best thing is that commands for plotting a graph could be put into a script file, and then parts of it can be changed later as needed, which can save a lot of work compared to manually drawing a graph (imagine you wanted to change the axes or symbols used to present the data points).
Personally, I also use it for curve-fitting. It has many toolboxes, one of which is a neat tool that allows me to find equations that model a set of data points.
Comsol:
Specialised tool for solving partial differential equations (PDEs) on complicated domains using the finite element method (FEM). This might sound obscure, but many real-world engineering needs reduce to this. Such things as:
Finding loads, stresses and strains in civil engineering structures with complicated real-world geometry (what happens when there is gusty wind blowing onto a building or bridge?)
How do currents flow in particular conductive objects?
Chemical reactions in various industrial reactors.
What is the power efficiency of a generator (magnet spinning in coil) design?
How to place aircon outlets in a nontrivially-shaped room to achieve both good temperature distribution and good efficiency?
Comsol, as any other FEM tool that can work with arbitrary equations, can do multiphysics, which means, for example, that one could solve for chemistry of a battery, as well as the temperature and pressure, and how that feeds back into the chemical reaction (speeds up or slows down). Compared with a tool where you need to provide the equations, in Comsol, most of the things that would be needed to solve most problems are already there, and just need to be selected and applied to the geometry, which is also built inside Comsol. Also, equations of arbitrary description can be introduced.
The physical descriptions of how these physical substances behave are called PDEs.
Once Comsol has finished solving a problem, the data could be exported for post-processing into Matlab, which has much more versatile tools for manipulating data and making various plots.
As far as I know, stateflow and simulink are often used at the same time, and are both environments developed by MathWorks, who make Matlab. May I know what's the difference between them?
Simulink is largely a controls oriented solution. It graphically depicts math like products, sums, integrals, etc. However, it's conditional logic facility is lacking. Any kind of if construct quickly becomes terse and unmanageable in my opinion. I've seen many models, and there is a clear line here where Simulink should end and Stateflow should start.
Stateflow deals extremely well with logic and, of course, state machines. Now with the addition of the Simulink Function blocks within Stateflow, we have a powerful combination to allow the state machine in Stateflow direct the rest of the program.
As far as functionality goes, they are both functionally complete, meaning anything you can code in C, you can code in Simulink or Stateflow. However, I would not recommend coding a PID loop in Stateflow, but it's possible. You could also easily create a state machine in Simulink, but I'd advise against it.
As far as code generation; in the beginning of the meld, the Stateflow and Simulink had separate code generators that were sewed together with more Simulink generated C code at code generation time. Then came CGIR (Code Generation Intermediary Representation), which unifies the code generator between Stateflow and Simulink. It came around 2007, and has continued to deliver substantial increases in performance. Generated code has increased in on target performance to a point where companies can use the code in their embedded systems and actually get a performance benefit rather than take a small hit. Also, the time to generate the code has also decreased substantially. CGIR is a replacement for the Target Language Compiler, however, never fear, TLC API will still be available, perhaps forever for those who have developed massive libraries of proprietary code generation libraries.
Hope this helps, let me know if something sounds fishy or if I need to clarify.
Quoting a Stateflow Webinar:
Simulink is used to respond to continuous changes in dynamic changes.
Stateflow is used to respond to instantaneous changes in dynamic changes.
Real-world systems have to respond to both continuous and instantaneous changes.
Use both Simulink and Stateflow so that you can use the right tool for the right job. Examples: Suspensions dynamics of a car are modelled with Simulink whereas, the gear transmission is modelled with Stateflow.
Learn more about Stateflow in general at:
http://www.mathworks.com/products/stateflow/examples.html
Stateflow has been updated for making it very easy to create state machines and flow charts in R2012b.
The major updates include a new graphical editor, state transition tables, MATLAB as the action language and an integrated debugger. Find short videos for these features and how they can be used at:
http://www.mathworks.com/products/stateflow/whatsnew.html
best,
Siddharth
I'm also currently involved with both simulink and stateflow. Till now I did everything in Simulink but once you have to implement logic( if, case) your model becomes visually difficult to be analysed after coding. But I think theoretically you can do everything also just in Simulink (correct me if I'm wrong).
The answer of macduff explains very good the differences.
Regards,
GR
I have worked on both on simulink and state flow environment both are Matlab tool. When u want to design a reactive system or event driven system at that time we should prefer stateflow instead of simulink.
because we can control an event in stateflow in better way compare to simulink .though debugging is easy in simulink but readability and code generation are easy in stateflow.
Complex state machine: Stateflow
Complex mathematical process: Simulink blocks
Others: both of Stateflow and Simulink are fine
Another major benefit of Stateflow is the integration and usage of external C code. This can be done by selecting the Action Language C within the Chart settings.
As commonly known the user can include external Code within the "Simulation Target" options. While a normal Simulink model need small workarounds to access the external C code (ceval(), Simulink.Parameter for globals etc.), Stateflow can directly access functions, defines, etc.
Problems like array of buses/structs can also be avoided by coding these arrays in external code.
For a lot of use cases I love the flexibility to directly interact with C code within Stateflow.