live operations on acquired data stream

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=addAnalogInputChannel(s,'Dev1','ai0','Voltage');
ch4.TerminalConfig = 'Differential';
ch5=addAnalogInputChannel(s,'Dev1','ai1','Voltage');
ch5.TerminalConfig = 'Differential';
% add listeners
lh = s.addlistener('DataAvailable', @(src, event) logData(src, event, fid1));
% start recording
tic
s.IsContinuous = true;
s.startBackground;
pause(duration);
s.stop % end recording
toc
delete(lh);
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]' ;
subplot(2,1,1)
plot(data(1,:),data(2,:));
xlabel('Time (s)'), ylabel('Amplitude (V)'), legend('ai0')
ylim([-0.5 0.5]);
subplot(2,1,2)
plot(data(1,:),data(3,:));
xlabel('Time (s)'), ylabel('Amplitude (V)'), legend('ai1')
ylim([-0.5 0.5]);
% mean(data(1,:))
fwrite(fid,data,'double');
end

댓글 수: 5

dpb
dpb 2021년 7월 14일
편집: dpb 2021년 7월 15일
We'd need to know what was there and what errors/warnings were received; we don't have your hardware so no way to have any idea what's happening...
However, you have a lot of extraneous overhead in your logData function -- similarly as to passing the file handle in fid, create the axes objects for the plots first and then plot into the axes handles. But, even more than that, don't call the high-level plot() routine at all; depending on the effect you're after use animatedline or only update the X/YData arrays after having drawn the initial plot instead of drawing the whole graph again.
There are examples and suggested ways to improve animation in the documentation for graphics section you can study.
But, at some point with realtime acquisition you will simply run out of compute power -- I do not have and so haven't ever experimented with the parallel toolbox; ergo I don't know how effective of use one could make of it for this purpose but that might be one direction to explore eventually. But, first work on the overhead in the existing code.
LO
LO 2021년 7월 15일
I had 2 problems:
file identifiers are mismatched: fid in the function and fid1 in the callback.
also the variable used for the operation (mean) should be evt.Data not Data.
now it seems to work.
I would be still interested in reading about the resources you meant and what you meant with overheads (I am trying to improve this code).
Thank you for your feedback on this.
LO
LO 2021년 7월 15일
Tank you, this is actually useful. I wonder if there is a way to estimate the limit of "computing power" during an acquisition session. I will check the documentation. Thank you again.
Basically my aim is to perform online zerocrossing analysis to determine the instant frequency of a modulated signal. I will post the code here if I make it :)
dpb
dpb 2021년 7월 15일
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.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Simultaneous and Synchronized Operations에 대해 자세히 알아보기

제품

릴리스

R2018a

질문:

LO
2021년 7월 14일

댓글:

dpb
2021년 7월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by