• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) 2008-2011 Octasic Inc.
2    Written by Jean-Marc Valin */
3 /*
4    Redistribution and use in source and binary forms, with or without
5    modification, are permitted provided that the following conditions
6    are met:
7 
8    - Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10 
11    - Redistributions in binary form must reproduce the above copyright
12    notice, this list of conditions and the following disclaimer in the
13    documentation and/or other materials provided with the distribution.
14 
15    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
19    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 
32 #include <math.h>
33 #include "mlp.h"
34 #include "arch.h"
35 #include "tansig_table.h"
36 #define MAX_NEURONS 100
37 
38 #if 0
39 static inline opus_val16 tansig_approx(opus_val32 _x) /* Q19 */
40 {
41 	int i;
42 	opus_val16 xx; /* Q11 */
43 	/*double x, y;*/
44 	opus_val16 dy, yy; /* Q14 */
45 	/*x = 1.9073e-06*_x;*/
46 	if (_x>=QCONST32(8,19))
47 		return QCONST32(1.,14);
48 	if (_x<=-QCONST32(8,19))
49 		return -QCONST32(1.,14);
50 	xx = EXTRACT16(SHR32(_x, 8));
51 	/*i = lrint(25*x);*/
52 	i = SHR32(ADD32(1024,MULT16_16(25, xx)),11);
53 	/*x -= .04*i;*/
54 	xx -= EXTRACT16(SHR32(MULT16_16(20972,i),8));
55 	/*x = xx*(1./2048);*/
56 	/*y = tansig_table[250+i];*/
57 	yy = tansig_table[250+i];
58 	/*y = yy*(1./16384);*/
59 	dy = 16384-MULT16_16_Q14(yy,yy);
60 	yy = yy + MULT16_16_Q14(MULT16_16_Q11(xx,dy),(16384 - MULT16_16_Q11(yy,xx)));
61 	return yy;
62 }
63 #else
64 /*extern const float tansig_table[501];*/
tansig_approx(float x)65 static inline float tansig_approx(float x)
66 {
67 	int i;
68 	float y, dy;
69 	float sign=1;
70     if (x>=8)
71         return 1;
72     if (x<=-8)
73         return -1;
74 	if (x<0)
75 	{
76 	   x=-x;
77 	   sign=-1;
78 	}
79 	i = (int)floor(.5f+25*x);
80 	x -= .04f*i;
81 	y = tansig_table[i];
82 	dy = 1-y*y;
83 	y = y + x*dy*(1 - y*x);
84 	return sign*y;
85 }
86 #endif
87 
88 #if 0
89 void mlp_process(const MLP *m, const opus_val16 *in, opus_val16 *out)
90 {
91 	int j;
92 	opus_val16 hidden[MAX_NEURONS];
93 	const opus_val16 *W = m->weights;
94 	/* Copy to tmp_in */
95 	for (j=0;j<m->topo[1];j++)
96 	{
97 		int k;
98 		opus_val32 sum = SHL32(EXTEND32(*W++),8);
99 		for (k=0;k<m->topo[0];k++)
100 			sum = MAC16_16(sum, in[k],*W++);
101 		hidden[j] = tansig_approx(sum);
102 	}
103 	for (j=0;j<m->topo[2];j++)
104 	{
105 		int k;
106 		opus_val32 sum = SHL32(EXTEND32(*W++),14);
107 		for (k=0;k<m->topo[1];k++)
108 			sum = MAC16_16(sum, hidden[k], *W++);
109 		out[j] = tansig_approx(EXTRACT16(PSHR32(sum,17)));
110 	}
111 }
112 #else
mlp_process(const MLP * m,const float * in,float * out)113 void mlp_process(const MLP *m, const float *in, float *out)
114 {
115     int j;
116     float hidden[MAX_NEURONS];
117     const float *W = m->weights;
118     /* Copy to tmp_in */
119     for (j=0;j<m->topo[1];j++)
120     {
121         int k;
122         float sum = *W++;
123         for (k=0;k<m->topo[0];k++)
124             sum = sum + in[k]**W++;
125         hidden[j] = tansig_approx(sum);
126     }
127     for (j=0;j<m->topo[2];j++)
128     {
129         int k;
130         float sum = *W++;
131         for (k=0;k<m->topo[1];k++)
132             sum = sum + hidden[k]**W++;
133         out[j] = tansig_approx(sum);
134     }
135 }
136 #endif
137