Calculate dominant frequency for multiple PSDs for a single frequency range

PSDDominantFrequencyForMultipleWindows(
  list.of.windows,
  name.of.col.containing.time.series,
  sampling_frequency,
  x_start,
  x_end,
  x_increment
)

Arguments

list.of.windows

A list of windows (dataframes).

name.of.col.containing.time.series

A string that specifies the name of the column in the windows that correspond to the time series that should be used for making PSD.

sampling_frequency

Numeric value specifying sampling frequency in hertz. If data is sampled once every second, then sampling frequency is 1 Hz. If data is sampled once every 2 seconds, then sampling frequency is 0.5 Hz.

x_start

Numeric value specifying start of x value (frequency) to look at.

x_end

Numeric value specifying end of x value to look at.

x_increment

Numeric value specifying the increment of the x-values to use.

Value

A vector where each element is the dominant frequency of each window.

Examples

#Create a vector of time that represent times where data are sampled. Fs = 100; #sampling frequency in Hz T = 1/Fs; #sampling period L = 1000; #length of time vector t = (0:(L-1))*T; #time vector #First signal #1. 1 Hz with amplitude of 2 S1 <- 2*sin(2*pi*1*t) level1.vals <- rep("a", length(S1)) level2.vals <- rep("1", length(S1)) S1.data.frame <- as.data.frame(cbind(t, S1, level1.vals, level2.vals)) colnames(S1.data.frame) <- c("Time", "Signal", "level1.ID", "level2.ID") S1.data.frame[,"Signal"] <- as.numeric(S1.data.frame[,"Signal"]) #Second signal #1. 1 Hz with amplitude of -4 #2. 2 Hz with amplitude of -2 S2 <- (-4)*sin(2*pi*1*t) - 2*sin(2*pi*2*t); level1.vals <- rep("a", length(S2)) level2.vals <- rep("2", length(S2)) S2.data.frame <- as.data.frame(cbind(t, S2, level1.vals, level2.vals)) colnames(S2.data.frame) <- c("Time", "Signal", "level1.ID", "level2.ID") S2.data.frame[,"Signal"] <- as.numeric(S2.data.frame[,"Signal"]) #Third signal #1. 1 Hz with amplitude of 2 #2. 2 Hz with amplitude of 2 S3 <- 2*sin(2*pi*1*t) + 2*sin(2*pi*2*t); level1.vals <- rep("a", length(S3)) level2.vals <- rep("3", length(S3)) S3.data.frame <- as.data.frame(cbind(t, S3, level1.vals, level2.vals)) colnames(S3.data.frame) <- c("Time", "Signal", "level1.ID", "level2.ID") S3.data.frame[,"Signal"] <- as.numeric(S3.data.frame[,"Signal"]) #Fourth signal #1. 1 Hz with amplitude of -2 S4 <- -2*sin(2*pi*1*t) level1.vals <- rep("b", length(S4)) level2.vals <- rep("3", length(S4)) S4.data.frame <- as.data.frame(cbind(t, S4, level1.vals, level2.vals)) colnames(S4.data.frame) <- c("Time", "Signal", "level1.ID", "level2.ID") S4.data.frame[,"Signal"] <- as.numeric(S4.data.frame[,"Signal"]) windows <- list(S1.data.frame, S2.data.frame, S3.data.frame, S4.data.frame) results <- PSDDominantFrequencyForMultipleWindows(windows, "Signal", Fs, 0, 10, 0.01)