dsp.FIRHalfbandDecimator - Decimate signal using polyphase FIR halfband filter - MATLAB (original) (raw)

Decimate signal using polyphase FIR halfband filter

Description

The dsp.FIRHalfbandDecimator System object™ performs an efficient polyphase decimation of the input signal by a factor of two. You can use dsp.FIRHalfbandDecimator to implement the analysis portion of a two-band filter bank to filter a signal into lowpass and highpass subbands.dsp.FIRHalfbandDecimator uses an FIR equiripple design or a Kaiser window design to construct the halfband filters and a polyphase implementation to filter the input.

To filter and downsample your data:

  1. Create the dsp.FIRHalfbandDecimator object and set its properties.
  2. 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

`firhalfbanddecim` = dsp.FIRHalfbandDecimator returns a halfband decimator, firhalfbanddecim, with the default settings. Under the default settings, the System object filters and downsamples the input data with a halfband frequency of 11025 Hz, a transition width of 4.1 kHz, and a stopband attenuation of 80 dB. The design method is set to "Auto".

`firhalfbanddecim` = dsp.FIRHalfbandDecimator(`Name=Value`) sets properties using one or more name-value arguments. For example, to specify a filter order of 52, setFilterOrder to 52.

example

Properties

expand all

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

Filter design parameters, specified as a character vector. When you set Specification to one of the following, you choose two of the three available design parameters to design the FIR Halfband filter.

The filter is designed using the optimal equiripple filter design method or the kaiser-window-based design method.

When you set Specification to"Coefficients", you specify the halfband filter coefficients directly through the Numerator property.

Filter order, specified as an even positive integer.

Dependencies

To enable this property, set Specification to"Filter order and stopband attenuation" or"Filter order and transition width".

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Stopband attenuation in dB, specified as a positive real scalar.

Dependencies

To enable this property, set Specification to"Filter order and stopband attenuation" or"Transition width and stopband attenuation".

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Transition width in Hz, specified as a positive real scalar or in normalized frequency units (since R2023b).

If you set theNormalizedFrequency property to:

(since R2023b)

Dependencies

To enable this property, set Specification to"Transition width and stopband attenuation" or "Filter order and transition width".

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

FIR halfband filter coefficients, specified as a row vector. The coefficients must comply with the FIR halfband impulse response format. For details on this format, see Halfband Filters and FIR Halfband Filter Design. If half the order of the filter, (length(Numerator) - 1)/2, is even, every other coefficient starting from the first coefficient must be a zero except for the center coefficient which must be a 0.5. If half the order of the filter is odd, the sequence of alternating zeros with a 0.5 at the center starts at the second coefficient.

Dependencies

To enable this property, set Specification to"Coefficients".

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Specify the filter design method as one of the following:

For more details on these two methods, see Algorithms.

Dependencies

To enable this property, set Specification to"Transition width and stopband attenuation","Filter order and stopband attenuation", or"Filter order and transition width".

Data Types: char | string

Since R2023b

Flag to set frequencies in normalized units, specified as one of these values:

Dependency

To enable this property, set Specification to any accepted value except "Coefficients".

Data Types: logical

Input sample rate in Hz, specified as a positive real scalar. The input sample rate defaults to 44100 Hz. If you specify transition width as one of your filter design parameters, the transition width cannot exceed 1/2 the input sample rate.

Dependency

To enable this property, set:

Data Types: single | double

Code Generation Properties

Allow arbitrary frame length for fixed-size input signals in the generated code, specified as true orfalse. When you specify:

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

Word and fraction lengths of coefficients, specified as a signed or unsigned numerictype object. The default,numerictype(1,16) corresponds to a signed numeric type object with 16-bit coefficients and a fraction length determined based on the coefficient values, to give the best possible precision.

This property is not tunable.

Word length of the output is same as the word length of the input. Fraction length of the output is computed such that the entire dynamic range of the output can be represented without overflow. For details on how the fraction length of the output is computed, see Fixed-Point Precision Rules for Avoiding Overflow in FIR Filters.

Rounding method for output fixed-point operations, specified as a character vector. For more information on the rounding modes, see Precision and Range.

Usage

Syntax

Description

