]> Creatis software - CreaPhase.git/blob - octave_packages/signal-1.1.3/buttord.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / signal-1.1.3 / buttord.m
1 ## Copyright (C) 1999 Paul Kienzle <pkienzle@users.sf.net>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## Compute butterworth filter order and cutoff for the desired response
17 ## characteristics. Rp is the allowable decibels of ripple in the pass 
18 ## band. Rs is the minimum attenuation in the stop band.
19 ##
20 ## [n, Wc] = buttord(Wp, Ws, Rp, Rs)
21 ##     Low pass (Wp<Ws) or high pass (Wp>Ws) filter design.  Wp is the
22 ##     pass band edge and Ws is the stop band edge.  Frequencies are
23 ##     normalized to [0,1], corresponding to the range [0,Fs/2].
24 ## 
25 ## [n, Wc] = buttord([Wp1, Wp2], [Ws1, Ws2], Rp, Rs)
26 ##     Band pass (Ws1<Wp1<Wp2<Ws2) or band reject (Wp1<Ws1<Ws2<Wp2)
27 ##     filter design. Wp gives the edges of the pass band, and Ws gives
28 ##     the edges of the stop band.
29 ##
30 ## Theory: |H(W)|^2 = 1/[1+(W/Wc)^(2N)] = 10^(-R/10)
31 ## With some algebra, you can solve simultaneously for Wc and N given
32 ## Ws,Rs and Wp,Rp.  For high pass filters, subtracting the band edges
33 ## from Fs/2, performing the test, and swapping the resulting Wc back
34 ## works beautifully.  For bandpass and bandstop filters this process
35 ## significantly overdesigns.  Artificially dividing N by 2 in this case
36 ## helps a lot, but it still overdesigns.
37 ##
38 ## See also: butter
39
40 function [n, Wc] = buttord(Wp, Ws, Rp, Rs)
41   if nargin != 4
42     print_usage;
43   elseif length(Wp) != length(Ws)
44     error("buttord: Wp and Ws must have the same length");
45   elseif length(Wp) != 1 && length(Wp) != 2
46     error("buttord: Wp,Ws must have length 1 or 2");
47   elseif length(Wp) == 2 && (all(Wp>Ws) || all(Ws>Wp) || diff(Wp)<=0 || diff(Ws)<=0)
48     error("buttord: Wp(1)<Ws(1)<Ws(2)<Wp(2) or Ws(1)<Wp(1)<Wp(2)<Ws(2)");
49   end
50
51   if length(Wp) == 2
52     warning("buttord: seems to overdesign bandpass and bandreject filters");
53   end
54
55   T = 2;
56   
57   ## if high pass, reverse the sense of the test
58   stop = find(Wp > Ws);
59   Wp(stop) = 1-Wp(stop); # stop will be at most length 1, so no need to
60   Ws(stop) = 1-Ws(stop); # subtract from ones(1,length(stop))
61   
62   ## warp the target frequencies according to the bilinear transform
63   Ws = (2/T)*tan(pi*Ws./T);
64   Wp = (2/T)*tan(pi*Wp./T);
65   
66   ## compute minimum n which satisfies all band edge conditions
67   ## the factor 1/length(Wp) is an artificial correction for the
68   ## band pass/stop case, which otherwise significantly overdesigns.
69   qs = log(10^(Rs/10) - 1);
70   qp = log(10^(Rp/10) - 1);
71   n = ceil(max(0.5*(qs - qp)./log(Ws./Wp))/length(Wp));
72
73   ## compute -3dB cutoff given Wp, Rp and n
74   Wc = exp(log(Wp) - qp/2/n);
75
76   ## unwarp the returned frequency
77   Wc = atan(T/2*Wc)*T/pi;
78   
79   ## if high pass, reverse the sense of the test
80   Wc(stop) = 1-Wc(stop);
81     
82 endfunction