diff options
author | Jean-Marc Valin <jean-marc.valin@octasic.com> | 2011-05-28 01:56:28 +0400 |
---|---|---|
committer | Jean-Marc Valin <jean-marc.valin@octasic.com> | 2011-05-28 01:56:28 +0400 |
commit | 17307b8385f4bcf18d2128eeba143641aa443e9b (patch) | |
tree | 698b193daaf8227497e5953772fe33ee38d77299 | |
parent | 3a63272142d5d9199885c21c47ec297d034a6324 (diff) |
run-time MLP code
-rw-r--r-- | src/detect.h | 31 | ||||
-rw-r--r-- | src/features.c | 2 | ||||
-rw-r--r-- | src/mlp.c | 103 | ||||
-rw-r--r-- | src/mlp.h | 41 | ||||
-rw-r--r-- | src/mlp_prob.c | 55 | ||||
-rw-r--r-- | src/tansig_table.h | 105 |
6 files changed, 336 insertions, 1 deletions
diff --git a/src/detect.h b/src/detect.h new file mode 100644 index 00000000..5f82804b --- /dev/null +++ b/src/detect.h @@ -0,0 +1,31 @@ +/* Copyright (c) 2011 Xiph.Org Foundation + Written by Jean-Marc Valin */ +/* + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + - Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + - Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include "arch.h" +#include "celt.h" + +void feature_analysis_fixed(CELTEncoder *celt_enc, const celt_int16 *x); diff --git a/src/features.c b/src/features.c index 292c4bc5..49653952 100644 --- a/src/features.c +++ b/src/features.c @@ -33,7 +33,7 @@ #include "celt.h" #include "modes.h" #include "arch.h" -#include "features.h" +#include "detect.h" #include "quant_bands.h" #define NBANDS 17 diff --git a/src/mlp.c b/src/mlp.c new file mode 100644 index 00000000..2e1e6a5e --- /dev/null +++ b/src/mlp.c @@ -0,0 +1,103 @@ +/* Copyright (c) 2008-2011 Octasic Inc. + Written by Jean-Marc Valin */ +/* + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + - Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + - Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + +#include <math.h> +#include "mlp.h" +#include "arch.h" +#include "tansig_table.h" +#define MAX_NEURONS 100 + +#ifdef FIXED_POINT +extern const celt_word16 tansig_table[501]; +static inline celt_word16 tansig_approx(celt_word32 _x) /* Q19 */ +{ + int i; + celt_word16 xx; /* Q11 */ + /*double x, y;*/ + celt_word16 dy, yy; /* Q14 */ + /*x = 1.9073e-06*_x;*/ + if (_x>=QCONST32(10,19)) + return QCONST32(1.,14); + if (_x<=-QCONST32(10,19)) + return -QCONST32(1.,14); + xx = EXTRACT16(SHR32(_x, 8)); + /*i = lrint(25*x);*/ + i = SHR32(ADD32(1024,MULT16_16(25, xx)),11); + /*x -= .04*i;*/ + xx -= EXTRACT16(SHR32(MULT16_16(20972,i),8)); + /*x = xx*(1./2048);*/ + /*y = tansig_table[250+i];*/ + yy = tansig_table[250+i]; + /*y = yy*(1./16384);*/ + dy = 16384-MULT16_16_Q14(yy,yy); + yy = yy + MULT16_16_Q14(MULT16_16_Q11(xx,dy),(16384 - MULT16_16_Q11(yy,xx))); + return yy; +} +#else +/*extern const float tansig_table[501];*/ +static inline double tansig_approx(double x) +{ + int i; + double y, dy; + if (x>=10) + return 1; + if (x<=-10) + return -1; + i = lrint(25*x); + x -= .04*i; + y = tansig_table[250+i]; + dy = 1-y*y; + y = y + x*dy*(1 - y*x); + return y; +} +#endif + +void mlp_process(const MLP *m, const celt_word16 *in, celt_word16 *out) +{ + int j; + celt_word16 hidden[MAX_NEURONS]; + const celt_word16 *W = m->weights; + /* Copy to tmp_in */ + for (j=0;j<m->topo[1];j++) + { + int k; + celt_word32 sum = SHL32(EXTEND32(*W++),8); + for (k=0;k<m->topo[0];k++) + sum = MAC16_16(sum, in[k],*W++); + hidden[j] = tansig_approx(sum); + } + for (j=0;j<m->topo[2];j++) + { + int k; + celt_word32 sum = SHL32(EXTEND32(*W++),14); + for (k=0;k<m->topo[1];k++) + sum = MAC16_16(sum, hidden[k], *W++); + out[j] = tansig_approx(EXTRACT16(PSHR32(sum,17))); + } +} + diff --git a/src/mlp.h b/src/mlp.h new file mode 100644 index 00000000..e45c9aa9 --- /dev/null +++ b/src/mlp.h @@ -0,0 +1,41 @@ +/* Copyright (c) 2008-2011 Octasic Inc. + Written by Jean-Marc Valin */ +/* + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + - Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + - Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef _MLP_H_ +#define _MLP_H_ + +#include "arch.h" + +typedef struct { + int layers; + const int *topo; + const celt_word16 *weights; +} MLP; + +void mlp_process(const MLP *m, const celt_word16 *in, celt_word16 *out); + +#endif /* _MLP_H_ */ diff --git a/src/mlp_prob.c b/src/mlp_prob.c new file mode 100644 index 00000000..1b4276ea --- /dev/null +++ b/src/mlp_prob.c @@ -0,0 +1,55 @@ +/* Copyright (c) 2008-2011 Octasic Inc. + Written by Jean-Marc Valin */ +/* + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + - Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + - Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include "mlp.h" +#include "mlp_data.c" + +int main(int argc, char **argv) +{ + int ret; + int i, j; + float *data; + int N, nbVectors; + + N = atoi(argv[1]); + nbVectors = atoi(argv[2]); + + data = malloc(sizeof(*data)*N); + for (i=0;i<nbVectors;i++) + { + float out; + for (j=0;j<N;j++) + ret = scanf("%f ", data+j); + mlp_process(&net, data, &out); + printf("%f\n", out); + } + + return 0; +} diff --git a/src/tansig_table.h b/src/tansig_table.h new file mode 100644 index 00000000..ac2e5be1 --- /dev/null +++ b/src/tansig_table.h @@ -0,0 +1,105 @@ +/* This file is auto-generated by gen_tables */ + +static const celt_word16 tansig_table[501] = { +-1.000000, -1.000000, -1.000000, -1.000000, -1.000000, +-1.000000, -1.000000, -1.000000, -1.000000, -1.000000, +-1.000000, -1.000000, -1.000000, -1.000000, -1.000000, +-1.000000, -1.000000, -1.000000, -1.000000, -1.000000, +-1.000000, -1.000000, -1.000000, -1.000000, -1.000000, +-1.000000, -1.000000, -1.000000, -1.000000, -1.000000, +-1.000000, -1.000000, -1.000000, -1.000000, -1.000000, +-1.000000, -1.000000, -1.000000, -1.000000, -1.000000, +-1.000000, -1.000000, -1.000000, -1.000000, -1.000000, +-1.000000, -1.000000, -1.000000, -1.000000, -1.000000, +-1.000000, -1.000000, -1.000000, -1.000000, -1.000000, +-1.000000, -1.000000, -1.000000, -1.000000, -1.000000, +-1.000000, -0.999999, -0.999999, -0.999999, -0.999999, +-0.999999, -0.999999, -0.999999, -0.999999, -0.999999, +-0.999999, -0.999999, -0.999999, -0.999999, -0.999998, +-0.999998, -0.999998, -0.999998, -0.999998, -0.999998, +-0.999997, -0.999997, -0.999997, -0.999997, -0.999997, +-0.999996, -0.999996, -0.999996, -0.999995, -0.999995, +-0.999994, -0.999994, -0.999994, -0.999993, -0.999992, +-0.999992, -0.999991, -0.999990, -0.999990, -0.999989, +-0.999988, -0.999987, -0.999986, -0.999984, -0.999983, +-0.999982, -0.999980, -0.999978, -0.999977, -0.999975, +-0.999973, -0.999970, -0.999968, -0.999965, -0.999962, +-0.999959, -0.999956, -0.999952, -0.999948, -0.999944, +-0.999939, -0.999934, -0.999929, -0.999923, -0.999916, +-0.999909, -0.999902, -0.999893, -0.999885, -0.999875, +-0.999865, -0.999853, -0.999841, -0.999828, -0.999813, +-0.999798, -0.999781, -0.999763, -0.999743, -0.999722, +-0.999699, -0.999673, -0.999646, -0.999617, -0.999585, +-0.999550, -0.999513, -0.999472, -0.999428, -0.999381, +-0.999329, -0.999273, -0.999213, -0.999147, -0.999076, +-0.999000, -0.998916, -0.998826, -0.998728, -0.998623, +-0.998508, -0.998384, -0.998249, -0.998104, -0.997946, +-0.997775, -0.997590, -0.997389, -0.997172, -0.996937, +-0.996682, -0.996407, -0.996108, -0.995784, -0.995434, +-0.995055, -0.994644, -0.994199, -0.993718, -0.993196, +-0.992631, -0.992020, -0.991359, -0.990642, -0.989867, +-0.989027, -0.988119, -0.987136, -0.986072, -0.984921, +-0.983675, -0.982327, -0.980869, -0.979293, -0.977587, +-0.975743, -0.973749, -0.971594, -0.969265, -0.966747, +-0.964028, -0.961090, -0.957917, -0.954492, -0.950795, +-0.946806, -0.942503, -0.937863, -0.932862, -0.927473, +-0.921669, -0.915420, -0.908698, -0.901468, -0.893698, +-0.885352, -0.876393, -0.866784, -0.856485, -0.845456, +-0.833655, -0.821040, -0.807569, -0.793199, -0.777888, +-0.761594, -0.744277, -0.725897, -0.706419, -0.685809, +-0.664037, -0.641077, -0.616909, -0.591519, -0.564900, +-0.537050, -0.507977, -0.477700, -0.446244, -0.413644, +-0.379949, -0.345214, -0.309507, -0.272905, -0.235496, +-0.197375, -0.158649, -0.119427, -0.079830, -0.039979, +0.000000, 0.039979, 0.079830, 0.119427, 0.158649, +0.197375, 0.235496, 0.272905, 0.309507, 0.345214, +0.379949, 0.413644, 0.446244, 0.477700, 0.507977, +0.537050, 0.564900, 0.591519, 0.616909, 0.641077, +0.664037, 0.685809, 0.706419, 0.725897, 0.744277, +0.761594, 0.777888, 0.793199, 0.807569, 0.821040, +0.833655, 0.845456, 0.856485, 0.866784, 0.876393, +0.885352, 0.893698, 0.901468, 0.908698, 0.915420, +0.921669, 0.927473, 0.932862, 0.937863, 0.942503, +0.946806, 0.950795, 0.954492, 0.957917, 0.961090, +0.964028, 0.966747, 0.969265, 0.971594, 0.973749, +0.975743, 0.977587, 0.979293, 0.980869, 0.982327, +0.983675, 0.984921, 0.986072, 0.987136, 0.988119, +0.989027, 0.989867, 0.990642, 0.991359, 0.992020, +0.992631, 0.993196, 0.993718, 0.994199, 0.994644, +0.995055, 0.995434, 0.995784, 0.996108, 0.996407, +0.996682, 0.996937, 0.997172, 0.997389, 0.997590, +0.997775, 0.997946, 0.998104, 0.998249, 0.998384, +0.998508, 0.998623, 0.998728, 0.998826, 0.998916, +0.999000, 0.999076, 0.999147, 0.999213, 0.999273, +0.999329, 0.999381, 0.999428, 0.999472, 0.999513, +0.999550, 0.999585, 0.999617, 0.999646, 0.999673, +0.999699, 0.999722, 0.999743, 0.999763, 0.999781, +0.999798, 0.999813, 0.999828, 0.999841, 0.999853, +0.999865, 0.999875, 0.999885, 0.999893, 0.999902, +0.999909, 0.999916, 0.999923, 0.999929, 0.999934, +0.999939, 0.999944, 0.999948, 0.999952, 0.999956, +0.999959, 0.999962, 0.999965, 0.999968, 0.999970, +0.999973, 0.999975, 0.999977, 0.999978, 0.999980, +0.999982, 0.999983, 0.999984, 0.999986, 0.999987, +0.999988, 0.999989, 0.999990, 0.999990, 0.999991, +0.999992, 0.999992, 0.999993, 0.999994, 0.999994, +0.999994, 0.999995, 0.999995, 0.999996, 0.999996, +0.999996, 0.999997, 0.999997, 0.999997, 0.999997, +0.999997, 0.999998, 0.999998, 0.999998, 0.999998, +0.999998, 0.999998, 0.999999, 0.999999, 0.999999, +0.999999, 0.999999, 0.999999, 0.999999, 0.999999, +0.999999, 0.999999, 0.999999, 0.999999, 0.999999, +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, +1.000000, +}; |