-
Notifications
You must be signed in to change notification settings - Fork 11
/
regress.c
82 lines (64 loc) · 1.64 KB
/
regress.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
libFooID - Free audio fingerprinting library
Copyright (C) 2006 Gian-Carlo Pascutto, Hogeschool Gent
Use of this software is allowed under either:
1) The GNU General Public License (GPL), as described
in LICENSE.GPL.
2) A modified BSD License, as described in LICENSE.BSDA.
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.
*/
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "common.h"
/*
http://mathworld.wolfram.com/LeastSquaresFitting.html
*/
void do_linear_regress(float *dbspec, int len, float *r)
{
int i;
float ssx;
float ssy;
float sxy;
float ssxx;
float ssyy;
float ssxy;
float avx;
float avy;
float rsq;
/* float b, a; */
ssx = 0.0f;
ssy = 0.0f;
sxy = 0.0f;
avx = 0.0f;
avy = 0.0f;
for (i = 0; i < len; i++) {
avx += i;
avy += dbspec[i];
ssx += i * i;
ssy += dbspec[i] * dbspec[i];
sxy += i * dbspec[i];
}
avx /= (float)len;
avy /= (float)len;
ssxx = ssx - (float)len * avx * avx;
ssyy = ssy - (float)len * avy * avy;
ssxy = sxy - (float)len * avx * avy;
if (ssyy <= 0.0f + EPSILON) {
*r = 1.0f;
}
/*
b = ssxy / ssxx;
a = avy - b * avx;
*/
rsq = (ssxy * ssxy) / (ssxx * ssyy);
/*
It seems that rsq ^ 0.25 follows roughly
a normal distribution, so thats our preferred
way of looking at things
*/
*r = (float)sqrt(sqrt(rsq));
}