MATLAB Examples

# Implement an FIR Decimator in MATLAB

To implement an FIR Decimator, you must first design it by using the designMultirateFIR function. Specify the decimation factor of interest (usually greater than 1) and an interpolation factor equal to 1. You can use the default half-polyphase length of 12 and the default stopband attenuation of 80 dB. Alternatively, you can also specify the half-polyphase length and stopband attenuation values.

Design an FIR decimator with the decimation factor set to 3 and the half-polyphase length set to 14. Use the default stopband attenuation of 80 dB.

```b = designMultirateFIR(1,3,14); ```

Provide the coefficients vector, b, as an input to the dsp.FIRDecimator System object?.

```FIRDecim = dsp.FIRDecimator(3,b); fvtool(FIRDecim); ```

By default, the fvtool shows the magnitude response. Navigate through the fvtool toolbar to see the phase response, impulse response, group delay, and other filter analysis information.

Filter a noisy sine wave input using the FIRDecim object. The sine wave has frequencies at 1000 Hz and 3000 Hz 3000 Hz. The noise is a white Gaussian noise with mean zero and standard deviation 1e-5. The decimated output will have one-third the sample rate as input. Initialize two dsp.SpectrumAnalyzer System objects, one for the input and the other for the output.

```f1 = 1000; f2 = 3000; Fs = 8000; source = dsp.SineWave('Frequency',[f1,f2],'SampleRate',Fs,... 'SamplesPerFrame',1026); specanainput = dsp.SpectrumAnalyzer('SampleRate',Fs,... 'PlotAsTwoSidedSpectrum',false,... 'ShowLegend',true,'YLimits',[-120 30],... 'Title','Noisy Input signal',... 'ChannelNames', {'Noisy Input'}); specanaoutput = dsp.SpectrumAnalyzer('SampleRate',Fs/3,... 'PlotAsTwoSidedSpectrum',false,... 'ShowLegend',true,'YLimits',[-120 30],... 'Title','Filtered output',... 'ChannelNames', {'Filtered output'}); ```

Stream in the input and filter the signal in a processing loop.

```for Iter = 1:100 input = sum(source(),2); noisyInput = input + (10^-5)*randn(1026,1); output = FIRDecim(noisyInput); specanainput(noisyInput) specanaoutput(output) end ```

The input has two peaks: one at 1000 Hz and the other at 3000 Hz. The filter has a lowpass response with a passband frequency of 0.3*pi rad/sample. With a sampling frequency of 8000 Hz, that is a passband frequency of 1200 Hz. The tone at 1000 Hz is unattenuated, because it falls in the passband of the filter. The tone at 3000 Hz is filtered out.

Similarly, you can design an FIR interpolator and FIR rate Converter by providing appropriate inputs to the designMultirateFIR function. To implement the filters, pass the designed coefficients to the dsp.FIRInterpolator and dsp.FIRRateConverter objects.