-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
executable file
·117 lines (92 loc) · 3.15 KB
/
main.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
#include "include/ButterTool.h"
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <vector>
/** The filter tool can be used in the following ways.
*
* 1- Defining specs Wp, Ws, Ap, As and obtaining order and cut-off frequency (rad/sec)
* ButterworthFilter bf;
* bf.Buttord(Wp, Ws, Ap, As); Wp and Ws in [rad/sec]
* bf.computeContinuousTimeTF();
* bf.computeDiscreteTimeTF();
*
* 2- Defining the order and cut-off frequency (rad/sec) directly and computing the filter TFs
* bf.setOrder(N); N is integer
* bf.setCuttoffFrequency(Wc); Wc in [rad/sec]
* bf.computeContinuousTimeTF();
* bf.computeDiscreteTimeTF();
*
* 3- Defining the order N, cut-off and sampling frequencies (Hz)
* bf.setOrder(N); N is integer
* bf.setCuttoffFrequency_Hz(fc, fs); cut-off fc and sampling fs are in [Hz]
* bf.computeContinuousTimeTF();
* bf.computeDiscreteTimeTF();
* */
int main()
{
// 1st Method
double Wp{2.}; // pass-band frequency [rad/sec]
double Ws{3.}; // stop-band frequency [rad/sec]
double Ap{6.}; // pass-band ripple mag or loss [dB]
double As{20.}; // stop band ripple attenuation [dB]
ButterworthFilter bf1;
bf1.Buttord(Wp, Ws, Ap, As);
auto NWc = bf1.getOrderCutOff();
print("The computed order and frequency for the give specification : ");
print("Minimum order N = ", NWc.N, ", and The cut-off frequency Wc = ", NWc.Wc, "rad/sec \n");
bf1.printFilterSpecs();
/**
* Approximate the continuous and discrete time transfer functions.
* */
bf1.computeContinuousTimeTF();
// Print continuous time roots.
bf1.printFilterContinuousTimeRoots();
bf1.printContinuousTimeTF();
// Compute the discrete time transfer function.
bf1.computeDiscreteTimeTF();
bf1.printDiscreteTimeTF();
// 2nd METHOD
// Setting filter order N and cut-off frequency explicitly
print("SECOND TYPE of FILTER INITIALIZATION ");
ButterworthFilter bf2;
bf2.setOrder(2);
bf2.setCuttoffFrequency(2.0);
bf2.printFilterSpecs();
// Get the computed order and Cut-off frequency
NWc = bf2.getOrderCutOff();
// Print continuous time roots.
bf2.computeContinuousTimeTF();
bf2.printFilterContinuousTimeRoots();
bf2.printContinuousTimeTF();
// Compute the discrete time transfer function.
bf2.computeDiscreteTimeTF();
bf2.printDiscreteTimeTF();
// 3rd METHOD
// defining a sampling frequency together with the cut-off fc, fs
print("THIRD TYPE of FILTER INITIALIZATION ");
ButterworthFilter bf3;
bf3.setOrder(2);
bf3.setCuttoffFrequency(10, 100);
bf3.printFilterSpecs();
bool use_sampling_frequency{true};
bf3.computeContinuousTimeTF(use_sampling_frequency);
bf3.printFilterContinuousTimeRoots();
bf3.printContinuousTimeTF();
// Compute Discrete Time TF
bf3.computeDiscreteTimeTF(use_sampling_frequency);
bf3.printDiscreteTimeTF();
auto AnBn = bf3.getAnBn();
auto An = bf3.getAn();
auto Bn = bf3.getBn();
print("An : ");
for (double it : An) {
std::cout << std::setprecision(4) << it << ", ";
}
print("\nBn : \n");
for (double it : Bn) {
std::cout << std::setprecision(4) << it << ", ";
}
return 0;
}