Data Acquisition in Simulink is Slow and Incorrect

13 views (last 30 days)
I have a very simple model to test my data acquisition setup. The model includes a DSP Sine Wave block going to an Analog Output block, and an Analog Input block going to a Time Scope (see below screenshot).
I am using an NI cDAQ-9174 chassis with an NI-9201 input and NI-9263 output. I have connected the input to the output. My goal with this simple model is to send a sine wave to the DAQ board and read it back in to the Time Scope. However, when I run the simulation, a 1-second simulation takes more than a minute to run. Furthermore, the data I can see in the Time Scope does not seem to resemble the Sine Wave output data at all.
I have tested the same setup with a MATLAB script, creating an 'output' variable with the sine wave data, queuing the data to send to the DAQ, and reading it back in. The results are pretty quick and accurate in MATLAB.
Why does this setup not work in Simulink?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 29 Aug 2019
Edited: MathWorks Support Team on 29 Aug 2019
The reason the model is slow when you are using the Analog Output and Analog Input blocks is because you are outputting one sample at a time with the Analog Output block (which was designed to output a chunk of data at a time). This is because Simulink is fundamentally sample-based. An equivalent analogy of the Simulink execution using sample MATLAB code would be if you wrote a for-loop where in each iteration you read one value from the 'output' variable, queue that scalar value, and call 'startForeground'. Because of the overhead of sending data to the DAQ board, there is a significant slowdown in Simulink.
To have something similar to what your script is doing in Simulink, you need to include a Buffer block to output a chunk of buffered data instead of single samples to the Analog Output block. This will allow you to speed up your model because you will not need to send data to the DAQ board as frequently.
There is also some delay and data incorrectness which comes from the fact that the Analog Input and Analog Output are in the same model. These blocks cannot execute at the same time when they are in the same model, so what is achieved is that the Analog Output will send some data, then the Analog Input will send some data, and they will continue to alternate. In this case you may notice that the data being read in is not actually the same data that is sent out. To solve this, you should put the input into a separate model, as shown below:
Output Model:
Input Model:
Now you will be able to see that the model simulates much faster, and the data matches what you expect. With a buffer size of 100, for instance, here is what the Time Scope looks like:
You can see in this image that the sinusoid exists. However, there seems to be noise between each period. In this example the frequency was set to 10Hz with a sample time of 0.001s. This is why each buffer of 100 samples is equivalent to one period. Each chunk of 100 samples that is sent to the DAQ board is guaranteed to be continuous, but because of the overhead of sending a chunk of data (as mentioned above), the various chunks are not continuous with each other. This is the reason for the noisy sections between each chunk.
To resolve this issue, you want to set the buffer size to be large enough that you do not have many chunks to send. This will eliminate much of the overhead. Below is a screenshot when the buffer size is set to 10000:

More Answers (0)

Categories

Find more on RF Blockset Models for Transceivers in Help Center and File Exchange

Tags

No tags entered yet.

Products


Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!