-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathislinphase.m
151 lines (134 loc) · 4.55 KB
/
islinphase.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
## Copyright (C) 2023 Leonardo Araujo <[email protected]>
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; see the file COPYING. If not, see
## <https://www.gnu.org/licenses/>.
## -*- texinfo -*-
## @deftypefn {Function File} {@var{flag} = } islinphase (@var{b}, @var{a})
## @deftypefnx {Function File} {@var{flag} = } islinphase (@var{sos})
## @deftypefnx {Function File} {@var{flag} = } islinphase (@dots{}, @var{tol})
##
## Determine whether a digital filter has linear phase. Returns a flag
## equal to true if the given filter has linear phase and false otherwise.
## The filter might be defined by the numerator coefficients, @var{b}, and the
## denominator coefficients, @var{a}, or, alternatively, by a matrix of
## second-order sections, @var{sos}. A tolerance @var{tol} might be given to
## define when two numbers are close enough to be considered equal.
##
## Example:
##
## @example
## b = [1 2 4 4 2 1];
## freqz (b);
## islinphase (b)
## @end example
##
## References:
##
## [1] Oppenheim, Alan, and Ronald Schafer. Discrete-Time Signal Processing.
## 3rd edition, Pearson, 2009.
##
## [2] Paquelet, Stéphane, and Vincent Savaux. “On the Symmetry of FIR Filter
## with Linear Phase.” Digital Signal Processing, vol. 81, Oct. 2018, pp. 57–60.
## https://doi.org/10.1016/j.dsp.2018.07.011.
## @end deftypefn
function flag = islinphase (b, a, tol)
if (nargin < 1 || nargin > 3 )
print_usage;
endif
if (nargin == 2 && ( ! isrow (a) || ! isrow (b) ))
error ( "coefficient array should be a row vector" );
endif
if (nargin == 1 && isrow (b)), a = 1; endif
if nargin < 3,
tol = eps^(3/4);
elseif length (tol) > 1,
error ( "a scalar is expected as the tolerance value" );
endif
if (nargin > 1 && isrow (a) && isrow (b)) || (nargin == 1 && isrow (b))
% remove leading and trailing zeros
b = b(find (b, 1):end);
a = a(find (a, 1):end);
b = b(1:find (b,1,"last"));
a = a(1:find (a,1,"last"));
% True linear phase characteristics are not achievable with traditional
% IIR (Infinite Impulse Response) filters due to their recursive nature.
if length (a) == 1 % FIR filter
% The corresponding impulse response of a linear phase system may
% have even symmetry about α, if 2α is an integer; i.e.,
% h[2α − n] = h[n].
% If it is a FIR filter, it suffices to check if the numerator is symmetric.
if mod ( length(b), 2) == 0,
if all (b(1:length(b)/2) == fliplr (b(length(b)/2+1:end)))
flag = true;
else
flag = false;
endif
else
if all (b(1:floor (length(b)/2)) == fliplr (b(ceil (length(b)/2)+1:end)))
flag = true;
else
flag = false;
endif
endif
else
% Generalized linear phase systems have constant group delay.
% Use grpdelay and check if it is constant up to a given tolerance.
gd = grpdelay(b, a, 128);
mgd = mean (gd);
if all (abs (gd - mgd) < tol)
flag = true;
else
flag = false;
endif
endif
elseif (nargin == 1 && all(size (b) > [1 1]))
[b, a] = sos2tf (b);
flag = islinphase (b, a, tol);
endif
endfunction
%!demo
%! f = islinphase (b, a)
%! ## test input validation
%!error n = islinphase ()
%!error n = islinphase (1, 1, 1, 1)
%!error n = islinphase (1, 1, 1, 1, 1)
%!error n = islinphase ([1:10]', 1)
%!error n = islinphase (1, [1:10]')
%!error n = islinphase ([1:10]', [1:10]')
%!error n = islinphase (1:10, 1:10, 1:10)
%!error n = islinphase (ones (3), ones (3))
%!test
%! a = 1; b = [1, 2, 3, 4, 3, 2, 1];
%! f = islinphase (b, a);
%! assert (f, true)
%!test
%! a = 1; b = [1, 2, 3, 4, 4, 3, 2, 1];
%! f = islinphase (b, a);
%! assert (f, true)
%!test
%! b = [1 -1]; a = [1 -2];
%! f = islinphase (b, a, 1E-1);
%! assert (f, false)
%!test
%! [b, a] = butter (1, .5);
%! f = islinphase (b, a);
%! assert (f, true)
%!test
%! [b, a] = butter (1, .25);
%! f = islinphase (b, a);
%! assert (f, false)
%!test
%! [b, a] = butter (8, .5);
%! f = islinphase (b, a);
%! assert (f, false)