Advertisement
Guest User

Untitled

a guest
Jun 25th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. # An R script to estimate MQ gas sensors correlation curve and compute Ro, min and max Rs/Ro
  2. #
  3. # Copyright (c) Davide Gironi, 2016
  4. #
  5. # Released under GPLv3.
  6. # Please refer to LICENSE file for licensing information.
  7.  
  8. # How to use this script:
  9. # 1) set limists as datasheet curve ("xlim" and "ylim")
  10. # 2) find out datasheet curve points, and write it out (to "pointsdata")
  11. # 3) measure the sensor resistance (set it to "mres")
  12. # 4) set (to "mres") the know amount of gas for the resistance measure of the previous step
  13.  
  14. library(data.table)
  15.  
  16. #remove old variables
  17. rm(list=ls())
  18.  
  19. #set limists as datasheet curve graph here
  20. #ex.
  21. # xlim = c(10, 1000)
  22. # ylim = c(0.1, 10)
  23. xlim = c(0.1, 1000)
  24. ylim = c(0.1, 1000)
  25.  
  26. #set measured resistance (ohm) at a know gas amount (ppm)
  27. #ex.
  28. # mres = 26954
  29. # mppm = 392
  30. mres = 20000
  31. mppm = 100
  32.  
  33. #extraced datasheet curve points
  34. #each line it's a point on cartesian coordinate system
  35. #ex.
  36. #4.2323, 19.2312
  37. #5.2313, 28.9231
  38. #the useful WebPlotDigitizer app can help you extract points from the graph
  39. pointsdata = "
  40. YOUR_VALUES_HERE
  41. "
  42.  
  43. #load points using fread
  44. setnames(points <- fread(pointsdata, sep=",", sep2="\n"), c("x","y"))
  45.  
  46. #set named list of points, and swapped list of points
  47. #points will be used to plot and compute values as datasheet figure
  48. #pointsrev will be used to plot and compute values for the correlation function, it's the datasheet figure with swapped axis
  49. x <- as.vector(points[,x])
  50. y <- as.vector(points[,y])
  51. points = list(x=x, y=y)
  52. pointsrev = list(x=y, y=x)
  53.  
  54. #the nls (Nonlinear Least Squares) it's used to perform the power regression on points
  55. #in order to work, nls needs an estimation of staring values
  56. #we use log-log slope estimation to find intitial values
  57.  
  58. #estimate fit curve initial values
  59. xfirst = head(points$x, n=1)
  60. xlast = tail(points$x, n=1)
  61. yfirst = head(points$y, n=1)
  62. ylast = tail(points$y, n=1)
  63. bstart= log(ylast/yfirst)/log(xlast/xfirst)
  64. astart = yfirst/(xfirst^bstart)
  65. #perform the fit
  66. fit <- nls("y~a*x^b", start=list(a=astart,b=bstart), data=points)
  67.  
  68. #estimate fitref curve initial values
  69. xfirstrev = head(pointsrev$x, n=1)
  70. xlastrev = tail(pointsrev$x, n=1)
  71. yfirstrev = head(pointsrev$y, n=1)
  72. ylastrev = tail(pointsrev$y, n=1)
  73. bstartrev = log(ylastrev/yfirstrev)/log(xlastrev/xfirstrev)
  74. astartrev = yfirstrev/(xfirstrev^bstartrev)
  75. fitrev <- nls("y~a*x^b", start=list(a=astartrev,b=bstartrev), data=pointsrev)
  76.  
  77. #display fit curve coefficients (log-log scale)
  78. cat("\nFit coefficients\n")
  79. coef(fit)
  80. fiteq = function(x){coef(fit)["a"]*x^(coef(fit)["b"])}
  81. plot(points, log="xy", col="blue", xlab="ppm", ylab="Rs/Ro", xlim=xlim, ylim=ylim, panel.first=grid(equilogs=FALSE))
  82. curve(fiteq, col="red", add=TRUE)
  83.  
  84. #display fitrev curve coefficients (log-log scale)
  85. cat("\nFit coefficients for flipped curve\n")
  86. coef(fitrev)
  87. fiteqrev = function(x){coef(fitrev)["a"]*x^(coef(fitrev)["b"])}
  88. plot(pointsrev, log="xy", col="blue", xlab="Rs/Ro", ylab="ppm", xlim=ylim, ylim=xlim, panel.first=grid(equilogs=FALSE))
  89. curve(fiteqrev, col="red", add=TRUE)
  90.  
  91. #display fit curve coefficients (linear scale)
  92. fiteq = function(x){coef(fit)["a"]*x^(coef(fit)["b"])}
  93. plot(points, col="blue", xlab="ppm", ylab="Rs/Ro", panel.first=grid(equilogs=FALSE))
  94. curve(fiteq, col="red", add=TRUE)
  95.  
  96. #display fitrev curve coefficients (linear scale)
  97. fiteqrev = function(x){coef(fitrev)["a"]*x^(coef(fitrev)["b"])}
  98. plot(pointsrev, col="blue", xlab="Rs/Ro", ylab="ppm", panel.first=grid(equilogs=FALSE))
  99. curve(fiteqrev, col="red", add=TRUE)
  100.  
  101. #estimate Ro
  102. Ro = mres*(coef(fitrev)["a"]/mppm)^(1/coef(fitrev)["b"])
  103. cat("\nEstimated Ro\n")
  104. cat(Ro)
  105. cat("\n")
  106.  
  107. #estimate min Rs/Ro
  108. minppm = xlim[2]
  109. minRsRo = (minppm/coef(fitrev)["a"])^(1/coef(fitrev)["b"])
  110. cat("\nEstimated min Rs/Ro\n")
  111. cat(minRsRo)
  112. cat("\n")
  113.  
  114. #estimate max Rs/Ro
  115. maxppm = xlim[1]
  116. maxRsRo = (maxppm/coef(fitrev)["a"])^(1/coef(fitrev)["b"])
  117. cat("\nEstimated max Rs/Ro\n")
  118. cat(maxRsRo)
  119. cat("\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement