Code covered by the BSD License  

Highlights from
DFiltFIR

image thumbnail
from DFiltFIR by Peter Kabal
Design linear-phase FIR filters - with upper/lower constraints and flexible specifications

MMdSpec (FType, NCof, BSpec, GridD)
function Gc = MMdSpec (FType, NCof, BSpec, GridD)
% Convert band-by-band specifications into a specification on a dense
% grid of points.
%
% Gc:  Structure containing the specifications on a dense grid of points
%
% FType: Filter type.
%      1 - Multiple passband/stopband filter
%      2 - Multiple passband/stopband filter (sin(x)/x compensation)
%      3 - Differentiator
%      4 - Hilbert transform filter
% NCof:  Number of filter coefficients
% BSpec: Band specifications
% GridD: Grid density. The number of points in the frequency grid used to
%     determine the positions of the extrema of the approximating function
%     is the product of GridD and the number of extrema (approximately
%     Ncof/2). If GridD is NaN, the number of grid points is chosen to be
%     the greater of 1000 or 16 times the number of extrema.

% Resolve the filter type
[FCase, NPar] = MM_FCase (FType, NCof);

% Convert to a specification on a dense grid of frequencies
GB = MMgrid (FCase, NPar, BSpec, GridD);

% Modify the specifications
%  - Receiving filters - sin(x)/x compensation
%  - Differentiators - change slope specification to value
% Transform the specifications for the different cases (odd/even number of
% coefficients, odd/even symmetry) into a common form for approximation by
% a sum of cosines. The grid frequencies are converted to cosine values,
% x = cos(2pi f).
Gc = MMmodSpec (FType, FCase, GB);

return

%-----------------
function [FCase, NPar] = MM_FCase (FType, NCof)

%             Sym   Asym
%     FType:  1  2  3  4    bpf rec dif hil
FCaseData = [ 1  1  3  3;   % NCof odd
              2  2  4  4];  % NCof even

Parity = (mod (NCof, 2) == 0) + 1;
FCase = FCaseData (Parity, FType);

switch FCase
  case 1
    NPar = (NCof + 1) / 2;
  case {2, 4}
    NPar = NCof / 2;
  case 3
    NPar = (NCof - 1) / 2;
end

return

Contact us at files@mathworks.com