This example shows how to design FIR halfband filters. Halfband filters are widely used in multirate signal processing applications when interpolating/decimating by a factor of two. Halfband filters are implemented efficiently in polyphase form, because approximately half of its coefficients are equal to zero.

Halfband filters have two important characteristics, the passband and stopband ripples must be the same, and the passband-edge and stopband-edge frequencies are equidistant from the halfband frequency Fs/4 (Or pi/2 rad/sample in normalized frequency).

**Obtaining the Halfband Coefficients**

The `firhalfband`

function returns the coefficients of an FIR halfband equiripple filter. As a simple example, consider a halfband filter whose dealing with data sampled at 96 kHz and a passband frequency of 22 kHz.

Fs = 96e3; Fp = 22e3; N = 100; NUM = firhalfband(N,Fp/(Fs/2)); hfvt = fvtool(NUM,'Fs',Fs,'Color','white'); hfvt.MagnitudeDisplay = 'Zero-phase';

By zooming in to the response, you can verify that the passband and stopband peak-to-peak ripples are the same. Also there is symmetry about the Fs/4 (24 kHz) point. The passband extends up to 22 kHz as specified and the stopband begins at 26 kHz. We can also verify that every other coefficient is equal to zero by looking at the impulse response. This makes the filter very efficient to implement for interpolation/decimation by a factor of 2.

```
hfvt.Analysis = 'impulse';
```

`dsp.FIRHalfbandInterpolator`

and `dsp.FIRHalfbandDecimator`

The `firhalfband`

function provides several other design options. However, for most cases it is preferable to work directly with `dsp.FIRHalfbandInterpolator`

and `dsp.FIRHalfbandDecimator`

. These two system objects will not only design the coefficients, but provide efficient implementation of the polyphase interpolator/decimator. They support filtering double/single precision floating-point data as well as fixed-point data. They also support C and HDL code generation as well as optimized ARM® Cortex® M and Cortex A code generation.

HBi = dsp.FIRHalfbandInterpolator('SampleRate',Fs,... 'Specification','Filter order and transition width','FilterOrder',N,... 'TransitionWidth',4000); fvtool(HBi,'Fs',2*Fs,'Color','white');

In order to perform the interpolation, the `step`

function is used. Because this is a multirate filter, it is important to define what is meant by the sample rate. For this and all other System objects, the sample rate refers to the sample rate of the input signal. However, FVTool defines the sample rate as the rate at which the filter is running. In the case of interpolation, you upsample and then filter (conceptually), therefore the sampling rate of FVTool needs to be specified as 2*Fs because of the upsampling by 2.

FrameSize = 256; SA = dsp.SpectrumAnalyzer('SampleRate',2*Fs,'SpectralAverages',5); SW10 = dsp.SineWave('Frequency',10e3','SampleRate',Fs,... 'SamplesPerFrame',FrameSize); SW20 = dsp.SineWave('Frequency',20e3','SampleRate',Fs,... 'SamplesPerFrame',FrameSize); tic while toc < 10 x = step(SW10) + step(SW20) + 0.01.*randn(FrameSize,1); % 96 kHz y = step(HBi,x); % 192 kHz step(SA,y); end

Notice that the spectral replicas are attenuated by about 40 dB which is roughly the attenuation provided by the halfband filter. Compensating for the group-delay in the filter, it is possible to plot the input and interpolated samples overlaid. Notice that the input samples are retained unchanged at the output of the filter. This is because one of the polyphase branches of the halfband is a pure delay branch which does not change the input samples.

GD = 50; n = 0:2:511; stem(n(1:end-GD/2),x(1:end-GD/2),'k','filled') hold on nu = 0:511; stem(nu(1:end-GD),y(GD+1:end)) legend('Input samples','Interpolated samples')

In the case of decimation, the sample rate specified in `dsp.FIRHalfbandDecimator`

corresponds to the sample rate of the filter, since you filter and then downsample (conceptually). So for decimators, the Fs specified in FVTool does not need to be multiplied by any factor.

