bb64bd34a1d4d95637bc776ee0c55644a80eb8e9
[aubio.git] / src / utils / scale.c
1 /*
2    Copyright (C) 2003 Paul Brossier
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "aubio_priv.h"
21 #include "fvec.h"
22 #include "utils/scale.h"
23
24 struct _aubio_scale_t {
25   smpl_t ilow;
26   smpl_t ihig;
27   smpl_t olow;
28   smpl_t ohig;
29
30   smpl_t scaler;
31   smpl_t irange;
32
33   /* not implemented yet : type in/out data
34      bool inint;
35      bool outint;
36      */
37 };
38
39 aubio_scale_t * new_aubio_scale (smpl_t ilow, smpl_t ihig, 
40     smpl_t olow, smpl_t ohig) {
41   aubio_scale_t * s = AUBIO_NEW(aubio_scale_t);
42   aubio_scale_set (s, ilow, ihig, olow, ohig);
43   return s;
44 }
45
46 void del_aubio_scale(aubio_scale_t *s) {
47   AUBIO_FREE(s);
48 }
49
50 void aubio_scale_set (aubio_scale_t *s, smpl_t ilow, smpl_t ihig,
51     smpl_t olow, smpl_t ohig) {
52   smpl_t inputrange = ihig - ilow;
53   smpl_t outputrange= ohig - olow;
54   s->ilow = ilow;
55   s->ihig = ihig;
56   s->olow = olow;
57   s->ohig = ohig;
58   if (inputrange == 0) {
59     s->scaler = 0.0f;
60   } else {
61     s->scaler = outputrange/inputrange;
62     if (inputrange < 0) {
63       inputrange = inputrange * -1.0f;
64     }
65   }
66 }
67
68 void aubio_scale_do (aubio_scale_t *s, fvec_t *input) 
69 {
70   uint_t i, j;
71   for (i=0; i < input->channels; i++){
72     for (j=0;  j < input->length; j++){
73       input->data[i][j] -= s->ilow;
74       input->data[i][j] *= s->scaler;
75       input->data[i][j] += s->olow;
76     }
77   }
78 }
79