source: src/ai/maxpool1d.c @ be3164d

feature/crepe
Last change on this file since be3164d was be3164d, checked in by Paul Brossier <piem@piem.org>, 2 years ago

[ai] only compile _debug function in debug mode

  • Property mode set to 100644
File size: 2.6 KB
Line 
1/*
2  Copyright (C) 2018 Paul Brossier <piem@aubio.org>
3
4  This file is part of aubio.
5
6  aubio is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10
11  aubio is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15
16  You should have received a copy of the GNU General Public License
17  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
18
19*/
20
21#include "aubio_priv.h"
22#include "fmat.h"
23#include "tensor.h"
24#include "maxpool1d.h"
25
26#include <float.h> // FLT_MAX
27
28struct _aubio_maxpool1d_t {
29  uint_t pool_size;
30  uint_t stride;
31};
32
33#if defined(DEBUG)
34static void aubio_maxpool1d_debug(aubio_maxpool1d_t *c,
35    aubio_tensor_t *input_tensor);
36#endif
37
38aubio_maxpool1d_t *new_aubio_maxpool1d(uint_t pool_size[1])
39{
40  aubio_maxpool1d_t *c = AUBIO_NEW(aubio_maxpool1d_t);
41
42  AUBIO_GOTO_FAILURE((sint_t)pool_size[0] > 0);
43
44  c->pool_size = pool_size[0];
45
46  c->stride = 1;
47
48  return c;
49
50failure:
51  del_aubio_maxpool1d(c);
52  return NULL;
53}
54
55void del_aubio_maxpool1d(aubio_maxpool1d_t* c) {
56  AUBIO_ASSERT(c);
57  AUBIO_FREE(c);
58}
59
60#if defined(DEBUG)
61void aubio_maxpool1d_debug(aubio_maxpool1d_t *c, aubio_tensor_t *input_tensor)
62{
63  AUBIO_DBG("maxpool1d: %15s -> (%d, %d) (no params)"
64      " (pool_size=(%d,), )\n", aubio_tensor_get_shape_string(input_tensor),
65      input_tensor->shape[0] / c->pool_size,
66      input_tensor->shape[1], c->pool_size);
67}
68#endif
69
70uint_t aubio_maxpool1d_get_output_shape(aubio_maxpool1d_t *c,
71    aubio_tensor_t *input, uint_t *shape)
72{
73  AUBIO_ASSERT(c);
74  AUBIO_ASSERT(shape && sizeof(shape) == 2*sizeof(uint_t));
75  AUBIO_ASSERT(input);
76  shape[0] = input->shape[0] / c->pool_size;
77  shape[1] = input->shape[1];
78
79#if defined(DEBUG)
80  aubio_maxpool1d_debug(c, input);
81#endif
82
83  return AUBIO_OK;
84}
85
86void aubio_maxpool1d_do(aubio_maxpool1d_t *c, aubio_tensor_t *input_tensor,
87    aubio_tensor_t *output_tensor)
88{
89  uint_t i, j, a;
90  AUBIO_ASSERT(c && input_tensor && output_tensor);
91
92  for (j = 0; j < output_tensor->shape[1]; j++) {
93    for (i = 0; i < output_tensor->shape[0]; i++) {
94      uint_t stride_i = i * c->pool_size;
95      smpl_t m = input_tensor->data[stride_i][j];
96      for (a = 0; a < c->pool_size; a++) {
97        m = MAX(m, input_tensor->data[stride_i + a][j]);
98      }
99      output_tensor->data[i][j] = m;
100    }
101  }
102}
Note: See TracBrowser for help on using the repository browser.