对数组数据进行平均、中值,高斯滤波,请给出python代码
时间: 2024-05-09 11:18:05 浏览: 184
好的,这是对数组数据进行平均、中值和高斯滤波的Python代码:
平均滤波:
```python
import numpy as np
def average_filter(data, filter_size):
kernel = np.ones(filter_size) / filter_size
filtered_data = np.convolve(data, kernel, mode='same')
return filtered_data
```
中值滤波:
```python
import numpy as np
def median_filter(data, filter_size):
filtered_data = np.zeros_like(data)
for i in range(len(data)):
start = max(0, i - filter_size)
end = min(len(data), i + filter_size + 1)
filtered_data[i] = np.median(data[start:end])
return filtered_data
```
高斯滤波:
```python
import numpy as np
from scipy import signal
def gaussian_filter(data, sigma):
size = int(2 * np.ceil(3 * sigma) + 1)
kernel = signal.gaussian(size, sigma)
kernel /= kernel.sum()
filtered_data = np.convolve(data, kernel, mode='same')
return filtered_data
```
希望这些代码能够帮到你!
阅读全文