FrameSize = 256; FsIn = 2*Fs; HBd = dsp.FIRHalfbandDecimator('SampleRate',FsIn,... 'Specification','Filter order and transition width','FilterOrder',N,... 'TransitionWidth',4000); fvtool(HBd,'Fs',FsIn,'Color','white'); SA = dsp.SpectrumAnalyzer('SampleRate',Fs,'SpectralAverages',5); SW10 = dsp.SineWave('Frequency',10e3','SampleRate',Fs,... 'SamplesPerFrame',FrameSize); SW20 = dsp.SineWave('Frequency',20e3','SampleRate',Fs,... 'SamplesPerFrame',FrameSize); tic while toc < 10 x = step(SW10) + step(SW20) + 0.01.*randn(FrameSize,1); % 96 kHz y = step(HBi,x); % 192 kHz xd = step(HBd,y); % 96 kHz step(SA,xd); end

**Obtaining the Filter Coefficients**

The filter coefficients can be extracted from the interpolator/decimator by using the `tf`

function.

```
NUM = tf(HBi); % Or NUM = TF(HBd);
```

**Using Different Design Specifications**

Instead of specifying the filter order and transition width, you can design a minimum-order filter that provides a given transition width as well as a given stopband attenuation.

Ast = 80; % 80 dB HBi = dsp.FIRHalfbandInterpolator('SampleRate',Fs,... 'Specification','Transition width and stopband attenuation',... 'StopbandAttenuation',Ast,'TransitionWidth',4000); fvtool(HBi,'Fs',2*Fs,'Color','white');

Notice that as with all interpolators, the passband gain in absolute units is equal to the interpolation factor (2 in the case of halfbands). This corresponds to a passband gain of 6.02 dB.

It is also possible to specify the filter order and the stopband attenuation.

HBd = dsp.FIRHalfbandDecimator('SampleRate',Fs,... 'Specification','Filter order and stopband attenuation',... 'StopbandAttenuation',Ast,'FilterOrder',N); fvtool(HBd,'Fs',Fs,'Color','white');

Unlike interpolators, decimators have a gain of 1 (0 dB) in the passband.

**Using Halfband Filters for Filter Banks**

Halfband interpolators and decimators can be used to efficiently implement synthesis/analysis filter banks. The halfband filters shown so far have all been lowpass filters. With a single extra adder, it is possible to obtain a highpass response in addition to the lowpass response and use the two responses for the filter bank implementation.

The following code simulates a quadrature mirror filter (QMF) bank. An 8 kHz signal consisting of 1 kHz and 3 kHz sine waves is separated into two 4 kHz signals using a lowpass/highpass halfband decimator. The lowpass signal retains the 1 kHz sine wave while the highpass signal retains the 3 kHz sine wave (which is aliased to 1 kHz after downsampling). The signals are then merged back together with a synthesis filter bank using a halfband interpolator. The highpass branch upconverts the aliased 1 kHz sine wave back to 3 kHz. The interpolated signal has an 8 kHz sampling rate.

Fs1 = 8000; % Units = Hz Spec = 'Filter order and transition width'; Order = 52; TW = 4.1e2; % Units = Hz % Construct FIR Halfband Interpolator HBi = dsp.FIRHalfbandInterpolator(... 'Specification',Spec,... 'FilterOrder',Order,... 'TransitionWidth',TW,... 'SampleRate',Fs1/2,... 'FilterBankInputPort',true); % Construct FIR Halfband Decimator HBd = dsp.FIRHalfbandDecimator(... 'Specification',Spec,... 'FilterOrder',Order,... 'TransitionWidth',TW,... 'SampleRate',Fs1); % Input f1 = 1000; f2 = 3000; InputWave = dsp.SineWave('Frequency',[f1,f2],'SampleRate',Fs1,... 'SamplesPerFrame',1024,'Amplitude',[1 0.25]); % Construct Spectrum Analyzer object to view the input and output SA = dsp.SpectrumAnalyzer('SampleRate',Fs1,... 'PlotAsTwoSidedSpectrum',false,'ShowLegend',true,'YLimits',... [-120 30],... 'Title',... 'Input Signal and Output Signal of Quadrature Mirror Filter'); SA.ChannelNames = {'Input','Output'}; tic while toc < 10 Input = sum(step(InputWave),2); NoisyInput = Input+(10^-5)*randn(1024,1); [Lowpass, Highpass] = step(HBd,NoisyInput); Output = step(HBi,Lowpass,Highpass); step(SA,[NoisyInput,Output]); end

**Advanced Design Options: Specifying Different Design Algorithms**

All designs presented so far have been optimal equiripple designs. Using `fdesign.interpolator`

and `fdesign.decimator`

, other design algorithms are available.

Fs = 44.1e3; N = 90; TW = 1000/Fs; % Transition width d = fdesign.interpolator(2,'halfband','N,TW',N,TW); HBeq = design(d,'equiripple','SystemObject',true); % Equiripple design HBls = design(d,'firls','SystemObject',true); % Least-squares design HBka = design(d,'kaiserwin','SystemObject',true); % Kaiser-window design

You can compare the designs with FVTool. The different designs allow for tradeoffs between minimum stopband attenuation and more overall attenuation.

hfvt = fvtool(HBeq,HBls,HBka,'Fs',2*Fs,'Color','white'); legend(hfvt, 'Equiripple design', 'Least-squares design', ... 'Kaiser-window design')

**Controlling the Stopband Attenuation**

Alternatively, one can specify the order and the stopband attenuation. This allows for tradeoffs between overall stopband attenuation and transition width.

Ast = 60; % Minimum stopband attenuation d = fdesign.interpolator(2,'halfband','N,Ast',N,Ast); HBeq = design(d,'equiripple','SystemObject',true); % Equiripple design HBka = design(d,'kaiserwin','SystemObject',true); % Kaiser-window design hfvt = fvtool(HBeq,HBka,'Fs',2*Fs,'Color','white'); legend(hfvt, 'Equiripple design', 'Kaiser-window design')

**Minimum-Order Designs**

Kaiser window designs can also be used in addition to equiripple designs when designing a filter of the minimum-order necessary to meet the design specifications. The actual order for the Kaiser window design is larger than that needed for the equiripple design, but the overall stopband attenuation is better in return.

Fs = 44.1e3; TW = 1000/(Fs/2); % Transition width Ast = 60; % 60 dB minimum attenuation in the stopband d = fdesign.decimator(2,'halfband','TW,Ast',TW,Ast); HBeq = design(d,'equiripple','SystemObject',true); % Equiripple design HBka = design(d,'kaiserwin','SystemObject',true); % Kaiser-window design hfvt = fvtool(HBeq,HBka,'Fs',Fs,'Color','white'); legend(hfvt, 'Equiripple Design', 'Kaiser-window design')

**Equiripple Designs with Increasing Stopband Attenuation**

Instead of designing Kaiser window filters, it is also possible to obtain increasing stopband attenuation with modified 'equiripple' designs.

HBeq1 = design(d,'equiripple','StopbandShape','1/f','StopbandDecay',4,... 'SystemObject',true); HBeq2 = design(d,'equiripple','StopbandShape','linear', ... 'StopbandDecay',53.333,'SystemObject',true); hfvt = fvtool(HBeq1,HBeq2,'Fs',Fs,'Color','white'); legend(hfvt,'Stopband decaying as (1/f)^4','Stopband decaying linearly')

**Highpass Halfband Filters**

A highpass halfband filter can be obtained from a lowpass halfband filter by changing the sign of every second coefficient. Alternatively, one can directly design a highpass halfband by setting the 'Type' property to 'Highpass'.

d = fdesign.decimator(2,'halfband','Type','Highpass','TW,Ast',TW,Ast); HBhp = design(d,'equiripple','StopbandShape','linear', ... 'StopbandDecay',53.333,'SystemObject',true); hfvt = fvtool(HBhp,HBeq2,'Fs',Fs,'Color','white'); legend(hfvt, 'Highpass halfband filter', 'Lowpass halfband filter')

Was this topic helpful?