How can I make a scatter plot when the signal above a certain threshold in real time?

4 views (last 30 days)
Hi all,
I am quite fresh in Matlab but want to design a program to simulate flowing paricles, the concept would close to flow cytometry.
The scenario is, the particle will induce several sine waves once it pass through the detecting zone. Each sine waves could be connected as a wave envelope since the particel transerving in the detection zone will have different sensitivity. Please see the fig1.
According to the sine wave amplitude, if the maximum value above a certain threshold which set by me, I can plot a dot on the XY scatter plot spontanously.
My question is, how can I modify my code for monitoring (real-time) measurement when I am scanning the particles?
I attached my code but they are just a sample for showing example, I think they should be more complicated if I want to use in my experiment.
Thanks! And hope everybody stay healthy and safe!
clc
close all
clear all
%Generate a similar signal
t=linspace(0,0.0025,10000);
fc= 5000;
fm= 200;
fs= 40000;
Am= 1;
Ac= 0.01;
m= Am/Ac;
wc= 2*pi*fc*t;
wm= 2*pi*fm*t;
ec= Ac*sin(wc);
em= Am*sin(wm);
y=Ac*(1+m*sin(wm).*sin(wc));
plot(t,y);
%Find the maximum amplitude and count it to make a XYscatter graph
[maxYvalue, indexAtmaxY]= max(y);
XvalueAtMaxYvalue= t(indexAtmaxY(1));
x= XvalueAtMaxYvalue.*1000;
scatter(x, maxYvalue)
xlabel('Time(us)')
ylabel('Voltage(a.u.)')
  2 Comments
Chen Kevin
Chen Kevin on 10 Apr 2020
Hi Darova,
Thanks for the suggestions! I've edited more information, hope it can express my question clearly now.

Sign in to comment.

Accepted Answer

darova
darova on 10 Apr 2020
Edited: darova on 10 Apr 2020
Here is my vision of your problem
x = -20:0.5:20;
f = @(x) sin(x).*sqrt(20^2-x.^2);
y = f(x);
ylim([-30 30])
cla
plot(x,y)
hold on
h1 = plot(0,0);
h2 = scatter(0,0);
for i = 1:length(x)
set(h2,'xdata',x(i),'ydata',f(x(i))*(f(x(i))>0));
set(h1,'xdata',[0 0]+x(i),'ydata',[-30 30])
xlim([-20 20]+x(i)) % move limits
pause(0.1) % wait a sec
drawnow
end
hold off
  10 Comments
Chen Kevin
Chen Kevin on 13 Apr 2020
Perfect! It works, thanks!
But how do you save the file to animation? Like you responsed before with the comment.
darova
darova on 13 Apr 2020
Example
clc,clear
% GRAPHICAL DRIVER
opengl software
x = linspace(0,5,20);
y = x.^2;
% axis([0 max(x) 0 max(y)])
h = plot(0,0,'or');
line(x,y)
filename = 'gif_animation.gif';
f = getframe(gcf);
[im,map] = rgb2ind(f.cdata,256);
imwrite(im,map,filename,'DelayTime',0,'Loopcount',inf);
for i = 1:length(x)
set(h,'xdata',x(i),'ydata',y(i))
pause(0.1)
f = getframe(gcf);
[im,map] = rgb2ind(f.cdata,256);
imwrite(im,map,filename,'DelayTime',0.1,'WriteMode','Append');
end

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 10 Apr 2020
Sometime you need to put a drawnow after your call() to plot to get the plot to show up in real time, otherwise, if it's in an intensive loop, it will only update the plot once the loop has finished:
plot(............)
drawnow; % Force screen to repaint immediately.
  5 Comments
Image Analyst
Image Analyst on 11 Apr 2020
Yes, when pause hits, it gives time for the operating system to process other messages in the queue that have basically been held up while the OS is processing what it thinks are more important operations. So the MATLAB stuff like iterating on the loop and incrementing the loop counter (which it thinks are super important) are basically paused and it then gives it time to get around to handling the other messages that it considered lower priority, like updating the display. You might check out this Wikipedia article on message based computing: https://en.wikipedia.org/wiki/Message_queue

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!