1
+ #define INTERPOLATE
2
+
3
+ using System ;
4
+ using System . Collections . Generic ;
5
+ using System . Diagnostics ;
6
+ using System . IO ;
7
+ using System . Linq ;
8
+ using static System . FormattableString ;
9
+
10
+ namespace gfoidl . DataCompression . Demos . SwingingDoor . Stats
11
+ {
12
+ static class Program
13
+ {
14
+ static void Main ( )
15
+ {
16
+ Directory . CreateDirectory ( Path . Combine ( Directory . GetCurrentDirectory ( ) , "data" ) ) ;
17
+ Environment . CurrentDirectory = Path . Combine ( Directory . GetCurrentDirectory ( ) , "data" ) ;
18
+
19
+ var data = CreateData ( ) . ToList ( ) ;
20
+ var compressed = data . SwingingDoorCompression ( 0.1 ) . ToList ( ) ;
21
+ var reconstructed = Reconstruct ( data , compressed ) . ToList ( ) ;
22
+
23
+ new DataPointSerializer ( ) . Write ( "compressed.csv" , compressed , header : ( "x" , "y" ) ) ;
24
+
25
+ Console . WriteLine ( $ "{ "Data items:" , - 18 } { data . Count , 4 } ") ;
26
+ Console . WriteLine ( $ "{ "Compressed items:" , - 18 } { compressed . Count , 4 } ") ;
27
+
28
+ using ( StreamWriter sw = File . CreateText ( "result.csv" ) )
29
+ {
30
+ sw . WriteLine ( "# x\t raw\t compressed\t error" ) ;
31
+
32
+ for ( int i = 0 ; i < data . Count ; ++ i )
33
+ sw . WriteLine ( Invariant ( $ "{ data [ i ] . X } \t { data [ i ] . Y } \t { reconstructed [ i ] . Y } \t { data [ i ] . Y - reconstructed [ i ] . Y } ") ) ;
34
+ }
35
+
36
+ GnuPlot ( ) ;
37
+ ShowChart ( ) ;
38
+ }
39
+ //---------------------------------------------------------------------
40
+ private static IEnumerable < DataPoint > CreateData ( )
41
+ {
42
+ var rnd = new Random ( ) ;
43
+
44
+ for ( int i = 0 ; i < 1_000 ; ++ i )
45
+ {
46
+ double x = i * 1e-2 ;
47
+ double y = 10 * Math . Log ( ( 10 - x ) + 2.5 ) + ( rnd . NextDouble ( ) * 0.2 - 0.1 ) + 5 * Math . Cos ( ( 10 - x ) / 10 * Math . PI ) ;
48
+
49
+ yield return ( x , y ) ;
50
+ }
51
+ }
52
+ //---------------------------------------------------------------------
53
+ private static IEnumerable < DataPoint > Reconstruct ( IEnumerable < DataPoint > raw , IEnumerable < DataPoint > compressed )
54
+ {
55
+ var compressedEnum = compressed . GetEnumerator ( ) ;
56
+ var rawEnum = raw . GetEnumerator ( ) ;
57
+
58
+ #if INTERPOLATE
59
+ double x = default ;
60
+ #endif
61
+ double y = default ;
62
+
63
+ while ( compressedEnum . MoveNext ( ) && rawEnum . MoveNext ( ) )
64
+ {
65
+ yield return compressedEnum . Current ;
66
+ x = compressedEnum . Current . X ;
67
+ y = compressedEnum . Current . Y ;
68
+
69
+ if ( ! compressedEnum . MoveNext ( ) ) yield break ;
70
+
71
+ #if INTERPOLATE
72
+ double k = ( compressedEnum . Current . Y - y ) / ( compressedEnum . Current . X - x ) ;
73
+ #endif
74
+
75
+ while ( rawEnum . MoveNext ( ) && rawEnum . Current . X < compressedEnum . Current . X )
76
+ #if INTERPOLATE
77
+ yield return ( rawEnum . Current . X , y + k * ( rawEnum . Current . X - x ) ) ;
78
+ #else
79
+ yield return ( rawEnum . Current . X , y ) ;
80
+ #endif
81
+ yield return compressedEnum . Current ;
82
+ }
83
+ }
84
+ //---------------------------------------------------------------------
85
+ private static void GnuPlot ( )
86
+ {
87
+ var gnuPlot = new Process ( ) ;
88
+ //gnuPlot.StartInfo.WorkingDirectory = Path.Combine(Directory.GetCurrentDirectory(), "data");
89
+ gnuPlot . StartInfo . FileName = "gnuplot" ;
90
+ gnuPlot . StartInfo . Arguments = "error.plt" ;
91
+ gnuPlot . Start ( ) ;
92
+ gnuPlot . WaitForExit ( ) ;
93
+ }
94
+ //---------------------------------------------------------------------
95
+ private static void ShowChart ( )
96
+ {
97
+ var png = new Process ( ) ;
98
+ png . StartInfo . UseShellExecute = true ; // defaults to false in .net Core
99
+ png . StartInfo . FileName = "error.png" ;
100
+ png . Start ( ) ;
101
+ }
102
+ }
103
+ }
0 commit comments