mport numpy as np def rts_smooth(data, window_size, smooth_factor): assert window_size % 2 == 1, "Window size must be odd" assert 0 <= smooth_factor <= 1, "Smooth factor must be between 0 and 1" half_window = (window_size - 1) // 2 data_length = len(data) smoothed_data = np.zeros(data_length) for i in range(half_window, data_length - half_window): window = data[i - half_window:i + half_window + 1] median = np.median(window) deviation = np.abs(window - median) threshold = smooth_factor * np.median(deviation) if np.abs(data[i] - median) > threshold: smoothed_data[i] = median else: smoothed_data[i] = data[i] # 处理首尾值 smoothed_data[:half_window] = data[:half_window] smoothed_data[data_length - half_window:] = data[data_length - half_window:] return smoothed_data 使用示例 data = [10, 15, 20, 12, 18, 22, 25, 16, 14, 23] window_size = 3 smooth_factor = 0.6 smoothed_data = rts_smooth(data, window_size, smooth_factor) print(smoothed_data)这段代码如果输入数据有很多维度怎么改
时间: 2024-01-15 19:03:17 浏览: 171
如果输入数据有多个维度,可以使用`np.apply_along_axis`函数来应用平滑函数到每个维度上。下面是修改后的代码示例:
```python
import numpy as np
def rts_smooth(data, window_size, smooth_factor):
assert window_size % 2 == 1, "Window size must be odd"
assert 0 <= smooth_factor <= 1, "Smooth factor must be between 0 and 1"
half_window = (window_size - 1) // 2
data_length = len(data)
smoothed_data = np.zeros_like(data)
def smooth_func(x):
for i in range(half_window, data_length - half_window):
window = x[i - half_window:i + half_window + 1]
median = np.median(window)
deviation = np.abs(window - median)
threshold = smooth_factor * np.median(deviation)
if np.abs(x[i] - median) > threshold:
smoothed_data[i] = median
else:
smoothed_data[i] = x[i]
smoothed_data[:half_window] = x[:half_window]
smoothed_data[data_length - half_window:] = x[data_length - half_window:]
return smoothed_data
smoothed_data = np.apply_along_axis(smooth_func, axis=0, arr=data)
return smoothed_data
# 使用示例
data = np.array([[10, 15, 20, 12, 18, 22, 25, 16, 14, 23],
[5, 8, 10, 12, 9, 7, 15, 20, 18, 17]])
window_size = 3
smooth_factor = 0.6
smoothed_data = rts_smooth(data, window_size, smooth_factor)
print(smoothed_data)
```
这样,`rts_smooth`函数将会对输入数据的每个维度进行平滑处理。输出结果将保持与输入数据相同的维度。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)