# Complex Bandpass Filter Design

This example shows how to design complex bandpass filters. Complex bandpass filters are used in many applications from IF subsampling digital down converters to vestigial sideband modulation schemes for analog and digital television broadcast. One easy way to design a complex bandpass filter is to start with a lowpass prototype and apply a complex shift frequency transformation. This example reviewes lowpass prototypes from single-stage single-rate FIR filters to multistage multirate FIR filters to IIR filters.

### Single-Stage Single-Rate and Multirate FIR Design

In a single-rate FIR design, you multiply each set of filter coefficients by (also known as heterodyne with) a complex exponential.

Rotate the zeros of the lowpass Nyquist filter prototype by a normalized frequency of 0.6.

```Hlp = design(fdesign.nyquist(8),SystemObject=true); % Lowpass prototype N = length(Hlp.Numerator)-1; Fc = .6; % Desired frequency shift Hbp = clone(Hlp); Hbp.Numerator = Hbp.Numerator.*exp(1j*Fc*pi*(0:N)); hfvt = fvtool(Hlp,Hbp,Color='white'); legend(hfvt,'Lowpass Prototype','Complex Bandpass',Location='NorthWest')```

You can apply the same technique to single-stage multirate filters as well.

### Multistage Multirate FIR Design

In multistage multirate FIR filters, you need to account for the different relative frequencies that each filter operates on. In a multistage decimator, the desired frequency shift applies only to the first stage. Subsequent stages must also scale the desired frequency shift by their respective cumulative decimation factor.

```Hd = designMultistageDecimator(16,16,0.1,75); Fc = -.2; % Desired frequency shift Hdbp = clone(Hd); Fck = Fc; for k = 1:Hdbp.getNumStages Stagek = Hdbp.(sprintf('Stage%i',k)); Nk = length(Stagek.Numerator)-1; Stagek.Numerator = Stagek.Numerator.*exp(1j*Fck*pi*(0:Nk)); % Update the frequency shift applied to the k-th stage Fck = Fck*Stagek.DecimationFactor; end hfvt = fvtool(Hd,Hdbp); legend(hfvt,'Lowpass Prototype','Complex Bandpass',Location='NorthWest')```

Similarly, in a multistage interpolator, the desired frequency shift applies only to the last stage. Previous stages must also scale the desired frequency shift by their respective cumulative interpolation factor.

```Hi = designMultistageInterpolator(16,16,0.1,75); Fc = .4; % Desired frequency shift Hibp = clone(Hi); Fck = Fc; for k = Hibp.getNumStages:-1:1 Stagek = Hibp.(sprintf('Stage%i',k)); Nk = length(Stagek.Numerator)-1; Stagek.Numerator = Stagek.Numerator.*exp(1j*Fck*pi*(0:Nk)); % Update the frequency shift applied to the k-th stage Fck = Fck* Stagek.InterpolationFactor; end hfvt = fvtool(Hi,Hibp); legend(hfvt,'Lowpass Prototype','Complex Bandpass',Location='NorthWest')```

You can design multistage bandpass filters easily by using the `dsp.ComplexBandpassDecimator` System object™. The object designs the bandpass filter based on the specified decimation factor, center frequency, and sample rate. There is no need to translate lowpass coefficients to bandpass as in the filters you designed in the previous steps. The object does this for you.

Design a complex bandpass filter with a decimation factor of 16, a center frequency of 5 KHz, a sampling rate of 44.1 KHz, a transition width of 100 Hz, and a stopband attenuation of 75 dB using the System object.

```bp = dsp.ComplexBandpassDecimator(16,5000,SampleRate=44100,... TransitionWidth=100,... StopbandAttenuation=75);```

Visualize the filter response using the `freqz` function.

`freqz(bp)`

Visualize the response of the different filter stages using the `visualizeFilterStages` function.

`visualizeFilterStages(bp);`

Only the first filter is shifted to 5 KHz. The subsequent filter stages are lowpass and have real coefficients. Set the `MinimizeComplexCoefficients` property to false to shift all filter stages to 5000 KHz.

Use the `cost` function to calculate the cost of the bandpass filter.

`cost(bp)`
```ans = struct with fields: NumCoefficients: 144 NumStates: 272 RealMultiplicationsPerInputSample: 27.8750 RealAdditionsPerInputSample: 27 ```

### Single-Rate IIR Design

In single-rate IIR designs, you can either use a complex frequency shift transformation or a lowpass to complex bandpass IIR transformation. In the latter case, you can also modify the bandwidth of the bandpass filter.

```Fp = .2; % Design a lowpass prototype, and obtain the second order coefficients Hiirlp = design(fdesign.lowpass(Fp,.25,.5,80),'ellip',SystemObject=true); B = (Hiirlp.ScaleValues(1:end-1)').*Hiirlp.Numerator; A = Hiirlp.Denominator; % Perform lowpass to complex bandpass transform Fc = .6; % Desired frequency shift [Bc,Ac] = iirlp2bpc(B,A, ... % Transform lowpass to complex bandpass Fp,[Fc-Fp, Fc+Fp]); % Lowpass passband frequency mapped % to bandpass passband frequencies % Construct a filter object and plot the responses Hiircbp = dsp.SOSFilter(Bc, Ac); hfvt = fvtool(Hiirlp,Hiircbp); legend(hfvt,'Lowpass Prototype','Complex Bandpass',Location='NorthWest')```