buffering and frame-based processing
buffer input into frames
multichannel signals of frame size 1 can be buffered into multichannel signals of frame size l using the buffer block. l is greater than 1.
the following figure is a graphical representation of a signal with frame size 1 being converted into a signal of frame size l by the buffer block.
in the following example, a two-channel 1 sample per frame signal is buffered into a two-channel 4 samples per frame signal using a buffer block:
open the ex_buffer_tut
model.
double-click the signal from workspace block. the signal from workspace dialog box opens. set the parameters as follows:
signal = [1:10;-1:-1:-10]'
sample time = 1
samples per frame = 1
form output after final data value =
setting to zero
based on these parameters, the signal from workspace block outputs a signal with a frame length of 1 and a sample period of 1 second. because you set the samples per frame parameter setting to 1, the signal from workspace block outputs one two-channel sample at each sample time.
save these parameters and close the dialog box by clicking ok.
double-click the buffer block. the buffer dialog box opens. set the parameters as follows:
output buffer size (per channel) = 4
buffer overlap = 0
initial conditions = 0
because you set the output buffer size parameter to 4, the buffer block outputs a frame signal with frame size 4.
run the model.
the figure below is a graphical interpretation of the model behavior during simulation.
alternatively, you can set the samples per frame parameter of the signal from workspace block to 4 and create the same signal shown above without using a buffer block. the signal from workspace block performs the buffering internally, in order to output a two-channel frame.
buffer signals into frames with overlap
in some cases it is useful to work with data that represents overlapping sections of an original signal. for example, in estimating the power spectrum of a signal, it is often desirable to compute the fft of overlapping sections of data. overlapping buffers are also needed in computing statistics on a sliding window, or for adaptive filtering.
the buffer overlap parameter of the buffer block specifies the number of overlap points l. in the overlap case (l > 0), the frame period for the output is (mo-l)*tsi, where tsi is the input sample period and mo is the buffer size.
note: set the buffer overlap parameter to a negative value to achieve output frame rates slower than in the nonoverlapping case. the output frame period is still tsi*(mo-l), but now with l < 0. only the mo newest inputs are included in the output buffers. the previous l inputs are discarded.
in the following example, a four-channel signal with frame length 1 and sample period 1 is buffered to a signal with frame size 3 and frame period 2. because of the buffer overlap, the input sample period is not conserved, and the output sample period is 2/3:
open the ex_buffer_tut3
model.
also, the variable sp_examples_src
is loaded into the matlab workspace. this variable is defined as follows:
sp_examples_src=[1 1 5 -1; 2 1 5 -2; 3 0 5 -3; 4 0 5 -4; 5 1 5 -5; 6 1 5 -6];
double-click the signal from workspace block. the signal from workspace dialog box opens. set the block parameters as follows:
signal = sp_examples_src
sample time = 1
samples per frame = 1
form output after final data value by = setting to zero
based on these parameters, the signal from workspace block outputs a signal with a sample period of 1 second. because you set the samples per frame parameter setting to 1, the signal from workspace block outputs one four-channel sample at each sample time. save these parameters and close the dialog box by clicking ok.
double-click the buffer block. the buffer dialog box opens. set the block parameters as follows, and then click ok:
output buffer size (per channel) = 3
buffer overlap = 1
initial conditions = 0
because you set the output buffer size parameter to 3, the buffer block outputs a signal with frame size 3. also, because you set the buffer overlap parameter to 1, the last sample from the previous output frame is the first sample in the next output frame.
run the model.
the following figure is a graphical interpretation of the model behavior during simulation.
at the matlab command prompt, type sp_examples_yout
.
sp_examples_yout = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 5 -1 2 1 5 -2 2 1 5 -2 3 0 5 -3 4 0 5 -4 4 0 5 -4 5 1 5 -5 6 1 5 -6 6 1 5 -6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
notice that the inputs do not begin appearing at the output until the fifth row, the second row of the second frame. this is due to the block latency.
see for general information about algorithmic delay. for instructions on how to calculate buffering delay, see buffer delay and initial conditions.
buffer frame inputs into other frame inputs
in the following example, a two-channel signal with frame size 4 is rebuffered to a signal with frame size 3 and frame period 2. because of the overlap, the input sample period is not conserved, and the output sample period is 2/3:
open the ex_buffer_tut4
model.
the variable sp_examples_src
is loaded into the matlab workspace. this variable is defined as:
sp_examples_src = [1 1; 2 1; 3 0; 4 0; 5 1; 6 1; 7 0; 8 0]
double-click the signal from workspace block. the signal from workspace dialog box opens. set the block parameters as follows:
signal =
sp_examples_src
sample time = 1
samples per frame = 4
based on these parameters, the signal from workspace block outputs a two-channel signal with a sample period of 1 second and a frame size of 4. save these parameters and close the dialog box by clicking ok.
double-click the buffer block. the buffer dialog box opens. set the block parameters as follows, and then click ok:
output buffer size (per channel) = 3
buffer overlap = 1
initial conditions = 0
based on these parameters, the buffer block outputs a two-channel signal with a frame size of 3.
run the model.
the following figure is a graphical representation of the model behavior during simulation.
note that the inputs do not begin appearing at the output until the last row of the third output matrix. this is due to the block latency.
see for general information about algorithmic delay. for instructions on how to calculate buffering delay, and see buffer delay and initial conditions.
buffer delay and initial conditions
in the examples buffer signals into frames with overlap and buffer frame inputs into other frame inputs, the input signal is delayed by a certain number of samples. the initial output samples correspond to the value specified for the initial condition parameter. the initial condition is zero in both examples mentioned above.
under most conditions, the buffer and unbuffer
blocks have some amount of delay or latency. this latency depends on both the
block parameter settings and the simulink® tasking mode. you can use the rebuffer_delay
function to determine the length of the block latency for any combination of
frame size and overlap.
the syntax rebuffer_delay(f,n,v)
returns the delay, in
samples, introduced by the buffering and unbuffering blocks during multitasking
operations, where f
is the input frame size,
n
is the output buffer size
parameter setting, and v
is the buffer
overlap parameter setting.
for example, you can calculate the delay for the model discussed in the buffer frame inputs into other frame inputs using the following command at the matlab® command line:
d = rebuffer_delay(4,3,1) d = 8
this result agrees with the block output in that example. notice that this model was simulated in simulink multitasking mode.
for more information about delay, see . for delay information
about a specific block, see the “latency” section of the block
reference page. for more information about the rebuffer_delay
function, see .
unbuffer frame signals into sample signals
you can unbuffer multichannel signals of frame length greater than 1 into multichannel signals of frame length equal to 1 using the block. the unbuffer block performs the inverse operation of the buffer block's buffering process, where signals with frame length 1 are buffered into a signal with frame length greater than 1. the unbuffer block generates an n-channel output containing one sample per frame from an n-channel input containing multiple channels per frame. the first row in each input matrix is always the first output.
the following figure is a graphical representation of this process.
the sample period of the output tso is related to the input frame period tfi by the input frame size mi.
the unbuffer block always preserves the signal sample period (tso = tsi). see for more information about rate conversions.
in the following example, a two-channel signal with four samples per frame is unbuffered into a two-channel signal with one sample per frame:
open the ex_unbuffer_tut
model.
double-click the signal from workspace block. the signal from workspace dialog box opens. set the block parameters as follows:
signal = [1:10;-1:-1:-10]'
sample time = 1
samples per frame = 4
form output after final data value by =
setting to zero
based on these parameters, the signal from workspace block outputs a two-channel signal with frame size 4. save these parameters an close the dialog box by clicking ok.
double-click the unbuffer block. the unbuffer dialog box opens. set the initial conditions parameter to 0, and then click ok.
the unbuffer block unbuffers a two-channel signal with four samples per frame into a two-channel signal with one sample per frame.
run the model.
the following figures is a graphical representation of what happens during the model simulation.
note: the unbuffer block generates initial conditions not shown in the figure below with the value specified by the initial conditions parameter. see the reference page for information about the number of initial conditions that appear in the output.
at the matlab command prompt, type sp_examples_yout
.
sp_examples_yout(:,:,1) = 0 0 sp_examples_yout(:,:,2) = 0 0 sp_examples_yout(:,:,3) = 0 0 sp_examples_yout(:,:,4) = 0 0 sp_examples_yout(:,:,5) = 1 -1 sp_examples_yout(:,:,6) = 2 -2 sp_examples_yout(:,:,7) = 3 -3 sp_examples_yout(:,:,8) = 4 -4 sp_examples_yout(:,:,9) = 5 -5 sp_examples_yout(:,:,10) = 6 -6 sp_examples_yout(:,:,11) = 7 -7
the unbuffer block unbuffers the signal into a two-channel signal. each page of the output matrix represents a different sample time.