1 /******************************************************************************
2 * $Id: AKFS_VNorm.c 580 2012-03-29 09:56:21Z yamada.rj $
3 ******************************************************************************
4 *
5 * Copyright (C) 2012 Asahi Kasei Microdevices Corporation, Japan
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19 #include "AKFS_VNorm.h"
20 #include "AKFS_Device.h"
21
22 /*!
23 */
AKFS_VbNorm(const int16 ndata,const AKFVEC vdata[],const int16 nbuf,const AKFVEC * o,const AKFVEC * s,const AKFLOAT tgt,const int16 nvec,AKFVEC vvec[])24 int16 AKFS_VbNorm(
25 const int16 ndata, /*!< Size of raw vector buffer */
26 const AKFVEC vdata[], /*!< Raw vector buffer */
27 const int16 nbuf, /*!< Size of data to be buffered */
28 const AKFVEC* o, /*!< Offset */
29 const AKFVEC* s, /*!< Sensitivity */
30 const AKFLOAT tgt, /*!< Target sensitivity */
31 const int16 nvec, /*!< Size of normalized vector buffer */
32 AKFVEC vvec[] /*!< Normalized vector buffer */
33 )
34 {
35 int i;
36
37 /* size check */
38 if ((ndata <= 0) || (nvec <= 0) || (nbuf <= 0)) {
39 return AKFS_ERROR;
40 }
41 /* dependency check */
42 if ((nbuf < 1) || (ndata < nbuf) || (nvec < nbuf)) {
43 return AKFS_ERROR;
44 }
45 /* sensitivity check */
46 if ((s->u.x <= AKFS_EPSILON) ||
47 (s->u.y <= AKFS_EPSILON) ||
48 (s->u.z <= AKFS_EPSILON) ||
49 (tgt <= 0)) {
50 return AKFS_ERROR;
51 }
52
53 /* calculate and store data to buffer */
54 if (AKFS_BufShift(nvec, nbuf, vvec) != AKFS_SUCCESS) {
55 return AKFS_ERROR;
56 }
57 for (i=0; i<nbuf; i++) {
58 vvec[i].u.x = ((vdata[i].u.x - o->u.x) / (s->u.x) * (AKFLOAT)tgt);
59 vvec[i].u.y = ((vdata[i].u.y - o->u.y) / (s->u.y) * (AKFLOAT)tgt);
60 vvec[i].u.z = ((vdata[i].u.z - o->u.z) / (s->u.z) * (AKFLOAT)tgt);
61 }
62
63 return AKFS_SUCCESS;
64 }
65
66 /*!
67 */
AKFS_VbAve(const int16 nvec,const AKFVEC vvec[],const int16 nave,AKFVEC * vave)68 int16 AKFS_VbAve(
69 const int16 nvec, /*!< Size of normalized vector buffer */
70 const AKFVEC vvec[], /*!< Normalized vector buffer */
71 const int16 nave, /*!< Number of averaeg */
72 AKFVEC* vave /*!< Averaged vector */
73 )
74 {
75 int i;
76
77 /* arguments check */
78 if ((nave <= 0) || (nvec <= 0) || (nvec < nave)) {
79 return AKFS_ERROR;
80 }
81
82 /* calculate average */
83 vave->u.x = 0;
84 vave->u.y = 0;
85 vave->u.z = 0;
86 for (i=0; i<nave; i++) {
87 if ((vvec[i].u.x == AKFS_INIT_VALUE_F) ||
88 (vvec[i].u.y == AKFS_INIT_VALUE_F) ||
89 (vvec[i].u.z == AKFS_INIT_VALUE_F)) {
90 break;
91 }
92 vave->u.x += vvec[i].u.x;
93 vave->u.y += vvec[i].u.y;
94 vave->u.z += vvec[i].u.z;
95 }
96 if (i == 0) {
97 vave->u.x = 0;
98 vave->u.y = 0;
99 vave->u.z = 0;
100 } else {
101 vave->u.x /= i;
102 vave->u.y /= i;
103 vave->u.z /= i;
104 }
105 return AKFS_SUCCESS;
106 }
107
108
109