buffer
Buffer signal vector into matrix of data frames
Syntax
Description
Examples
Continuous Overlapping Buffers
Create a buffer containing 100 frames, each with 10 samples.
data = buffer(1:1000,10);
Take the frames (columns) in the matrix data
to be the sequential outputs of a data acquisition board sampling a physical signal: data(:,1)
is the first A/D output, containing the first 11 signal samples, data(:,2)
is the second output, containing the next 11 signal samples, and so on.
You want to rebuffer this signal from the acquired frame size of 11 to a frame size of 4 with an overlap of 1. Call buffer
to operate on each successive input frame, using the opt
parameter to maintain consistency in the overlap from one buffer to the next.
Set the buffer parameters. Specify a value of –5 for y(1)
. The carryover vector is empty initially.
n = 4; p = 1; opt = -5; z = [];
Now repeatedly call buffer
, each time passing in a new signal frame (column) from data
. Overflow samples (returned in z
) are carried over and prepended to the input in the subsequent call to buffer
.
For the first four iterations, show the input frame [z;x]'
, the input and output values of opt
, the output buffer y
, and the overflow z
. The size of the output matrix, y
, can vary by a single column from one iteration to the next. This is typical for buffering operations with overlap or underlap.
for i = 1:size(data,2) x = data(:,i); [y,z,oppt] = buffer([z;x],n,p,opt); if i <= 4 disp("Iteration " + i) ifrm = [z;x]' opts = [opt oppt] y z disp("-----------") end opt = oppt; end
Iteration 1
ifrm = 1×11
10 1 2 3 4 5 6 7 8 9 10
opts = 1×2
-5 9
y = 4×3
-5 3 6
1 4 7
2 5 8
3 6 9
z = 10
-----------
Iteration 2
ifrm = 1×12
19 20 11 12 13 14 15 16 17 18 19 20
opts = 1×2
9 18
y = 4×3
9 12 15
10 13 16
11 14 17
12 15 18
z = 2×1
19
20
-----------
Iteration 3
ifrm = 1×10
21 22 23 24 25 26 27 28 29 30
opts = 1×2
18 30
y = 4×4
18 21 24 27
19 22 25 28
20 23 26 29
21 24 27 30
z = 0x1 empty double column vector
-----------
Iteration 4
ifrm = 1×11
40 31 32 33 34 35 36 37 38 39 40
opts = 1×2
30 39
y = 4×3
30 33 36
31 34 37
32 35 38
33 36 39
z = 40
-----------
Continuous Underlapping Buffers
Create a buffer containing 100 frames, each with 9 samples.
data = buffer(1:900,9);
Take data(:,1)
as the first A/D output, containing the first 11 signal samples, data(:,2)
as the second output, containing the next 11 signal samples, and so on.
You want to rebuffer this signal from the acquired frame size of 11 to a frame size of 4 with an underlap of 2. To do this, you will repeatedly call buffer
to operate on each successive input frame, using the opt
parameter to maintain consistency in the underlap from one buffer to the next.
Set the buffer parameters. Specify a new frame size of 4 and an underlap of –2. Skip the first input element, x(1)
, by setting opt
to 1. The carryover vector is empty initially.
n = 4; p = -2; opt = 1; z = [];
Now repeatedly call buffer
, each time passing in a new signal frame (column) from data
. Overflow samples (returned in z
) are carried over and prepended to the input in the subsequent call to buffer
.
For the first three iterations, show the input frame [z';x]'
, the input and output values of opt
, the output buffer y
, and the overflow z
. The size of the output matrix, y
, can vary by a single column from one iteration to the next. This is typical for buffering operations with overlap or underlap.
for i = 1:size(data,2) x = data(:,i); [y,z,oppt] = buffer([z';x],n,p,opt); if i <= 3 disp("Iteration "+ i) ifrm = [z';x]' opts = [opt oppt] y z disp("-----------") end opt = oppt; end
Iteration 1
ifrm = 1×11
8 9 1 2 3 4 5 6 7 8 9
opts = 1×2
1 0
y = 4×1
2
3
4
5
z = 1×2
8 9
-----------
Iteration 2
ifrm = 1×9
10 11 12 13 14 15 16 17 18
opts = 1×2
0 1
y = 4×2
8 14
9 15
10 16
11 17
z = 1x0 empty double row vector
-----------
Iteration 3
ifrm = 1×11
26 27 19 20 21 22 23 24 25 26 27
opts = 1×2
1 0
y = 4×1
20
21
22
23
z = 1×2
26 27
-----------
Input Arguments
x
— Input signal
vector
Input signal, specified as a vector.
n
— Frame length
positive integer scalar
Frame length, specified as a positive integer scalar.
p
— Number of overlapping or underlapping samples
0 (default) | integer scalar
Number of overlapping or underlapping samples, specified as an integer scalar.
For
0
<p
<n
(overlap),buffer
repeats the finalp
samples of each frame at the beginning of the following frame. For example, ifx
=1:30
andn
=7
, an overlap ofp
=3
looks like this.The first frame starts with
p
zeros (the default initial condition), and the number of columns iny
isceil(L/(n-p))
.For
p
<0
(underlap),buffer
skipsp
samples between consecutive frames. For example, ifx
=1:30
andn
=7
, a buffer with underlap ofp
=-3
looks like this.The number of columns in
y
isceil(L/(n-p))
.
opt
— Option
zeros(p,1)
(default) | "nodelay"
| vector | integer
Option, specified as a vector or integer.
For
0
<p
<n
(overlap),opt
specifies a length-p
vector to insert beforex(1)
in the buffer. This vector can be considered an initial condition, which is needed when the current buffering operation is one in a sequence of consecutive buffering operations. To maintain the desired frame overlap from one buffer to the next,opt
should contain the finalp
samples of the previous buffer in the sequence. See Continuous Buffering below.By default,
opt
iszeros(p,1)
for an overlapping buffer. Setopt
to"nodelay"
to skip the initial condition and begin filling the buffer immediately withx(1)
. In this case,L
must belength(p)
or longer. For example, ifx
=1:30
andn
=7
, a buffer with overlap ofp
=3
looks like this.For
p
<0
(underlap),opt
is an integer value in the range[0,-p]
specifying the number of initial input samples,x(1:opt)
, to skip before adding samples to the buffer. The first value in the buffer is thereforex(opt+1)
. By default,opt
is zero for an underlapping buffer.This 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 tobuffer
. If the previous input had fewer thanp
points that could be skipped after filling the final frame of that buffer, the remainingopt
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.
Output Arguments
y
— Data frame
matrix
Data frame, returned as a matrix. Each data frame occupies one column of
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
.
If
y
is an overlapping buffer, it hasn
rows andm
columns, wherem = floor(L/(n-p))
whenlength(opt) = p
orm = ceil((L-p)/(n-p))
whenopt = "nodelay"
.If
y
is an underlapping buffer, it hasn
rows andm
columns, wherem = floor((L-opt)/(n-p)) + (rem((L-opt),(n-p)) >= n)
.
z
— Remaining samples
vector
Remaining samples, returned as a vector. If the number of samples in the input
vector (after the appropriate overlapping or underlapping operations) exceeds the number
of places available in the n
-by-m
buffer, the
remaining samples in x
are output in vector z
,
which for an overlapping buffer has length L - m*(n-p)
when
length(opt) = p
or L - ((m-1)*(n-p)+n)
when
opt = "nodelay"
, and for an underlapping buffer has length
(L-opt) - m*(n-p)
.
If
y
is an overlapping buffer or a nonoverlapping buffer, thenz
has the same orientation (row or column) asx
.If
y
is an underlapping buffer, thenz
is returned as a row vector.
If there are no remaining samples in the input after the buffer with the specified
overlap or underlap is filled, z
is an empty vector.
opt
— Last p
samples
vector
Last p
samples, returned as a vector. In an underlapping
buffer, 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 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
for an example of how this works in practice.
For
0
<p
<n
(overlap),opt
(as an output) contains the finalp
samples in the last frame of the buffer. This vector can be used as the initial condition for a subsequent buffering operation in a sequence of consecutive buffering operations. This allows the desired frame overlap to be maintained from one buffer to the next.For
p
<0
(underlap),opt
(as an output) is the difference between the total number of points to skip between frames (p
) and the number of points inx
that were available to be skipped after filling the last frame:opt = m*(n-p) + opt - L
, whereopt
on the right is the input argument tobuffer
, andopt
on the left is the output argument.z
is the empty vector. Herem
is the number of columns in the buffer, withm = floor((L-opt)/(n-p)) + (rem((L-opt),(n-p))>=n)
.Note that for an underlapping buffer output,
opt
is always zero when outputz
contains data.The
opt
output for an underlapping buffer is especially useful when the current buffering operation is one in a sequence of consecutive buffering operations. Theopt
output from each buffering operation specifies the number of samples that need to be skipped at the start of the next buffering operation to maintain the desired frame underlap from one buffer to the next. If fewer thanp
points were available to be skipped after filling the final frame of the current buffer, the remainingopt
points need to be removed from the first frame of the next buffer.
Diagnostics
Error messages are displayed when p
≥n
or
length(opt)
≠length(p)
in an overlapping buffer
case:
Frame overlap P must be less than the buffer size N. Initial conditions must be specified as a length-P vector.
More About
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.
For 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
two-output-argument syntax:
[y,z] = buffer([z;x],n) % x is a column vector. [y,z] = buffer([z,x],n) % x is a row vector.
This simply captures any buffer overflow in z
, and prepends the data
to the subsequent input in the next call to buffer
. Again, the input
signal, x
, of frame size L
, has been converted to a
signal of frame size n
without any insertion or deletion of samples.
Note that continuous buffering cannot be done with the single-output syntax y =
buffer(...)
, because the last frame of 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 and output to
buffer
. The two examples on this page demonstrate how the
opt
parameter should be used.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
Input
p
must be constant at compile time.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced before R2006aR2023b: Use non-double input arguments
The buffer
function supports non-double input
arguments.
R2023a: Generate C and C++ code
Generate C and C++ code for the buffer
function. You must have
MATLAB®
Coder™ to use this functionality.
See Also
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)