[ylow](#d126e292140) = firhalfbanddecim([x](#d126e292073)) filters the input signal x using the FIR halfband filter,firhalfbanddecim, and downsamples the output by a factor of 2.

example

[[ylow](#d126e292140),[yhigh](#d126e292231)] = firhalfbanddecim([x](#d126e292073)) computes the ylow and yhigh, of the analysis filter bank, firhalfbanddecim for inputx. A _Ki_-by-N input matrix is treated as N independent channels. The System object generates two power-complementary output signals by adding and subtracting the two polyphase branch outputs respectively.ylow and yhigh are of the same size and data type.

example

Input Arguments

expand all

Data input, specified as a column vector or a matrix of size_Ki_-by-N. If the input signal is a matrix, each column of the matrix is treated as an independent channel.

The number of rows in the input signal Ki can be arbitrary and does not have to be a multiple of 2. (since R2023b)

This object supports variable-size input signal, that is, the frame length (number of rows) of the signal can change even when the object is locked. However, the number of channels (columns) must remain constant.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | fi
Complex Number Support: Yes

Output Arguments

expand all

Lowpass subband of decimator output, returned as a column vector or a matrix. The output, ylow is a lowpass halfband filtered and downsampled version of the input x. Due to the halfband nature of the filter, the downsampling factor is always 2.

When the input is of size_Ki_-by-N, and Ki is not a multiple of 2, the lowpass subband has an upper bound size ofceil(Ki/2)-by-N. (since R2023b)

If Ki is a multiple of 2, then the lowpass subband is of size (Ki/2)-by-N. The number of channels (columns) does not change.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | fi
Complex Number Support: Yes

Highpass subband of decimator output, returned as a column vector or a matrix. The output, yhigh is a highpass halfband filtered and downsampled version of the input x. Due to the halfband nature of the filter, the downsampling factor is always 2.

When the input is of size_Ki_-by-N, and Ki is not a multiple of 2, the highpass subband has an upper bound size ofceil(Ki/2)-by-N. (since R2023b)

If Ki is a multiple of 2, then the highpass subband is of size (Ki/2)-by-N. The number of channels (columns) does not change.

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:

expand all

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

collapse all

Create two lowpass halfband decimation filters. The design method in the first filter is set to "Equiripple" and in the second filter is set to "Kaiser".

Specify the filter order to be 52. Specify the transition width in normalized frequency units.

filterspec = "Filter order and transition width"; Order = 52; TW = 0.1859; firhalfbanddecimEqui = dsp.FIRHalfbandDecimator(... NormalizedFrequency=true,... Specification=filterspec,... FilterOrder=Order,... TransitionWidth=TW,... DesignMethod="Equiripple"); firhalfbanddecimKaiser = dsp.FIRHalfbandDecimator(... NormalizedFrequency=true,...... Specification=filterspec,... FilterOrder=Order,... TransitionWidth=TW,... DesignMethod="Kaiser");

Plot the impulse response of both the filters. The zeroth-order coefficient is delayed 26 samples, which is equal to the group delay of the filter. This yields a causal halfband filter.

hfvt = filterAnalyzer(firhalfbanddecimEqui,firhalfbanddecimKaiser,... Analysis="impulse"); setLegendStrings(hfvt,["Equiripple","Kaiser"])

Plot the magnitude and phase response.

If the filter specifications are tight, say a very high filter order with a very narrow transition width, the filter designed using the "Kaiser" method converges more effectively.

hvftMag = filterAnalyzer(firhalfbanddecimEqui,firhalfbanddecimKaiser,... Analysis="magnitude"); setLegendStrings(hvftMag,["Equiripple","Kaiser"])

hvftPhase = filterAnalyzer(firhalfbanddecimEqui,firhalfbanddecimKaiser,... Analysis="phase"); setLegendStrings(hvftPhase,["Equiripple","Kaiser"])

Design an equiripple FIR halfband decimator object of order 48 using the designHalfbandFIR function. Set the Verbose argument to true.

hbFIR = designHalfbandFIR(FilterOrder=48,SystemObject=true,... Structure="decim",Verbose=true)

designHalfbandFIR(FilterOrder=48, TransitionWidth=0.1, DesignMethod="equiripple", Passband="lowpass", Structure="decim", InputSampleRate="normalized", PhaseConstraint="linear", Datatype="double", SystemObject=true)

hbFIR = dsp.FIRHalfbandDecimator with properties:

Main Specification: 'Coefficients' Numerator: [0 -0.0041 0 0.0040 0 -0.0058 0 0.0082 0 -0.0114 0 0.0155 0 -0.0209 0 0.0286 0 -0.0400 0 0.0597 0 -0.1037 0 0.3175 0.5000 0.3175 0 -0.1037 0 0.0597 0 -0.0400 0 0.0286 0 -0.0209 0 0.0155 0 -0.0114 0 0.0082 0 -0.0058 0 0.0040 0 -0.0041 0]

Show all properties

Create a dsp.DynamicFilterVisualizer object and visualize the magnitude response of the filter.

dfv = dsp.DynamicFilterVisualizer(NormalizedFrequency=true); dfv(hbFIR);

The input is a cosine wave with an angular frequency of π4 radians/sample.

input = cos(pi/4*(0:39)');

Decimate the cosine signal using the FIR halfband decimator.

Plot the original and decimated signals. In order to plot the two signals in the same plot, you must account for the output delay of the FIR halfband decimator and the scaling introduced by the filter. Use the outputDelay function to compute the delay introduced by the decimator. Shift the output by this delay value.

Visualize the input and the resampled signals. After a short transition, the output converges to a cosine of frequency π2, as expected, which is twice the frequency of the input signal, π4. Due to the decimation factor of 2, the output samples coincide with every other input sample.

[delay,FsOut] = outputDelay(hbFIR,FsIn=1)

nInput = (0:length(input)-1); tOutput = (0:length(output)-1)/FsOut-delay; stem(tOutput,output,"filled",MarkerSize=4); hold on; stem(nInput,input); hold off; xlim([-10,15]) legend("Decimated by 2","Input signal","Location","best");

Figure contains an axes object. The axes object contains 2 objects of type stem. These objects represent Decimated by 2, Input signal.

Use a halfband analysis filter bank and interpolation filter to extract the low frequency subband from a speech signal.

Note: The audioDeviceWriter System object™ is not supported in MATLAB Online.

Set up the audio file reader, the analysis filter bank, audio device writer, and interpolation filter. The sample rate of the audio data is 22050 Hz. The order of the halfband filter is 52, with a transition width of 2 kHz.

Set the design method to "Auto". This mode chooses one of the filter design methods, equiripple or Kaiser, based on the design parameters of the filter.

afr = dsp.AudioFileReader(... "speech_dft.mp3",... SamplesPerFrame=1024);

filtSpec = "Filter order and transition width"; Order = 52; TW = 2000;

firhalfbanddecim = dsp.FIRHalfbandDecimator(... Specification=filtSpec,... FilterOrder=Order,... TransitionWidth=TW,... DesignMethod="Auto",... SampleRate=afr.SampleRate);

firhalfbandinterp = dsp.FIRHalfbandInterpolator(... Specification=filtSpec,... FilterOrder=Order,... TransitionWidth=TW,... DesignMethod="Auto",... SampleRate=afr.SampleRate/2);

adw = audioDeviceWriter(SampleRate=afr.SampleRate);

View the magnitude response of the halfband filter.

filterAnalyzer(firhalfbanddecim)

Read the speech signal from the audio file in frames of 1024 samples. Filter the speech signal into lowpass and highpass subbands with a halfband frequency of 5512.5 Hz. Reconstruct a lowpass approximation of the speech signal by interpolating the lowpass subband. Play the filtered output.

while ~isDone(afr) audioframe = afr(); xlo = firhalfbanddecim(audioframe); ylow = firhalfbandinterp(xlo); adw(ylow); end

Wait until the audio file is played to the end, then close the input file and release the audio output resource.

release(afr);
release(adw);

Use a halfband decimator and interpolator to implement a two-channel filter bank. This example uses an audio file input and shows that the power spectrum of the filter bank output does not differ significantly from the input.

Note: The audioDeviceWriter System object™ is not supported in MATLAB Online.

Set up the audio file reader and device writer. Construct the FIR halfband decimator and interpolator. Finally, set up the spectrum analyzer to display the power spectra of the filter-bank input and output.

AF = dsp.AudioFileReader("speech_dft.mp3",SamplesPerFrame=1024); AP = audioDeviceWriter(SampleRate=AF.SampleRate);

filterspec = "Filter order and transition width"; Order = 52; TW = 2000;

firhalfbanddecim = dsp.FIRHalfbandDecimator(... Specification=filterspec,FilterOrder=Order,... TransitionWidth=TW,DesignMethod="Auto",... SampleRate=AF.SampleRate);

firhalfbandinterp = dsp.FIRHalfbandInterpolator(... Specification=filterspec,FilterOrder=Order,... TransitionWidth=TW,SampleRate=AF.SampleRate/2,... DesignMethod="Auto",... FilterBankInputPort=true);

SpecAna = spectrumAnalyzer(SampleRate=AF.SampleRate,... PlotAsTwoSidedSpectrum=false,... ShowLegend=true,... ChannelNames={"Input signal","Filtered output signal"});

Read the audio 1024 samples at a time. Filter the input to obtain the lowpass and highpass subband signals decimated by a factor of two. This is the analysis filter bank. Use the halfband interpolator as the synthesis filter bank. Display the running power spectrum of the audio input and the output of the synthesis filter bank. Play the output.

while ~isDone(AF) audioInput = AF(); [xlo,xhigh] = firhalfbanddecim(audioInput); audioOutput = firhalfbandinterp(xlo,xhigh); spectrumInput = [audioInput audioOutput]; SpecAna(spectrumInput); AP(audioOutput); end

release(AF); release(AP); release(SpecAna);

Create a halfband decimator. Use a minimum-order design with a transition width of 0.0952 in normalized frequency units and a stopband attenuation of 60 dB.

Set the design method to "Auto". This mode chooses one of the filter design methods, equiripple or Kaiser, based on the specified filter design parameters.

hfirhalfbanddecim = dsp.FIRHalfbandDecimator(... NormalizedFrequency=true,... Specification="Transition width and stopband attenuation",... TransitionWidth=0.0952,... StopbandAttenuation=60,... DesignMethod="Auto");

Filter a two-channel input into low and highpass subbands. The input signal can be of arbitrary frame size, that is, the number of input rows does not have to be a multiple of 2.

x = randn(1025,2); [ylow,yhigh] = hfirhalfbanddecim(x);

More About

expand all

An ideal lowpass halfband filter is given by

An ideal filter is not realizable because the impulse response is noncausal and not absolutely summable. However, the impulse response of an ideal lowpass filter possesses some important properties that are required in a realizable approximation. The impulse response of an ideal lowpass halfband filter is:

The ideal highpass halfband filter is given by

Evaluating the preceding integral gives the following impulse response

The impulse response of an ideal highpass halfband filter is:

The FIR halfband decimator uses a causal FIR approximation to the ideal halfband response, which is based on minimizing the ℓ∞ norm of the error (minimax). See Algorithms for more information.

The coefficients of a Kaiser window are computed from this equation:

where _I_0 is the zeroth-order modified Bessel function of the first kind.

To obtain a Kaiser window that represents an FIR filter with stopband attenuation of_α_ dB, use this β.

The filter order n is given by:

where Δ_ω_ is the transition width.

Algorithms

expand all

The FIR halfband decimator algorithm uses the equiripple or the Kaiser window method to design the FIR halfband filter. When the design constraints are tight, such as very high stopband attenuation or very narrow transition width, use the Kaiser window method. When the design constraints are not tight, use the equiripple method. If you are not sure of which method to use, set the design method to Auto. In this mode, the algorithm automatically chooses a design method that optimally meets the specified filter constraints.

Halfband Equiripple Design

In the equiripple method, the algorithm uses a minimax (minimize the maximum error) FIR design to design a fullband linear phase filter with the desired specifications. The algorithm upsamples a fullband filter to replace the even-indexed samples of the filter with zeros and creates a halfband filter. It then sets the filter tap corresponding to the group delay of the filter in samples to 1/2. This yields a causal linear-phase FIR filter approximation to the ideal halfband filter defined in Halfband Filters. See [1] for a description of this filter design method using the Remez exchange algorithm. Since you can design a filter using this approximation method with a constant ripple both in the passband and stopband, the filter is also known as the equiripple filter.

Kaiser Window Design

In the Kaiser window method, the algorithm first truncates the ideal halfband filter defined in Halfband Filters, then it applies a Kaiser window defined in Kaiser Window. This yields a causal linear-phase FIR filter approximation to the ideal halfband filter.

For more information on designing FIR halfband filters, see FIR Halfband Filter Design.

The FIR halfband decimator uses an efficient polyphase implementation for halfband filters when you filter the input signal. The chief advantage of the polyphase implementation is that you can downsample the signal prior to filtering. This allows you to filter at the lower sampling rate.

Splitting a filter’s impulse response h(n) into two polyphase components results in an even polyphase component with_z_-transform of

and an odd polyphase component with _z_-transform of

The _z_-transform of the filter can be written in terms of the even and odd polyphase components as

You can represent filtering the input signal and then downsampling it by 2 using this figure.

Using the multirate noble identity for downsampling, you can move the downsampling operation before the filtering. This allows you to filter at the lower rate.

For a halfband filter, the only nonzero coefficient in the even polyphase component is the coefficient corresponding to z0. Implementing the halfband filter as a causal FIR filter shifts the nonzero coefficient to approximately z-N/4 where_N_ is the number of filter taps. This process is illustrated in the following figure. The top plot shows a halfband filter of order 52. The bottom plot shows the even polyphase component. Both of these filters are noncausal. Delaying the even polyphase component by 13 samples creates a causal FIR filter.

To efficiently implement the halfband decimator, the algorithm replaces the delay block and downsampling operator with a commutator switch. When the first input sample is delivered, the commutator switch feeds this input to the even branch and the halfband decimator computes the first output value. As more input samples come in, the switch delivers one sample at a time to each branch alternatively. The decimator generates output every time the even branch generates an output. This halves the sampling rate of the input signal.

Which polyphase component reduces to a simple delay depends on whether the half order of the filter is even or odd. Here is the implementation when the filter half order is even. In this diagram, _H_0(z) becomes the gain followed by the delay.

When the filter half order is odd,_H_1(z) becomes the gain followed by the delay. This is because the delay required to make the even polyphase component causal can be odd or even depending on the filter half order.

To confirm this behavior, run the following code in the MATLAB command prompt and inspect the polyphase components of the following filters.

filterspec = "Filter order and stopband attenuation"; halfOrderEven = dsp.FIRHalfbandDecimator(Specification=filterspec,... FilterOrder=64,StopbandAttenuation=80,DesignMethod="Auto"); halfOrderOdd = dsp.FIRHalfbandDecimator(Specification=filterspec,... FilterOrder=54,StopbandAttenuation=80,DesignMethod="Auto"); polyphase(halfOrderEven) polyphase(halfOrderOdd)

Analysis Filter Bank

The FIR halfband decimator acts as an analysis filter bank and generates two power-complementary output signals by adding and subtracting the two polyphase branch outputs respectively.

Analysis portion of the two-channel filter bank. Switch at the input operates at a sample rate of fs. First branch contains H0(z) and second branch contains H1(z). Outputs from both branches are added to form the lowpass output on the first branch. Outputs from both branches are subtracted to from the highpass output on the second branch. Output sample rate is fs/2.

For more information on filter banks, see Overview of Filter Banks.

To summarize, the FIR halfband decimator:

References

[1] Harris, F.J. Multirate Signal Processing for Communication Systems, Prentice Hall, 2004, pp. 208–209.

Extended Capabilities

Version History

Introduced in R2014b

expand all

When you set the NormalizedFrequency property totrue, you must specify the transition width in normalized frequency units (0 to 1).

When you set the NormalizedFrequency property totrue while creating the object and you do not set the transition width, the object automatically sets the default transition width to normalized frequency units using the default sample rate of 44100 Hz.

firhalfbanddecim = dsp.FIRHalfbandDecimator(NormalizedFrequency=true)

firhalfbanddecim = dsp.FIRHalfbandDecimator with properties:

      Specification: "Transition width and stopband attenuation"
    TransitionWidth: 0.1859
StopbandAttenuation: 80
       DesignMethod: "Auto"
NormalizedFrequency: true

Show all properties

When you set the NormalizedFrequency property to true after you create the object, you must specify the transition width in normalized units before you run the object algorithm.

firhalfbanddecim = dsp.FIRHalfbandDecimator

firhalfbanddecim = dsp.FIRHalfbandDecimator with properties:

      Specification: "Transition width and stopband attenuation"
    TransitionWidth: 4100
StopbandAttenuation: 80
       DesignMethod: "Auto"
NormalizedFrequency: false
         SampleRate: 44100

Show all properties

To specify the normalized frequency values, setNormalizedFrequency totrue and manually convert the frequency values in Hz to normalized values using the input sample rate in Hz. For example, if the input sample rate is 44100 Hz, you can compute the corresponding values in normalized units using these equations.

firhalfbanddecim.NormalizedFrequency = true; firhalfbanddecim.TransitionWidth = 4100/(44100/2)

firhalfbanddecim = dsp.FIRHalfbandDecimator with properties:

      Specification: "Transition width and stopband attenuation"
    TransitionWidth: 0.1859
StopbandAttenuation: 80
       DesignMethod: "Auto"
NormalizedFrequency: true

Show all properties

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

When you generate code, to support arbitrary frame length for fixed-size signals, you must set the AllowArbitraryInputLength property totrue while generating code.

See Also

Functions

Objects

Blocks

Topics