/*---------------------------------------------------------------------------*\ FILE........: linreg.c AUTHOR......: David Rowe DATE CREATED: April 2015 Linear regression C module based on: http://stackoverflow.com/questions/5083465/fast-efficient-least-squares-fit-algorithm-in-c Use: $ gcc linreg.c -o linreg -D__UNITTEST__ -Wall $ ./linreg Then compare yfit results with octave/tlinreg.m \*---------------------------------------------------------------------------*/ /* Copyright (C) 2015 David Rowe All rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, see . */ #include #include #include #include "linreg.h" #include "comp_prim.h" namespace FreeDV { void linreg(COMP *m, COMP *b, float x[], COMP y[], int n) { float sumx = 0.0; /* sum of x */ float sumx2 = 0.0; /* sum of x^2 */ COMP sumxy = {0.0,0.0}; /* sum of x * y */ COMP sumy = {0.0,0.0}; /* sum of y */ COMP sumy2 = {0.0,0.0}; /* sum of y**2 */ float denom; COMP zero; int i; for (i=0; i