[R-gregmisc-users] SF.net SVN: r-gregmisc: [1292] trunk/SII
Brought to you by:
warnes
From: <wa...@us...> - 2008-06-14 20:03:34
|
Revision: 1292 http://r-gregmisc.svn.sourceforge.net/r-gregmisc/?rev=1292&view=rev Author: warnes Date: 2008-06-14 13:03:25 -0700 (Sat, 14 Jun 2008) Log Message: ----------- Implement 'interpolate' as a separate argument, and when 'interpolate=TRUE' replace NA's in any provided measurement vector. Modified Paths: -------------- trunk/SII/R/sii.R trunk/SII/man/sii.Rd Modified: trunk/SII/R/sii.R =================================================================== --- trunk/SII/R/sii.R 2008-06-14 20:01:31 UTC (rev 1291) +++ trunk/SII/R/sii.R 2008-06-14 20:03:25 UTC (rev 1292) @@ -5,7 +5,6 @@ loss, freq, method=c( - "interpolate", "critical", "equal-contributing", "one-third octave", @@ -20,7 +19,8 @@ "ShortPassage", "SPIN", "CST" - ) + ), + interpolate=FALSE ) { ## Assumptions: @@ -28,7 +28,7 @@ ## freq: If provided, frequencies in Hz at which speech, noise, and/or ## threshold values are measured. If missing, frequencies will ## corresponding to those utilized by the specified method. - ## Note that, frequencies must be provided for method="interpolate" + ## Note that, frequencies must be provided if "interpolate=TRUE" ## ## speech: Speech level in dB at each frequency, or one of levels of ## stated vocal effort ("raised", "normal", "loud", "shout") @@ -49,7 +49,6 @@ ## Get the appropriate table of constants data.name <- switch(method, - "interpolate"="critical", "critical"="critical", "one-third octave"="onethird", "equal-contributing"="equal", @@ -79,8 +78,8 @@ ## Handle missing freq if(missing(freq)) - if(method=="interpolate") - stop("`freq' must be specified when `method=\"interpolate\"'") + if(interpolate) + stop("`freq' must be specified when `interpolate=TRUE'") else freq <- table$fi @@ -117,29 +116,13 @@ ) stop("`noise', `threshold', and `loss` must have the same length as `freq'.") - ## Check for & handle missing values - nas <- (is.na(noise) | is.na(threshold) | is.na(loss) | is.na(freq) ) + ## Check for missing values + any.nas <- any(is.na(c(noise,threshold,loss,freq)) ) if(!const.speech) - nas <- nas | is.na(speech) + any.nas <- any.nas || any(is.na(speech)) + if(any.nas && !interpolate) + stop("Missing values only permitted when `interpolate=TRUE'") - if(any(nas) ) - { - if(method=="interpolate") - { - warning(sum(nas), " missing values omitted.") - if(!const.speech) - speech <- speech [!nas] - - noise <- noise [!nas] - threshold <- threshold[!nas] - loss <- loss [!nas] - freq <- freq [!nas] - - } - else - stop("Missing values only permitted for method `interpolate'") - } - ## Sort values into frequency order ord <- order(freq) freq <- freq [ord] @@ -155,30 +138,41 @@ retval <- list() retval$call <- match.call() retval$orig <- list( freq, speech, noise, threshold, loss ) + - if(method=="interpolate") + if(interpolate) { - approx.l <- function(x,y) - { + + sii.freqs <- table[,"fi"] + + approx.l <- function(obs.freq,value,target.freq) + { + nas <- is.na(value) + if(any(nas)) + { + warning(sum(nas), " missing values ommitted") + value <- value[!nas] + obs.freq <- obs.freq[!nas] + } + tmp <- approx( - log(x), - y, - log(sii.freqs), + x=log10(obs.freq), + y=value, + xout=log10(target.freq), method="linear", rule=2 ) tmp$y } - - sii.freqs <- table[,"fi"] + #debug(approx.l) ## Interpolate unobserved frequencies - noise <- approx.l(freq, noise) - threshold <- approx.l(freq, threshold) - loss <- approx.l(freq, loss) + noise <- approx.l(freq, noise, sii.freqs) + threshold <- approx.l(freq, threshold, sii.freqs) + loss <- approx.l(freq, loss, sii.freqs) if(!const.speech) - speech <- approx.l(freq, speech) + speech <- approx.l(freq, speech, sii.freqs) freq <- sii.freqs } @@ -231,7 +225,6 @@ ## 4.3.2.3 slope per octive of spread of masking, Ci if(method=="critical" || - method=="interpolate" || method=="equal-contributing") { sii.tab$"Ci" <- -80 + 0.6*( sii.tab$"Bi" + 10*log10(table$"hi" - table$"li") ) @@ -242,7 +235,6 @@ } if(method=="critical" || - method=="interpolate" || method=="equal-contributing") { Zifun <- function(i) Modified: trunk/SII/man/sii.Rd =================================================================== --- trunk/SII/man/sii.Rd 2008-06-14 20:01:31 UTC (rev 1291) +++ trunk/SII/man/sii.Rd 2008-06-14 20:03:25 UTC (rev 1292) @@ -12,10 +12,11 @@ \usage{ sii(speech = c("normal", "raised", "loud", "shout"), noise, threshold, loss, freq, - method = c("interpolate", "critical", "equal-contributing", + method = c("critical", "equal-contributing", "one-third octave", "octave"), importance = c("SII", "NNS", "CID22", "NU6", "DRT", - "ShortPassage", "SPIN", "CST")) + "ShortPassage", "SPIN", "CST"), + interpolate=FALSE) \method{print}{SII}(x, digits=2, ...) \method{plot}{SII}(x, ...) \method{summary}{SII}(object, digits=2, ...) @@ -38,21 +39,24 @@ defaults to 0 dB for each frequency.} \item{freq}{Vector of frequencies for which \code{speech}, \code{noise}, \code{threshold}, and/or \code{loss} are specified. - If \code{method="interpolate"}, \code{freq} must be specified. + If \code{interpolate=TRUE}, \code{freq} must be specified. Otherwise, it must either match the required value for SII calculation method given by argument \code{method}, or be missing, in which case it will default to the values required for the specified method.} \item{method}{A character string specifying the SII calculation method ("critical", "one-third octave", "equal-contributing", - "octave"), or \code{"interpolate"} in which case the critical-band - method will be applied, with interpolation from the provided - frequencies to the required 21 frequency values.} + "octave")} \item{importance}{Either a numeric vector providing $F_i$, the - transfer function (importance weights) at each frequency, or a character - string indicating which transfer function to employ ("SII", "NNS", - "CID22", "NU6", "DRT", "ShortPassage", "SPIN", "CST"). Defaults to - the standard SII transfer function, \code{importance="SII"}.} + transfer function (importance weights) at each frequency, or a + character string indicating which transfer function to employ + ("SII", "NNS", "CID22", "NU6", "DRT", "ShortPassage", "SPIN", + "CST"). Defaults to the standard SII transfer function, + \code{importance="SII"}.} + \item{interpolate}{Logical flag indicating whether to interpolate + from the provide measurement values and frequencies to those + required by the specified method via linear interpolation on the log + scale.} \item{object,x}{SII object} \item{digits}{Number of digits to display} \item{...}{Optional arguments to \code{print}, \code{summary}, and @@ -78,13 +82,11 @@ Further, this implementation provides a mechanism for interpolating/extrapolating available measurements to those required - for calculating the critical band procedure (via the argument - \code{method="interpolate"}). Interpolation is accomplished using - linear interpolation (on log-scaled data) to the frequencies - required for the critical-band SII procedure. Interpolation is - performed (if necessary) for \code{noise}, \code{threshold}, and - \code{loss}. - + for the specified calculation procedure. When + \code{interpolate=TRUE}, required values for \code{speech}, + \code{noise}, \code{threshold}, and \code{loss} will be computed + using linear interpolation (on log-scaled data). In this case, + missing values may be provided and will be appropriately interpolated. } \value{ The return value is an object of class SII, containing the following @@ -196,7 +198,9 @@ speech="raised", threshold=c(25,25,30,35,45,45,55,60), freq=c(250, 500, 1000, 2000, 3000, 4000, 6000, 8000), - importance="NU6" + method="critical", + importance="NU6", + interpolate=TRUE ) sii.left This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |