-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot_double_polynomial_fitting.py
257 lines (197 loc) · 11 KB
/
plot_double_polynomial_fitting.py
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import h5py
from matplotlib.lines import Line2D
from sklearn.linear_model import RANSACRegressor
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
# Define the window size for calculating RMS values and moving average filter
rms_window_size = 200
moving_average_window_size = 1 # Adjust the window size for the moving average filter
with h5py.File('MLogs/10_Sine_34gg_2kHz_TestPCB_26-July-2023_16-49-14/data.mat', 'r') as file:
# Extract the data
data = {}
for key, value in file.items():
# Check if the value is 1-dimensional
if len(value.shape) == 1:
data[key] = value[:]
else:
# If not 1-dimensional, convert to a list of 1-dimensional arrays
for i in range(value.shape[1]):
data[f"{key}_{i+1}"] = value[:, i]
# Convert to DataFrame
df = pd.DataFrame(data)
# Extract the columns from the data
time = df.iloc[0, 10000:-20000]
cmd_voltage = df.iloc[1, 10000:-20000]
displacement = df.iloc[2, 10000:-20000]
ss_voltage = df.iloc[3, 10000:-20000]
ss_current = df.iloc[4, 10000:-20000]
# Factor for laser displacement and reference
displacement = displacement * 4 + 30
displacement = displacement - np.mean(displacement.iloc[:50])
# Calculate the number of samples and the number of RMS calculations
num_samples = len(time)
num_rms = num_samples // rms_window_size
# Initialize arrays for RMS values and adjusted time and error
adjusted_time = np.zeros(num_rms)
adj_displacement = np.zeros(num_rms)
rms_voltage_values = np.zeros(num_rms)
rms_current_values = np.zeros(num_rms)
#impedance = np.zeros(num_rms)
displacement = displacement.rolling(window=moving_average_window_size, min_periods=1).mean()
# Calculate RMS values and adjusted time
for i in range(num_rms):
start_index = i * rms_window_size
end_index = (i + 1) * rms_window_size - 1
# Calculate RMS values
rms_voltage_values[i] = np.sqrt(np.mean(ss_voltage.iloc[start_index:end_index] ** 2))
rms_current_values[i] = np.sqrt(np.mean(ss_current.iloc[start_index:end_index] ** 2))
# Adjusted time is set to the middle time value of the window
adjusted_time[i] = np.mean(time.iloc[start_index:end_index])
adj_displacement[i] = np.mean(displacement.iloc[start_index:end_index])
degree = 3
rms_voltage_values = pd.Series(rms_voltage_values)
if rms_voltage_values.isna().any():
print(f"NaN values found in window ALDER")
rms_voltage_values = rms_voltage_values.rolling(window=moving_average_window_size, min_periods=1).mean()
if rms_voltage_values.isna().any():
print(f"NaN values found in window ")
rms_voltage_values = rms_voltage_values / rms_current_values
# coefficients = np.polyfit(impedance, adj_displacement, degree)
# est_displ = np.polyval(coefficients, impedance)
diff_rms_voltage = rms_voltage_values - rms_voltage_values.shift(2)
print(diff_rms_voltage)
#diff_rms_voltage = rms_voltage_values.diff()
diff_rms_voltage[0] = diff_rms_voltage[2]
diff_rms_voltage[1] = diff_rms_voltage[0]
#diff_rms_voltage[2] = diff_rms_voltage[1]
#diff_rms_voltage[3] = diff_rms_voltage[2]
positive_derivative = rms_voltage_values[diff_rms_voltage >= 0]
negative_derivative = rms_voltage_values[diff_rms_voltage < 0]
print(len(positive_derivative))
print(len(negative_derivative))
pos_rms_voltage_values = rms_voltage_values[positive_derivative.index]
neg_rms_voltage_values = rms_voltage_values[negative_derivative.index]
pos_adj_displacement = adj_displacement[positive_derivative.index]
neg_adj_displacement = adj_displacement[negative_derivative.index]
pos_adjusted_time = adjusted_time[positive_derivative.index]
neg_adjusted_time = adjusted_time[negative_derivative.index]
# X = pos_rms_voltage_values.values.reshape(-1, 1)
# y = pos_adj_displacement
# model = make_pipeline(PolynomialFeatures(degree=3), RANSACRegressor())
# model.fit(X, y)
# coefficients = model.named_steps['ransacregressor'].estimator_.coef_# Getting the coefficients of the polynomial
# X_fit = np.linspace(X.min(), X.max(), 100).reshape(-1, 1)
# y_fit = model.predict(X_fit)
# est_displ_pos_predict = model.predict(X)
# estimated_displacement_pos_predict = np.full_like(rms_voltage_values, np.nan)
# estimated_displacement_pos_predict[positive_derivative.index] = est_displ_pos_predict
coefficients = np.polyfit(rms_voltage_values, adj_displacement, degree)
est_displ = np.polyval(coefficients, rms_voltage_values)
coefficients_pos = np.polyfit(pos_rms_voltage_values, pos_adj_displacement, degree)
est_displ_pos = np.polyval(coefficients_pos, pos_rms_voltage_values)
coefficients_neg = np.polyfit(neg_rms_voltage_values, neg_adj_displacement, degree)
est_displ_neg = np.polyval(coefficients_neg, neg_rms_voltage_values)
estimated_displacement_pos = np.full_like(rms_voltage_values, np.nan)
estimated_displacement_pos[positive_derivative.index] = est_displ_pos
estimated_displacement_neg = np.full_like(rms_voltage_values, np.nan)
estimated_displacement_neg[negative_derivative.index] = est_displ_neg
A = 6
plt.rc('text', usetex=True)
plt.rc('font', family='serif', weight='bold')
t2_color = 'y'
t1_color = '#2ca02c'
p2_color = 'c'
p1_color = '#1f77b4'
plt.rcParams['axes.labelsize'] = 25 # Set x and y labels fontsize
plt.rcParams['legend.fontsize'] = 16 # Set legend fontsize
plt.rcParams['xtick.labelsize'] = 20 # Set x tick labels fontsize
plt.rcParams['ytick.labelsize'] = 20 # Set y tick labels fontsize
plt.rcParams['grid.linewidth'] = 1.5
plt.rcParams['axes.linewidth'] = 1.5
plt.rcParams['font.weight'] = 'bold' # Set default font weight to bold
line_width = 2.5
fig, axs = plt.subplots(2, 2, figsize=(16, 9)) # 1 row, 2 columns
sorted_indices = np.argsort(rms_voltage_values)
sorted_voltage_values = rms_voltage_values[sorted_indices]
sorted_displ = est_displ[sorted_indices]
pos_rms_voltage_values = pos_rms_voltage_values.values if isinstance(pos_rms_voltage_values, pd.Series) else pos_rms_voltage_values
sorted_indices_0 = np.argsort(pos_rms_voltage_values)
sorted_voltage_values_pos = pos_rms_voltage_values[sorted_indices_0]
sorted_displ_pos = est_displ_pos[sorted_indices_0]
#sorted_displ_pos_predict = est_displ_pos_predict[sorted_indices]
neg_rms_voltage_values = neg_rms_voltage_values.values if isinstance(neg_rms_voltage_values, pd.Series) else neg_rms_voltage_values
sorted_indices_1 = np.argsort(neg_rms_voltage_values)
sorted_voltage_values_neg = neg_rms_voltage_values[sorted_indices_1]
sorted_displ_neg = est_displ_neg[sorted_indices_1]
offset = np.min(adj_displacement)
adj_displacement -= offset
pos_adj_displacement -= offset
neg_adj_displacement -= offset
est_displ -= offset
estimated_displacement_neg -= offset
estimated_displacement_pos -= offset
sorted_displ -= offset
sorted_displ_pos -= offset
sorted_displ_neg -= offset
axs[0][0].plot(rms_voltage_values, adj_displacement, 'mediumturquoise', linestyle='None', marker='x', markersize=8, markeredgewidth=1, label='Measured Data Points')
axs[0][0].plot(sorted_voltage_values, sorted_displ, 'darkcyan', linewidth=line_width, label='Polynomial Fit: Coefficients: ')
axs[0][0].set_ylabel(r'Displacement (mm)') # Y-axis label with increased font size and bold
axs[0][0].set_xlabel(r'$V_{Hrms}$ (V)', weight='bold') # X-axis label with increased font size and bold
axs[0][0].grid(True) # Add grid with dashed lines
axs[0][0].set_ylim(-0.08, None) # Setting lower limit to -0.1
axs[0][1].plot(pos_rms_voltage_values, pos_adj_displacement, color='mediumpurple', linestyle='None', marker='x', markersize=8, markeredgewidth=1, label='Measured Data Points')
axs[0][1].plot(neg_rms_voltage_values, neg_adj_displacement, 'springgreen', linestyle='None', marker='x', markersize=8, markeredgewidth=1, label='Measured Data Points')
axs[0][1].plot(sorted_voltage_values_pos, sorted_displ_pos, color='darkslateblue', linewidth=line_width, label='Polynomial Fit: Coefficients: ')
axs[0][1].plot(sorted_voltage_values_neg, sorted_displ_neg, 'limegreen', linewidth=line_width, label='Polynomial Fit: Coefficients: ')
axs[0][1].set_xlabel(r'$V_{Hrms}$ (V)', weight='bold') # X-axis label with increased font size and bold
axs[0][1].grid(True) # Add grid with dashed lines
axs[0][1].set_ylim(-0.08, None) # Setting lower limit to -0.1
estimated_displacement = np.where(diff_rms_voltage >= 0, estimated_displacement_pos, estimated_displacement_neg)
#estimated_displacement_predict = np.where(diff_rms_voltage >= 0.01, estimated_displacement_pos_predict, estimated_displacement_neg)
rmse = np.sqrt(np.mean((adj_displacement - est_displ) ** 2))
range_gt_displ = np.max(adj_displacement) - np.min(adj_displacement)
nrmse = rmse / range_gt_displ
rmse_doublefit = np.sqrt(np.mean((adj_displacement - estimated_displacement) ** 2))
range_gt_displ = np.max(adj_displacement) - np.min(adj_displacement)
nrmse_doublefit = rmse_doublefit / range_gt_displ
axs[1][0].plot(adjusted_time, adj_displacement, linewidth=line_width, color='r')
axs[1][0].plot(adjusted_time, est_displ, linewidth=line_width, color=p1_color)
axs[1][0].set_ylabel(r'Displacement (mm)') # Y-axis label with increased font size and bold
axs[1][0].set_xlabel(r'Time (s)', weight='bold') # X-axis label with increased font size and bold
axs[1][0].grid(True) # Add grid with dashed lines
axs[1][0].set_ylim(-0.075, 0.48) # Setting lower limit to -0.1
print(nrmse)
#axs[1][0].set_title(f'20 Hz (Normal Mapping)',fontsize=25, fontweight='bold')
axs[1][0].title.set_fontweight('bold') # Set the title font weight to bold
axs[1][1].plot(adjusted_time, adj_displacement, linewidth=line_width, color='r')
axs[1][1].plot(adjusted_time, estimated_displacement, linewidth=line_width, color=p1_color)
axs[1][1].set_xlabel(r'Time (s)', weight='bold') # X-axis label with increased font size and bold
axs[1][1].grid(True) # Add grid with dashed lines
axs[1][1].set_ylim(-0.075, 0.48) # Setting lower limit to -0.1
print(nrmse_doublefit)
#axs[1][1].set_title(f'20 Hz (Dual Mapping)',fontsize=25)
legend_elements = [
Line2D([0], [0], color='darkcyan', lw=2, label='Cubic Polynomial Fit'),
Line2D([0], [0], color='mediumturquoise', marker='x', linestyle='None', markersize=10, markeredgewidth=2, label='Measured Data Points'),
Line2D([0], [0], color='limegreen', lw=2, label='Cub. Pol. Fit (neg. deriv.)'),
Line2D([0], [0], color='springgreen', marker='x', linestyle='None', markersize=10, markeredgewidth=2, label='Data Points (neg. deriv.)'),
Line2D([0], [0], color='darkslateblue', lw=2, label='Cub. Pol. Fit (pos. deriv.)'),
Line2D([0], [0], color='mediumpurple', marker='x', linestyle='None', markersize=10, markeredgewidth=2, label='Data Points (pos. deriv.)'),
Line2D([0], [0], color=p1_color, lw=2, label='Est. Displacement (voltage-method)'),
Line2D([0], [0], color='r', lw=2, label='Ground Truth Displacement')
]
fig.legend(handles=legend_elements, loc='upper center', handlelength=2,ncol=4, bbox_to_anchor=(0.5, 1.01), fontsize=18)
fig.subplots_adjust(
top=0.89,
bottom=0.215,
left=0.075,
right=0.98,
hspace=0.315,
wspace=0.105
)
plt.savefig('Final-Plot-10.pdf')
# Show the plot
plt.show()