Contents

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.

BlockLibrary

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:

  1. At the MATLAB® command prompt, type ex_downsample_tut1ex_downsample_tut1.

    The Downsample Example T1 model opens.

  2. From the Display menu, point to Signals & Ports, and select Signal Dimensions.

    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 frame-based 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 OK.

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

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

  1. At the MATLAB command prompt, type ex_downsample_tut2ex_downsample_tut2.

    The Downsample Example T2 model opens.

  2. From the Display menu, point to Signals & Ports, and select Signal Dimensions.

    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 frame-based 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 OK.

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

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

  1. At the MATLAB command prompt, type ex_vectorscope_tut1ex_vectorscope_tut1.

    The Vector Scope Example model opens.

  2. Double-click the upper Sine Wave block. The Source Block Parameters: Sine Wave dialog box opens.

  3. Set the block parameters as follows:

    • Frequency (Hz) = 1

    • Sample time = 0.1

    • Samples per frame = 128

    Based on the Sample time and the Samples per frame parameters, the Sine Wave outputs a sinusoid with a frame period of 128*0.1 or 12.8 seconds.

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

  5. Double-click the lower Sine Wave block.

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

    • Frequency (Hz) = 2

    • Sample time = 0.1

    • Samples per frame = 128

    Based on the Sample time and the Samples per frame parameters, the Sine Wave outputs a sinusoid with a frame period of 128*0.1 or 12.8 seconds.

  7. Double-click the Magnitude FFT block. The Function Block Parameters: Magnitude FFT dialog box opens.

  8. Select the Inherit FFT length from input dimensions check box, and then click OK.

    This setting instructs the block to use the input frame size (128) as the FFT length (which is also the output size).

  9. Double-click the Vector Scope block. The Sink Block Parameters: Vector Scope dialog box opens.

  10. Set the block parameters as follows, and then click OK:

    • Click the Scope Properties tab.

    • Input domain = Frequency

    • Click the Axis Properties tab.

    • Minimum Y-limit = -10

    • Maximum Y-limit = 40

  11. Run the model.

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

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

    The Vector Scope block uses the input frame size (128) and period (12.8) to deduce the original signal's sample period (0.1), which allows it to correctly display the peaks at 1 Hz and 2 Hz.

  12. Double-click the Magnitude FFT block. The Function Block Parameters: Magnitude FFT dialog box opens.

  13. Set the block parameters as follows:

    • Clear the Inherit FFT length from input dimensions check box.

    • Set the FFT length parameter to 256.

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

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

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

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

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

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:

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:

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

  • Unbuffer — unbuffers a frame-based signal to a sample-based signal (frame size = 1)

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=(MoL)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=(MoL)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:

  1. At the MATLAB command prompt, type ex_buffer_tut1ex_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 frame-based 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 OK.

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

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

    • 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_tut2ex_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 frame-based 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 OK.

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

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

    • 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

    Tfo=(MoL)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.

Was this topic helpful?