0
votes

I am trying to plot real-time data from a sensor that has a frequency of 25 Hz. I read data from the sensor using TCPIP protocol, parse it and then plot the data. However, the plotting part is not very fast and starts to lag after some time. So for e.g. if I move the sensor, I see the response 5 seconds later. I used Serial Plotter in Arduino (which has much less specifications than my laptop) but it is able to plot real-time data without any delays / problems.

My code looks a bit like the following

IMUData = nan(1500,6);
InterfaceObject = tcpip('my_ip_address',50001);
InterfaceObject.BytesAvailableFcn ={@PlotSensorData};

And the PlotSensorData function looks like

    function PlotSensorData(~,~)

        RecievedData = fscanf(InterfaceObject,'%s');
        Identifier = RecievedData(6); % 6th byte is the sensor identifier

        DataStartIdx = 28; % For each sensor, data start position is common 

        if Identifier == 'I'

            DataEndIdx = DataEndPosition(RecievedData, 1); 

            SlicedData = RecievedData(DataStartIdx:DataEndIdx);
            ParsedData = textscan(SlicedData,'%f', 'Delimiter',',');

            % Append new data to IMUData matrices
            prevval = IMUData;
            val = [prevval(2:end,:); ParsedData{1}'];
            IMUData = val;

            set(PlotHandle{1},'ydata',val(:,3));                     
            set(TopAxes,'ylim',[-15 15]); 
            drawnow limitrate;
       end

  end

Also, instead using plot, I have already tried animatedLine. It seems faster initially as it plots very fast, but after sometime, it starts to lag as well and the lag is more than 10 Sec.

So my questions are

  • What can be done to speed up the real-time data in MATLAB.
  • Also, I have already tried plotting the data after a certain number of samples (say 10, 20) are received instead of plotting after every received sample, but the results are still lagging and the GUI hangs as well. Is there any other strategy that I can use? In Python I used Multi-threading, but can I use it here as well? Or is there a better approach to handle this data rate?
  • I understand that Arduino is only running one script but the computer has a lot of overhead, but how is Arduino able to plot the data so fast while MATLAB just hangs up?

Thanks

1
Have you profiled your code to check it is the plotting, and not the ever-growing array which you assign to and from, which is lagging? Do you need to always set the ylim property when it remains constant? - Wolfie
Hi, Thanks for coming back. I do initialize my arrays - Nischal
You do not in this case, this line is appending new data to IMUData via the intermediate val and prevval variables: val = [prevval(2:end,:); ParsedData{1}'];. Have you used the profiler, or at least tic/toc to check where the bottleneck is? - Wolfie
Hi Wolfie profiler shows the most time goes in fscanf and also in prevval = IMUData and IMUData = val. - Nischal
Now we're making progess - if that's the case then plotting isn't causing your lag! Please provide a minimal reproducible example to show what InterfaceObject looks like, perhaps you can do something more efficient that fscanf. Then as I say, taking advantage of pre-allocation would improve speed and avoid the two assignments you say take a long time. You initialise IMUData as an array of NaNs, but immediately extend it instead of indexing into it. Create a counter (which increments each iteration) and assign data into row x of IMUData instead of using prevval and val. - Wolfie

1 Answers

0
votes

Reading Data Asynchronously instead of continuously solved my problem.