1 ## Copyright (C) 2001 Paulo Neis <p_neis@yahoo.com.br>
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
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
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/>.
16 ## usage: [n,wp] = ellipord(wp,ws, rp,rs)
18 ## Calculate the order for the elliptic filter (discrete)
19 ## wp: Cutoff frequency
21 ## rp: decibels of ripple in the passband.
22 ## rs: decibels of ripple in the stopband.
26 ## - Lamar, Marcus Vinicius, Notas de aula da disciplina TE 456 - Circuitos
27 ## Analogicos II, UFPR, 2001/2002.
29 function [n, Wp] = ellipord(Wp, Ws, Rp, Rs)
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) )");
49 # sampling frequency of 2 Hz
52 Wpw = tan(pi.*Wp./T); # prewarp
53 Wsw = tan(pi.*Ws./T); # prewarp
55 ##pass/stop band to low pass filter transform:
56 if (length(Wpw)==2 && length(Wsw)==2)
58 w02 = Wpw(1) * Wpw(2); # Central frequency of stop/pass band (square)
62 ws = (Wsw(2)-w3)/(Wpw(2)-Wpw(1));
64 ws = (w4-Wsw(1))/(Wpw(2)-Wpw(1));
66 ws = (Wsw(2)-Wsw(1))/(Wpw(2)-Wpw(1));
68 elseif (length(Wpw)==2 && length(Wsw)==1)
70 w02 = Wpw(1) * Wpw(2);
73 ws = (Wsw - w3)/(Wpw(2) - Wpw(1));
76 ws = (w4 - Wsw)/(Wpw(2) - Wpw(1));
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);
89 n=ceil(log10(16*D)/log10(1/q));