## Convert Sample and Frame Rates in Simulink

### 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 to 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.

BlockLibrary

Downsample *

Signal Operations

Filtering / Multirate Filters

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.

### Note

When a Simulink® model contains signals with various frame rates, the model is called multirate. You can find a discussion of multirate models in Excess Algorithmic Delay (Tasking Latency). Also see Time-Based Scheduling and Code Generation (Simulink Coder).

### 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:

1. At the MATLAB® command prompt, type `ex_downsample_tut1`.

The Downsample Example T1 model opens.

2. In the Debug tab, select > .

When you run the model, the dimensions of the signals appear next to the lines connecting the blocks.

3. Double-click the Signal From Workspace block. The Source Block Parameters: Signal From Workspace dialog box opens.

4. Set the block parameters as follows:

• Sample time = `0.125`

• Samples per frame = `8`

Based on these parameters, the Signal From Workspace block outputs a signal with a sample period of 0.125 second and a frame size of 8.

5. Save these parameters and close the dialog box by clicking .

6. Double-click the Downsample block. The Function Block Parameters: Downsample dialog box opens.

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

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

8. Run the model.

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

Because ${T}_{fi}={M}_{i}×{T}_{si}$, the input frame period, ${T}_{fi}$, is ${T}_{fi}=8×0.125=1$ second. This value is displayed by the first Probe block. Therefore the input frame rate, $1/{T}_{fi}$, is also 1 frame per second.

The second Probe block in the model verifies that the output from the Downsample block has a frame period, ${T}_{fo}$, of 2 seconds, twice the frame period of the input. However, because the frame rate of the output, $1/{T}_{fo}$, is 0.5 frames per second, the Downsample block actually downsampled the original signal to half its original rate. As a result, the output sample period, ${T}_{so}={T}_{fo}/{M}_{o}$, 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:

1. At the MATLAB command prompt, type `ex_downsample_tut2`.

The Downsample Example T2 model opens.

2. In the Debug tab, select > .

When you run the model, the dimensions of the signals appear next to the lines connecting the blocks.

3. Double-click the Signal From Workspace block. The Source Block Parameters: Signal From Workspace dialog box opens.

4. Set the block parameters as follows:

• Sample time = `0.125`

• Samples per frame = `8`

Based on these parameters, the Signal From Workspace block outputs a signal with a sample period of 0.125 second and a frame size of 8.

5. Save these parameters and close the dialog box by clicking .

6. Double-click the Downsample block. The Function Block Parameters: Downsample dialog box opens.

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

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

8. Run the model.

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

Because ${T}_{fi}={M}_{i}×{T}_{si}$, the input frame period, ${T}_{fi}$, is ${T}_{fi}=8×0.125=1$ second. This value is displayed by the first Probe block. Therefore the input frame rate, $1/{T}_{fi}$, is also 1 frame per 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, ${T}_{so}={T}_{fo}/{M}_{o}$is 0.25 second. This process occurred without any change to the frame rate (${T}_{fi}={T}_{fo}$).

### 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.

BlockLibrary

Buffer

Signal Management/ Buffers

Delay Line

Signal Management/ Buffers

Unbuffer

Signal Management/ Buffers

Variable Selector

Signal Management/ Indexing

#### 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:

`${T}_{f}=M{T}_{s}$`

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:

• Buffer — redistributes signal samples to a larger or smaller frame size

• Unbuffer — unbuffers a signal with frame size M and frame period Tf to a signal with frame size `1` and frame period Ts

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

`${T}_{fo}=\frac{{M}_{o}{T}_{fi}}{{M}_{i}}$`

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 signal and always preserves the signal's data and sample period

`${T}_{so}={T}_{fi}/{M}_{i}$`

where Tfi and Mi are the period and size, respectively, of the frame signal.

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

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

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

`${T}_{so}=\frac{\left({M}_{o}-L\right){T}_{si}}{{M}_{o}}$`
• 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

`${T}_{so}=\frac{{T}_{si}}{{M}_{o}}$`
• 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

`${T}_{so}=\frac{{M}_{i}{T}_{si}}{{M}_{o}}$`

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:

1. At the MATLAB command prompt, type `ex_buffer_tut1`.

The Buffer Example T1 model opens.

2. Double-click the Signal From Workspace block. The Source Block Parameters: Signal From Workspace dialog box opens.

3. Set the parameters as follows:

• Signal = `1:1000`

• Sample time = `0.125`

• Samples per frame = `8`

• Form output after final data value = `Setting to zero`

Based on these parameters, the Signal from Workspace block outputs a signal with a sample period of 0.125 second. Each output frame contains eight samples.

4. Save these parameters and close the dialog box by clicking .

5. Double-click the Buffer block. The Function Block Parameters: Buffer dialog box opens.

6. Set the parameters as follows, and then click :

• Output buffer size (per channel) = `16`

• Buffer overlap = `0`

• Initial conditions = `0`

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

7. 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:

1. At the MATLAB command prompt, type `ex_buffer_tut2`.

The Buffer Example T2 model opens.

2. Double-click the Signal From Workspace block. The Source Block Parameters: Signal From Workspace dialog box opens.

3. Set the parameters as follows:

• Signal = `1:1000`

• Sample time = `0.125`

• Samples per frame = `8`

• Form output after final data value = `Setting to zero`

Based on these parameters, the Signal from Workspace block outputs a signal with a sample period of 0.125 second. Each output frame contains eight samples.

4. Save these parameters and close the dialog box by clicking .

5. Double-click the Buffer block. The Function Block Parameters: Buffer dialog box opens.

6. Set the parameters as follows, and then click :

• Output buffer size (per channel) = `16`

• Buffer overlap = `4`

• Initial conditions = `0`

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.

7. 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

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

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 ${T}_{so}={T}_{fo}/{M}_{o}=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.

Watch now