Convert Sample and Frame Rates in Simulink

Note:
Starting in R2010b, many DSP System Toolbox™ blocks
received a new parameter to control whether they perform sample- or
frame-based processing. The following content has not been updated
to reflect this change. For more information, see the Frame-Based Processing section
of the Release Notes.

Rate Conversion Blocks

There are two common types of operations that impact the frame
and sample rates of a signal: direct rate conversion and frame rebuffering.
Direct rate conversions, such as upsampling and downsampling, can
be implemented by altering either the frame rate or the frame size
of a signal. Frame rebuffering, which is used alter the frame size
of a signal in order to improve simulation throughput, usually changes
either the sample rate or frame rate of the signal as well.

The following table lists the principal rate conversion blocks
in DSP System Toolbox software. Blocks marked with an asterisk
(*) offer the option of changing the rate by either adjusting the
frame size or frame rate.

Block

Library

Downsample *

Signal Operations

Dyadic Analysis Filter Bank

Filtering / Multirate Filters

Dyadic Synthesis Filter Bank

Filtering / Multirate Filters

FIR Decimation *

Filtering / Multirate Filters

FIR Interpolation *

Filtering / Multirate Filters

FIR Rate Conversion

Filtering / Multirate Filters

Repeat *

Signal Operations

Upsample *

Signal Operations

Direct Rate Conversion

Rate conversion blocks accept an input signal at one sample
rate, and propagate the same signal at a new sample rate. Several
of these blocks contain a Rate options parameter
offering two options for multirate versus single-rate processing:

Enforce single-rate processing:
When you select this option, the block maintains the input sample
rate.

Allow multirate processing:
When you select this option, the block downsamples the signal such
that the output sample rate is K times slower than the input sample
rate.

Rate Conversion by Frame-Rate Adjustment

One way to change the sample rate of a signal, 1/Tso,
is to change the output frame rate (Tfo ≠ Tfi),
while keeping the frame size constant (Mo = Mi).
Note that the sample rate of a signal is defined as 1/Tso = Mo/Tfo:

Set the Rate options parameter
to Allow multirate processing, and then
click OK.

The Downsample block is configured to downsample the signal
by changing the frame rate rather than the frame size.

Run the model.

After the simulation, the model should look similar to the following
figure.

Because Tfi=Mi×Tsi, the input frame period, Tfi, is Tfi=8×0.125=1 second. This value is displayed by the first Probe
block. Therefore the input frame rate, 1/Tfi, is also 1 second.

The second Probe block in the model verifies that the output
from the Downsample block has a frame period, Tfo, of 2 seconds, twice the frame
period of the input. However, because the frame rate of the output, 1/Tfo, is 0.5 second, the Downsample block actually downsampled
the original signal to half its original rate. As a result, the output
sample period, Tso=Tfo/Mo, is doubled to 0.25 second without
any change to the frame size. The signal dimensions in the model confirm
that the frame size did not change.

Rate Conversion by Frame-Size Adjustment

One way to change the sample rate of a signal is by changing
the frame size (that is Mo ≠ Mi),
but keep the frame rate constant (Tfo = Tfi).
Note that the sample rate of a signal is defined as 1/Tso = Mo/Tfo:

Set the Rate options parameter
to Enforce single-rate processing, and
then click OK.

The Downsample block is configured to downsample the signal
by changing the frame size rather than the frame rate.

Run the model.

After the simulation, the model should look similar to the following
figure.

Because Tfi=Mi×Tsi, the input frame period, Tfi, is Tfi=8×0.125=1 second. This value is displayed by the first Probe
block. Therefore the input frame rate, 1/Tfi, is also 1 second.

The Downsample block downsampled the input signal to half its
original frame size. The signal dimensions of the output of the Downsample
block confirm that the downsampled output has a frame size of 4,
half the frame size of the input. As a result, the sample period of
the output, Tso=Tfo/Mo, now has a sample period of 0.25 second. This process
occurred without any change to the frame rate (Tfi=Tfo).

Avoid Unintended Rate Conversion

It is important to be aware of where rate conversions occur
in a model. In a few cases, unintentional rate conversions can produce
misleading results:

Based on these parameters, the Magnitude FFT block zero-pads the length-128
input frame to a length of 256 before performing
the FFT.

Run the model.

The model should now look similar to the following figure. Note
that the signal leaving the Magnitude FFT block is 256-by-1.

The Vector Scope window displays
the magnitude FFT of a signal composed of two sine waves, with frequencies
of 2 Hz and 4 Hz.

In this case, based on the input frame size (256)
and frame period (12.8), the Vector Scope block incorrectly
calculates the original signal's sample period to be (12.8/256) or
0.05 second. As a result, the spectral peaks appear incorrectly at
2 Hz and 4 Hz rather than 1 Hz
and 2 Hz.

The source of the error described above is unintended rate conversion.
The zero-pad operation performed by the Magnitude FFT block halves
the sample period of the sequence by appending 128 zeros
to each frame. To calculate the spectral peaks correctly, the Vector
Scope block needs to know the sample period of the original signal.

To correct for the unintended rate
conversion, double-click the Vector Scope block.

Set the block parameters as follows:

Click the Axis Properties tab.

Clear the Inherit sample time from input check
box.

Set the Sample
time of original time series parameter to the actual sample
period of 0.1.

Run the model.

The Vector Scope block now accurately plots the spectral peaks
at 1 Hz and 2 Hz.

In general, when you zero-pad or overlap buffers, you are changing
the sample period of the signal. If you keep this in mind, you can
anticipate and correct problems such as unintended rate conversion.

Frame Rebuffering Blocks

There are two common types of operations that impact the frame
and sample rates of a signal: direct rate conversion and frame rebuffering.
Direct rate conversions, such as upsampling and downsampling, can
be implemented by altering either the frame rate or the frame size
of a signal. Frame rebuffering, which is used alter the frame size
of a signal in order to improve simulation throughput, usually changes
either the sample rate or frame rate of the signal as well.

Sometimes you might need to rebuffer a signal to a new frame
size at some point in a model. For example, your data acquisition
hardware may internally buffer the sampled signal to a frame size
that is not optimal for the signal processing algorithm in the model.
In this case, you would want to rebuffer the signal to a frame size
more appropriate for the intended operations without introducing any
change to the data or sample rate.

The following table lists the principal DSP System Toolbox buffering
blocks.

Blocks for Frame Rebuffering with Preservation of the Signal

Buffering operations provide another mechanism for rate changes
in signal processing models. The purpose of many buffering operations
is to adjust the frame size of the signal, M, without altering the
signal's sample rate Ts.
This usually results in a change to the signal's frame rate, Tf,
according to the following equation:

Tf=MTs

However, the equation above is only true if no samples are added
or deleted from the original signal. Therefore, the equation above
does not apply to buffering operations that generate overlapping frames,
that only partially unbuffer frames, or that alter the data sequence
by adding or deleting samples.

There are two blocks in the Buffers library that can be used
to change a signal's frame size without altering the signal itself:

The Buffer block preserves the signal's data and sample period
only when its Buffer overlap parameter is set
to 0. The output frame period, Tfo, is

Tfo=MoTfiMi

where Tfi is the input
frame period, Mi is the
input frame size, and Mo is
the output frame size specified by the Output buffer size
(per channel) parameter.

The Unbuffer block unbuffers
a frame-based signal to its sample-based equivalent, and always preserves
the signal's data and sample period

Tso=Tfi/Mi

where Tfi and Mi are
the period and size, respectively, of the frame-based input.

Both the Buffer and Unbuffer blocks preserve the sample period
of the sequence in the conversion (Tso = Tsi).

Blocks for Frame Rebuffering with Alteration of the Signal

Some forms of buffering alter the signal's data or sample period
in addition to adjusting the frame size. This type of buffering is
desirable when you want to create sliding windows by overlapping consecutive
frames of a signal, or select a subset of samples from each input
frame for processing.

The blocks that alter a signal while adjusting its frame size
are listed below. In this list, Tsi is
the input sequence sample period, and Tfi and Tfo are
the input and output frame periods, respectively:

The Buffer block adds duplicate samples to a sequence
when the Buffer overlap parameter, L, is set
to a nonzero value. The output frame period is related to the input
sample period by

Tfo=(Mo−L)Tsi

where Mo is the output
frame size specified by the Output buffer size (per channel) parameter.
As a result, the new output sample period is

Tso=(Mo−L)TsiMo

The Delay Line block adds duplicate samples to the
sequence when the Delay line size parameter, Mo,
is greater than 1. The output and input frame periods
are the same, Tfo = Tfi = Tsi, and the
new output sample period is

Tso=TsiMo

The Variable Selector block can remove, add, and/or
rearrange samples in the input frame when Select is
set to Rows. The output and input frame
periods are the same, Tfo = Tfi,
and the new output sample period is

Tso=MiTsiMo

where Mo is the length
of the block's output, determined by the Elements vector.

In all of these cases, the sample period of the output sequence
is not equal to the sample period of the input
sequence.

Buffer Signals by Preserving the Sample Period

In the following example, a signal with a sample period of 0.125
second is rebuffered from a frame size of 8 to a
frame size of 16. This rebuffering process doubles
the frame period from 1 to 2 seconds, but does not change the sample
period of the signal (Tso = Tsi = 0.125). The process also does not add
or delete samples from the original signal:

Based on these parameters, the Buffer block rebuffers the signal
from a frame size of 8 to a frame size of 16.

Run the model.

The following figure shows the model after simulation.

Note that the input to the Buffer block has a frame size of
8 and the output of the block has a frame size of 16. As shown by
the Probe blocks, the rebuffering process doubles the frame period
from 1 to 2 seconds.

Buffer Signals by Altering the Sample Period

Some forms of buffering alter the signal's data or sample period
in addition to adjusting the frame size. In the following example,
a signal with a sample period of 0.125 second is rebuffered from a
frame size of 8 to a frame size of 16
with a buffer overlap of 4:

Based on these parameters, the Buffer block rebuffers the signal
from a frame size of 8 to a frame size of 16.
Also, after the initial output, the first four samples of each output
frame are made up of the last four samples from the previous output
frame.

Run the model.

The following figure shows the model after the simulation has
stopped.

Note that the input to the Buffer block has a frame size of
8 and the output of the block has a frame size of 16. The relation
for the output frame period for the Buffer block is

Tfo=(Mo−L)Tsi

Tfo is (16-4)*0.125,
or 1.5 seconds, as confirmed by the second Probe block.
The sample period of the signal at the output of the Buffer block
is no longer 0.125 second. It is now Tso=Tfo/Mo=1.5/16=0.0938 second. Thus, both the signal's data and the signal's
sample period have been altered by the buffering operation.