forked from t-inamura/iirlib
/
threshold.cpp
710 lines (599 loc) · 19.7 KB
/
threshold.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
// --*- C++ -*--
/*
* iirlib Threshold Module
*
* threshold.cpp
* Last modifyed on 2008-09-30 by Tetsunari Inamura
*
* Copyright (c) Tetsunari Inamura 1998--2015
* All Rights Reserved.
*/
/* Change Log
*
* 1998 Dec 10th
* created from another project
* 1998 Dec 16th
* Threshold_InitLever debugged
* Threshold_SetValue debugged
* 1999 Jan 6th
* target of threshold changed from int to double
* 1998 Feb 17th
* separeted from another project (database.c)
* 1999 Mar 8th
* LoadFromFile added
* 2nd dimension is added
* 1999 Mar 13th
* several debugging
* 1999 Jun 22nd
* if VALUE_UNOBSERVED is found, discrete process will skip it
* 1999 Jul 12th
* Threshold::AddNewState added
* Threshold::SaveToFile added
* 1999 Jul 22nd
* mean value added
* 1999 Oct 15th
* member variable 'mean' added
* 2008 Jan 29th
* change into c++ by Tareeq
* 2008 Feb 9th
* TL_SUCCESS, TL_FAIL discarded. Using TRUE and FALSE
* 2008 Feb 17th
* debug flag is embedded in class
* 2008 Sep 27th
* Modify InsertSegment , add PushBackSegment
*/
/* TODO
* It will be required to check the number of lines describing "Number of state" in Constructor
*/
#include "threshold.h"
#include "iirlib.h"
Threshold::Threshold ()
{
debug=0;
segment_seq.resize(0);
if (debug) tl_message ("threshold is created");
}
/*----------------------------------------------------------------------------*/
// Added : 1999 Mar 8th
// Memo : Set the MIN and MAX value
// Input : target_index : index for discrete segment
// Input : max_v
// Input : min_v
// Input : state_value : discrete value of the target segment (normally, it is equal to the target_index)
/*----------------------------------------------------------------------------*/
int Threshold::SetValue (int target_index, double max_v, double min_v, int discrete_state_value)
{
if (target_index<0 || target_index > number_of_state)
{
tl_warning ("No such state No.%d", target_index);
return FALSE;
}
segment_seq[target_index].SetMinMaxValue (0, min_v, max_v);
segment_seq[target_index].DiscreteValue (discrete_state_value);
return TRUE;
}
int Threshold::SetDebug (int value)
{
debug = value;
return TRUE;
}
/*----------------------------------------------------------------------------*/
// Added : 1999 Mar 8th
// Debugged : 1999 Mar 13th : change the order of 2D array like a max[][] (inamura misunderstood the order)
// Modified : 1999 Jul 22nd : Name registor function
// Input : *filename : Threshold type file "*.thd" (FullPath is required)
/*----------------------------------------------------------------------------*/
int Threshold::LoadFromFile (char *filename)
{
FILE *fp;
char line[MAX_STRING], *charp;
int i, j, tmp, state_no, min, max, mean, ret;
if (debug) tl_message ("File : %s", filename);
// if the suffix is not ".thd", it becomes to be error
if (!tl_strmember (filename, ".thd")) {
tl_warning ("This file {%s} doesn't include .thd suffix!", filename);
return FALSE;
}
// Cehck the existance of the file
if (( fp = fopen( filename, "r"))==NULL) {
tl_warning ("No such file {%s}", filename);
// If the previous file is not 'free-ed', the return might be NULL
return FALSE;
}
label = filename;
fgets (line, MAX_STRING, fp);
if (!tl_strmember( line, "# Number of State")) {
tl_message ("syntax error in %s, Description Error [%s]", filename, line);
return FALSE;
}
fgets (line, MAX_STRING, fp);
sscanf( line, "%d", &tmp );
if (tmp <= 0) {
tl_warning ("Syntax error: number of State = %d", tmp);
return FALSE;
}
number_of_state = tmp;
segment_seq.resize(number_of_state);
// Reading the number of dimension of raw data
fgets (line, MAX_STRING, fp);
if (!tl_strmember( line, "Parameter Dimension")) {
tl_message ("Description Error %s", line );
return FALSE;
}
fgets (line, MAX_STRING, fp);
sscanf (line, "%d", &tmp);
if (tmp <= 0) {
tl_warning ("Syntax Error: Dimension = %d", tmp);
return FALSE;
}
dimension = tmp;
// reading each line after skipping one line
fgets (line, MAX_STRING, fp);
for (i=0; i<number_of_state; i++)
{
// loop for number of states
fgets (line, MAX_STRING, fp);
charp = line;
// Reading index of the target segment
ret = sscanf_int_with_seeking (&charp, &state_no);
if (state_no < 0)
{
tl_warning ("State No. is wrong %d", state_no);
return FALSE;
}
// Reading value for the segment
ret = sscanf_int_with_seeking( &charp, &tmp);
if (tmp < 0)
{
tl_warning ("Discrete Value is wrong %d", tmp);
return FALSE;
}
segment_seq[state_no].DiscreteValue (tmp);
// Reading Min and Max value for j-th dimension of the segment
for (j=0; j<dimension; j++)
{
ret = sscanf_int_with_seeking (&charp, &min);
if (debug) tl_message("read min value = %d", min);
ret = sscanf_int_with_seeking (&charp, &max);
if (debug) tl_message("read max value = %d", max);
if (min > max)
{
tl_warning ("min greater than max : min = %d, max = %d!!, in file %s", min, max, filename);
tl_warning ("State No.%d State Value=%d", state_no, segment_seq[state_no].DiscreteValue() );
return -1;
}
if (debug) tl_message ("State No.%d , dim = %d : max = %d , min = %d", state_no, j, max, min );
segment_seq[state_no].SetMinMaxValue (j, min, max); // for j-th dimension
//max[j][state_no] = max;
//min[j][state_no] = min;
// If mean value is not described, (min+max)/2 is automatically set (added on 1999 Oct 15th)
ret = sscanf_int_with_seeking (&charp, &mean);
if (debug) tl_message ("sscanf_int_with_seeking done");
if (ret==FALSE)
{
if (debug) tl_message ("mean not found (state_no = %d, j = %d)", state_no, j);
segment_seq[state_no].SetMeanValue (j, (max + min)/2);
if (debug) tl_message("there is no mean value, so set %d", (int)(max + min)/2);
}
else
{
if (debug) tl_message ("mean found");
segment_seq[state_no].SetMeanValue (j, mean);
if (debug) tl_message("read and set mean value = %d", mean);
}
}
}
fclose (fp);
if (debug) tl_message ("finished successfully");
return 1;
}
/*----------------------------------------------------------------------------*/
// Added : 1999 Mar 8th
/*----------------------------------------------------------------------------*/
Threshold::~Threshold ()
{
if (debug) Verify();
#if 0
for (vector<SegmentState>::iterator p=segment_seq.begin(); p!=segment_seq.end(); p++) {
delete *p;
}
#endif
}
/*----------------------------------------------------------------------------*/
// Added : 1999 Jul 12th
// Memo : reference of member variable number_of_state
/*----------------------------------------------------------------------------*/
int Threshold::NumOfState (void)
{
return number_of_state;
}
/*----------------------------------------------------------------------------*/
// Added : 1999 Jul 12th
// return : (int) : dimension
/*----------------------------------------------------------------------------*/
int Threshold::Dimension (void)
{
return dimension;
}
/*----------------------------------------------------------------------------*/
// Created : 2008-09-30 by inamura
// input : (int) : dimension
/*----------------------------------------------------------------------------*/
int Threshold::Dimension (int dim)
{
dimension = dim;
return dimension;
}
/*----------------------------------------------------------------------------*/
// Created on 2008-01-23
/*----------------------------------------------------------------------------*/
int Threshold::MaxDiscreteValue(void)
{
int value = 0;
for (int i=0; i<number_of_state; i++) {
if (segment_seq[i].DiscreteValue() > value)
value = segment_seq[i].DiscreteValue();
}
return value;
}
// Created on 2008-01-23
int Threshold::SortDiscreteValue(void)
{
for (int i=0; i<number_of_state; i++) {
segment_seq[i].DiscreteValue(i);
}
return TRUE;
}
/*----------------------------------------------------------------------------*/
// Added : 1999 Jul 12th
// Memo : Add a new SegmentState
// Input : *max : max value of the new segment
// Input : *min : min value of the new segment
/*----------------------------------------------------------------------------*/
int Threshold::AddNewState (double min_value, double max_value)
{
SegmentState segment (min_value, max_value);
if (debug) tl_message ("Start!");
// The new value of state is one more larger than the current max value
segment.DiscreteValue (MaxDiscreteValue()+1);
number_of_state++;
segment_seq.push_back (segment);
if (debug) tl_message ("End!");
return 1;
}
#if 0
/*----------------------------------------------------------------------------*/
// Added : 1999 Oct 17th
/*----------------------------------------------------------------------------*/
int Threshold::AddDiscreteState (int value)
{
number_of_state++;
if (debug) tl_message ("add value %d", value);
return 1;
}
#endif
/*----------------------------------------------------------------------------*/
// Added : 1999 Jul 12th
/*----------------------------------------------------------------------------*/
int Threshold::SaveToFile (char *filename)
{
int i, j;
FILE *fp;
if (debug) tl_message ("Start");
if ((fp = fopen(filename, "w"))==NULL)
{
tl_warning ("Cannot open the file : %s", filename);
return FALSE;
}
if (debug) tl_message ("Filename : %s", filename);
fprintf( fp, "# Number of State\n%d\n", number_of_state );
fprintf( fp, "# Parameter Dimension\n%d\n", dimension );
if (dimension==2 )
fprintf( fp, "# State No.&Value min max mean\tmin max mean\n");
else
fprintf( fp, "# State No.&Value min max mean\n");
for (i=0; i<number_of_state; i++)
{
fprintf (fp, "%d\t%d\t", i, segment_seq[i].DiscreteValue());
for (j=0; j<dimension; j++)
{
fprintf (fp, "%g %g %g", segment_seq[i].MinValue(j), segment_seq[i].MaxValue(j), segment_seq[i].Mean(j) );
if (j!=dimension-1)
fprintf (fp, "\t");
}
fprintf (fp, "\n");
}
fclose(fp);
if (debug) tl_message ("End");
return TRUE;
}
/*----------------------------------------------------------------------------*/
// Added : 1999 Jul 22nd
// Mem o : query of typical data (mean value)
// Input : state : discrete value of target state
// Output: *data : typical data (pointer for the sequence of data)
/*----------------------------------------------------------------------------*/
vector<double> Threshold::TypicalData (int state_val, int *data)
{
for (int i=0; i<number_of_state; i++)
{
if (segment_seq[i].DiscreteValue() == state_val)
return segment_seq[i].MeanVector();
}
tl_warning ("No such state : No.%d in {%s}", state_val, label.c_str());
return segment_seq[0].MeanVector();
}
/*----------------------------------------------------------------------------*/
// Created on 2008-01-23 by inamura
// Modified on 2008-09-24 by inamura
/*----------------------------------------------------------------------------*/
int Threshold::DivideState (int index)
{
SegmentState *new_segment = NULL;
// min value of No.index : not changed
// max value of No.index : changed to (min + max) / 2 of old No.index
// min value of No.index+1 : changed to (min + max) / 2 of old No.index
// max value of No.index+1 : changed to max of old No.index
for (int dim=0; dim<dimension; dim++) {
double new_thre_value = (segment_seq[index].MinValue(dim) + segment_seq[index].MaxValue(dim)) / 2;
if (debug) tl_message ("new_thre_value = %g (%g + %g)/2", new_thre_value, segment_seq[index].MinValue(dim), segment_seq[index].MaxValue(dim));
new_segment = new SegmentState (new_thre_value, segment_seq[index].MaxValue(dim));
segment_seq[index].SetMaxValue(dim, new_thre_value);
InsertSegment (index, new_segment);
}
number_of_state++;
return TRUE;
}
// Created on 2008-01-23 by inamura
int Threshold::ConcatenateState (int low_index, int high_index)
{
// Low_index's min , (max)
// High_index's (min) , max
if (debug) tl_message("start");
for (int dim=0; dim<dimension; dim++) {
segment_seq[low_index].SetMaxValue(dim, segment_seq[high_index].MaxValue(dim));
vector<SegmentState>::iterator p = segment_seq.begin();
p += high_index;
segment_seq.erase (p);
}
number_of_state--;
SortDiscreteValue();
return TRUE;
}
/*----------------------------------------------------------------------------*/
// Added : 1999 Mar 13th
// Memo : Debug message
/*----------------------------------------------------------------------------*/
int Threshold::Verify(void)
{
int i, j;
cerr << "------ Print All ------" << endl;
cerr << "number_of_state =" << number_of_state << endl;
cerr << "dimension =" << dimension << endl;
for (i=0; i<dimension; i++) {
for (j=0; j<number_of_state; j++)
segment_seq[j].Verify();
}
cerr << endl;
tl_message ("------ Print All End ------ \n\n");
return TRUE;
}
/*----------------------------------------------------------------------------*/
// Modified : 1999 Jun 22nd
// Input : double value : Target data to be discreted
// Return : (int) : If the input data was unobserved data (-9999), it will be returned without midification
// Memo : This function is valid for only 1-Dimensional value
/*----------------------------------------------------------------------------*/
int Threshold::Query1D (double value)
{
int i;
if (debug) tl_message ("thre=%s , value = %g", label.c_str(), value );
for (i=0; i<number_of_state; i++)
if (segment_seq[i].MaxValue(0) >= value && segment_seq[i].MinValue(0) <= value) {
if (debug) tl_message ("result = %d", i);
return segment_seq[i].DiscreteValue();
}
if (value==VALUE_UNOBSERVED)
return VALUE_UNOBSERVED;
tl_warning ("No such category (value = %g) in {%s}", value, label.c_str());
return -1;
}
/*----------------------------------------------------------------------------*/
// Added : 1999 Jan 6th
// Memo : Discrete 2 dimensional data
/*----------------------------------------------------------------------------*/
int Threshold::Query2D (double value1, double value2)
{
int i;
for (i=0; i<number_of_state; i++)
{
SegmentState seg = segment_seq[i];
if (seg.MaxValue(0) >= value1 && seg.MinValue(0) <= value1 && seg.MaxValue(1) >= value2 && seg.MinValue(1) <=value2)
{
if (debug) tl_message ("(%g, %g) --> %d", value1, value2, i);
return seg.DiscreteValue();
}
}
if (value1==VALUE_UNOBSERVED)
return VALUE_UNOBSERVED;
tl_warning ("No such category (value = %g, %g) in {%s}", value1, value2, label.c_str());
return FALSE;
}
/*----------------------------------------------------------------------------*/
// Added : 1999 Mar 8th
// Modified : 2008 Feb 9th : Integrate of 1-D and 2-D Querying
// Input : double value : value to be discrete
// Return : (int) : Discrete value
/*----------------------------------------------------------------------------*/
int Threshold::QueryDiscreteValue (double continuous_value)
{
if (dimension!=1) {
tl_warning ("dimension is not 1");
return FALSE;
}
return Query1D (continuous_value);
}
/*----------------------------------------------------------------------------*/
// Added : 1999 Mar 8th
// Modified : 2008 Feb 9th : Integrate of 1-D and 2-D Querying
// Return : (int) : Discrete value
/*----------------------------------------------------------------------------*/
int Threshold::QueryDiscreteValue (vector<double> vec)
{
if (vec.size() == 1 || vec.size() > 2) {
tl_warning ("dimension is not 2");
return FALSE;
}
return Query2D (vec[0], vec[1]);
}
/*----------------------------------------------------------------------------*/
// Memo : Changing the joystick lever input into discrete value
// Input : x : x coordinate of the joystick lever (Left:Right <--> -100:+100)
// Input : y : y coordinate of the joystick lever (Left:Right <--> -100:+100)
/*----------------------------------------------------------------------------*/
int Threshold::LeverXYToState (int x, int y)
{
double rad;
int i, deg, scale;
rad = atan2( (double)y, (double)x );
if (rad < 0)
deg = (int) ( (rad + 2*M_PI) * 180 / M_PI );
else
deg = (int) ( rad * 180 / M_PI );
scale = (int)sqrt( (double)x*x + (double)y*y );
for (i=0; i<number_of_state; i++)
{
SegmentState seg = segment_seq[i];
if (seg.MaxValue(0) >= deg && seg.MinValue(0) <= deg && seg.MaxValue(1) >= scale && seg.MaxValue(1) <= scale)
return seg.DiscreteValue();
}
tl_message ("!Error! (x,y)=(%d,%d) :: (deg,scl)=(%d,%d)", x, y, deg, scale);
return FALSE;
}
int Threshold::InsertSegment (int index, SegmentState *segment)
{
int debug=0;
if (debug) tl_message ("start");
if (debug) Verify();
vector<SegmentState>::iterator p;
number_of_state ++;
p = segment_seq.begin();
p += index+1;
segment_seq.insert (p, *segment);
if (debug) tl_message ("end");
if (debug) Verify();
return TRUE;
}
/*----------------------------------------------------------------------------*/
// Created on 2008-09-24 by inamura
/*----------------------------------------------------------------------------*/
int Threshold::PushBackSegment (SegmentState *segment)
{
number_of_state ++;
segment_seq.push_back (*segment);
return TRUE;
}
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
// SegmentState //
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
SegmentState::SegmentState ()
{
debug=0;
tl_message ("segment state is created");
min.resize(1);
max.resize(1);
mean.resize(1);
}
SegmentState::SegmentState (double min_value, double max_value)
{
min.resize(1);
max.resize(1);
mean.resize(1);
min[0] = min_value;
max[0] = max_value;
mean[0] = (double)(min_value + max_value)/2;
}
SegmentState::~SegmentState ()
{
//tl_message ("segment state is deleted");
}
double SegmentState::MinValue (int dim)
{
return min[dim];
}
int SegmentState::SetMinValue (int dim, double min_value)
{
max[dim] = min_value;
return TRUE;
}
double SegmentState::MaxValue (int dim)
{
return max[dim];
}
int SegmentState::SetMaxValue (int dim, double max_value)
{
max[dim] = max_value;
return TRUE;
}
double SegmentState::Mean (int dim)
{
return mean[dim];
}
vector<double> SegmentState::MeanVector (void)
{
return mean;
}
int SegmentState::Verify (void)
{
cerr << "Max = " << max[0] << "\tMin = " << min[0] << "\tDiscreteValue = " << discrete_value << endl;
return 1;
}
int SegmentState::SetMinMaxValue (int dim, double min_value, double max_value)
{
if (dim<0 || dim>=(int)(max.size()))
{
tl_warning ("input dimension <%d> is wrong parameter", dim);
return FALSE;
}
else if (1)
{
tl_message ("set min=%g, max=%g as %d-th dimension", min_value, max_value, dim);
}
min[dim] = min_value;
max[dim] = max_value;
tl_message ("end of function");
return 1;
}
int SegmentState::SetMeanValue (int dim, double mean_value)
{
mean[dim] = mean_value;
return 1;
}
int SegmentState::DiscreteValue (int value)
{
discrete_value = value;
return 1;
}
int SegmentState::DiscreteValue (void)
{
return discrete_value;
}
/*----------------------------------------------------------------------------*/
// Memo : Set the MAX and MIN values for 2-D threshold
// Input : state_value : Discrete value for the segment
// Input : min_p1 : Min value for 1st dimension
// Input : max_p1 : Max value for 1st dimension
// Input : min_p2 : Min value for 2nd dimension
// Input : max_p2 : Max value for 2nd dimension
/*----------------------------------------------------------------------------*/
int SegmentState::SetValue2D (int state_val, double min_p1, double max_p1, double min_p2, double max_p2)
{
max[0] = max_p1;
min[0] = min_p1;
max[1] = max_p2;
min[1] = min_p2;
discrete_value = state_val;
return TRUE;
}