source: src/fmat.c @ 923a7a8

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 923a7a8 was 923a7a8, checked in by Paul Brossier <piem@piem.org>, 11 years ago

src/{fvec,fmat}.c: use memcpy and memset to optimise when possible, add option to disable

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