-
Notifications
You must be signed in to change notification settings - Fork 0
/
genpts_sin.cpp
167 lines (133 loc) · 4.32 KB
/
genpts_sin.cpp
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
161
162
163
164
165
166
167
/****************************************************
Name : Jatin Saini
Roll No. : 19ME30068
AI_2021: Neural Network Assignment Part 2
****************************************************/
//Generating artificial training and test data
#include <stdio.h>
#include <math.h>
#include <ctime>
#include <graphics.h>
//a point in 2D with the label
typedef struct
{
int x;
int y;
int label;
}point;
// y = Amplitude*sin(frequency*x +Phase) + Offset
// So, there are 4 parameters: A,f,theta, offset
typedef struct
{
float Amp;
float freq;
float phase;
float offset;
}sin_params;
// taking parameters as input and generating points
void generate_points(int train_count, point train_pts[], int test_count, point test_pts[], sin_params sinf)
{
srand(time(0));
int maxx = getmaxx();
int maxy = getmaxy();
// we choose a random 'x' maintaining bounds so that both 'x' and 'y' lie inside the display screen
// generating and labeling training set
for(int i=0; i<train_count; i++)
{
train_pts[i].x = rand()%(maxx+1);
train_pts[i].y = rand()%(maxy+1);
if ( train_pts[i].y - (sinf.Amp*sin(sinf.freq*train_pts[i].x + sinf.phase) + sinf.offset) >= 0)
train_pts[i].label = 1; //sets the label as 1 to all upper half/on points
else
train_pts[i].label = 0; //sets the label as 0 to all lower half points
}
// generating and labeling test set
for(int i=0; i<test_count; i++)
{
test_pts[i].x = rand()%(maxx+1);
test_pts[i].y = rand()%(maxy+1);
if ( test_pts[i].y - (sinf.Amp*sin(sinf.freq*test_pts[i].x + sinf.phase) + sinf.offset) >= 0)
test_pts[i].label = 1; //sets the label as 1 to all upper half/on points
else
test_pts[i].label = 0; //sets the label as 0 to all lower half points
}
}
// stores the training set and the test set points in a .txt files
void store_points(int train_count, point train_pts[], int test_count, point test_pts[])
{
FILE *fptr;
fptr = fopen("training_set.txt", "w");
for(int i=0; i<train_count; i++)
fprintf(fptr, "%d %d %d\n", train_pts[i].x, train_pts[i].y, train_pts[i].label);
fclose(fptr);
fptr = fopen("test_set.txt", "w");
for(int i=0; i<test_count; i++)
fprintf(fptr, "%d %d %d\n", test_pts[i].x, test_pts[i].y, test_pts[i].label);
fclose(fptr);
}
// shows the training set and test set on screen via graphics
void show_points(int train_count, point train_pts[], int test_count, point test_pts[])
{
int radius = 2;
int color = WHITE;
//training set
for(int i=0; i<train_count; i++)
{
if(train_pts[i].label == 1)
color = GREEN;
else
color = RED;
setcolor(color);
circle(train_pts[i].x, train_pts[i].y, radius);
setfillstyle(SOLID_FILL, color);
floodfill(train_pts[i].x, train_pts[i].y, color);
}
//test set
color = YELLOW;
setcolor(color);
setfillstyle(SOLID_FILL, color);
for(int i=0; i<test_count; i++)
{
circle(test_pts[i].x, test_pts[i].y, radius);
floodfill(test_pts[i].x, test_pts[i].y, color);
}
return;
}
// draw the graph of a sine function using the passed parameters
void show_boundary(sin_params sinf)
{
int radius = 1;
int color = WHITE;
setcolor(color);
setfillstyle(SOLID_FILL, color);
int maxx = getmaxx();
point boundary[maxx+1];
for(int i=0; i<=maxx; i++)
{
boundary[i].x = i;
boundary[i].y = sinf.Amp*sin(sinf.freq*boundary[i].x + sinf.phase) + sinf.offset;
circle(boundary[i].x, boundary[i].y, radius);
floodfill(boundary[i].x, boundary[i].y, color);
}
return;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main()
{
int gd=DETECT, gm;
initgraph(&gd, &gm, (char *)"");
// initwindow(1530, 795);
int train_count = 1000; //training points
int test_count = 300; //test points
// y = Amplitude*sin(frequency*x +Phase) + Offset
sin_params sinf = {30, 0.05, 60, 200};
point train_pts[train_count];
point test_pts[test_count];
generate_points(train_count, train_pts, test_count, test_pts, sinf);
store_points(train_count, train_pts, test_count, test_pts);
show_boundary(sinf);
show_points(train_count, train_pts, test_count, test_pts);
getch();
closegraph();
return 0;
}