00 /**********************************************************************
01 Copyright (c) 2014 Mark C. Erickson
02
03 Permission is hereby granted, free of charge, to any person obtaining
04 a copy of this software and associated documentation files (the
05 "Software"), to deal in the Software without restriction, including
06 without limitation the rights to use, copy, modify, merge, publish,
07 distribute, sublicense, and/or sell copies of the Software, and to
08 permit persons to whom the Software is furnished to do so, subject to
09 the following conditions:
10
11 The above copyright notice and this permission notice shall be included
12 in all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 **********************************************************************/
22
Disclaimer...
23 desc:EQ, Bandpass Nth-Order, v1.0.1
24
25 slider1:2000<40,16000,1>Cutoff [Hz]
26 slider2:2<2,8,2>Order [N]
27
28 @init
29 M = 1;
30 Q = sqrt(2);
31
32
33 @slider
34
35 function find_Dk(index stages)
36 (
37 _k = index;
38 _M = stages;
39 2.0 * sin($pi*(2*_k-1)/(2*_M));
40 );
41
42 slider1 > 39 ? (
43 slider2 > 1 ? (
44 N = slider2; // order N, 2 < N < 8
45 M = N / 2; // # of cascaded elements M, 1 < M < 4
46 w = 2.0 * $pi * slider1 / srate;
47 dE = 2.0 * tan( w / (2.0 * Q)) / sin(w);
48
49 k = 1;
50 j = 1;
51 Dk = find_Dk(k, M);
52 Ak = (1 + sqr(dE / 2.0)) / (Dk * dE / 2.0);
53 dk = sqrt(( dE * Dk ) / (Ak + sqrt(sqr(Ak) - 1) ));
54 Bk = Dk * (dE / 2.0) / dk;
55 Wk = Bk + sqrt(sqr(Bk) - 1);
56 theta_1k = 2.0 * atan(tan(w/2.0) / Wk);
57
58 beta1 = 0.5 * (( 1.0 - dk / 2.0 * sin(theta_1k) ) / ( 1.0 + dk / 2.0 * sin(theta_1k) ));
59 gamma1 = ( 0.5 + beta1 ) * cos(theta_1k);
60 alpha1 = 0.5 * (0.5 - beta1) * sqrt(1+ sqr(de * Dk / ( Ak + sqrt( Ak*Ak -1 ) )));
61
62 M > 1 ? (
63 k = 1;
64 j = 2;
65 theta_2k = 2.0 * atan( Wk * tan(w/2.0) );
66
67 beta2 = 0.5 * (( 1.0 - dk / 2.0 * sin(theta_2k) ) / ( 1.0 + dk / 2.0 * sin(theta_2k) ));
68 gamma2 = ( 0.5 + beta2 ) * cos(theta_2k);
69 alpha2 = 0.5 * (0.5 - beta2) * sqrt(1+ sqr(de * Dk / ( Ak + sqrt( Ak*Ak -1 ) )));
70 );
71
72 M > 2 ? (
73 k = 2;
74 j = 1;
75 Dk = find_Dk(k, M);
76 Ak = (1 + sqr(dE / 2.0)) / (Dk * dE / 2.0);
77 dk = sqrt(( dE * Dk ) / (Ak + sqrt(sqr(Ak) - 1) ));
78 Bk = Dk * (dE / 2.0) / dk;
79 Wk = Bk + sqrt(sqr(Bk) - 1);
80 theta_1k = 2.0 * atan(tan(w/2.0) / Wk);
81
82 beta3 = 0.5 * (( 1.0 - dk / 2.0 * sin(theta_1k) ) / ( 1.0 + dk / 2.0 * sin(theta_1k) ));
83 gamma3 = ( 0.5 + beta3 ) * cos(theta_1k);
84 alpha3 = 0.5 * (0.5 - beta3) * sqrt(1+ sqr(de * Dk / ( Ak + sqrt( Ak*Ak -1 ) )));
85 );
86
87 M > 3 ? (
88 k = 2;
89 j = 2;
90 theta_2k = 2.0 * atan( Wk * tan(w/2.0) );
91
92 beta4 = 0.5 * (( 1.0 - dk / 2.0 * sin(theta_2k) ) / ( 1.0 + dk / 2.0 * sin(theta_2k) ));
93 gamma4 = ( 0.5 + beta4 ) * cos(theta_2k);
94 alpha4 = 0.5 * (0.5 - beta4) * sqrt(1+ sqr(de * Dk / ( Ak + sqrt( Ak*Ak -1 ) )));
95 );
96
97 );
98 );
99
100 @sample
101 x_1 = spl0;
102 y_1 = 2.0 * ( alpha1 * ( x_1 - x2_1 ) + gamma1 * y1_1 - beta1 * y2_1 );
103 x2_1 = x1_1;
104 x1_1 = x_1;
105 y2_1 = y1_1;
106 y1_1 = y_1;
107 spl0 = y_1;
108
109 M > 1 ? (
110 x_2 = spl0;
111 y_2 = 2.0 * ( alpha2 * ( x_2 - x2_2 ) + gamma2 * y1_2 - beta2 * y2_2 );
112 x2_2 = x1_2;
113 x1_2 = x_2;
114 y2_2 = y1_2;
115 y1_2 = y_2;
116 spl0 = y_2 * 2.0;
117 );
118
119 M > 2 ? (
120 x_3 = spl0;
121 y_3 = 2.0 * ( alpha3 * ( x_3 - x2_3 ) + gamma3 * y1_3 - beta3 * y2_3 );
122 x2_3 = x1_3;
123 x1_3 = x_3;
124 y2_3 = y1_3;
125 y1_3 = y_3;
126 spl0 = y_3 * sqrt(2);
127 );
128
129 M > 3 ? (
130 x_4 = spl0;
131 y_4 = 2.0 * ( alpha4 * ( x_4 - x2_4 ) + gamma4 * y1_4 - beta4 * y2_4 );
132 x2_4 = x1_4;
133 x1_4 = x_4;
134 y2_4 = y1_4;
135 y1_4 = y_4;
136 spl0 = y_4 * 2;
137 );
138