Last active
December 30, 2015 14:59
-
-
Save ledovsky/7845693 to your computer and use it in GitHub Desktop.
Function to plot real time signals (e.g. from COM-port) using matlab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% plotnow - function to plot real time signals | |
% (e.g. from COM-port) using matlab | |
% | |
% arr - array of data. | |
% fs - sampling frequency | |
% t_width - time range to be shown | |
% ymin and ymax - range in y-axe | |
% | |
% Example usage: | |
% | |
% com_port = serial('COM1', 'BaudRate', 9600) | |
% fopen(com_port) | |
% i=0 | |
% max = 255 | |
% a = [] | |
% while(com_port.BytesAvailable > 0) | |
% data = fread(com_port, 10); | |
% new = (5 .* data / max).' | |
% a = [a new] | |
% plotnow(a, 1, 15, 0, 5); | |
% i = i + 1; | |
% pause(0.25); | |
% end | |
% | |
% fclose(com_port) | |
% | |
function plotnow(arr, fs, t_width, ymin, ymax) | |
dt = 1 / fs; | |
len = length(arr); | |
n_width = round(t_width * fs); | |
if n_width >= len | |
t_disp = 0:dt:(len-1)*dt; | |
arr_disp = arr; | |
xmin = 0; | |
xmax = dt*n_width; | |
else | |
n_min = len - n_width; | |
t_disp = n_min*dt:dt:len*dt; | |
arr_disp = arr(n_min:len); | |
xmin = n_min*dt; | |
xmax = len*dt; | |
end | |
plot(t_disp, arr_disp); | |
axis([xmin xmax ymin ymax]); | |
drawnow; | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment