# Buffer

Buffer input sequence to smaller or larger frame size

## Library

Signal Management / Buffers

`dspbuff3`

## Description

The Buffer block always performs frame-based processing. The block redistributes the data in each column of the input to produce an output with a different frame size. Buffering a signal to a larger frame size yields an output with a slower frame rate than the input. For example, consider the following illustration for scalar input.

Buffering a signal to a smaller frame size yields an output with a faster frame rate than the input. For example, consider the following illustration of scalar output.

The block coordinates the output frame size and frame rate of nonoverlapping buffers such that the sample period of the signal is the same at both the input and output: Tso = Tsi.

This block supports triggered subsystems when the block input and output rates are the same.

### Buffering Single Channel Signals

The following table shows the output dimensions of the Buffer block when the input is a single-channel signal. Mo is the value of the Output buffer size parameter.

Input DimensionsOutput Dimensions
1-by-1 (scalar)Mo-by-1
Mi-by-1 (column vector)Mo-by-1

The input frame period is Mi · Tsi, where Mi is the input frame size and Tsi is the input sample period. The output frame period is $\left({M}_{o}-L\right){T}_{si}$, where L is the value of the Buffer overlap parameter and Tsi is the input sample period. When you set the Buffer overlap parameter to Mo – 1, the output frame period equals the input sample period.

### Buffering Multichannel Signals

The following table shows the output dimensions of the Buffer block when the input is a multichannel signal. Mo is the value of the Output buffer size parameter and can be greater or less than the input frame size, Mi. The block buffers each of the N input channels independently.

Input DimensionsOutput Dimensions
1-by-N (row vector)Mo-by-N
Mi-by-N (matrix)Mo-by-N

The input frame period is Mi · Tsi, where Mi is the input frame size and Tsi is the input sample period. The output frame period is $\left({M}_{o}-L\right){T}_{si}$, which equals the sequence sample period when the Buffer overlap is ${M}_{o}-1$. Thus, the output sample period is related to the input sample period by

`${T}_{so}=\frac{\left({M}_{o}-L\right){T}_{si}}{{M}_{i}}$`

### Buffering with Overlap or Underlap

The Buffer overlap parameter, L, specifies the amount of overlap or underlap in each successive output frame. To overlap the data in the buffer, specify a value of L in the range $0\le L<{M}_{o}$, where Mo is the value of the Output buffer size parameter. The block takes L samples (rows) from the current output and repeats them in the next output. In cases of overlap, the block acquires ${M}_{o}-L$ new input samples before propagating the buffered data to the output.

When $L<0$, you are buffering the signal with underlap. The block discards L input samples after the buffer fills and outputs the buffer with period $\left({M}_{o}-L\right){T}_{si}$, which is longer than in the zero-overlap case.

The output frame period is $\left({M}_{o}-L\right){T}_{si}$, which equals the input sequence sample period, Tsi, when the Buffer overlap is ${M}_{o}-1$.

### Latency

Zero-tasking latency means that the first input sample, received at t = 0, appears as the first output sample. In the Simulink® single-tasking mode, the Buffer block has zero-tasking latency for the following special cases:

• Scalar input and output (${M}_{o}={M}_{i}=1$) with zero or negative Buffer overlap ($L\le 0$)

• Input frame size is an integer multiple of the output frame size

`${M}_{i}=k{M}_{o}$`

where k is an integer with zero Buffer overlap ($L=0$); notable cases of this include

• Any input frame size Mi with scalar output (${M}_{o}=1$) and zero Buffer overlap ($L=0$)

• Equal input and output frame sizes (${M}_{o}={M}_{i}$) with zero Buffer overlap ($L=0$)

For all cases of single-tasking operation other than those listed above, the Buffer block's buffer is initialized to the value(s) specified by the Initial conditions parameter. The block reads from this buffer to generate the first D output samples, where

`$D=\left\{\begin{array}{cc}{M}_{o}+L& \left(L\ge 0\right)\\ {M}_{o}& \left(L<0\right)\end{array}$`

The dimensions of the Initial conditions parameter depend on the Buffer overlap, L, and whether the input is single-channel or multichannel:

• When $L\ne 0$, the Initial conditions parameter must be a scalar.

• When $L=0$, the Initial conditions parameter can be a scalar, or it can be a vector with the following constraints:

• For single-channel inputs, the Initial conditions parameter can be a vector of length Mo if Mi is 1, or a vector of length Mi if Mo is 1.

• For multichannel inputs, the Initial conditions parameter can be a vector of length Mo * N if Mi is 1, or a vector of length Mi * N if Mo is 1.

For all multitasking operations, use the `rebuffer_delay` function to compute the exact delay, in samples, that the Buffer block introduces for a given combination of buffer size and buffer overlap.

For general buffering between arbitrary frame sizes, the Initial conditions parameter must be a scalar value, which is then repeated across all elements of the initial output(s). However, in the special case where the input is a 1-by-N row vector, and the output of the block is an Mo-by-N matrix, Initial conditions can be

• An Mo-by-N matrix

• A length-Mo vector to be repeated across all columns of the initial output(s)

• A scalar to be repeated across all elements of the initial output(s)

In the special case where the output is a 1-by-N row vector, which is the result of unbuffering an Mi-by-N matrix, the Initial conditions can be

• A vector containing Mi samples to output sequentially for each channel during the first Mi sample times

• A scalar to be repeated across all elements of the initial output(s)

### Behavior in Enabled Subsystems

The Buffer block cannot be used in an enabled subsystem under the following conditions:

• In a multirate multitasking environment

• When the Buffer overlap parameter is set to a negative value

The Buffer block has an internal reservoir that temporarily stores data. When the Buffer block is used in an enabled subsystem, there is the possibility that the reservoir can overrun or underrun. The block implements safeguards against these occurrences.

Overrun occurs when more data enters into the buffer than it can hold. For example, consider buffering a scalar input to a frame of size three with a buffer that accepts input every second and outputs every three seconds. If you place this buffer inside an enabled subsystem that is disabled every three seconds at t = 3s, t = 6s, and so on, the buffer accumulates data in its internal reservoir without being able to empty it. This condition results in overrun.

Underrun occurs when the buffer runs out of data to output. For example, again consider buffering a scalar input to a frame size of three with a buffer that accepts input every second and outputs every three seconds. If you place this buffer inside an enabled subsystem that is disabled at t = 10s, t = 11s, t = 13s, t = 14s, t = 16s, and t = 17s, its internal reservoir becomes drained, and there is no data to output at t = 18s. This condition results in underrun.

To protect from overrun and underrun, the Buffer block keeps a record of the amount of data in its internal reservoir. When the Buffer block reads data, the amount of data in its reservoir goes up. When data is output from the Buffer block, the amount of data in its reservoir goes down. To protect from overrun, the oldest samples in the reservoir are discarded whenever amount of data in the reservoir is larger than the actual buffer size. To protect from underrun, the most recent samples are repeated whenever an output is due and there is no data in the reservoir.

## Examples

Buffering a two-channel input with overlap

The ex_buffer_tut4 model buffers a two-channel input using an Output buffer size of three and a Buffer overlap of one. The following diagram illustrates the inputs and outputs of the Buffer block.

Notice that the output is delayed by eight samples. This is latency occurs because of the parameter settings in this model, and because the model is running in Simulink multitasking mode. The first eight output samples therefore adopt the value specified for the Initial conditions parameter, which in this case is set to zero. You can use the `rebuffer_delay` function to determine the latency of the Buffer block for any combination of frame size and overlap values.

Buffering a four-channel input with overlap

The ex_buffer_tut3 buffers a four-channel input using a Output buffer size of three and a Buffer overlap of one. The following diagram illustrates the inputs and outputs of the Buffer block.

Notice that the input vectors do not begin appearing at the output until the second row of the second matrix. This is due to latency in the Buffer block. The first output matrix (all zeros in this example) reflects the value of the Initial conditions parameter, while the first row of zeros in the second output is a result of the one-sample overlap between consecutive output frames.

You can use the `rebuffer_delay` function with a frame size of 1 to precisely compute the delay (in samples). For the previous example,

```d = rebuffer_delay(1,3,1) d = 4```

This agrees with the four samples of delay (zeros) per channel shown in the previous figure.

## Dialog Box

Output buffer size

Specify the number of consecutive samples, Mo, from each channel to buffer into the output frame.

Buffer overlap

Specify the number of samples, L, by which consecutive output frames overlap.

Initial conditions

Specify the value of the block's initial output for cases of nonzero latency; a scalar, vector, or matrix.

Treat Mx1 and unoriented sample-based signals as

Specify how the block treats sample-based M-by-1 column vectors and unoriented sample-based vectors of length M. You can select one of the following options:

• `One channel `— When you select this option, the block treats M-by-1 and unoriented sample-based inputs as a column vector (one channel). In this mode, the output of the block is an Mo-by-1 column vector, where Mo is the Output buffer size.

• ```M channels (this choice will be removed – see release notes)``` — When you select this option, the block treats M-by-1 and unoriented sample-based inputs as a 1-by-M row vector. Because the block always does frame-based processing, the block interprets the 1-by-M row vector as M individual channels. In this mode, the output of the block is an Mo-by-M matrix, where Mo is the Output buffer size, and M is the length of the sample-based input vector.

 Note:   This parameter will be removed in a future release. At that time, the Buffer block will always perform frame-based processing.

## Supported Data Types

Port

Supported Data Types

Input

• Double-precision floating point

• Single-precision floating point

• Fixed point

• Boolean

• 8-, 16-, and 32-bit signed integers

• 8-, 16-, and 32-bit unsigned integers

Output

• Double-precision floating point

• Single-precision floating point

• Fixed point

• Boolean

• 8-, 16-, and 32-bit signed integers

• 8-, 16-, and 32-bit unsigned integers

 Delay Line DSP System Toolbox Unbuffer DSP System Toolbox `rebuffer_delay` DSP System Toolbox