1 /* movstat/mmacc.c
2  *
3  * Copyright (C) 2018 Patrick Alken
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or (at
8  * your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 /*
21  * This module contains routines for tracking minimum/maximum values of a
22  * moving fixed-sized window. It is based on the algorithm of:
23  *
24  * [1] Daniel Lemire, Streaming Maximum-Minimum Filter Using No More than Three Comparisons per Element,
25  *     Nordic Journal of Computing, Volume 13, Number 4, pages 328-339, 2006
26  *
27  * Also available as a preprint here: https://arxiv.org/abs/cs/0610046
28  */
29 
30 #include <config.h>
31 #include <stdlib.h>
32 #include <math.h>
33 #include <gsl/gsl_math.h>
34 #include <gsl/gsl_vector.h>
35 #include <gsl/gsl_movstat.h>
36 
37 typedef double mmacc_type_t;
38 typedef mmacc_type_t ringbuf_type_t;
39 
40 #include "deque.c"
41 #include "ringbuf.c"
42 
43 typedef struct
44 {
45   size_t n;             /* window size */
46   size_t k;             /* number of samples in current window */
47   mmacc_type_t xprev;   /* previous sample added to window */
48   ringbuf *rbuf;        /* ring buffer storing current window, size n */
49   deque *minque;        /* double-ended queue of min values (L) */
50   deque *maxque;        /* double-ended queue of max values (U) */
51 } mmacc_state_t;
52 
53 static size_t mmacc_size(const size_t n);
54 static int mmacc_init(const size_t n, void * vstate);
55 static int mmacc_insert(const mmacc_type_t x, void * vstate);
56 static int mmacc_delete(void * vstate);
57 static int mmacc_min(void * params, mmacc_type_t * result, const void * vstate);
58 static int mmacc_max(void * params, mmacc_type_t * result, const void * vstate);
59 static int mmacc_minmax(void * params, mmacc_type_t * result, const void * vstate);
60 
61 static size_t
mmacc_size(const size_t n)62 mmacc_size(const size_t n)
63 {
64   size_t size = 0;
65 
66   size += sizeof(mmacc_state_t);
67   size += ringbuf_size(n);          /* rbuf */
68   size += 2 * deque_size(n + 1);    /* minque/maxque */
69 
70   return size;
71 }
72 
73 static int
mmacc_init(const size_t n,void * vstate)74 mmacc_init(const size_t n, void * vstate)
75 {
76   mmacc_state_t * state = (mmacc_state_t *) vstate;
77 
78   state->n = n;
79   state->k = 0;
80   state->xprev = 0.0;
81 
82   state->rbuf = (ringbuf *) ((unsigned char *) vstate + sizeof(mmacc_state_t));
83   state->minque = (deque *) ((unsigned char *) state->rbuf + ringbuf_size(n));
84   state->maxque = (deque *) ((unsigned char *) state->minque + deque_size(n + 1));
85 
86   ringbuf_init(n, state->rbuf);
87   deque_init(n + 1, state->minque);
88   deque_init(n + 1, state->maxque);
89 
90   return GSL_SUCCESS;
91 }
92 
93 static int
mmacc_insert(const mmacc_type_t x,void * vstate)94 mmacc_insert(const mmacc_type_t x, void * vstate)
95 {
96   mmacc_state_t * state = (mmacc_state_t *) vstate;
97   int head, tail;
98 
99   if (state->k == 0)
100     {
101       /* first sample */
102       ringbuf_insert(x, state->rbuf);
103       head = state->rbuf->head;
104       deque_push_back(head, state->maxque);
105       deque_push_back(head, state->minque);
106     }
107   else
108     {
109       if (x > state->xprev)
110         {
111           deque_pop_back(state->maxque);
112 
113           while (!deque_is_empty(state->maxque))
114             {
115               if (x <= state->rbuf->array[deque_peek_back(state->maxque)])
116                 break;
117 
118               deque_pop_back(state->maxque);
119             }
120         }
121       else
122         {
123           deque_pop_back(state->minque);
124 
125           while (!deque_is_empty(state->minque))
126             {
127               if (x >= state->rbuf->array[deque_peek_back(state->minque)])
128                 break;
129 
130               deque_pop_back(state->minque);
131             }
132         }
133 
134       /* store new sample into ring buffer */
135       tail = state->rbuf->tail;
136       ringbuf_insert(x, state->rbuf);
137       head = state->rbuf->head;
138 
139       deque_push_back(head, state->maxque);
140       deque_push_back(head, state->minque);
141 
142       if (state->k == state->n)
143         {
144           /*
145            * window is full - check if oldest window element is a global minimum/maximum
146            * of current window - if so pop it from U/L queues;
147            * the check head != tail ensures there is more than 1 element in the
148            * queue, do not pop if queue has only 1 element, since this element would
149            * be the newest sample
150            */
151           if (state->maxque->head != state->maxque->tail && tail == deque_peek_front(state->maxque))
152             deque_pop_front(state->maxque);
153           else if (state->minque->head != state->minque->tail && tail == deque_peek_front(state->minque))
154             deque_pop_front(state->minque);
155         }
156     }
157 
158   if (state->k < state->n)
159     ++(state->k);
160 
161   state->xprev = x;
162 
163   return GSL_SUCCESS;
164 }
165 
166 static int
mmacc_delete(void * vstate)167 mmacc_delete(void * vstate)
168 {
169   mmacc_state_t * state = (mmacc_state_t *) vstate;
170 
171   if (state->k > 0)
172     {
173       /*
174        * check if oldest window element is a global minimum/maximum; if so
175        * pop it from U/L queues
176        */
177       if (state->rbuf->tail == deque_peek_front(state->maxque))
178         deque_pop_front(state->maxque);
179       else if (state->rbuf->tail == deque_peek_front(state->minque))
180         deque_pop_front(state->minque);
181 
182       /* remove oldest element from ring buffer */
183       ringbuf_pop_back(state->rbuf);
184 
185       --(state->k);
186     }
187 
188   return GSL_SUCCESS;
189 }
190 
191 static int
mmacc_min(void * params,mmacc_type_t * result,const void * vstate)192 mmacc_min(void * params, mmacc_type_t * result, const void * vstate)
193 {
194   const mmacc_state_t * state = (const mmacc_state_t *) vstate;
195 
196   (void) params;
197 
198   if (state->k == 0)
199     {
200       GSL_ERROR ("no samples yet added to workspace", GSL_EINVAL);
201     }
202   else
203     {
204       *result = state->rbuf->array[deque_peek_front(state->minque)];
205       return GSL_SUCCESS;
206     }
207 }
208 
209 static int
mmacc_max(void * params,mmacc_type_t * result,const void * vstate)210 mmacc_max(void * params, mmacc_type_t * result, const void * vstate)
211 {
212   const mmacc_state_t * state = (const mmacc_state_t *) vstate;
213 
214   (void) params;
215 
216   if (state->k == 0)
217     {
218       GSL_ERROR ("no samples yet added to workspace", GSL_EINVAL);
219     }
220   else
221     {
222       *result = state->rbuf->array[deque_peek_front(state->maxque)];
223       return GSL_SUCCESS;
224     }
225 }
226 
227 static int
mmacc_minmax(void * params,mmacc_type_t * result,const void * vstate)228 mmacc_minmax(void * params, mmacc_type_t * result, const void * vstate)
229 {
230   const mmacc_state_t * state = (const mmacc_state_t *) vstate;
231 
232   (void) params;
233 
234   if (state->k == 0)
235     {
236       GSL_ERROR ("no samples yet added to workspace", GSL_EINVAL);
237     }
238   else
239     {
240       result[0] = state->rbuf->array[deque_peek_front(state->minque)];
241       result[1] = state->rbuf->array[deque_peek_front(state->maxque)];
242       return GSL_SUCCESS;
243     }
244 }
245 
246 static const gsl_movstat_accum min_accum_type =
247 {
248   mmacc_size,
249   mmacc_init,
250   mmacc_insert,
251   mmacc_delete,
252   mmacc_min
253 };
254 
255 const gsl_movstat_accum *gsl_movstat_accum_min = &min_accum_type;
256 
257 static const gsl_movstat_accum max_accum_type =
258 {
259   mmacc_size,
260   mmacc_init,
261   mmacc_insert,
262   mmacc_delete,
263   mmacc_max
264 };
265 
266 const gsl_movstat_accum *gsl_movstat_accum_max = &max_accum_type;
267 
268 static const gsl_movstat_accum minmax_accum_type =
269 {
270   mmacc_size,
271   mmacc_init,
272   mmacc_insert,
273   mmacc_delete,
274   mmacc_minmax
275 };
276 
277 const gsl_movstat_accum *gsl_movstat_accum_minmax = &minmax_accum_type;
278