-
Notifications
You must be signed in to change notification settings - Fork 1
/
isminphase.m
95 lines (82 loc) · 3.04 KB
/
isminphase.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
## 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{L} = } isminphase (@var{b}, @var{a})
## @deftypefnx {Function File} {@var{L} = } isminphase (@var{sos})
## @deftypefnx {Function File} {@var{L} = } isminphase (@dots{}, @var{tol})
##
## Determine whether a digital filter is minimum phase. 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 toleranve @var{tol} might be given to define when two numbers are close enough
## to be considered equal.
##
## Example:
## @example
## a = [1 0.5]; b = [3 1];
## isminphase (b, a)
## @end example
##
## Ref [1] Oppenheim, Alan, and Ronald Schafer. Discrete-Time Signal Processing.
## 3rd edition, Pearson, 2009.
## @end deftypefn
function flag = isminphase (b, a, tol)
if (nargin < 1 || nargin > 3 || (nargin == 2 && ! isrow (b)) || (length (a) > 1 && ! isrow (a)) || (length (b) > 1 && ! isrow (b)) || (nargin == 3 && ! isscalar (tol)) )
print_usage;
endif
if (nargin == 2 && ! isrow (b))
tol = a;
endif
if (nargin == 1 && isrow (b)), a = 1; endif
if nargin < 3, tol = eps^(3/4); endif
if (nargin == 2 && isrow (a) && isrow (b)) || (nargin == 1 && isrow (b))
zm = abs (roots (b));
pm = abs (roots (a));
flag = (all (zm < 1 - tol) || isempty (zm)) && (all (pm < 1 - tol) || isempty (pm));
elseif (nargin == 1 && all(size (b) > [1 1]))
[b, a] = sos2tf (b);
flag = isminphase (b, a, tol);
endif
endfunction
%!demo
%! f = isminphase (b, a)
%! ## test input validation
%!error n = isminphase ()
%!error n = isminphase (1, 1, 1, 1)
%!error n = isminphase (1, 1, 1, 1, 1)
%!error n = isminphase ([1:10]', 1)
%!error n = isminphase (1, [1:10]')
%!error n = isminphase ([1:10]', [1:10]')
%!error n = isminphase (1:10, 1:10, 1:10)
%!error n = isminphase (ones (3), ones (3))
%!test
%! b = [3 1];
%! a = [1 .5];
%! f = isminphase (b, a);
%! assert (f, true)
%!test
%! [b, a] = butter (1, .5);
%! f = isminphase (b, a);
%! assert (f, false)
%!test
%! [b, a] = butter (8, .5);
%! f = isminphase (b, a);
%! assert (f, false)
%!test
%! b = 1.25^2 * conv (conv (conv ([1 -0.9*e^(-j*0.6*pi)], [1 -0.9*e^(j*0.6*pi)]), [1 -0.8*e^(-j*0.8*pi)]), [1 -0.8*e^(j*0.8*pi)]);
%! a = 1;
%! f = isminphase (b, a);
%! assert (f, true)