Skip to content

Commit

Permalink
add new function for calculating points from wavenumber windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
wincowgerDEV committed Jan 4, 2024
1 parent dc2a11e commit 8f8dd47
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ S3method(as_OpenSpecy,list)
S3method(c_spec,OpenSpecy)
S3method(c_spec,default)
S3method(c_spec,list)
S3method(calc_window_points,OpenSpecy)
S3method(calc_window_points,default)
S3method(collapse_spec,OpenSpecy)
S3method(collapse_spec,default)
S3method(conform_spec,OpenSpecy)
Expand Down Expand Up @@ -71,6 +73,7 @@ export(ai_classify)
export(as_OpenSpecy)
export(as_hyperSpec)
export(c_spec)
export(calc_window_points)
export(check_OpenSpecy)
export(check_lib)
export(collapse_spec)
Expand Down
37 changes: 35 additions & 2 deletions R/smooth_intens.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#' @param polynomial polynomial order for the filter
#' @param window number of data points in the window, filter length (must be
#' odd).
#' @param wavenum_width the width of the window you want in wavenumbers.
#' @param derivative the derivative order if you want to calculate the
#' derivative. Zero (default) is no derivative.
#' @param abs logical; whether you want to calculate the absolute value of the
Expand All @@ -26,11 +27,17 @@
#'
#' @return
#' \code{smooth_intens()} returns an \code{OpenSpecy} object.
#'
#'
#' \code{calc_window_points()} returns a single numberic vector object of the
#' number of points needed to fill the window and can be passed to \code{smooth_intens()}.
#' For many applications, this is more reusable than specifying a static number of points.
#'
#' @examples
#' data("raman_hdpe")
#'
#' smooth_intens(raman_hdpe)
#'
#' smooth_intens(raman_hdpe, window = calc_window_points(x = raman_hdpe, wavenum_width = 70))
#'
#' @author
#' Win Cowger, Zacharias Steinmetz
Expand Down Expand Up @@ -59,7 +66,8 @@ smooth_intens.default <- function(x, ...) {
#' @rdname smooth_intens
#'
#' @export
smooth_intens.OpenSpecy <- function(x, polynomial = 3, window = 11,
smooth_intens.OpenSpecy <- function(x, polynomial = 3,
window = 11,
derivative = 1, abs = TRUE,
make_rel = TRUE, ...) {
filt <- x$spectra[, lapply(.SD, .sgfilt, p = polynomial, n = window,
Expand All @@ -70,6 +78,31 @@ smooth_intens.OpenSpecy <- function(x, polynomial = 3, window = 11,
return(x)
}

#' @export
calc_window_points <- function(x, ...) {
UseMethod("calc_window_points")
}

#'
#' @export
calc_window_points.default <- function(x, ...) {
stop("object 'x' needs to be of class 'OpenSpecy'")
}

#'
#' @export
calc_window_points.OpenSpecy <- function(x, wavenum_width = 70){
raw_points <- floor(wavenum_width/spec_res(x))
if(raw_points %% 2 == 0){
raw_points <- raw_points - 1
}
if(raw_points > length(x$wavenumber)){
stop("The wavenum_width must be shorter than the full spectrum.")
}
return(raw_points)
}


#' @importFrom signal filter sgolay
.sgfilt <- function(y, p, n, m, abs = F, ...) {
out <- signal::filter(filt = sgolay(p = p, n = n, m = m, ...), x = y)
Expand Down
8 changes: 8 additions & 0 deletions man/smooth_intens.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions tests/testthat/test-smooth_intens.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ test_that("smooth_intens() handles input errors correctly", {
smooth_intens(1:1000) |> expect_error()
})

test_that("calc_window_points() will return consistent values", {
calc_window_points(raman_hdpe, 70) |> expect_equal(27)
calc_window_points(raman_hdpe, 50) |> expect_equal(19)
calc_window_points(raman_hdpe, 140) |> expect_equal(55)
calc_window_points(raman_hdpe, 10000) |> expect_error()
})

test_that("smooth_intens() works as expected", {
smt <- smooth_intens(raman_hdpe, polynomial = 3) |> expect_silent()

Expand Down

0 comments on commit 8f8dd47

Please sign in to comment.