Commit fde4e66c authored by Ivo Peterek's avatar Ivo Peterek

FIX: ignore added zeros when applying smoothing. #8

parent 32b1f283
......@@ -276,20 +276,28 @@ class Window(QtWidgets.QDialog):
tmp_line = []
sample_numbers = []
sample_vals = []
zero_numbers = []
for sample in line:
sample_numbers.append(sample[0])
sample_vals.append(sample[1])
# Take out zeroes
if sample[1] == 0:
zero_numbers.append((sample[0]))
else:
sample_numbers.append(sample[0])
sample_vals.append(sample[1])
smoothed_sample_vals = signal.savgol_filter(sample_vals, window_length, poly_order)
for i, number in enumerate(sample_numbers):
if sample_vals[i] == 0:
smoothed_sample_vals[i] = 0
if smoothed_sample_vals[i] < self.min_sample_val and smoothed_sample_vals[i] != 0:
self.min_sample_val = smoothed_sample_vals[i]
if smoothed_sample_vals[i] > self.max_sample_val:
self.max_sample_val = smoothed_sample_vals[i]
if number - 1 in zero_numbers: # Put zeroes back
tmp_line.append((number - 1, 0))
tmp_line.append((number, smoothed_sample_vals[i]))
if number+1 in zero_numbers: # Put zeroes back
tmp_line.append((number+1, 0))
smooth_samples_data.append(tmp_line)
self.samples_data = smooth_samples_data
......@@ -330,7 +338,7 @@ class Window(QtWidgets.QDialog):
# for i in range(1):
# smooth_samples = signal.savgol_filter(smooth_samples, smooth_window_size, poly_order)
smooth_samples = [(i, e) for i, e in enumerate(smooth_samples)]
smooth_samples = [(nums[i], e) for i, e in enumerate(smooth_samples)]
# Derivative of smooth samples function
smooth_samples_d = signal.savgol_filter(engs, smooth_window_size, poly_order, deriv=1)
......@@ -364,8 +372,8 @@ class Window(QtWidgets.QDialog):
derivative = smooth_samples_d[iter_ind]
radius = [smooth_samples[iter_ind][1], smooth_samples[iter_ind+1][1], smooth_samples[iter_ind-1][1]]
if np.abs(derivative[1]) <= 0.001:
print(iter_ind,radius)
# if np.abs(derivative[1]) <= 0.001:
# print(iter_ind,radius)
if np.abs(derivative[1]) <= 0.005 and (smooth_samples[iter_ind][1] == max(radius) or smooth_samples[iter_ind][1] == min(radius)):
is_extrema = True
......@@ -385,36 +393,36 @@ class Window(QtWidgets.QDialog):
prev_sample = sample
print(num_extrema)
print('linear approx: start')
pwlf_fit = pwlf.PiecewiseLinFit(nums, engs)
x_fit = pwlf_fit.fit(2*num_extrema+1)
y_fit = pwlf_fit.predict(x_fit)
print(x_fit)
print(y_fit)
fit = list(zip(x_fit, y_fit))
print('linear approx: done')
print('linear approx: start')
pwlf_fit = pwlf.PiecewiseLinFit(nums, engs)
x_fit = pwlf_fit.fit(2 * num_extrema + 3)
y_fit = pwlf_fit.predict(x_fit)
print(x_fit)
print(y_fit)
fit2 = list(zip(x_fit, y_fit))
print('linear approx: done')
# print('linear approx: start')
# pwlf_fit = pwlf.PiecewiseLinFit(nums, engs)
# x_fit = pwlf_fit.fit(2*num_extrema+1)
# y_fit = pwlf_fit.predict(x_fit)
#
# print(x_fit)
# print(y_fit)
# fit = list(zip(x_fit, y_fit))
#
# print('linear approx: done')
#
# print('linear approx: start')
# pwlf_fit = pwlf.PiecewiseLinFit(nums, engs)
# x_fit = pwlf_fit.fit(2 * num_extrema + 3)
# y_fit = pwlf_fit.predict(x_fit)
#
# print(x_fit)
# print(y_fit)
# fit2 = list(zip(x_fit, y_fit))
#
# print('linear approx: done')
self.samples_data_lables = []
self.samples_data.extend(clusters)
for num, cluster in enumerate(clusters):
self.samples_data_lables.append('Cluster ' + str(num))
self.samples_data.append(fit)
self.samples_data_lables.append('linear approx.')
self.samples_data.append(fit2)
self.samples_data_lables.append('2nd linear approx.')
# self.samples_data.append(fit)
# self.samples_data_lables.append('linear approx.')
# self.samples_data.append(fit2)
# self.samples_data_lables.append('2nd linear approx.')
self.samples_data.append(smooth_samples)
self.samples_data_lables.append('Sav-Gol approx.')
self.plot()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment