Displaying Value beside graph
조회 수: 10 (최근 30일)
이전 댓글 표시
I've typed up a code that displays the time and freqeuecny domain of a signal from an arduino in real time. I would like to display the live value of the freqeuecny domain (FFT) beside the graph/same window or in separate window. Any suggestions?
clear all
ar = serial('COM6','BaudRate',9600);
Fs = 50;
try
fopen(ar);
catch err
fclose(instrfind);
error('Make sure you select the correct COM Port where the Arduino is connected.');
end
Tmax = 20; % Total time for data collection (s)
Ts = 0.001; % Sampling time (s), it correspond to 1000Hz
i = 0;
data = 0;
t = 0;
x_label_names = {'Time (s)', 'Frequency (Hz)'};
subplot(2,1,1)
h = animatedline;
xlabel('Time (s)')
ylabel('Amplitude (mV)')
tic % Start time
while toc <= Tmax
i = i + 1;
% Fill the data matrix with the data read from arduino
data(i) = fscanf(ar,'%d');
% If matlab read the data faster than the sampling rate set in arduino, force sampling to be the same as the
% sampling time set in matpab code, If matlab is reading slower, nothing can be done.
t(i) = toc;
if i > 1
T = toc - t(i-1);
while T < Ts
T = toc - t(i-1);
end
end
t(i) = toc;
for k = 1:length(t(i))
addpoints(h,t(i),data(i));
[timeLogs,voltLogs] = getpoints(h);
nfft = length(voltLogs); %length of domnain signal
nfft2 = 2.^nextpow2(nfft); %lenght of signal in power of 2
ff = fft(voltLogs,nfft2);
fff = ff(1:nfft2/2);
xfft = Fs.*(0:nfft2/2-1)/nfft2
subplot(2,1,2)
plot(xfft,abs(fff));
xlabel('Frequency (Hz)')
ylabel('Amplitude (mV)')
drawnow;
end
end
댓글 수: 0
답변 (2개)
Koundinya
2018년 12월 14일
The value can be displayed in the figure title, legend or the axis label. You could format the variable to be displayed into a string using sprintf.
for k = 1:length(t(i))
addpoints(h,t(i),data(i));
[timeLogs,voltLogs] = getpoints(h);
nfft = length(voltLogs); %length of domnain signal
nfft2 = 2.^nextpow2(nfft); %lenght of signal in power of 2
ff = fft(voltLogs,nfft2);
fff = ff(1:nfft2/2);
xfft = Fs.*(0:nfft2/2-1)/nfft2
subplot(2,1,2)
plot(xfft,abs(fff));
% Value to be displayed
value_string=sprintf('Frequency(Hz) = %d',xfft);
% Display the updated value in x-axis label
xlabel(value_string);
% Or Display the updated value in the title
title(value_string);
%xlabel('Frequency (Hz)')
ylabel('Amplitude (mV)')
drawnow;
end
댓글 수: 0
sMall
2020년 1월 28일
I do not want to use title, legend or axis label. What would be the other ways?
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Simulation, Tuning, and Visualization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!