adagsl_335d13f0/examples/filter/src/filter.adb

  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
with System ;
with Ada.Text_IO;            use Ada.Text_IO;
with Ada.Text_IO.C_Streams;
with Ada.Long_Float_Text_IO; use Ada.Long_Float_Text_IO;
with Ada.Integer_Text_IO;    use Ada.Integer_Text_IO;
with Interfaces.C;           use Interfaces.C;
with Interfaces.C.Strings;   use Interfaces.C.Strings;
with Ada.Strings.Unbounded;  use Ada.Strings.Unbounded;
with Ada.Command_Line;       use Ada.Command_Line;
with Ada.Numerics.Elementary_Functions ; use Ada.Numerics.Elementary_Functions;

with GNAT.Source_Info; use GNAT.Source_Info;

with gsl ;
with gsl.math ;
with gsl.rng ;
with gsl.randist ;
with gsl.vector_double ;
with gsl.vector_int ;
with gsl.filter ;

procedure Filter is
   Verbose : boolean := true;
   N : size_t := 500 ;          -- Time Series length
   K : size_t := 51 ;           -- Kernel Size
   alpha : gsl.double_array := ( 0.5 , 3.0 , 10.0 );
   Status : Int ;

   procedure Test1 is
   -- 
   -- 24.5.1 Gaussian Example 1
   --
      rng : access gsl.rng.gsl_rng := gsl.rng.alloc(gsl.rng.default) ;
      x, y1, y2, y3, k1, k2, k3 : access gsl.vector_double.gsl_vector ;
      gws : access gsl.filter.gsl_filter_gaussian_workspace :=
               gsl.filter.gaussian_alloc (K) ;
      sum : double := 0.0 ;
      ui : double ;
      kernelfile, datafile : File_Type ;
   begin
      Put_Line(enclosing_entity);
      x := gsl.vector_double.alloc(N);
      y1 := gsl.vector_double.alloc(N);
      y2 := gsl.vector_double.alloc(N);
      y3 := gsl.vector_double.alloc(N);
      k1 := gsl.vector_double.alloc(K);
      k2 := gsl.vector_double.alloc(K);
      k3 := gsl.vector_double.alloc(K);

      for i in 0..N-1
      loop
         ui := gsl.randist.gaussian(rng,1.0);
         sum := sum + ui ;
         gsl.vector_double.set(x, i , sum );
      end loop ;

      Status := gsl.filter.gaussian_kernel(alpha(0) , 0 , 0 , k1 );
      Status := gsl.filter.gaussian_kernel(alpha(1) , 0 , 0 , k2 );
      Status := gsl.filter.gaussian_kernel(alpha(2) , 0 , 0 , k3 );

      Status := gsl.filter.gaussian(gsl.filter.GSL_FILTER_END_PADVALUE,alpha(0),0,x,y1,gws) ;
      Status := gsl.filter.gaussian(gsl.filter.GSL_FILTER_END_PADVALUE,alpha(1),0,x,y2,gws) ;
      Status := gsl.filter.gaussian(gsl.filter.GSL_FILTER_END_PADVALUE,alpha(2),0,x,y3,gws) ;

      Ada.Long_Float_Text_IO.Default_aft := 4 ;
      Ada.Long_Float_Text_IO.Default_exp := 0 ;

      Create(kernelfile,Out_File,enclosing_entity & ".kernel.csv");
      Set_Output(kernelfile);
      for i in 0..K-1
      loop
         Put(Integer(i)); Put(" ; ");
         Put(Long_Float(gsl.vector_double.get(k1,i))); Put(" ; ");
         Put(Long_Float(gsl.vector_double.get(k2,i))); Put(" ; ");
         Put(Long_Float(gsl.vector_double.get(k3,i))); Put(" ; ");
         New_Line;
      end loop ;
      Set_Output(Standard_Output);
      Close(kernelfile);

      Create(datafile,Out_File,enclosing_entity & ".data.csv");
      Set_Output(datafile);
      for i in 0..N-1
      loop
         Put(Integer(i)); Put(" ; ");
         Put(Long_Float(gsl.vector_double.get(x,i))); Put(" ; ");
         Put(Long_Float(gsl.vector_double.get(y1,i))); Put(" ; ");
         Put(Long_Float(gsl.vector_double.get(y2,i))); Put(" ; ");
         Put(Long_Float(gsl.vector_double.get(y3,i))); Put(" ; ");
         New_Line;
      end loop ;
      Set_Output(Standard_Output);
      Close(datafile);
   end Test1 ;

   procedure Test2 is
   --
   -- 24.5.2 Gaussian Example 2
   --
      myname : String := gnat.Source_Info.enclosing_entity ;
      logfile : File_Type ;

      x ,
      dxi ,
      y ,
      dy ,
      d2y : access gsl.vector_double.gsl_vector ;

      gws : access gsl.filter.gsl_filter_gaussian_workspace ;
      rng : access gsl.rng.gsl_rng := gsl.rng.alloc(gsl.rng.default);
      xi, ei : double ;
   begin
      if Verbose
      then
         Put_Line(myname);
      end if ;
      N := 1000 ;
      x := gsl.vector_double.alloc(N) ;
      y := gsl.vector_double.alloc(N) ;
      dy := gsl.vector_double.alloc(N) ;
      d2y := gsl.vector_double.alloc(N) ;
      dxi := gsl.vector_double.alloc(N) ;
      gws := gsl.filter.gaussian_alloc (K) ;

      for i in 1..N
      loop
         if i > N/2
         then
            xi := 0.5;
         else 
            xi := 0.0 ;
         end if;
         ei := gsl.randist.gaussian(rng,0.1);
         gsl.vector_double.set(x,size_t(i-1),xi+ei);
      end loop ;

   for i in 1..N
      loop
         if i = 1
         then
            gsl.vector_double.set(dxi,i-1,
                                  gsl.vector_double.get(x,i) - gsl.vector_double.get(x,i-1));
         elsif i = n
         then
            gsl.vector_double.set(dxi,i-1,gsl.vector_double.get(x,i-1) - gsl.vector_double.get(x,i-2));
         else
            gsl.vector_double.set(dxi,i-1,0.5*(gsl.vector_double.get(x,i) - gsl.vector_double.get(x,i-2)));
         end if;

      end loop ;

      gsl.vector_double.WriteCSV(x,myname & ".data.csv");

      Status := gsl.filter.gaussian(gsl.filter.GSL_FILTER_END_PADVALUE,alpha(2),0,x,y,gws);
      Status := gsl.filter.gaussian(gsl.filter.GSL_FILTER_END_PADVALUE,alpha(2),1,x,dy,gws);
      Status := gsl.filter.gaussian(gsl.filter.GSL_FILTER_END_PADVALUE,alpha(2),2,x,d2y,gws);

      gsl.vector_double.WriteCSV( x , myname & ".filtered.csv" ,
                                 y , dy , d2y , dxi );

      gsl.vector_double.free(x);
      gsl.vector_double.free(y);
      gsl.vector_double.free(dy);
      gsl.vector_double.free(d2y);
      gsl.vector_double.free(dxi);
      gsl.rng.free(rng);
      gsl.filter.gaussian_free(gws);
   end Test2 ;
   procedure Test3 is
   -- 24.5.3 Square Wave Signal Example
      myname : String := gnat.Source_Info.enclosing_entity ;
      logfile : File_Type ;

      gws : access gsl.filter.gsl_filter_gaussian_workspace ;
      rng : access gsl.rng.gsl_rng := gsl.rng.alloc(gsl.rng.default);

      t,
      x,
      y_median,
      y_rmedian : access gsl.vector_double.gsl_vector ;

      medianws : access gsl.filter.gsl_filter_median_workspace ;
      rmedianws : access gsl.filter.gsl_filter_rmedian_workspace ;

      freq : double := 5.0 ;
      ti : double ;
      tmp : double ;
      xi, ei : double ;
   begin
      if Verbose
      then
         Put_Line(myname);
      end if ;
      N := 1000 ;
      K := 7 ;
      gws := gsl.filter.gaussian_alloc(K);
      t := gsl.vector_double.alloc(N);
      x := gsl.vector_double.alloc(N);
      y_median := gsl.vector_double.alloc(N) ;
      y_rmedian := gsl.vector_double.alloc(N) ;


      medianws := gsl.filter.median_alloc(K) ;
      rmedianws := gsl.filter.rmedian_alloc(K) ;
   
      for i in 1..N
      loop
         ti := double(i) / double(N-1) ;
         tmp := double(Ada.Numerics.Elementary_Functions.Sin( 2.0 * Ada.Numerics.PI * float(freq) * float(ti)));
         if tmp >= 0.0
         then
            xi := 1.0 ;
         else
            xi := -1.0 ;
         end if ;
         ei := gsl.randist.gaussian(rng,0.1);
         gsl.vector_double.set(t,i-1,ti);
         gsl.vector_double.set(x,i-1,xi+ei);
      end loop ;
      Status := gsl.filter.median(gsl.filter.GSL_FILTER_END_PADVALUE,x,y_median,medianws);
      Status := gsl.filter.rmedian(gsl.filter.GSL_FILTER_END_PADVALUE,x,y_rmedian,rmedianws);
      gsl.vector_double.WriteCSV(t,myname & ".csv" ,x,y_median,y_rmedian) ;

      gsl.vector_double.free(t);
      gsl.vector_double.free(x);
      gsl.vector_double.free(y_median) ;
      gsl.vector_double.free(y_rmedian) ;

      gsl.filter.median_free(medianws);
      gsl.filter.rmedian_free(rmedianws);

   end Test3 ;

   procedure Test4 is
      myname : String := gnat.Source_Info.enclosing_entity ;
      logfile : File_Type ;
      t : double ;

      x,
      y,
      xmedian ,
      xsigma ,
      lupper,
      llower,
      watch : access gsl.vector_double.gsl_vector ;
      ioutlier : access gsl.vector_int.gsl_vector_int ;
      rng : access gsl.rng.gsl_rng := gsl.rng.alloc(gsl.rng.default) ;
      iws : access gsl.filter.gsl_filter_impulse_workspace ;

      xi, ei, u, outlier : double ;
      noutlier : aliased size_t ;
   begin
      -- 24.5.4 Impulse Detection Example
      if Verbose
      then
         Put_Line(myname);
      end if ;
      N := 1000 ;
      K := 25 ;
      t := 4.0 ;
      x := gsl.vector_double.alloc(N);
      y := gsl.vector_double.alloc(N);
      xmedian := gsl.vector_double.alloc(N);
      xsigma := gsl.vector_double.alloc(N);
      ioutlier := gsl.vector_int.alloc(N) ;
      lupper := gsl.vector_double.alloc(N) ;
      llower := gsl.vector_double.alloc(N) ;
      watch := gsl.vector_double.alloc(N) ;
      iws := gsl.filter.impulse_alloc(K) ;

      for i in 1..N
      loop
         u := gsl.rng.uniform(rng) ;
         ei := gsl.randist.gaussian(rng,2.0);
         xi := 10.0 * 
                  double(Ada.Numerics.Elementary_Functions.sin( 2.0 * 
                                                         Ada.Numerics.PI * 
                                                         Float(i) / Float (N))) ;
         if u < 0.01
         then
            outlier := 15.0 * double(gsl.math.GSL_SIGN(ei)) ;
         else
            outlier := 0.0;
         end if ;

         gsl.vector_double.set(x, i-1 , xi+ei+outlier);
      
      end loop ;
      Status := gsl.filter.impulse(gsl.filter.GSL_FILTER_END_TRUNCATE, 
                         gsl.filter.GSL_FILTER_SCALE_QN, t, x, y,
                         xmedian, xsigma, noutlier'access , ioutlier, iws );

      for i in 1..N
      loop
         gsl.vector_double.set(lupper,i-1,
                           gsl.vector_double.get(xmedian,i-1) + t * gsl.vector_double.get(xsigma,i-1));
         gsl.vector_double.set(llower,i-1,
                           gsl.vector_double.get(xmedian,i-1) - t * gsl.vector_double.get(xsigma,i-1));
         gsl.vector_double.set
           (watch,
            i - 1,
            gsl.vector_double.get(xmedian,i-1) * double(gsl.vector_int.get(ioutlier,i-1)) ); 
      end loop ;
      gsl.vector_double.WriteCSV
        (x,
         myname & ".csv",
         y , xmedian, xsigma, llower, lupper, watch );
      gsl.vector_int.WriteCSV
         (ioutlier, myname & ".outlier.csv" );

      gsl.vector_double.free(x);
      gsl.vector_double.free(y);
      gsl.vector_double.free(xmedian);
      gsl.vector_double.free(xsigma );
      gsl.vector_double.free(lupper);
      gsl.vector_double.free(llower);
      gsl.vector_int.free(ioutlier);
      gsl.rng.free(rng) ;
      gsl.filter.impulse_free(iws) ;
   end Test4 ;

   procedure Template is
      myname : String := gnat.Source_Info.enclosing_entity ;
      logfile : File_Type ;
   begin
      if Verbose
      then
         Put_Line(myname);
      end if ;
      Create(logfile,Out_File,myname & ".csv");
      Set_Output(logfile);
      Set_Output(Standard_Output);
      Close(logfile);
   end Template ;
begin
   if Argument_Count > 0
   then
      N := size_t'Value( Argument(1) );
      if Argument_Count > 1
      then
         K := size_t'Value( Argument(2) );
      end if ;
   end if ;
   Template ;
   Test1 ;
   Test2 ;
   Test3 ;
   Test4 ;
end Filter;