Hiroshi M / GyroSensor
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers util.c Source File

util.c

00001 /**
00002  *****************************************************************************
00003  * File Name    : util.c
00004  *
00005  * Title        : Utility functions Source File
00006  * Revision     : 0.1
00007  * Notes        :
00008  * Target Board : mbed NXP LPC1768
00009  * Tool Chain   : ????
00010  *
00011  * Revision History:
00012  * When         Who         Description of change
00013  * -----------  ----------- -----------------------
00014  * 2012/07/10   Hiroshi M   init
00015  *****************************************************************************
00016  *
00017  * Copyright (C) 2012 Hiroshi M, MIT License
00018  *
00019  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00020  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00021  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00022  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00023  * furnished to do so, subject to the following conditions:
00024  *
00025  * The above copyright notice and this permission notice shall be included in all copies or
00026  * substantial portions of the Software.
00027  *
00028  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00029  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00030  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00031  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00032  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00033  *
00034  **/
00035 
00036 /* Includes ----------------------------------------------------------------- */
00037 #include "util.h"
00038 
00039 /* Private typedef ---------------------------------------------------------- */
00040 /* Private define ----------------------------------------------------------- */
00041 /* Private macro ------------------------------------------------------------ */
00042 /* Private variables -------------------------------------------------------- */
00043 /* Private function prototypes -----------------------------------------------*/
00044 
00045 /**
00046  * least squares method
00047  *
00048  */
00049 int lsm(int *y, int x)
00050 {
00051     int n;
00052     static long a0, a1, a2;
00053     long s01 = 0, s02 = 0, s11 = 0, s12 = 0;
00054 
00055     if (x == 0) {
00056         for (n = 0; n < N; n++) {
00057             s01 += n;
00058             s02 += (long)y[n];
00059             s11 += n * n;
00060             s12 += n * (long)y[n];
00061         }
00062 
00063         a0 = (s02 * s11 - s01 * s12) / (N * s11 - s01 * s01);
00064         a1 = (N * s12 - s01 * s02);
00065         a2 = (N * s11 - s01 * s01 * 2);
00066     }
00067     return (int)(a0 + a1 * (N + x) / a2);
00068 }
00069 
00070 
00071 int xabs(int x,int y)
00072 {
00073     if(x >= y) {
00074         return (x - y);
00075     } else {
00076         return (y - x);
00077     }
00078 }