Title: | Higher-Order Spectral Analysis |
---|---|
Description: | Higher-order spectra or polyspectra of time series, such as bispectrum and bicoherence, have been investigated in abundant literature and applied to problems of signal detection in a wide range of fields. This package aims to provide a simple API to estimate and analyze them. The current implementation is based on Brillinger and Irizarry (1998) <doi:10.1016/S0165-1684(97)00217-X> for estimating bispectrum or bicoherence, Lii and Helland (1981) <doi:10.1145/355958.355961> for cross-bispectrum, and Kim and Powers (1979) <doi:10.1109/TPS.1979.4317207> for cross-bicoherence. |
Authors: | Takeshi Abe [aut, cre] |
Maintainer: | Takeshi Abe <[email protected]> |
License: | GPL-3 |
Version: | 0.3.0 |
Built: | 2025-02-28 05:48:48 UTC |
Source: | https://github.com/tabe/rhosa |
Estimate magnitude-squared bicoherence from given real- or complex-valued time series data.
bicoherence( data, window_function = NULL, mc = FALSE, mc_cores = getOption("mc.cores", 2L), alpha = 0.05, p_adjust_method = "BH" )
bicoherence( data, window_function = NULL, mc = FALSE, mc_cores = getOption("mc.cores", 2L), alpha = 0.05, p_adjust_method = "BH" )
data |
Given time series, as a data frame or matrix with which columns correspond to sampled stretches. |
window_function |
A window function's name for tapering. Defaults to
Currently the following window functions are available: Hamming window ("hamming"), Hann window ("hann"), and Blackman window ("blackman"). |
mc |
If |
mc_cores |
The number of cores in use for parallel computation, passed
|
alpha |
The alpha level of the hypotesis test. Defaults to 0.05. |
p_adjust_method |
The correction method for p-values, given to
|
A data frame including the following columns:
The first elements of frequency pairs.
The second elements of frequency pairs.
The estimate of magnitude-squared bicoherence at the respective frequency pair.
The (corrected, if requested) p-value for hypothesis testing under null hypothesis that bicoherence is 0.
TRUE if the null hypothesis of the above hypothesis test is rejected
with given alpha
level.
Brillinger, D.R. and Irizarry, R.A. "An investigation of the second- and higher-order spectra of music." Signal Processing, Volume 65, Issue 2, 30 March 1998, Pages 161-179.
f <- function(x) { sin(2 * x) + sin(3 * x + 1) + sin(2 * x) * sin(3 * x + 1) } v <- sapply(seq_len(1280), f) + rnorm(1280) m <- matrix(v, nrow = 128) bc1 <- bicoherence(m) bc2 <- bicoherence(m, "hamming") bc3 <- bicoherence(m, "hann", mc = TRUE, mc_cores = 1L)
f <- function(x) { sin(2 * x) + sin(3 * x + 1) + sin(2 * x) * sin(3 * x + 1) } v <- sapply(seq_len(1280), f) + rnorm(1280) m <- matrix(v, nrow = 128) bc1 <- bicoherence(m) bc2 <- bicoherence(m, "hamming") bc3 <- bicoherence(m, "hann", mc = TRUE, mc_cores = 1L)
Calculate the biperiodogram of real-valued time series
biperiodogram( x, dft_given = FALSE, mc = FALSE, mc_cores = getOption("mc.cores", 2L) )
biperiodogram( x, dft_given = FALSE, mc = FALSE, mc_cores = getOption("mc.cores", 2L) )
x |
Given time series (or its DFT), as a data frame or matrix with which columns correspond to sampled stretches |
dft_given |
If TRUE, suppose that DFTs are given instead of time series
data and skip the fast fourier transform. Default: |
mc |
If |
mc_cores |
The number of cores in use for parallel computation, passed
|
A list with names
The first elements of frequency pairs.
The second elements of frequency pairs.
The biperiodogram as a matrix. Each of its rows is for a frequency pair; its columns correspond to stretches.
Hinich, M.J., 1994. Higher order cumulants and cumulant spectra. Circuits Systems and Signal Process 13, 391–402. doi:10.1007/BF01183737
f <- function(x) { sin(2 * x) + sin(3 * x + 1) + sin(2 * x) * sin(3 * x + 1) } v <- sapply(seq_len(1280), f) + rnorm(1280) m <- matrix(v, nrow = 128) bp <- biperiodogram(m) m2 <- stats::mvfft(m) bp2 <- biperiodogram(m2, dft_given = TRUE)
f <- function(x) { sin(2 * x) + sin(3 * x + 1) + sin(2 * x) * sin(3 * x + 1) } v <- sapply(seq_len(1280), f) + rnorm(1280) m <- matrix(v, nrow = 128) bp <- biperiodogram(m) m2 <- stats::mvfft(m) bp2 <- biperiodogram(m2, dft_given = TRUE)
Estimate bispectrum from real- or complex-valued time series data.
bispectrum( data, window_function = NULL, mc = FALSE, mc_cores = getOption("mc.cores", 2L) )
bispectrum( data, window_function = NULL, mc = FALSE, mc_cores = getOption("mc.cores", 2L) )
data |
Given time series, as a data frame or matrix with which columns correspond to sampled stretches. |
window_function |
A window function's name for tapering. Defaults to
Currently the following window functions are available: Hamming window ("hamming"), Hann window ("hann"), and Blackman window ("blackman"). |
mc |
If |
mc_cores |
The number of cores in use for parallel computation, passed
|
A data frame including the following columns:
The first elements of frequency pairs.
The second elements of frequency pairs.
The estimated bispectrum at each frequency pair.
Brillinger, D.R. and Irizarry, R.A. "An investigation of the second- and higher-order spectra of music." Signal Processing, Volume 65, Issue 2, 30 March 1998, Pages 161-179.
f <- function(x) { sin(2 * x) + sin(3 * x + 1) + sin(2 * x) * sin(3 * x + 1) } v <- sapply(seq_len(1280), f) + rnorm(1280) m <- matrix(v, nrow = 128) bs1 <- bispectrum(m) bs2 <- bispectrum(m, "hamming") bs3 <- bispectrum(m, "blackman", mc = TRUE, mc_cores = 1L)
f <- function(x) { sin(2 * x) + sin(3 * x + 1) + sin(2 * x) * sin(3 * x + 1) } v <- sapply(seq_len(1280), f) + rnorm(1280) m <- matrix(v, nrow = 128) bs1 <- bispectrum(m) bs2 <- bispectrum(m, "hamming") bs3 <- bispectrum(m, "blackman", mc = TRUE, mc_cores = 1L)
Estimate cross-bicoherence from three real-valued time series data.
cross_bicoherence( x, y, z = y, dft_given = FALSE, mc = FALSE, mc_cores = getOption("mc.cores", 2L) )
cross_bicoherence( x, y, z = y, dft_given = FALSE, mc = FALSE, mc_cores = getOption("mc.cores", 2L) )
x |
Given 1st time series, as a data frame or matrix with which columns correspond to sampled stretches. |
y |
Given 2nd time series, with the same dimension as x. |
z |
Optional 3rd time series, with the same dimension as x (and thus as y).
If omitted, |
dft_given |
If TRUE, suppose that DFTs are given instead of time series
data and skip the fast fourier transform. Default: |
mc |
If |
mc_cores |
The number of cores in use for parallel computation, passed
|
A data frame including the following columns:
The first elements of frequency pairs.
The second elements of frequency pairs.
The estimated value of magnitude-squared cross-bicoherence at the respective frequency pair.
Kim, Y.C., Powers, E.J., 1979. Digital Bispectral Analysis and Its Applications to Nonlinear Wave Interactions. IEEE Trans. Plasma Sci. 7, 120–131. https://doi.org/10.1109/TPS.1979.4317207
x <- seq_len(1280) v1 <- sapply(x, function(x) {sin(2 * x)}) + rnorm(1280) v2 <- sapply(x, function(x) {sin(3 * x + 1)}) + rnorm(1280) v3 <- sapply(x, function(x) {cos(2 * x) * cos(3 * x + 1)}) + rnorm(1280) m1 <- matrix(v1, nrow = 128) m2 <- matrix(v2, nrow = 128) m3 <- matrix(v3, nrow = 128) xbc1 <- cross_bicoherence(m1, m2, m3) d1 <- stats::mvfft(m1) d2 <- stats::mvfft(m2) d3 <- stats::mvfft(m3) xbc2 <- cross_bicoherence(d1, d2, d3, dft_given = TRUE) xbc3 <- cross_bicoherence(d1, d2, d3, dft_given = TRUE, mc = TRUE, mc_cores = 1L)
x <- seq_len(1280) v1 <- sapply(x, function(x) {sin(2 * x)}) + rnorm(1280) v2 <- sapply(x, function(x) {sin(3 * x + 1)}) + rnorm(1280) v3 <- sapply(x, function(x) {cos(2 * x) * cos(3 * x + 1)}) + rnorm(1280) m1 <- matrix(v1, nrow = 128) m2 <- matrix(v2, nrow = 128) m3 <- matrix(v3, nrow = 128) xbc1 <- cross_bicoherence(m1, m2, m3) d1 <- stats::mvfft(m1) d2 <- stats::mvfft(m2) d3 <- stats::mvfft(m3) xbc2 <- cross_bicoherence(d1, d2, d3, dft_given = TRUE) xbc3 <- cross_bicoherence(d1, d2, d3, dft_given = TRUE, mc = TRUE, mc_cores = 1L)
Estimate cross-bispectrum from three real-valued time series data.
cross_bispectrum( x, y, z = y, dft_given = FALSE, mc = FALSE, mc_cores = getOption("mc.cores", 2L) )
cross_bispectrum( x, y, z = y, dft_given = FALSE, mc = FALSE, mc_cores = getOption("mc.cores", 2L) )
x |
Given 1st time series, as a data frame or matrix with which columns correspond to sampled stretches. |
y |
Given 2nd time series, with the same dimension as x. |
z |
Optional 3rd time series, with the same dimension as x (and thus as y).
If omitted, |
dft_given |
If TRUE, suppose that DFTs are given instead of time series
data and skip the fast fourier transform. Default: |
mc |
If |
mc_cores |
The number of cores in use for parallel computation, passed
|
A data frame including the following columns:
The first elements of frequency pairs.
The second elements of frequency pairs.
The estimated cross-bispectrum at each frequency pair.
K. S. Lii and K. N. Helland. 1981. Cross-Bispectrum Computation and Variance Estimation. ACM Trans. Math. Softw. 7, 3 (September 1981), 284–294. DOI:https://doi.org/10.1145/355958.355961
x <- seq_len(1280) v1 <- sapply(x, function(x) {sin(2 * x)}) + rnorm(1280) v2 <- sapply(x, function(x) {sin(3 * x + 1)}) + rnorm(1280) v3 <- sapply(x, function(x) {cos(2 * x) * cos(3 * x + 1)}) + rnorm(1280) m1 <- matrix(v1, nrow = 128) m2 <- matrix(v2, nrow = 128) m3 <- matrix(v3, nrow = 128) xbs1 <- cross_bispectrum(m1, m2, m3) d1 <- stats::mvfft(m1) d2 <- stats::mvfft(m2) d3 <- stats::mvfft(m3) xbs2 <- cross_bispectrum(d1, d2, d3, dft_given = TRUE) xbs3 <- cross_bispectrum(d1, d2, d3, dft_given = TRUE, mc = TRUE, mc_cores = 1L)
x <- seq_len(1280) v1 <- sapply(x, function(x) {sin(2 * x)}) + rnorm(1280) v2 <- sapply(x, function(x) {sin(3 * x + 1)}) + rnorm(1280) v3 <- sapply(x, function(x) {cos(2 * x) * cos(3 * x + 1)}) + rnorm(1280) m1 <- matrix(v1, nrow = 128) m2 <- matrix(v2, nrow = 128) m3 <- matrix(v3, nrow = 128) xbs1 <- cross_bispectrum(m1, m2, m3) d1 <- stats::mvfft(m1) d2 <- stats::mvfft(m2) d3 <- stats::mvfft(m3) xbs2 <- cross_bispectrum(d1, d2, d3, dft_given = TRUE) xbs3 <- cross_bispectrum(d1, d2, d3, dft_given = TRUE, mc = TRUE, mc_cores = 1L)
Generate test signals which involve three oscillators described in Kim and Powers (1979).
kim_and_powers_model( fbfN = 0.22, fcfN = 0.375, fdfN = fbfN + fcfN, num_points = 128, num_records = 64, noise_sd = 0.1, phase_coherence = TRUE, product_term = FALSE )
kim_and_powers_model( fbfN = 0.22, fcfN = 0.375, fdfN = fbfN + fcfN, num_points = 128, num_records = 64, noise_sd = 0.1, phase_coherence = TRUE, product_term = FALSE )
fbfN |
b's frequency divided by the Nyquist frequency; |
fcfN |
c's frequency divided by the Nyquist frequency; |
fdfN |
d's frequency divided by the Nyquist frequency; |
num_points |
The number of sampling points in a record; 128 by default. |
num_records |
The number of records; 64 by default. |
noise_sd |
The standard deviation of a Gaussian noise perturbing samples; 0.1 (-20dB) by default. |
phase_coherence |
If TRUE (default), the phase coherence in the signal d is on; otherwise off. |
product_term |
If TRUE, the product of b and c is included in the model; FALSE by default. |
This function produces a list of numeric vectors; its each element represents a test signal in which three oscillators b, c, and d are superimposed. The ratio of the frequency of b (f1) to the Nyquist frequency is 0.220 and the ratio of the frequency of c (f2) to the Nyquist frequency is 0.375, by default. The d's frequency f3 is equal to f1 + f2 unless specified otherwise. Optionally the product of b and c is also added to signals.
A matrix of num_points
rows x num_records
columns.
data <- kim_and_powers_model()
data <- kim_and_powers_model()
Estimate false discovery rate by fitting scaled chi-squared distribution as an empirical null of cross-bicoherence with Schwartzman's mode matching method.
mode_matching(xbc, t_max = NULL, d = 0.001)
mode_matching(xbc, t_max = NULL, d = 0.001)
xbc |
cross-bicoherence, returned from |
t_max |
the upper limit of interval
, see the reference. |
d |
the bin width of the tuning parameter. |
Schwartzman, Armin. “Empirical Null and False Discovery Rate Inference for Exponential Families.” Annals of Applied Statistics 2, no. 4 (December 2008): 1332–59. https://doi.org/10.1214/08-AOAS184.
Simulate observations by a three-channel model of quadratic phase coupling.
three_channel_model( f1, f2, f3, num_samples = 256, num_observations = 100, input_freq = c(1.2, 0.7, 0.8), noise_sd = 1 )
three_channel_model( f1, f2, f3, num_samples = 256, num_observations = 100, input_freq = c(1.2, 0.7, 0.8), noise_sd = 1 )
f1 |
A function of period |
f2 |
A function of period |
f3 |
A function of period |
num_samples |
The number of sampling points in an observation. |
num_observations |
The number of observations. |
input_freq |
The scaling factor for the frequencies of input periodic functions. It can be a scalar or a vector of length three. If a scalar is given, the same frequency is used for all of inputs. |
noise_sd |
The standard deviation of a Gaussian noise perturbing samples. It can be a scalar or a vector of length three. If a scalar is given, the same value is used for all of noises. Giving 0 is possible and specifies no noise. |
Given three periodic functions, this function generates a list of three data
frames in which each column represents a simulated observation at a channel.
The phase is chosen at random from for each
observation and each channel.
A list of six data frames:
i1
, i2
, i3
, o1
, o2
, and o3
.
Each element has num_observations
columns and num_samples
rows.
i1
, i2
, and i3
are observations of input signals;
o1
, o2
, and o3
are of output.
sawtooth <- function(r) { x <- r/(2*pi) x - floor(x) - 0.5 } data <- three_channel_model(cos, sin, sawtooth, input_freq = c(0.2, 0.3, 0.4), noise_sd = 0.9)
sawtooth <- function(r) { x <- r/(2*pi) x - floor(x) - 0.5 } data <- three_channel_model(cos, sin, sawtooth, input_freq = c(0.2, 0.3, 0.4), noise_sd = 0.9)