%% Minimum-Order Lowpass Filter
% Design a minimum-order lowpass filter with a 500 Hz passband cutoff frequency and 600 Hz stopband cutoff frequency. Specify a sampling frequency of 2000 Hz. Require at least 40 dB of attenuation in the stopband and less than 3 dB of ripple in the passband.

rp = 3;           % Passband ripple in dB 
rs = 40;          % Stopband ripple in dB
fs = 2000;        % Sampling frequency
f = [500 600];    % Cutoff frequencies
a = [1 0];        % Desired amplitudes

%
% Convert the deviations to linear units. Design the filter and visualize its magnitude and phase responses.

dev = [(10^(rp/20)-1)/(10^(rp/20)+1) 10^(-rs/20)]; 
[n,fo,ao,w] = firpmord(f,a,dev,fs);
b = firpm(n,fo,ao,w);
freqz(b,1,1024,fs)
title('Lowpass Filter Designed to Specifications')

% The filter falls slightly short of meeting the stopband attenuation and passband ripple specifications. Using n+1 instead of n in the call to firpm achieves the desired amplitude characteristics.
% Copyright 2015 The MathWorks, Inc.