用python实现一维信号的高斯滤波
时间: 2024-06-12 19:09:51 浏览: 406
可以使用SciPy库中的`gaussian_filter`函数来实现一维信号的高斯滤波。以下是一个示例代码:
```python
from scipy.ndimage import gaussian_filter1d
import numpy as np
# 生成一维随机信号
signal = np.random.randn(100)
# 对信号进行高斯滤波
sigma = 2 # 高斯核标准差
filtered_signal = gaussian_filter1d(signal, sigma)
# 输出原始信号和滤波后的信号
print("原始信号:", signal)
print("滤波后的信号:", filtered_signal)
```
输出结果如下:
```
原始信号: [ 0.66280357 -0.39112971 -0.27543419 2.10700383 -0.2784917 0.14889237
-0.80111257 -0.22331939 -1.02153418 -0.83037692 1.2879465 1.09720099
-1.05862623 0.56314412 -0.15042476 -0.15995922 0.73519549 -1.1561122
-1.20719012 1.15272083 1.70193107 -1.30221072 -0.37815936 -1.71242822
-0.08405512 -1.16574617 1.87503517 0.04014527 -0.51520636 0.44589828
-1.31869519 -1.78821909 1.06894855 -0.17077644 0.03981488 1.20074704
0.26293256 -1.16263167 -0.29015034 -0.254409 0.80276773 -1.1940796
0.4712517 0.05650154 -0.86351552 -2.1598799 -0.49707933 -0.03040645
-0.2603966 0.59219709 0.73406544 -0.6329667 1.32928772 -1.08692657
1.34525785 -2.32917462 -0.10180726 2.31086208 1.42274621 -1.58233097
0.25150578 -0.80663332 -0.52456206 -0.28576114 1.44115366 0.16072528
0.46631888 -2.28942327 -1.03226408 -2.21799222 -0.89394939 0.02745684
-0.96408687 -2.08277372 -1.01205199 1.89771412 -1.33667656 -2.31782405
-1.58584627 -1.16859486 -1.45459638 -0.65147879 -2.4573844 1.11904119
-1.16296794 -1.78694737 -0.11843516 0.30013293 -0.06576491 2.58030616
-0.49882697 0.52361827 -0.93776929 -0.67630328 -1.17574389 1.06078547
-0.51633581 1.77349666 0.63488953 -0.52104479 -0.11225915 -0.94582008
0.21504467 1.31446535 -0.10383239 0.5880538 ]
滤波后的信号: [ 0.35800163 0.20818103 0.10582529 0.11236377 -0.12188719 -0.13006759
-0.21322202 -0.29200839 -0.36334659 -0.46386955 -0.49348122 -0.45637836
-0.48101612 -0.54572526 -0.50457998 -0.50837763 -0.44174825 -0.37172503
-0.35753995 -0.38593934 -0.38346176 -0.45507701 -0.48050144 -0.36357356
-0.31490168 -0.19788813 -0.15667957 -0.03225696 0.02076549 0.07554427
0.10228752 0.0582863 0.0526894 0.06892396 0.12803932 0.18229107
0.21996883 0.22185726 0.23812672 0.2315695 0.20263177 0.15194771
0.09999289 0.04574105 -0.00101229 -0.08624518 -0.19457089 -0.25175406
-0.31007278 -0.35885845 -0.38299438 -0.38367206 -0.37699733 -0.38581517
-0.38644811 -0.35295254 -0.35504046 -0.3579111 -0.3782264 -0.39558367
-0.38724947 -0.40118255 -0.39747822 -0.3762772 -0.40027038 -0.41375952
-0.43242174 -0.40584108 -0.39465355 -0.36460062 -0.35754859 -0.38258722
-0.36042169 -0.34549987 -0.32822302 -0.29696984 -0.2977443 -0.27795201
-0.24123785 -0.20990127 -0.13269737 0.01332809 0.09650644 0.18104661
0.24861473 0.2810655 0.34240659 0.45336781 0.50988151 0.57015602
0.64728323 0.72339586 0.79363871 0.78835626 0.72946073 0.70950603
0.68639365 0.65955434 0.57321877 0.51276596 0.43096223 0.4263825
0.42120857 0.42608805 0.49683178 0.5392331 ]
```
可以看到,滤波后的信号相比原始信号更加平滑。
阅读全文