-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatlab_crocproc.m
63 lines (57 loc) · 1.74 KB
/
matlab_crocproc.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
clear all
clc
close all
%Delete previous connections
delete(instrfind({'Port'},{'/dev/tty.usbserial-A900abKE'}));
%Create a serial connection
s = serial('/dev/tty.usbserial-A900abKE','BaudRate',115200,'Terminator','CR/LF');
warning('off','MATLAB:serial:fscanf:unsuccessfulRead');
%Open Port
fopen(s);
%Initialize the variables
Nvalues=2000; %Number of values ??we want to read
m1=zeros(1,Nvalues);
windowSize1 = 30;
i=1;
k=0;
tic;
starttime = tic;
while k<Nvalues
%Read the serial port
a = fscanf(s,'%f.%f')';
if a(1) <= 110000
m1(i)=a(1);
else
m1(i) = m1(i-1);%removes impossible serial reads
end
figure(1);
subplot(4,1,1); %plot the raw input
plot(m1);
xlabel('Time [# Datapoints]');ylabel('Pressure [Pa]');
%axis([0 Nvalues 94000 105000])
axis([0 Nvalues min(m1)-200 max(m1)+200]);
if i > windowSize1 %plot the averaged (low passed values)
subplot(4,1,2);
f1 = filter(ones(1,windowSize1)/windowSize1,1,m1);
plot(f1);
xlabel('Time - Running average windowsize 30');ylabel('[Pa]');
%axis([0 Nvalues 94000 105000])
axis([0 Nvalues min(m1)-200 max(m1)+200]);
if i > ((windowSize1*2) +1)
subplot(4,1,3);
f2(i) = filter(ones(1,windowSize1)/windowSize1,1,(f1(i)-f1(i-1)));
plot(f2);
xlabel('Time - Averaged derivative of above');ylabel('[Pa]');
axis([0 Nvalues -5 5]);
end
end
%Increment the counter
i=i+1;
k=k+1;
end
fclose(s);
delete(s);
elapsed_time = toc(starttime);
fprintf('We read %d datapoints in %f seconds\n',Nvalues, elapsed_time);
fprintf('The update rate is thus %f Hz\n',Nvalues/elapsed_time);
%axis([0 Nvalues min(m1)-200 max(m1)+200]);