Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

https://hal.archives-ouvertes.fr/hal-04452256
20 February 2024, 14:40:46 UTC
  • Code
  • Branches (0)
  • Releases (1)
  • Visits
    • Branches
    • Releases
      • HEAD
    • e68ff9d
    • /
    • R
    • /
    • f_cs_analyses.R
    Raw File Download

    To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
    Select below a type of object currently browsed in order to display its associated SWHID and permalink.

    • content
    • directory
    • snapshot
    • release
    origin badgecontent badge
    swh:1:cnt:7d645fc473eedde3c3cda107e4ec197d75176b41
    origin badgedirectory badge
    swh:1:dir:a9f1cf2137766b74e143323af9a9fbe49232c39f
    origin badgesnapshot badge
    swh:1:snp:8310261df5798cf39e9eff9e2565153bbefa20cd
    origin badgerelease badge
    swh:1:rel:b076d4e89c69861c65112117ecd2efc6969a7d84

    This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
    Select below a type of object currently browsed in order to generate citations for them.

    • content
    • directory
    • snapshot
    • release
    Generate software citation in BibTex format (requires biblatex-software package)
    Generating citation ...
    Generate software citation in BibTex format (requires biblatex-software package)
    Generating citation ...
    Generate software citation in BibTex format (requires biblatex-software package)
    Generating citation ...
    Generate software citation in BibTex format (requires biblatex-software package)
    Generating citation ...
    f_cs_analyses.R
    ## ---------------------------
    ##
    ## Purpose of script: define functions for part of the app: simple correlational and spectral analyses
    ##
    ## Author: Guillaume Cinkus
    ##
    ## Date Created: 2021-08-09
    ##
    ## Email: guillaume.cinkus@gmail.com
    ##
    ## ---------------------------
    ##
    ## Notes:
    ##   
    ##
    ## ---------------------------
    
    # timestep is either 1 for daily timestep or 24 for hourly timestep
    acspf <- function(discharge, max_lag = 125, timestep = 1) {
      acspf <- list("k" = as.numeric(),
                   "rk" = as.numeric(),
                   "f" = as.numeric(),
                   "sf" = as.numeric(),
                   "mem_ef" = as.numeric(),
                   "reg_time" = as.numeric())
    
      max_lag <- max_lag * timestep
      
      n <- length(discharge)
      acf <- stats::acf(discharge, max_lag)
      
      sf <- rep(NA, max_lag)
      k <- 1:max_lag
      f <- k / (2 * max_lag)
      for (j in 1:max_lag) {
        sum <- 0
        sum <- sum((1 + cos((pi * k) / max_lag)) / 2 * acf$acf[k+1] * cos(2 * pi * f[j] * k))
        sf[j] <- 2 * (1 + 2 * sum)
      }
      
      # slice to max f value = 0.5 for plotting
      f <- (f * timestep)[1:max_lag]
      sf <- (sf / timestep)[1:max_lag]
      
      # calculation of memory effect
      index_l0.2 <- min(which(acf$acf < 0.2))
      index_me <- which.min(c(abs(acf$acf[index_l0.2-1] - 0.2), abs(acf$acf[index_l0.2] - 0.2))) + index_l0.2 - 2
      memory_effect <- acf$lag[index_me] / timestep
      
      # calculation of regulation time
      regulation_time <- (max(sf) / 2) 
      
      # list for export
      acspf$k = acf$lag
      acspf$rk = acf$acf
      acspf$f = f
      acspf$sf = sf
      acspf$mem_ef <- memory_effect
      acspf$reg_time <- regulation_time
      
      return(acspf)
    }
    
    plot_acf <- function(k, rk) {
      x <- data.frame(k = k, rk = rk)
      ggplot(x, aes(k, rk)) +
        geom_hline(yintercept = 0.2, linetype = "dashed", color = "#2d2d2d", alpha = 0.5) +
        geom_line(size = 0.8) +
        theme_bw() +
        ggtitle("Autocorrelation Function") +
        xlab("k [T]") +
        ylab(expression(r[k])) +
        theme(title = element_text(size = 16, color = "#2d2d2d"),
              axis.title = element_text(size = 16, color = "#2d2d2d"),
              axis.text = element_text(size = 14, color = "#2d2d2d"))
    }
    
    plot_spf <- function(f, sf) {
      x <- data.frame(f = f, sf = sf)
      ggplot(x, aes(f, sf)) +
        geom_line(size = 0.8) +
        theme_bw() +
        ggtitle("Variance Density Spectrum") +
        xlab(expression(paste("f [T"^-1, "]"))) +
        ylab(expression(s[f])) +
        coord_cartesian(xlim = c(0, 0.5)) +
        theme(title = element_text(size = 16, color = "#2d2d2d"),
              axis.title = element_text(size = 16, color = "#2d2d2d"),
              axis.text = element_text(size = 14, color = "#2d2d2d"))
    }
    

    back to top

    Software Heritage — Copyright (C) 2015–2026, The Software Heritage developers. License: GNU AGPLv3+.
    The source code of Software Heritage itself is available on our development forge.
    The source code files archived by Software Heritage are available under their own copyright and licenses.
    Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API