From c810ef66bab65a8126714723d007dd2312896cbb Mon Sep 17 00:00:00 2001 From: Taha Ahmed Date: Tue, 15 Mar 2011 23:11:51 +0100 Subject: [PATCH] Updated It2charge() and fixed the relevant parts of amperometry2df(). It2charge() is now explicitly meant to be used only from within other functions, such as amperometry2df(). --- CHI.R | 5 +---- common.R | 31 +++++++++++++++++++++---------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/CHI.R b/CHI.R index 10413f6..82a187f 100644 --- a/CHI.R +++ b/CHI.R @@ -298,10 +298,7 @@ amperometry2df <- function(datafilename, wearea = 1) { ff <- cbind(ff, currentdensity = currentdensity) # Calculate charge densities and differentials charge.df <- It2charge(ff$currentdensity, ff$time) - ff <- cbind(ff, timediff = charge.df$timediff, - chargedensity = charge$charge, - sumchargedensity = charge$sumcharge, - dQdt = charge$dQdt) + ff <- cbind(ff, charge.df) # ### Collect attributes of this experiment # These attributes are specific for each kind of experiment, diff --git a/common.R b/common.R index f63d6fa..3d20cf8 100644 --- a/common.R +++ b/common.R @@ -17,11 +17,22 @@ ################################################## ################### It2charge #################### ################################################## -It2charge <- function (current, time) { - ## Calculates charge etc. for amperometric data - ## current may be a current or a current density - ## (up to the calling function to keep track of which it is) - # Calculate the difference of the time vector +It2charge <- function (time, current) { + ## Description: + ## Calculates cumulative charge, differentials, etc. from + ## amperometric data (current and time). + ## __Intended to be used from within other functions (CHI.R)__ + ## Usage: + ## It2charge(time, current) + ## Arguments: + ## time: a vector with time data. + ## current: a vector of the same length as time, with current data. + ## May be either currents or current densities, no matter. + ## Value: + ## Returns a dataframe with columns: + ## timediff, dIdt, charge, sumcharge + # + # Calculate the time vector difference timediff <- c(time[1], diff(time)) # timediff times the current gives the charge, # since the time vector can be considered as @@ -29,11 +40,11 @@ It2charge <- function (current, time) { # the current with the time elapsed since the last # current measurement (the timediff). charge <- current * timediff - differential <- cumsum(charge) / time - # Prepare the return dataframe - ff <- data.frame(time = time, timediff = timediff, - current = current, charge = charge, - sumcharge = cumsum(charge), dQdt = differential) + dIdt <- current / time + # Return value + ff <- data.frame(timediff = timediff, + dIdt = dIdt, charge = charge, + sumcharge = cumsum(charge)) return(ff) }