File: apply.c

package info (click to toggle)
gsl 2.5+dfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 25,628 kB
  • sloc: ansic: 245,573; sh: 11,435; makefile: 911; python: 68
file content (210 lines) | stat: -rw-r--r-- 6,601 bytes parent folder | download
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
/* movstat/apply.c
 *
 * Copyright (C) 2018 Patrick Alken
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or (at
 * your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <config.h>
#include <stdlib.h>
#include <math.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_movstat.h>

/*
gsl_movstat_apply_accum()
  Apply moving window statistic to input vector. This is a generalized
routine to handle window endpoints and apply a given accumulator to
the input vector.

Inputs: endtype      - end point handling criteria
        x            - input vector, size n
        accum        - accumulator to apply moving window statistic
        accum_params - parameters to pass to accumulator
        y            - output vector, size n
        z            - second output vector (i.e. minmax), size n; can be NULL
        w            - workspace

Notes:
1) It is allowed to have x = y for in-place moving statistics
*/

int
gsl_movstat_apply_accum(const gsl_movstat_end_t endtype,
                        const gsl_vector * x,
                        const gsl_movstat_accum * accum,
                        void * accum_params,
                        gsl_vector * y,
                        gsl_vector * z,
                        gsl_movstat_workspace * w)
{
  if (x->size != y->size)
    {
      GSL_ERROR("input and output vectors must have same length", GSL_EBADLEN);
    }
  else if (z != NULL && x->size != z->size)
    {
      GSL_ERROR("input and output vectors must have same length", GSL_EBADLEN);
    }
  else
    {
      const int n = (int) x->size;
      const int H = w->H; /* number of samples to left of current sample */
      const int J = w->J; /* number of samples to right of current sample */
      int i;
      double x1 = 0.0;    /* pad values for data edges */
      double xN = 0.0;
      double result[2];

      /* initialize accumulator */
      (accum->init)(w->K, w->state);

      /* pad initial window if necessary */
      if (endtype != GSL_MOVSTAT_END_TRUNCATE)
        {
          if (endtype == GSL_MOVSTAT_END_PADZERO)
            {
              x1 = 0.0;
              xN = 0.0;
            }
          else if (endtype == GSL_MOVSTAT_END_PADVALUE)
            {
              x1 = gsl_vector_get(x, 0);
              xN = gsl_vector_get(x, n - 1);
            }

          /* pad initial windows with H values */
          for (i = 0; i < H; ++i)
            (accum->insert)(x1, w->state);
        }
      else if (accum->delete_oldest == NULL) /* FIXME XXX */
        {
          /* save last K - 1 samples of x for later (needed for in-place input/output) */
          int idx1 = GSL_MAX(n - J - H, 0);
          int idx2 = n - 1;

          for (i = idx1; i <= idx2; ++i)
            w->work[i - idx1] = gsl_vector_get(x, i);
        }

      /* process input vector and fill y(0:n - J - 1) */
      for (i = 0; i < n; ++i)
        {
          double xi = gsl_vector_get(x, i);
          int idx = i - J;

          (accum->insert)(xi, w->state);

          if (idx >= 0)
            {
              (accum->get)(accum_params, result, w->state);
              gsl_vector_set(y, idx, result[0]);

              if (z != NULL)
                gsl_vector_set(z, idx, result[1]);
            }
        }

      if (endtype == GSL_MOVSTAT_END_TRUNCATE)
        {
          /* need to fill y(n-J:n-1) using shrinking windows */
          int idx1 = GSL_MAX(n - J, 0);
          int idx2 = n - 1;

          if (accum->delete_oldest == NULL)
            {
              int wsize = n - GSL_MAX(n - J - H, 0); /* size of work array */

              for (i = idx1; i <= idx2; ++i)
                {
                  int nsamp = n - GSL_MAX(i - H, 0); /* number of samples in this window */
                  int j;

                  (accum->init)(w->K, w->state);

                  for (j = wsize - nsamp; j < wsize; ++j)
                    (accum->insert)(w->work[j], w->state);

                  /* yi = acc_get [ work(i:K-2) ] */
                  (accum->get)(accum_params, result, w->state);
                  gsl_vector_set(y, i, result[0]);

                  if (z != NULL)
                    gsl_vector_set(z, i, result[1]);
                }
            }
          else
            {
              for (i = idx1; i <= idx2; ++i)
                {
                  if (i - H > 0)
                    {
                      /* delete oldest window sample as we move closer to edge */
                      (accum->delete_oldest)(w->state);
                    }

                  /* yi = acc_get [ work(i:K-2) ] */
                  (accum->get)(accum_params, result, w->state);
                  gsl_vector_set(y, i, result[0]);

                  if (z != NULL)
                    gsl_vector_set(z, i, result[1]);
                }
            }
        }
      else
        {
          /* pad final windows and fill y(n-J:n-1) */
          for (i = 0; i < J; ++i)
            {
              int idx = n - J + i;

              (accum->insert)(xN, w->state);

              if (idx >= 0)
                {
                  (accum->get)(accum_params, result, w->state);
                  gsl_vector_set(y, idx, result[0]);

                  if (z != NULL)
                    gsl_vector_set(z, idx, result[1]);
                }
            }
        }

      return GSL_SUCCESS;
    }
}

/*
gsl_movstat_apply()
  Apply user-defined moving window function to input vector

Inputs: endtype - end point handling criteria
        F       - user-defined function
        x       - input vector, size n
        y       - output vector, size n
        w       - workspace
*/

int
gsl_movstat_apply(const gsl_movstat_end_t endtype, const gsl_movstat_function * F,
                  const gsl_vector * x, gsl_vector * y, gsl_movstat_workspace * w)
{
  int status = gsl_movstat_apply_accum(endtype, x, gsl_movstat_accum_userfunc, (void *) F, y, NULL, w);
  return status;
}