978abb733457a9f041446f99fb62fcf801d25eaa
[aubio.git] / src / fmat.c
1 /*
2   Copyright (C) 2009 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
24 fmat_t * new_fmat (uint_t length, uint_t height) {
25   if ((sint_t)length <= 0 || (sint_t)height <= 0 ) {
26     return NULL;
27   }
28   fmat_t * s = AUBIO_NEW(fmat_t);
29   uint_t i,j;
30   s->height = height;
31   s->length = length;
32   s->data = AUBIO_ARRAY(smpl_t*,s->height);
33   for (i=0; i< s->height; i++) {
34     s->data[i] = AUBIO_ARRAY(smpl_t, s->length);
35     for (j=0; j< s->length; j++) {
36       s->data[i][j]=0.;
37     }
38   }
39   return s;
40 }
41
42 void del_fmat (fmat_t *s) {
43   uint_t i;
44   for (i=0; i<s->height; i++) {
45     AUBIO_FREE(s->data[i]);
46   }
47   AUBIO_FREE(s->data);
48   AUBIO_FREE(s);
49 }
50
51 void fmat_set_sample(fmat_t *s, smpl_t data, uint_t channel, uint_t position) {
52   s->data[channel][position] = data;
53 }
54
55 smpl_t fmat_get_sample(fmat_t *s, uint_t channel, uint_t position) {
56   return s->data[channel][position];
57 }
58
59 void fmat_get_channel(fmat_t *s, uint_t channel, fvec_t *output) {
60   output->data = s->data[channel];
61   output->length = s->length;
62   return;
63 }
64
65 smpl_t * fmat_get_channel_data(fmat_t *s, uint_t channel) {
66   return s->data[channel];
67 }
68
69 smpl_t ** fmat_get_data(fmat_t *s) {
70   return s->data;
71 }
72
73 /* helper functions */
74
75 void fmat_print(fmat_t *s) {
76   uint_t i,j;
77   for (i=0; i< s->height; i++) {
78     for (j=0; j< s->length; j++) {
79       AUBIO_MSG(AUBIO_SMPL_FMT " ", s->data[i][j]);
80     }
81     AUBIO_MSG("\n");
82   }
83 }
84
85 void fmat_set(fmat_t *s, smpl_t val) {
86   uint_t i,j;
87   for (i=0; i< s->height; i++) {
88     for (j=0; j< s->length; j++) {
89       s->data[i][j] = val;
90     }
91   }
92 }
93
94 void fmat_zeros(fmat_t *s) {
95 #if HAVE_MEMCPY_HACKS
96   uint_t i;
97   for (i=0; i< s->height; i++) {
98     memset(s->data[i], 0, s->length * sizeof(smpl_t));
99   }
100 #else
101   fmat_set(s, 0.);
102 #endif
103 }
104
105 void fmat_ones(fmat_t *s) {
106   fmat_set(s, 1.);
107 }
108
109 void fmat_rev(fmat_t *s) {
110   uint_t i,j;
111   for (i=0; i< s->height; i++) {
112     for (j=0; j< FLOOR(s->length/2); j++) {
113       ELEM_SWAP(s->data[i][j], s->data[i][s->length-1-j]);
114     }
115   }
116 }
117
118 void fmat_weight(fmat_t *s, fmat_t *weight) {
119   uint_t i,j;
120   uint_t length = MIN(s->length, weight->length);
121   for (i=0; i< s->height; i++) {
122     for (j=0; j< length; j++) {
123       s->data[i][j] *= weight->data[0][j];
124     }
125   }
126 }
127
128 void fmat_copy(fmat_t *s, fmat_t *t) {
129   if (s->height != t->height) {
130     AUBIO_ERR("trying to copy %d rows to %d rows \n",
131             s->height, t->height);
132     return;
133   }
134   if (s->length != t->length) {
135     AUBIO_ERR("trying to copy %d columns to %d columns\n",
136             s->length, t->length);
137     return;
138   }
139 #if HAVE_MEMCPY_HACKS
140   uint_t i;
141   for (i=0; i< s->height; i++) {
142     memcpy(t->data[i], s->data[i], t->length * sizeof(smpl_t));
143   }
144 #else
145   uint_t i,j;
146   for (i=0; i< t->height; i++) {
147     for (j=0; j< t->length; j++) {
148       t->data[i][j] = s->data[i][j];
149     }
150   }
151 #endif
152 }
153