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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
/*
-Course-
DAT300: Data-driven support for cyber-physical systems
-Project-
Intrusion Detection for Industrial Control Networks
-Group 8-
Hassan Ghalayini - hassang@student.chalmers.se
Malama Kasanda - malama@student.chalmers.se
Vaios Taxiarchis - vaios@student.chalmers.se
Modified by Robin Krahl <guskraro@student.gu.se>, Group 3:
- Write sensor readings and distance to text files
- Take the arguments N, L, r from argc
- Formatting
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "midbro.h"
int main(int argc, char **argv)
{
clock_t begin;
int l=0;
double t=30;
if (argc != 4) {
fprintf(stderr, "Usage: %s N L r\n", argv[0]);
fprintf(stderr, " Example values: N = 1000, L = 500, r = 18\n");
fprintf(stderr, "Wrong argument count. Aborting.\n");
return 1;
}
int N = atoi(argv[1]);
int L = atoi(argv[2]);
int r = atoi(argv[3]);
/* Arrays and variables */
int sL=86336;
double s[sL];
double U[L][r];
double X[L];
double product_Xt_P_X;
double product_Xt_X;
int i=0,j=0,c=0,d=0,k=0;
double sum=0.0,dist=0.0;
start_data_capture();
/* File Descriptor to read projection matrix from U.txt */
FILE *file;
char ch='a';
int flag=0;
file=fopen("U.txt", "r");
if (!file) {
fprintf(stderr, "Could not open U.txt. Aborting.\n");
return 1;
}
printf(" >Reading U[%dx%d] matrix from .txt file...",L,r);
/* Read all values to an array */
for(i=0;i<L;i++)
{
if(flag==1){break;}
for(j=0;j<r;j++)
{
/* Use %lf format specifier, to read doubles with scanf */
if(!fscanf(file,"%lf",&U[i][j]))
break;
/* Break the inner loop and set flag=true */
ch = getc(file);
if(ch == EOF)
{
flag=1;
break;
}
/* Break the inner loop when find \n */
else if(ch - '0'== -38)
break;
}
}
/* Close File Descriptor */
fclose(file);
printf("Completed!\n");
for(i=0;i<N;i++)
{
s[i] = (double)request_value();
}
int p=1;
X[0]=0;
for(i=N-L+1;i<N;i++)
{
X[p] = s[i];
sum = sum + X[p]*X[p];
p++;
}
product_Xt_X = sum;
sum = 0;
FILE *file_sensor = fopen("sensor.dat", "w");
FILE *file_distance = fopen("distance.dat", "w");
begin = clock();
while(1)
{
int value = request_value();
s[p] = (double) value;
fprintf(file_sensor, "%d\n", value);
/* Xt[1xL] * X[Lx1] */
product_Xt_X = product_Xt_X - X[0]*X[0] + s[p]*s[p];
/* Generate new test vector Xtest */
for(j=0;j<L-1;j++)
{
X[j] = X[j+1];
}
X[L-1] = s[p];
product_Xt_P_X = 0;
/* P_1 = Xt[1xL] * U[Lxr] */
for (d=0;d<r;d++)
{
for (k=0;k<L;k++)
{
sum = sum + X[k]*U[k][d];
}
product_Xt_P_X = product_Xt_P_X + sum*sum;
sum = 0;
}
dist = (product_Xt_X - product_Xt_P_X)/L;
p++;
printf("%lf\n",dist);
fprintf(file_distance, "%lf\n", dist);
fflush(file_sensor);
fflush(file_distance);
}
fclose(file_sensor);
fclose(file_distance);
printf(" [Program exits]\n");
/* Stop timer */
clock_t end = clock();
/* Print the time elapsed */
printf("Time elapsed: %f milliseconds\n", 1000*((double)(end - begin) / CLOCKS_PER_SEC));
return 0;
}
|