buffer {gsignal}R Documentation

Buffer signal vector into matrix of data segments

Description

Partition a signal vector into nonoverlapping, overlapping, or underlapping data segments.

Usage

buffer(x, n, p = 0, opt, zopt = FALSE)

Arguments

x

The data to be buffered.

n

The number of rows in the produced data buffer. This is an positive integer value and must be supplied.

p

An integer less than n that specifies the under- or overlap between column in the data frame. Default 0.

opt

In the case of an overlap, opt can be either a vector of length p or the string 'nodelay'. If opt is a vector, then the first p entries in y will be filled with these values. If opt is the string 'nodelay', then the first value of y corresponds to the first value of x. In the case of an underlap, opt must be an integer between 0 and -p. The represents the initial underlap of the first y. The default value for opt the vector matrix (0L, 1, p) in the case of an overlap, or 0 otherwise.

zopt

Logical. If TRUE, return values for z and opt in addition to y. Default is FALSE (return only y).

Details

y <- buffer(x, n) partitions a signal vector x of length L into nonoverlapping data segments of length n. Each data segment occupies one column of matrix output y, which has n rows and ceil(L / n) columns. If L is not evenly divisible by n, the last column is zero-padded to length n.

y <- buffer(x, n, p) overlaps or underlaps successive frames in the output matrix by p samples.

In y <- buffer(x, n, p, opt), opt specifies a vector of samples to precede x[1] in an overlapping buffer, or the number of initial samples to skip in an underlapping buffer.

The opt option is especially useful when the current buffering operation is one in a sequence of consecutive buffering operations. To maintain the desired frame underlap from one buffer to the next, opt should equal the difference between the total number of points to skip between frames (p) and the number of points that were available to be skipped in the previous input to buffer. If the previous input had fewer than p points that could be skipped after filling the final frame of that buffer, the remaining opt points need to be removed from the first frame of the current buffer. See Continuous Buffering for an example of how this works in practice.

buf <- buffer(..., zopt = TRUE) returns the last p samples of a overlapping buffer in output buf$opt. In an underlapping buffer, buf$opt is the difference between the total number of points to skip between frames (-p) and the number of points in x that were available to be skipped after filling the last frame:

In a sequence of buffering operations, the buf$opt output from each operation should be used as the opt input to the subsequent buffering operation. This ensures that the desired frame overlap or underlap is maintained from buffer to buffer, as well as from frame to frame within the same buffer. See Continuous Buffering below for an example of how this works in practice.

Continuous Buffering

In a continuous buffering operation, the vector input to the buffer function represents one frame in a sequence of frames that make up a discrete signal. These signal frames can originate in a frame-based data acquisition process, or within a frame-based algorithm like the FFT.
As an example, you might acquire data from an A/D card in frames of 64 samples. In the simplest case, you could rebuffer the data into frames of 16 samples; buffer with n = 16 creates a buffer of four frames from each 64-element input frame. The result is that the signal of frame size 64 has been converted to a signal of frame size 16; no samples were added or removed.
In the general case where the original signal frame size, L, is not equally divisible by the new frame size, n, the overflow from the last frame needs to be captured and recycled into the following buffer. You can do this by iteratively calling buffer on input x with the zopt parameter set to TRUE. This simply captures any buffer overflow in buf$z, and prepends the data to the subsequent input in the next call to buffer.
Note that continuous buffering cannot be done without the zopt parameter being set to TRUE, because the last frame of y (buf$y in this case) is zero padded, which adds new samples to the signal.
Continuous buffering in the presence of overlap and underlap is handled with the opt parameter, which is used as both an input (opt and output (buf$opt) to buffer. The two examples on this page demonstrate how the opt parameter should be used.

Value

If zopt equals FALSE (the default), this function returns a single numerical array containing the buffered data (y). If zopt equals TRUE, then a list containing 3 variables is returned: y: the buffered data, z: the over or underlap (if any), opt: the over- or underlap that might be used for a future call to buffer to allow continuous buffering.

Author(s)

David Bateman, adb014@gmail.com.
Conversion to R by Geert van Boxtel, G.J.M.vanBoxtel@gmail.com

Examples

## Examples without continuous buffering
y <- buffer(1:10, 5)
y <- buffer(1:10, 4)
y <- buffer(1:30, 7, 3)
y <- buffer(1:30, 7, -3)
y <- buffer(1:30, 7, 3, 'nodelay')

## Continuous buffering examples
# with overlap:
data <- buffer(1:1100, 11)
n <- 4
p <- 1
buf <- list(y = NULL, z = NULL, opt = -5)
for (i in 1:ncol(data)) {
  x <- data[,i]
  buf <- buffer(x = c(buf$z,x), n, p, opt=buf$opt, zopt = TRUE)
}
# with underlap:
data <- buffer(1:1100, 11)
n <- 4
p <- -2
buf <- list(y = NULL, z = NULL, opt = 1)
for (i in 1:ncol(data)) {
  x <- data[,i]
  buf <- buffer(x = c(buf$z,x), n, p, opt=buf$opt, zopt = TRUE)
}


[Package gsignal version 0.3-5 Index]