live operations on acquired data stream

1 view (last 30 days)
LO on 14 Jul 2021
Commented: dpb on 15 Jul 2021
I am trying to plot and make online calculation on analog data acquired through a national instrument device. I am using the following code. With this code I am able to plot the acquired data live, on two different subplots while writing data on file.
I would like at the same time to perform operations while data acquisition goes on. something simple: are voltage level passing a certain threshold? what's the freq of the signal ? I added a line (now commented) in the logData function... in the example I am trying to calculate the mean of the acquired data chunk plotted in that moment. However if I run the script it gives me a lot of warnings (it calculates the mean though).
If I try to perform slightly more complex operations I get no outputs and a lot of warnings about errors... I guess it could simply be that acquisition is too fast to perform any of these operations... would it be possible to buffer the data somehow to allow this to happen? I am not even sure if that's the problem, perhaps it is a matter of adding more listeners/callbacks/functions ? I tried to test these ideas but I had no success... I would be glad to get any feedback ! Thanks
% create session
% daq.reset
d = daq.getDevices;
s = daq.createSession('ni');
s.Rate = 20000; % sample rate
Fs = s.Rate;
s.DurationInSeconds = 10;
% add channels
ch4.TerminalConfig = 'Differential';
ch5.TerminalConfig = 'Differential';
% add listeners
lh = s.addlistener('DataAvailable', @(src, event) logData(src, event, fid1));
% start recording
s.IsContinuous = true;
s.stop % end recording
function logData(src, evt, fid)
% Add the time stamp and the data values to data. Toda write data sequentially,
% transpose the matrix.
% Copyright 2011 The MathWorks, Inc.
data = [evt.TimeStamps, evt.Data]' ;
xlabel('Time (s)'), ylabel('Amplitude (V)'), legend('ai0')
ylim([-0.5 0.5]);
xlabel('Time (s)'), ylabel('Amplitude (V)'), legend('ai1')
ylim([-0.5 0.5]);
% mean(data(1,:))
dpb on 15 Jul 2021
Certainly is no way to estimate a priori; you'll find out when you can't get everything done you're trying to before the next packet arrives. With MATLAB and all in m-files unless you can keep things highly vectorized and minimize screen refreshes, you won't get a tremendously high throughput -- it is still, after all, interpreted/compiled, not fully compiled to object code.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by