How can I optimize serial data plot in real time from linear CCD?

14 Ansichten (letzte 30 Tage)
Lukas Peterek
Lukas Peterek am 16 Mär. 2024
Kommentiert: Raghava S N am 1 Apr. 2025
Hello, I have a problem with plotting serial data from linear CCD sensor.
CCD (TCD1304DG) is conected and operated by STM32F411 which then sends vaules of each pixel by USB to the computer. It sends whole line of 3694 pixels at a time (the line has a start symbol 'A'). I want to plot this whole line of values whenever i get a new one. I put together this code which is good at reading the data and plotting it, but after after a while the garph starts to stutter and reaction time is slower. The STM part is probably not a problem, becuase when I stop and start the code it is working alright but only for a while.
Is there any way how can i optmize this? I would like to read the data for several hours.
%% Reading and plotting CCD
CCD = figure;
%Button for stopping the plot and serial communication
H = uicontrol('Style', 'PushButton', ...
'String', 'Stop', ...
'Callback', 'delete(gcbf)','Position',[5 5 60 30]);
pixels = (1:3694);
stm = serialport("COM3",9600);
while(ishandle(H))
data = read(stm,14780,"string");
%Preparing data for plotting
data_str = strsplit(data);
data_double = str2double(data_str);
data_double_clean = data_double(2:end);
data_double_clean(end) = [];
%Showing maximum value of the plotted line
data_peaky = data_double_clean(36:3694-13);
[pk,loc] = max(data_peaky);
T = uicontrol('Style','text','String',loc+36,'Position',[0 100 60 30]);
%Checking lenght and start symbol then plotting
if(length(data_double_clean)==length(pixels) && data_str(1) == 'A')
plot(pixels,data_double_clean);
end
%Graph settings
title('CCD display','Interpreter','latex');
xlim([36 3680]);
ylim([100 255]);
xlabel('Pixel','Interpreter','latex');
ylabel('Intenzita','Interpreter','latex');
set(gca,'TickLabelInterpreter','latex');
drawnow;
pause(0.01);
flush(stm);
end
clear stm;
  1 Kommentar
Raghava S N
Raghava S N am 1 Apr. 2025
Hi,
A couple of things can be done to optimize this functionality, mainly dealing with the method of update and the frequency of updates.
Firstly, a "timer" object can be used to update the plot when new serial data is received. For more information about the "timer" object, refer to this documentation link - https://www.mathworks.com/help/matlab/ref/timer.html.
When it comes to the code to update the plot with the new data, every time "plot" is called, a new graphics object is created. Which is a lot given that this is a frequent call that needs to run for hours. An alternative is to plot once and update its x and y data every time you have something to plot. The "set" function can be used for this task - https://www.mathworks.com/help/matlab/ref/set.html.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by