source: src/fvec.c @ 567727d

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

[fvec] add fvec_vecadd

  • Property mode set to 100644
File size: 3.9 KB
RevLine 
[96fb8ad]1/*
[a6db140]2  Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
[96fb8ad]3
[a6db140]4  This file is part of aubio.
[96fb8ad]5
[a6db140]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.
[96fb8ad]10
[a6db140]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/>.
[96fb8ad]18
19*/
20
21#include "aubio_priv.h"
[6c7d49b]22#include "fvec.h"
[96fb8ad]23
[1120f86]24fvec_t * new_fvec(uint_t length) {
[c21acb9]25  fvec_t * s;
[767990e]26  if ((sint_t)length <= 0) {
27    return NULL;
28  }
[c21acb9]29  s = AUBIO_NEW(fvec_t);
[96fb8ad]30  s->length = length;
[c7860af]31  s->data = AUBIO_ARRAY(smpl_t, s->length);
[96fb8ad]32  return s;
33}
34
35void del_fvec(fvec_t *s) {
36  AUBIO_FREE(s->data);
37  AUBIO_FREE(s);
38}
39
[c34336e]40void fvec_set_sample(fvec_t *s, smpl_t data, uint_t position) {
[c7860af]41  s->data[position] = data;
[96fb8ad]42}
[c7860af]43
[1120f86]44smpl_t fvec_get_sample(const fvec_t *s, uint_t position) {
[c7860af]45  return s->data[position];
[96fb8ad]46}
47
[1120f86]48smpl_t * fvec_get_data(const fvec_t *s) {
[96fb8ad]49  return s->data;
50}
51
[3b2d32c]52/* helper functions */
53
[1120f86]54void fvec_print(const fvec_t *s) {
[c7860af]55  uint_t j;
56  for (j=0; j< s->length; j++) {
57    AUBIO_MSG(AUBIO_SMPL_FMT " ", s->data[j]);
[32f5a01]58  }
[c7860af]59  AUBIO_MSG("\n");
[32f5a01]60}
[3b2d32c]61
[c34336e]62void fvec_set_all (fvec_t *s, smpl_t val) {
[986131d]63#if defined(HAVE_INTEL_IPP)
[4b943729]64  aubio_ippsSet(val, s->data, (int)s->length);
[44e9eeb5]65#elif defined(HAVE_ATLAS)
66  aubio_catlas_set(s->length, val, s->data, 1);
67#elif defined(HAVE_ACCELERATE)
[1dd95c7]68  aubio_vDSP_vfill(&val, s->data, 1, s->length);
[986131d]69#else
70  uint_t j;
71  for ( j = 0; j< s->length; j++ )
72  {
73    s->data[j] = val;
74  }
[5b896e0]75#endif
[3b2d32c]76}
77
78void fvec_zeros(fvec_t *s) {
[986131d]79#if defined(HAVE_INTEL_IPP)
[4b943729]80  aubio_ippsZero(s->data, (int)s->length);
[986131d]81#elif defined(HAVE_ACCELERATE)
82  aubio_vDSP_vclr(s->data, 1, s->length);
83#elif defined(HAVE_MEMCPY_HACKS)
[923a7a8]84  memset(s->data, 0, s->length * sizeof(smpl_t));
85#else
[986131d]86  fvec_set_all(s, 0.);
[5b896e0]87#endif
[3b2d32c]88}
89
90void fvec_ones(fvec_t *s) {
[c34336e]91  fvec_set_all (s, 1.);
[3b2d32c]92}
93
94void fvec_rev(fvec_t *s) {
[c7860af]95  uint_t j;
[e141b23]96  for (j=0; j< FLOOR((smpl_t)s->length/2); j++) {
[c7860af]97    ELEM_SWAP(s->data[j], s->data[s->length-1-j]);
[3b2d32c]98  }
99}
100
[567727d]101void fvec_vecadd(fvec_t *s, const fvec_t *bias) {
102  uint_t j;
103  uint_t length = MIN(s->length, bias->length);
104  for (j = 0; j < length; j++) {
105    s->data[j] += bias->data[j];
106  }
107}
108
[1120f86]109void fvec_weight(fvec_t *s, const fvec_t *weight) {
[3b2d32c]110  uint_t length = MIN(s->length, weight->length);
[986131d]111#if defined(HAVE_INTEL_IPP)
[4b943729]112  aubio_ippsMul(s->data, weight->data, s->data, (int)length);
[b701179]113#elif defined(HAVE_ACCELERATE)
[986131d]114  aubio_vDSP_vmul( s->data, 1, weight->data, 1, s->data, 1, length );
115#else
116  uint_t j;
117  for (j = 0; j < length; j++) {
[c7860af]118    s->data[j] *= weight->data[j];
[3b2d32c]119  }
[5b896e0]120#endif /* HAVE_ACCELERATE */
[3b2d32c]121}
122
[1120f86]123void fvec_weighted_copy(const fvec_t *in, const fvec_t *weight, fvec_t *out) {
[986131d]124  uint_t length = MIN(in->length, MIN(out->length, weight->length));
125#if defined(HAVE_INTEL_IPP)
[4b943729]126  aubio_ippsMul(in->data, weight->data, out->data, (int)length);
[b701179]127#elif defined(HAVE_ACCELERATE)
[986131d]128  aubio_vDSP_vmul(in->data, 1, weight->data, 1, out->data, 1, length);
129#else
[7166ef8]130  uint_t j;
[986131d]131  for (j = 0; j < length; j++) {
[7166ef8]132    out->data[j] = in->data[j] * weight->data[j];
133  }
[986131d]134#endif
[7166ef8]135}
136
[1120f86]137void fvec_copy(const fvec_t *s, fvec_t *t) {
[3b2d32c]138  if (s->length != t->length) {
[923a7a8]139    AUBIO_ERR("trying to copy %d elements to %d elements \n",
[396103a]140        s->length, t->length);
[923a7a8]141    return;
[3b2d32c]142  }
[986131d]143#if defined(HAVE_INTEL_IPP)
[4b943729]144  aubio_ippsCopy(s->data, t->data, (int)s->length);
[630191c]145#elif defined(HAVE_BLAS)
[44e9eeb5]146  aubio_cblas_copy(s->length, s->data, 1, t->data, 1);
147#elif defined(HAVE_ACCELERATE)
[1dd95c7]148  aubio_vDSP_mmov(s->data, t->data, 1, s->length, 1, 1);
[986131d]149#elif defined(HAVE_MEMCPY_HACKS)
150  memcpy(t->data, s->data, t->length * sizeof(smpl_t));
151#else
152  uint_t j;
153  for (j = 0; j < t->length; j++) {
154    t->data[j] = s->data[j];
155  }
[1dd95c7]156#endif
[3b2d32c]157}
Note: See TracBrowser for help on using the repository browser.