]> Creatis software - CreaPhase.git/blob - octave_packages/signal-1.1.3/ellipord.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / signal-1.1.3 / ellipord.m
1 ## Copyright (C) 2001 Paulo Neis <p_neis@yahoo.com.br>
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 ## usage: [n,wp] = ellipord(wp,ws, rp,rs)
17 ##
18 ## Calculate the order for the elliptic filter (discrete)
19 ## wp: Cutoff frequency
20 ## ws: Stopband edge
21 ## rp: decibels of ripple in the passband.
22 ## rs: decibels of ripple in the stopband.
23 ##
24 ## References: 
25 ##
26 ## - Lamar, Marcus Vinicius, Notas de aula da disciplina TE 456 - Circuitos 
27 ##   Analogicos II, UFPR, 2001/2002.
28
29 function [n, Wp] = ellipord(Wp, Ws, Rp, Rs)
30
31   [rp, cp]=size(Wp);
32   [rs, cs]=size(Ws);
33   if ( !(length(Wp)<=2 && (rp==1 || cp==1) && length(Ws)<=2 && (rs==1 || cs==1)) )
34     error ("ellipord: frequency must be given as w0 or [w0, w1]");
35   elseif (!all(Wp >= 0 & Wp <= 1 & Ws >= 0 & Ws <= 1)) #####
36     error ("ellipord: critical frequencies must be in (0 1)");
37   elseif (!(length(Wp)==1 || length(Wp) == 2 & length(Ws)==1 || length(Ws) == 2))
38     error ("ellipord: only one filter band allowed");
39   elseif (length(Wp)==2 && !(Wp(1) < Wp(2)))
40     error ("ellipord: first band edge must be smaller than second");
41   elseif (length(Ws)==2 && !(length(Wp)==2))
42     error ("ellipord: you must specify band pass borders.");
43   elseif (length(Wp)==2 && length(Ws)==2 && !(Ws(1) < Wp(1) && Ws(2) > Wp(2)) )
44     error ("ellipord: ( Wp(1), Wp(2) ) must be inside of interval ( Ws(1), Ws(2) )");
45   elseif (length(Wp)==2 && length(Ws)==1 && !(Ws < Wp(1) || Ws > Wp(2)) )
46     error ("ellipord: Ws must be out of interval ( Wp(1), Wp(2) )");
47   endif
48
49   # sampling frequency of 2 Hz
50   T = 2;
51
52   Wpw = tan(pi.*Wp./T); # prewarp
53   Wsw = tan(pi.*Ws./T); # prewarp
54
55   ##pass/stop band to low pass filter transform:
56   if (length(Wpw)==2 && length(Wsw)==2)
57     wp=1;
58     w02 = Wpw(1) * Wpw(2);      # Central frequency of stop/pass band (square)
59     w3 = w02/Wsw(2);
60     w4 = w02/Wsw(1);
61     if (w3 > Wsw(1))
62       ws = (Wsw(2)-w3)/(Wpw(2)-Wpw(1));
63     elseif (w4 < Wsw(2))
64       ws = (w4-Wsw(1))/(Wpw(2)-Wpw(1));
65     else
66       ws = (Wsw(2)-Wsw(1))/(Wpw(2)-Wpw(1));
67     endif
68   elseif (length(Wpw)==2 && length(Wsw)==1)
69     wp=1;
70     w02 = Wpw(1) * Wpw(2);
71     if (Wsw > Wpw(2))
72       w3 = w02/Wsw;
73       ws = (Wsw - w3)/(Wpw(2) - Wpw(1));
74     else
75       w4 = w02/Wsw;
76       ws = (w4 - Wsw)/(Wpw(2) - Wpw(1));
77     endif
78   else
79     wp = Wpw;
80     ws = Wsw;
81   endif
82
83   k=wp/ws;
84   k1=sqrt(1-k^2);
85   q0=(1/2)*((1-sqrt(k1))/(1+sqrt(k1)));
86   q= q0 + 2*q0^5 + 15*q0^9 + 150*q0^13; %(....)
87   D=(10^(0.1*Rs)-1)/(10^(0.1*Rp)-1);
88
89   n=ceil(log10(16*D)/log10(1/q));
90 endfunction