cpsd: Cross power spectral density

View source: R/cpsd.R

cpsdR Documentation

Cross power spectral density

Description

Estimates the cross power spectral density (CPSD) of discrete-time signals.

Usage

cpsd(
  x,
  window = nextpow2(sqrt(NROW(x))),
  overlap = 0.5,
  nfft = ifelse(isScalar(window), window, length(window)),
  fs = 1,
  detrend = c("long-mean", "short-mean", "long-linear", "short-linear", "none")
)

csd(
  x,
  window = nextpow2(sqrt(NROW(x))),
  overlap = 0.5,
  nfft = ifelse(isScalar(window), window, length(window)),
  fs = 1,
  detrend = c("long-mean", "short-mean", "long-linear", "short-linear", "none")
)

Arguments

x

input data, specified as a numeric vector or matrix. In case of a vector it represents a single signal; in case of a matrix each column is a signal.

window

If window is a vector, each segment has the same length as window and is multiplied by window before (optional) zero-padding and calculation of its periodogram. If window is a scalar, each segment has a length of window and a Hamming window is used. Default: nextpow2(sqrt(length(x))) (the square root of the length of x rounded up to the next power of two). The window length must be larger than 3.

overlap

segment overlap, specified as a numeric value expressed as a multiple of window or segment length. 0 <= overlap < 1. Default: 0.5.

nfft

Length of FFT, specified as an integer scalar. The default is the length of the window vector or has the same value as the scalar window argument. If nfft is larger than the segment length, (seg_len), the data segment is padded nfft - seg_len zeros. The default is no padding. Nfft values smaller than the length of the data segment (or window) are ignored. Note that the use of padding to increase the frequency resolution of the spectral estimate is controversial.

fs

sampling frequency (Hertz), specified as a positive scalar. Default: 1.

detrend

character string specifying detrending option; one of:

"long-mean"

remove the mean from the data before splitting into segments (default)

"short-mean"

remove the mean value of each segment

"long-linear"

remove linear trend from the data before splitting into segments

"short-linear"

remove linear trend from each segment

"none"

no detrending

Details

cpsd estimates the cross power spectral density function using Welch’s overlapped averaged periodogram method [1].

Value

A list containing the following elements:

freq

vector of frequencies at which the spectral variables are estimated. If x is numeric, power from negative frequencies is added to the positive side of the spectrum, but not at zero or Nyquist (fs/2) frequencies. This keeps power equal in time and spectral domains. If x is complex, then the whole frequency range is returned.

cross

NULL for univariate series. For multivariate series, a matrix containing the squared coherence between different series. Column i + (j - 1) * (j - 2)/2 of coh contains the cross-spectral estimates between columns i and j of x, where i < j.

Note

The function cpsd (and its deprecated alias csd) is a wrapper for the function pwelch, which is more complete and more flexible.

Author(s)

Peter V. Lanspeary, pvl@mecheng.adelaide.edu.au.
Conversion to R by Geert van Boxtel, G.J.M.vanBoxtel@gmail.com.

References

[1] Welch, P.D. (1967). The use of Fast Fourier Transform for the estimation of power spectra: A method based on time averaging over short, modified periodograms. IEEE Transactions on Audio and Electroacoustics, AU-15 (2): 70–73.

Examples

fs <- 1000
f <- 250
t <- seq(0, 1 - 1/fs, 1/fs)
s1 <- sin(2 * pi * f * t) + runif(length(t))
s2 <- sin(2 * pi * f * t - pi / 3) + runif(length(t))
rv <- cpsd(cbind(s1, s2), fs = fs)
plot(rv$freq, 10 * log10(rv$cross), type="l", xlab = "Frequency",
     ylab = "Cross Spectral Density (dB)")


gjmvanboxtel/gsignal documentation built on Nov. 22, 2023, 8:19 p.m.