File size: 3,319 Bytes
f5dbfe6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import matplotlib.pyplot as plt

encoderCountReal = 0.0
encoderCountInteger = 0

ENCODER_COUNT = 2**32

filterData = [0, 0]

PRESCALER = 1 * 2**32
INPUT_ROUNDING = 32
OUTPUT_ROUNDING = 32
A1 = int(-0.04515176221313341 * 2**32)
A2 = int(-0.9404614587008322 * 2**32)

B0 = int(0.003596694771508549 * 2**32)
B1 = int(0.007193389543017098 * 2**32)
B2 = int(0.003596694771508549 * 2**32)

times = []
filteredPos = []
realEncoder = []
integerEncoder = []

for i in range(4000):

    # update encoder
    encoderCountReal += .001
    encoderCountInteger = round(encoderCountReal, 0)
    if(encoderCountInteger > ENCODER_COUNT-1):
        encoderCountInteger -= ENCODER_COUNT
        encoderCountReal -= ENCODER_COUNT
    elif(encoderCountInteger < 0):
        encoderCountInteger += ENCODER_COUNT
        encoderCountReal += ENCODER_COUNT

    if(encoderCountInteger > 0):
        print("true")

    if(i == 200):
        encoderCountReal += ENCODER_COUNT -50

    temp = int(encoderCountInteger * PRESCALER)
    temp -= int(filterData[0] * A1)
    temp -= int(filterData[1] * A2)

    dn = round(temp / pow(2, INPUT_ROUNDING), 0)

    
    temp2 = int(dn * B0)
    temp2 += int(filterData[0] * B1)
    temp2 += int(filterData[1] * B2)
    

    yn = round(temp2 / pow(2, OUTPUT_ROUNDING), 0)

    filterData.insert(0, dn)
    filterData.pop(-1)

    print(filterData)

    times.append(i)
    filteredPos.append(yn)
    realEncoder.append(encoderCountReal)
    integerEncoder.append(encoderCountInteger)


plt.plot(times, filteredPos)
plt.plot(times, realEncoder)
plt.plot(times, integerEncoder)
plt.show()


# import numpy as np

# class BiquadFilter:
#     def __init__(self, b0, b1, b2, a1, a2):
#         # Initialize coefficients
#         self.b0, self.b1, self.b2 = b0, b1, b2
#         self.a1, self.a2 = a1, a2
        
#         # Initialize state variables
#         self.x1, self.x2 = 0.0, 0.0
#         self.y1, self.y2 = 0.0, 0.0

#     def process(self, x):
#         # Direct Form II filter implementation
#         y = self.b0 * x + self.b1 * self.x1 + self.b2 * self.x2 - self.a1 * self.y1 - self.a2 * self.y2
        
#         # Update state
#         self.x2 = self.x1
#         self.x1 = x
#         self.y2 = self.y1
#         self.y1 = y
        
#         return y

# # Example usage
# if __name__ == "__main__":
#     # Define filter coefficients (example values)
#     b0, b1, b2 = 5.074331248486237e-9, 1.0148662496972475e-8, 5.074331248486237e-9
#     a1, a2 = -1.999798506779029, 0.9997985270763541
    
#     # Create a BiquadFilter object
#     filter = BiquadFilter(b0, b1, b2, a1, a2)
    
#     # Example input signal (simple sinusoidal signal)
#     fs = 44100  # Sampling frequency
#     t = np.linspace(0, 1, fs, endpoint=False)  # Time vector
#     input_signal = np.sin(2 * np.pi * 50 * t)  # 5 Hz sine wave

#     # Process the signal through the filter
#     output_signal = np.array([filter.process(x) for x in input_signal])
    
#     # You can use matplotlib to visualize the input and output signals
#     import matplotlib.pyplot as plt
#     plt.figure()
#     plt.subplot(2, 1, 1)
#     plt.title('Input Signal')
#     plt.plot(t, input_signal)
#     plt.subplot(2, 1, 2)
#     plt.title('Output Signal')
#     plt.plot(t, output_signal)
#     plt.show()