dsp.FIRRateConverter - Perform polyphase FIR sample rate conversion - MATLAB (original) (raw)
Perform polyphase FIR sample rate conversion
Description
The dsp.FIRRateConverter
System object™ performs an efficient polyphase sample rate conversion using a rational factor_L_/M along the first dimension.
The FIR rate converter cascades an interpolator with a decimator. Conceptually, the rate converter (as shown in the schematic) consists of an upsampler, followed by a combined anti-imaging and anti-aliasing FIR filter, followed by a downsampler. The coefficients of the anti-imaging and anti-aliasing FIR filter can be specified through theNumerator
property, or can be automatically designed by the object using the designMultirateFIR function. For an example, seeResample Signal Using FIR Rate Converter.
The upsampler increases the sample rate of the signal by a factor L and the downsampler reduces the sample rate of the signal by a factor M. Use upsampling and downsampling factors that are relatively prime or coprime. The resulting discrete-time signal has a sample rate that is L/M times the original sample rate.
Note that the actual object algorithm implements a polyphase structure, an efficient equivalent of the combined system depicted in the diagram. For more details, see Algorithms.
To perform sampling rate conversion:
- Create the
dsp.FIRRateConverter
object and set its properties. - Call the object with arguments, as if it were a function.
To learn more about how System objects work, see What Are System Objects?
Creation
Syntax
Description
`firrc` = dsp.FIRRateConverter
returns an FIR sample rate converter object with an interpolation factor of 3 and a decimation factor of 2. The object designs the FIR filter coefficients using thedesignMultirateFIR(3,2)
function.
`firrc` = dsp.FIRRateConverter(`L`,`M`)
returns an FIR sample rate converter with the integer-valuedInterpolationFactor
property set to L
and theDecimationFactor
property set to M
. The object designs its filter coefficients based on the rate conversion factors that you specify while creating the object, using the designMultirateFIR(L,M)
function. The designed filter corresponds to a lowpass filter with normalized cutoff frequency no greater than min(π/L,π/M)
in radial frequency units.
`firrc` = dsp.FIRRateConverter(`L`,`M`,`"Auto"`)
returns an FIR sample rate converter where theNumeratorSource
property is set to"Auto"
. In this mode, every time there is an update in the rate conversion factors, the object redesigns the filter using the design method specified inDesignMethod
.
`firrc` = dsp.FIRRateConverter(`L`,`M`,`num`)
returns an FIR sample rate converter with the Numerator
property set to num
.
`firrc` = dsp.FIRRateConverter(`L`,`M`,`method`)
returns an FIR sample rate converter with theDesignMethod
property set tomethod
. When you pass the design method as an input, the NumeratorSource
property is automatically set to "Auto"
.
`firrc` = dsp.FIRRateConverter(___,`Name=Value`)
returns an FIR sample rate converter with each specified property set to the specified value. Enclose each property name in quotes. You can use this syntax with any previous input argument combinations.
Example: firrc = dsp.FIRRateConverter(FullPrecisionOverride="false")
enables the fixed-point data types to be controlled through the individual fixed-point property settings.
`firrc` = dsp.FIRRateConverter(`L`,`M`,`"legacy"`)
returns an FIR sample rate converter where the filter coefficients are designed using firpm(70,[0 0.28 0.32 1],[1 1 0 0])
. The designed filter has a cutoff frequency of π/3 radians/sample.
Properties
Unless otherwise indicated, properties are nontunable, which means you cannot change their values after calling the object. Objects lock when you call them, and therelease function unlocks them.
If a property is tunable, you can change its value at any time.
For more information on changing property values, seeSystem Design in MATLAB Using System Objects.
Main Properties
Interpolation factor L, specified as a positive integer.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Decimation factor M, specified as a positive integer.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
FIR filter coefficient source, specified as one of the following:
"Property"
–– The numerator coefficients are specified through theNumerator
property."Auto"
–– The numerator coefficients are designed automatically using the design method specified inDesignMethod
.
Numerator coefficients of the FIR filter, specified as a row vector in powers of_z_-1.
The following equation defines the transfer function of the filter:
The vector b = [b(0), b(1), …, b(N)] represents the vector of filter coefficients.
Use a lowpass filter with normalized cutoff frequency no greater thanmin(1/L,1/M)
, where L is the interpolation factor and M is the decimation factor. The gain of the lowpass filter should equal L, the interpolation factor. Choose the length of the filter to be a multiple of the interpolation factor. To design such a filter, use the designMultirateFIR function. For an example, see Resample Signal Using FIR Rate Converter. All initial filter states are zero.
Dependencies
This property is visible only when you setNumeratorSource
to"Property"
.
When NumeratorSource
is set to"Auto"
, the numerator coefficients are automatically redesigned using the design method specified inDesignMethod
. To access the filter coefficients in the automatic design mode, type_objName.
_Numerator
in the MATLAB® command prompt.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Design method of the FIR filter coefficients, specified as one of the following:
"Kaiser"
–– Kaiser method. Approximate combined anti-aliasing and anti-imaging lowpass filter using thedesignMultirateFIR
function."ZOH"
–– Zero order hold method. Hold the input sequence values."Linear"
–– Linear interpolation method.
Dependencies
This property is visible only when you set theNumeratorSource
property to"Auto"
, or if you pass the"auto"
keyword as an input while creating the object.
Code Generation Properties
Allow arbitrary frame length for fixed-size input signals in the generated code, specified as true
or false
. When you specify:
true
–– The input frame length does not have to be a multiple of the decimation factor. The output of the object in the generated code is a variable-size array.false
–– The input frame length must be a multiple of the decimation factor.
When you specify variable-size signals, the input frame length can be arbitrary and the object ignores this property in the generated code. When you run this object in MATLAB, the object supports arbitrary input frame lengths for fixed-size and variable-size signals and this property does not affect the object behavior.
Data Types: logical
Fixed-Point Properties
Specify whether to use full precision rules. If you set FullPrecisionOverride
to true
, which is the default, the object computes all internal arithmetic and output data types using full precision rules. These rules provide the most accurate fixed-point numerics. It also turns off the display of other fixed-point properties because they do not apply individually. These rules guarantee that no quantization occurs within the object. Bits are added, as needed, to ensure that no roundoff or overflow occurs. If you set FullPrecisionOverride
to false
, fixed-point data types are controlled through individual fixed-point property settings. For more information, see Full Precision for Fixed-Point System Objects.
Specify the rounding method as one of:"Ceiling"
, "Convergent"
,"Floor"
, "Nearest"
,"Round"
, "Simplest"
, or"Zero"
.
Dependencies
This property applies only if the object is not in full precision mode.
Specify the overflow action as "Wrap"
or"Saturate"
.
Dependencies
This property applies only if the object is not in full precision mode.
Specify the filter coefficient fixed-point data type as"Same word length as input"
or"Custom"
.
Specify the filter coefficient fixed-point type as a numerictype (Fixed-Point Designer) object with a Signedness
of"Auto"
.
Dependencies
This property applies only when theCoefficientsDataType
property is"Custom"
.
Specify the product fixed-point data type as "Full precision"
, "Same as input"
, or"Custom"
.
Specify the product fixed-point type as a scaled numerictype (Fixed-Point Designer) object with a Signedness
of"Auto"
.
Dependencies
This property applies only when theProductDataType
property is"Custom"
.
Specify the accumulator fixed-point data type as"Full precision"
, "Same as product"
, "Same as input"
, or"Custom"
.
Specify the accumulator fixed-point type as a scaled numerictype (Fixed-Point Designer) object with a Signedness
of"Auto"
.
Dependencies
This property applies only when theAccumulatorDataType
property is"Custom"
.
Specify the output fixed-point data type as "Same as accumulator"
, "Same as product"
,"Same as input"
, or"Custom"
.
Specify the output fixed-point type as a scaled numerictype (Fixed-Point Designer) object with a Signedness
of"Auto"
.
Dependencies
This property applies only when theOutputDataType
property is"Custom"
.
Usage
Syntax
Description
[y](#d126e297753) = firrc([x](#d126e297638))
resamples the input x
and returns the resampled signaly
.
Input Arguments
Data input, specified as a column vector or a matrix of size_P_-by-Q. The columns in the input signal represent Q independent channels.
Under most conditions, the number of inputs rows P can be arbitrary and does not have to be a multiple of theDecimationFactor
property. See this table for details.
Input Signal | When you Run Object in MATLAB | When you Generate Code Using MATLAB Coder™ |
---|---|---|
Fixed-size | Object supports arbitrary input frame length | Object supports arbitrary input frame length when you setAllowArbitraryInputLength to true while generating code |
Variable-size | Object supports arbitrary input frame length | Object supports arbitrary input frame length |
Variable-size signals change in frame length once you lock the object while the fixed-size signals remain constant. When the object does not support arbitrary frame length, the input frame length must be a multiple of theDecimationFactor
property.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| fi
Complex Number Support: Yes
Output Arguments
Resampled output, returned as a column vector or a matrix. When the input is of size _P_-by-Q, and P is not a multiple of the decimation factor M, the output signal has an upper bound size ofceil
(LP/M)-by-Q. If P is a multiple of the decimation factor, then the output is of size (LP/M)-by-Q.
The number of rows in the output signal is given by_LP_/M, where P is the number of input rows, L is the interpolation factor, and_M_ is the decimation factor.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| fi
Complex Number Support: Yes
Object Functions
To use an object function, specify the System object as the first input argument. For example, to release system resources of a System object named obj
, use this syntax:
freqz | Frequency response of discrete-time filter System object |
---|---|
freqzmr | Compute DTFT approximation of impulse response of multirate or single-rate filter |
filterAnalyzer | Analyze filters with Filter Analyzer app |
info | Information about filter System object |
cost | Estimate cost of implementing filter System object |
coeffs | Returns the filter System object coefficients in a structure |
polyphase | Polyphase decomposition of multirate filter |
outputDelay | Determine output delay of single-rate or multirate filter |
step | Run System object algorithm |
---|---|
release | Release resources and allow changes to System object property values and input characteristics |
reset | Reset internal states of System object |
Examples
Resample a 100 Hz sine wave signal by a factor of 3/2.
Create a dsp.SineWave
object which generates a sinusoidal signal with 54 samples per frame, contains a tone at 100 Hz, and has a sample rate of 1250 Hz.
sine = dsp.SineWave(1,100,SampleRate=1250,SamplesPerFrame=54);
% Grab a frame x = sine();
% Calculate input time vector tx = (0:length(x)-1)/sine.SampleRate;
Design Default Filter
Create a dsp.FIRRateConverter
object. The object uses a combined anti-imaging and anti-aliasing FIR filter. By default, this filter is designed using the designMultirateFIR
function. The function designs the filter based on the rate conversion factors that you specify, and stores the coefficients in the Numerator
property. For an interpolation factor of 3 and a decimation factor of 2, the object designs the coefficients using designMultirateFIR(3,2)
.
firrc = dsp.FIRRateConverter(3,2);
Resample by a Factor of 3/2
Resample the signal by a factor of 3/2.
Plot the original and resampled signals. In order to plot the two signals on the same plot, you must account for the output delay of the FIR rate converter and the scaling introduced by the filter. Use the outputDelay function to compute the delay
value introduced by the rate converter. Shift the output by this delay value.
Visualize the input and the resampled signals. The input and output values coincide every 3 output samples, and every other input sample, owing to the conversion rate of 3/2.
FsIn = sine.SampleRate; [delay,FsOut] = outputDelay(firrc,FsIn=FsIn); % Calculate output times for vector y in input units ty = (0:length(y)-1)/FsOut-delay;
stem(tx,x,"filled",MarkerSize=4) hold on stem(ty,y) hold off xlim([0.0 0.0145]) ylim([-1.5 1.5]) legend("Original input","Resampled")
Resample by a Factor of 5/3 in Automatic Filter Design Mode
Now change the interpolation factor to 5 and the decimation factor to 3. In order for the filter design to be updated automatically based on the new rate conversion factors, set the NumeratorSource
property to "Auto"
. Alternately, you can pass the keyword "auto"
as an input while creating the object. The object then operates in the automatic filter design mode. Every time there is a change in the rate conversion factors, the object updates the filter design accordingly.
release(firrc) firrc.NumeratorSource = "Auto"; firrc.InterpolationFactor = 5; firrc.DecimationFactor = 3
firrc = dsp.FIRRateConverter with properties:
Main InterpolationFactor: 5 DecimationFactor: 3 NumeratorSource: 'Auto' DesignMethod: 'Kaiser'
Show all properties
To access the filter coefficients in the automatic filter design mode, type firrc.Numerator
in the MATLAB command prompt.
Resample the signal with the updated rate conversion values.
Plot the original and resampled signals. Recalculate the output delay and the output sample rate since the rate conversion factors have changed. Note that the input and output coincide every 3 input samples, and every 5 output samples, owing to the 5/3 conversion factor.
[delay,FsOut] = outputDelay(firrc,FsIn=FsIn); % Calculate output times for vector yAuto in input units tyAuto = (0:length(yAuto)-1)/FsOut-delay;
stem(tx,x,"filled",MarkerSize=4) hold on stem(tyAuto,yAuto,"r") hold off xlim([0.0 0.015]) ylim([-1.5 1.5]) legend("Original input","Resampled")
Specify Signal Interpolation Model
In the automatic design mode, you can also specify the underlying D/A signal interpolation model through the DesignMethod
property.
Set DesignMethod
to "linear"
and change the interpolation factor to 11.
release(firrc) firrc.DesignMethod = "linear"; firrc.InterpolationFactor = 11;
Resample the signal using the linear interpolation model.
Plot the original and resampled signals. The output samples lie on a piecewise-linear curve. Note that the input and output coincide every three input samples and every 11 output samples as expected from the ratio 11/3.
[delay,FsOut] = outputDelay(firrc,FsIn=FsIn); % Calculate output times for vector yLinear in input units tyLinear = (0:length(yLinear)-1)/FsOut-delay;
stem(tx,x,"filled",MarkerSize=4) hold on stem(tyLinear,yLinear) plot(tyLinear,yLinear,Color=[1 0 0 0.3]) hold off xlim([0.0 0.009]) ylim([-1.5 1.5]) legend("Original input","Resampled")
Resample an audio signal from 48 kHz to 44 kHz and play the resampled signal using the audioDeviceWriter object.
Note: The audioDeviceWriter
System object™ is not supported in MATLAB Online.
Create a dsp.AudioFileReader object. The object reads an audio file that has a sample rate of 48 kHz.
L = 11; M = 12; afr = dsp.AudioFileReader('audio48kHz.wav', ... 'OutputDataType','single', ... 'SamplesPerFrame',4*M);
Create a dsp.FIRRateConverter
object with an interpolation factor of L = 11 and a decimation factor of M = 12 (the co-prime representation of the ratio 44k/48k), a reasonable approximation of the standard 44.1 kHz rate. The object designs the filter using designMultirateFIR(11,12)
and stores the coefficients in the Numerator
property of the object.
firrc = dsp.FIRRateConverter(L,M)
firrc = dsp.FIRRateConverter with properties:
Main InterpolationFactor: 11 DecimationFactor: 12 NumeratorSource: 'Property' Numerator: [0 2.3076e-05 5.4790e-05 9.3620e-05 1.3665e-04 1.7959e-04 2.1699e-04 2.4263e-04 2.5004e-04 2.3320e-04 1.8731e-04 1.0959e-04 0 -1.3820e-04 -2.9809e-04 -4.6908e-04 -6.3723e-04 -7.8603e-04 -8.9751e-04 -9.5372e-04 … ] (1×264 double)
Show all properties
Create an audioDeviceWriter
object. Specify the sample rate to be 44100 Hz.
adw = audioDeviceWriter(44100);
Read the audio file, convert the sample rate of the audio signal, and play the resampled audio.
while ~isDone(afr) audio1 = afr(); audio2 = firrc(audio1); adw(audio2); end release(afr) release(adw)
More About
For a hardware-optimized FIR rate converter algorithm that supports HDL code generation, use the dsphdl.FIRRateConverter (DSP HDL Toolbox) System object. This object has hardware-friendly valid and reset control signals, and models exact hardware latency behavior. The object supports HDL code generation with HDL Coder™ tools.
Algorithms
The FIR rate converter is implemented efficiently using a polyphase structure.
To derive the polyphase structure, start with the transfer function of the FIR filter: This FIR filter is a combined anti-imaging and anti-aliasing filter.
N+1 is the length of the FIR filter.
You can rearrange this equation as follows:
L is the number of polyphase components, and its value equals the interpolation factor that you specify.
You can write this equation as:
E0(zL),E1(zL), ...,EL-1(zL) are polyphase components of the FIR filter H(z).
Conceptually, the FIR rate converter contains an upsampler, followed by a combined anti-imaging, anti-aliasing FIR filter H(z), which is followed by a downsampler.
Replace H(z) with its polyphase representation.
Here is the multirate noble identity for interpolation.
Applying the noble identity for interpolation moves the upsampling operation to after the filtering operation. This move enables you to filter the signal at a lower rate.
You can replace the upsampling operator, delay block, and the adder with a commutator switch. To account for the downsampler that follows, the switch moves in steps of size_M_. The switch receives the first sample from branch 0 and moves in the counter clockwise direction, each time skipping _M_−1 branches.
As an example, consider a rate converter with L set to 5 and_M_ set to 3. The polyphase components are_E0(z),E1(z),E2(z),E3(z), and_E4(z). The switch starts on the first branch 0, skips branches 1 and 2, receives the next sample from branch 3, then skips branches 4 and 0, receives the next sample from branch 2, and so on. The sequence of branches from which the switch receives the data sample is [0, 3, 1, 4, 2, 0, 3, 1, ….].
The rate converter implements the L/M conversion by first applying the interpolation factor L to the incoming data, and using the commutator switch at the end to receive only 1 in M samples, effectively accounting for the downsampling factor M. Hence, the sample rate at the output of the FIR rate converter is Lfs/M.
Extended Capabilities
Version History
Introduced in R2012a
Starting in R2025a, the Filter Design HDL Coder™ product is discontinued. So, this object no longer supports HDL code generation by using the generatehdl
function. For a hardware-optimized FIR rate converter algorithm that supports HDL code generation, use the dsphdl.FIRRateConverter (DSP HDL Toolbox) System object.
Starting in R2022b, this object supports an input signal with an arbitrary frame length, so the input frame length does not have to be a multiple of the decimation factor.
When you generate code, to support arbitrary frame length for fixed-size signals, you must set the AllowArbitraryInputLength
property totrue
while generating code.