Skip to content

Commit e7bba82

Browse files
committed
impro
1 parent 8b052ae commit e7bba82

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/JTools.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ export cuboidModel, plotCuboid!, plotCone!, plotCylinder!, nicholsgrid, plotEcdf
3939
export plotBox, plotBox!, lines3!, multilines!, plotSphere!, plotSensorFov!, mergeMesh
4040
include("functions/makieTools.jl")
4141

42-
export crossmat, logrange, mag2db, db2mag, unwrap!, modd, isMultiple, signum, polyfit, polyval
42+
export crossmat, logrange, mag2db, db2mag, unwrap!, modd, isMultiple, signum
43+
export polyfit, polyval, interp1
4344
include("functions/math.jl")
4445

4546
export lsq

src/functions/math.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ isMultiple(x, y) = modd(x, y) == 0.0
2626

2727
signum(x) = x 0.0 ? 1.0 : -1.0
2828

29+
# Returns interpolating coefficients C, so that
30+
# y ≈ C[1] + C[2]*x + C[3]*x^2 + ...
2931
function polyfit(x, y, n)
3032
A = ones(length(x), n + 1)
3133
for i in 1:n
@@ -34,10 +36,22 @@ function polyfit(x, y, n)
3436
return A\y
3537
end
3638

39+
# Evaluate polynomial from coefficients C, so that
40+
# y = C[1] + C[2]*x + C[3]*x^2 + ...
3741
function polyval(C, x)
3842
y = 0.0
3943
for i in eachindex(C)
4044
y += C[i]*x^(i - 1)
4145
end
4246
return y
4347
end
48+
49+
function interp1(x, y, xi)
50+
if xi x[end]
51+
return y[end]
52+
elseif xi < x[1]
53+
return y[1]
54+
end
55+
id0 = sum(xi .≥ x)
56+
return y[id0] + (y[id0 + 1] - y[id0])/(x[id0 + 1] - x[id0])*(xi - x[id0])
57+
end

0 commit comments

Comments
 (0)