%% Lowpass Butterworth Filter Specification and Design
% Design a lowpass Butterworth filter that has a passband edge frequency of 0.4\pi rad/sample, a stopband frequency of 0.5\pi rad/sample, a passband ripple of 1 dB, and a stopband attenuation of 80 dB.
% Create a lowpass filter design specification object using the fdesign.lowpass function. Specify the design parameters.

lowpassSpecs = fdesign.lowpass(0.4,0.5,1,80);

%
% To view a list of design methods available for the specification object, use the designmethods function. If multiple methods are available, pick one that best meets the design criteria. For this example, pick 'butter'.

designmethods(lowpassSpecs,'SystemObject',true)

%
% Furthermore, you can specify the design options used in designing the filter. To see a list of available options, run the designoptions function on lowpassSpecs. The design options are dependent on the design method you pick. The design method, in this case, 'butter', must be specified as an argument to the designoptions function.

designoptions(lowpassSpecs,'butter','Systemobject',true)

%
% The filter order necessary to meet a set of design constraints must also be rounded up to an integer value. This loosens some of the constraints, and as a consequence, some design specifications are met while others are exceeded. The 'MatchExactly' option allows you to match the passband or stopband exactly while exceeding the specification for the other band. Design the filter so that it matches the passband exactly.
% The resulting filter is a dsp.BiquadFiter System object™. For details on how to apply this filter on streaming data, refer to dsp.BiquadFilter.

IIRbutter = design(lowpassSpecs,'butter','MatchExactly','passband', ...
    'SystemObject',true)

%
% Use fvtool to visualize the magnitude response of the filter.

fvtool(IIRbutter)

% Copyright 2019 The MathWorks, Inc.