-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpower_regression.r
executable file
·149 lines (132 loc) · 4.84 KB
/
power_regression.r
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# An R script to estimate MQ gas sensors correlation curve and compute Ro, min and max Rs/Ro
#
# Copyright (c) Fariz Wicaksono, 2021
#
# Released under GPLv3.
# Please refer to LICENSE file for licensing information.
# How to use this script:
# 1) set limits as datasheet curve ("xlim" and "ylim")
# ex.
# xlim = c(10, 1000)
# ylim = c(0.1, 10)
# 2) find out datasheet curve points, and write it out (to "pointsdata")
# each line it's a point on cartesian coordinate system
# the useful WebPlotDigitizer app can help you extract points from the graph
# ex.
# pointsdata = "
# 10.052112405371744, 2.283698378106183
# 20.171602728600178, 1.8052797165878915
# 30.099224396434586, 1.5715748803154423
# 50.09267987761949, 1.3195287228519417
# 80.38812026903305, 1.1281218760133969
# 90.12665922665023, 1.0815121769656304
# 100.52112405371739, 1.0430967861855598
# 199.62996638292853, 0.8000946404902397
# "
# 3) optional for Ro estimation: measure the sensor resistance (set it to "mres" ohm value) at a know amount of gas
# set it to 0 if you do not need the Ro estimation
# ex.
# mres = 26954
# 4) optional for Ro estimation: set the know amount of gas for the resistance measure of the previous step (to "mppm")
# set it to 0 if you do not need the Ro estimation
# ex.
# mppm = 392
# 5) optional for min-max Rs/Ro estimation: set the minand max amount of gas the sensor will react to (as "minppm" and "maxppm")
# set it to 0 if you do not need the min-max Rs/Ro estimation
# ex.
# minppm = 10
# maxppm = 200
library(data.table)
#remove old variables
rm(list=ls())
#set input values
xlim = c(0.1, 10)
ylim = c(0.1, 10)
minppm = 0
maxppm = 0
mres = 0
mppm = 0
pointsdata = "
1, 1
10, 0.9
30, 0.8
80, 0.7
"
#load points using fread
setnames(points <- fread(pointsdata, sep=",", sep2="\n"), c("x","y"))
#set named list of points, and swapped list of points
#points will be used to plot and compute values as datasheet figure
#pointsrev will be used to plot and compute values for the correlation function, it's the datasheet figure with swapped axis
x <- as.vector(points[,x])
y <- as.vector(points[,y])
points = list(x=x, y=y)
pointsrev = list(x=y, y=x)
#the nls (Nonlinear Least Squares) it's used to perform the power regression on points
#in order to work, nls needs an estimation of staring values
#we use log-log slope estimation to find intitial values
#estimate fit curve initial values
xfirst = head(points$x, n=1)
xlast = tail(points$x, n=1)
yfirst = head(points$y, n=1)
ylast = tail(points$y, n=1)
bstart= log(ylast/yfirst)/log(xlast/xfirst)
astart = yfirst/(xfirst^bstart)
#perform the fit
fit <- nls("y~a*x^b", start=list(a=astart,b=bstart), data=points)
#estimate fitref curve initial values
xfirstrev = head(pointsrev$x, n=1)
xlastrev = tail(pointsrev$x, n=1)
yfirstrev = head(pointsrev$y, n=1)
ylastrev = tail(pointsrev$y, n=1)
bstartrev = log(ylastrev/yfirstrev)/log(xlastrev/xfirstrev)
astartrev = yfirstrev/(xfirstrev^bstartrev)
fitrev <- nls("y~a*x^b", start=list(a=astartrev,b=bstartrev), data=pointsrev)
#plot fit curve (log-log scale)
fiteq = function(x){coef(fit)["a"]*x^(coef(fit)["b"])}
plot(points, log="xy", col="blue", xlab="ppm", ylab="Rs/Ro", xlim=xlim, ylim=ylim, panel.first=grid(equilogs=FALSE))
curve(fiteq, col="red", add=TRUE)
#plot fitrev curve (log-log scale)
fiteqrev = function(x){coef(fitrev)["a"]*x^(coef(fitrev)["b"])}
plot(pointsrev, log="xy", col="blue", xlab="Rs/Ro", ylab="ppm", xlim=ylim, ylim=xlim, panel.first=grid(equilogs=FALSE))
curve(fiteqrev, col="red", add=TRUE)
#plot fit curve (linear scale)
fiteq = function(x){coef(fit)["a"]*x^(coef(fit)["b"])}
plot(points, col="blue", xlab="ppm", ylab="Rs/Ro", panel.first=grid(equilogs=FALSE))
curve(fiteq, col="red", add=TRUE)
#plot fitrev curve (linear scale)
fiteqrev = function(x){coef(fitrev)["a"]*x^(coef(fitrev)["b"])}
plot(pointsrev, col="blue", xlab="Rs/Ro", ylab="ppm", panel.first=grid(equilogs=FALSE))
curve(fiteqrev, col="red", add=TRUE)
#estimate min Rs/Ro
cat("\nCorrelation function coefficients")
cat("\nEstimated a\n")
cat(" ")
cat(coef(fitrev)["a"])
cat("\nEstimated b\n")
cat(" ")
cat(coef(fitrev)["b"])
cat("\n")
#estimate min Rs/Ro
if (minppm != 0) {
minRsRo = (maxppm/coef(fitrev)["a"])^(1/coef(fitrev)["b"])
cat("\nEstimated min Rs/Ro\n")
cat(" ")
cat(minRsRo)
cat("\n")
}
#estimate max Rs/Ro
if (maxppm != 0) {
maxRsRo = (minppm/coef(fitrev)["a"])^(1/coef(fitrev)["b"])
cat("\nEstimated max Rs/Ro\n")
cat(" ")
cat(maxRsRo)
cat("\n")
}
#estimate Ro
if (mppm != 0 && mres != 0) {
Ro = mres*(coef(fitrev)["a"]/mppm)^(1/coef(fitrev)["b"])
cat("\nEstimated Ro\n")
cat(" ")
cat(Ro)
cat("\n")
}