diff options
| author | Marin Ivanov <[email protected]> | 2025-07-25 10:17:14 +0300 |
|---|---|---|
| committer | Marin Ivanov <[email protected]> | 2026-01-18 20:09:26 +0200 |
| commit | 0168586485e6310c598713c911b1dec5618d61a1 (patch) | |
| tree | 6aabc2a12ef8fef70683f5389bea00f948015f77 /octave/linreg.m | |
* codec2 cut-down version 1.2.0
* Remove codebook and generation of sources
* remove c2dec c2enc binaries
* prepare for emscripten
Diffstat (limited to 'octave/linreg.m')
| -rw-r--r-- | octave/linreg.m | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/octave/linreg.m b/octave/linreg.m new file mode 100644 index 0000000..666b65c --- /dev/null +++ b/octave/linreg.m @@ -0,0 +1,35 @@ +% linreg.m +% David Rowe April 2015 +% +% Based on: +% http://stackoverflow.com/questions/5083465/fast-efficient-least-squares-fit-algorithm-in-c +% +% finds y = mx + b to best fit n points x and y + +function [m b] = linreg(x,y,n) + sumx = 0.0; % sum of x + sumx2 = 0.0; % sum of x^2 + sumxy = 0.0; % sum of x * y + sumy = 0.0; % sum of y + sumy2 = 0.0; % sum of y**2 + + for i=1:n + sumx += x(i); + sumx2 += x(i)^2; + sumxy += x(i) * y(i); + sumy += y(i); + sumy2 += y(i)^2; + end + + denom = (n * sumx2 - sumx*sumx); + + if denom == 0 + % singular matrix. can't solve the problem. + m = 0; + b = 0; + else + m = (n * sumxy - sumx * sumy) / denom; + b = (sumy * sumx2 - sumx * sumxy) / denom; + end + +endfunction |
