-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAFD_test.cpp
149 lines (132 loc) · 4.25 KB
/
AFD_test.cpp
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <iostream>
#include <gmpxx.h>
#include <mpreal.h>
#include "TransferFunction.h"
#include "mpreal_ex.h"
#include "FilterDesign.h"
using mpfr::mpreal;
using mpcomplex = std::complex<mpreal>;
auto main() -> int {
mpreal::set_default_prec(mpfr::digits2bits(50));
mpreal k;
std::cout.precision(20);
std::cout << "Analog_Filter_Designer" << '\n';
int ftype;
std::cout << "Filter type(0:butterworth\t1:chebyshev1\t2:chebyshev2\t3:elliptic):";
std::cin >> ftype;
std::cout <<'\n';
std::cout << "band_type(0:lowpass\t1:highpass\t2:bandpass\t3:bandstop):";
int type;
std::cin >> type;
mpreal wp, wpl, wpu;
mpreal ws, wsl, wsu;
if ((filter_band_type(type) == lowpass) || (filter_band_type(type) == highpass)) {
std::cout << "Wp=";
std::cin >> wp;
std::cout << "Ws=";
std::cin >> ws;
} else {
std::cout << "Wpl=";
std::cin >> wpl;
std::cout << "Wpu=";
std::cin >> wpu;
std::cout << "Wsl=";
std::cin >> wsl;
std::cout << "Wsu=";
std::cin >> wsu;
}
std::cout << "Ap(dB)=";
mpreal Ap;
std::cin >> Ap;
std::cout << "As(dB)=";
mpreal As;
std::cin >> As;
zeros z;
poles p;
mpreal H0;
std::vector<std::array<mpfr::mpreal, 3>> B;
std::vector<std::array<mpfr::mpreal, 3>> A;
if ((filter_band_type(type) == lowpass) || (filter_band_type(type) == highpass)) {
if (ftype == 0) {
auto [z1, p1, H01, B1, A1] = AF::butterworth_filter(wp, ws, Ap, As, (filter_band_type) type);
z = z1;
p = p1;
H0 = H01;
B = std::move(B1);
A = std::move(A1);
} else if (ftype == 1) {
auto [z1, p1, H01, B1, A1] = AF::chebyshev1_filter(wp, ws, Ap, As, (filter_band_type) type);
z = z1;
p = p1;
H0 = H01;
B = std::move(B1);
A = std::move(A1);
} else if (ftype == 2) {
auto [z1, p1, H01, B1, A1] = AF::chebyshev2_filter(wp, ws, Ap, As, (filter_band_type) type);
z = z1;
p = p1;
H0 = H01;
B = std::move(B1);
A = std::move(A1);
} else {
auto [z1, p1, H01, B1, A1] = AF::ellipitic_filter(wp, ws, Ap, As, (filter_band_type) type);
z = z1;
p = p1;
H0 = H01;
B = std::move(B1);
A = std::move(A1);
}
} else {
if (ftype == 0) {
auto [z1, p1, H01, B1, A1] = AF::butterworth_filter(wpu, wpl, wsu, wsl, Ap, As, (filter_band_type) type);
z = z1;
p = p1;
H0 = H01;
B = std::move(B1);
A = std::move(A1);
} else if (ftype == 1) {
auto [z1, p1, H01, B1, A1] = AF::chebyshev1_filter(wpu, wpl, wsu, wsl, Ap, As, (filter_band_type) type);
z = z1;
p = p1;
H0 = H01;
B = std::move(B1);
A = std::move(A1);
} else if (ftype == 2) {
auto [z1, p1, H01, B1, A1] = AF::chebyshev2_filter(wpu, wpl, wsu, wsl, Ap, As, (filter_band_type) type);
z = z1;
p = p1;
H0 = H01;
B = std::move(B1);
A = std::move(A1);
} else {
auto [z1, p1, H01, B1, A1] = AF::ellipitic_filter(wpu, wpl, wsu, wsl, Ap, As, (filter_band_type) type);
z = z1;
p = p1;
H0 = H01;
B = std::move(B1);
A = std::move(A1);
}
}
std::cout << "\n零点z" << '\n';
for (auto &zi: z) {
std::cout << zi << '\n';
}
std::cout << "\n极点p" << '\n';
for (auto &pi: p) {
std::cout << pi << '\n';
}
std::cout << "\n直流增益H0" << '\n';
std::cout << H0 << '\n';
std::cout << "H(s)" << '\n';
for (int i = 0; i < B.size(); i++) {
std::cout << TransferFunction(B[i], A[i]).toLaTeX() << '\n';
}
for (int i = 0; i < B.size(); i++) {
std::cout << B[i][0] << ',' << B[i][1] << ',' << B[i][2] << ',' << A[i][0] << ',' << A[i][1] << ',' << A[i][2]
<< '\n';
}
for (int i = 0; i < B.size(); i++) {
std::cout << TransferFunction(B[i], A[i]).toMMA() << '*';
}
return 0;
}