一维信号形态滤波python代码

求可以用于一维信号处理的数学形态学滤波的python代码,网上都是用于二维图像处理的代码。

对图像进行形态学变换。变换对象一般为灰度图或二值图,功能函数放在morphology子模块内。
1、膨胀(dilation)
原理:一般对二值图像进行操作。找到像素值为1的点,将它的邻近像素点都设置成这个值。1值表示白,0值表示黑,因此膨胀操作可以扩大白色值范围,压缩黑色值范围。一般用来扩充边缘或填充小的孔洞。
功能函数:skimage.morphology.dilation(image, selem=None)
selem表示结构元素,用于设定局部区域的形状和大小。

from skimage import data
import skimage.morphology as sm
import matplotlib.pyplot as plt
img=data.checkerboard()
dst1=sm.dilation(img,sm.square(5))  #用边长为5的正方形滤波器进行膨胀滤波
dst2=sm.dilation(img,sm.square(15))  #用边长为15的正方形滤波器进行膨胀滤波
plt.figure('morphology',figsize=(8,8))
plt.subplot(131)
plt.title('origin image')
plt.imshow(img,plt.cm.gray)
plt.subplot(132)
plt.title('morphological image')
plt.imshow(dst1,plt.cm.gray)
plt.subplot(133)
plt.title('morphological image')
plt.imshow(dst2,plt.cm.gray)

注意,如果处理图像为二值图像(只有0和1两个值),则可以调用:
skimage.morphology.binary_dilation(image, selem=None)

2、腐蚀(erosion)
函数:skimage.morphology.erosion(image, selem=None)
selem表示结构元素,用于设定局部区域的形状和大小。
和膨胀相反的操作,将0值扩充到邻近像素。扩大黑色部分,减小白色部分。可用来提取骨干信息,去掉毛刺,去掉孤立的像素。

from skimage import data
import skimage.morphology as sm
import matplotlib.pyplot as plt
img=data.checkerboard()
dst1=sm.erosion(img,sm.square(5))  #用边长为5的正方形滤波器进行膨胀滤波
dst2=sm.erosion(img,sm.square(25))  #用边长为25的正方形滤波器进行膨胀滤波
plt.figure('morphology',figsize=(8,8))
plt.subplot(131)
plt.title('origin image')
plt.imshow(img,plt.cm.gray)
plt.subplot(132)
plt.title('morphological image')
plt.imshow(dst1,plt.cm.gray)
plt.subplot(133)
plt.title('morphological image')
plt.imshow(dst2,plt.cm.gray)

3、开运算(opening)
函数:skimage.morphology.openning(image, selem=None)
selem表示结构元素,用于设定局部区域的形状和大小。

from skimage import io,color
import skimage.morphology as sm
import matplotlib.pyplot as plt
img=color.rgb2gray(io.imread('d:/pic/mor.png'))
dst=sm.opening(img,sm.disk(9))  #用边长为9的圆形滤波器进行膨胀滤波
plt.figure('morphology',figsize=(8,8))
plt.subplot(121)
plt.title('origin image')
plt.imshow(img,plt.cm.gray)
plt.axis('off')
plt.subplot(122)
plt.title('morphological image')
plt.imshow(dst,plt.cm.gray)
plt.axis('off')

4、闭运算(closing)
函数:skimage.morphology.closing(image, selem=None)

selem表示结构元素,用于设定局部区域的形状和大小。

先膨胀再腐蚀,可用来填充孔洞。

from skimage import io,color
import skimage.morphology as sm
import matplotlib.pyplot as plt
img=color.rgb2gray(io.imread('d:/pic/mor.png'))
dst=sm.closing(img,sm.disk(9))  #用边长为5的圆形滤波器进行膨胀滤波
plt.figure('morphology',figsize=(8,8))
plt.subplot(121)
plt.title('origin image')
plt.imshow(img,plt.cm.gray)
plt.axis('off')
plt.subplot(122)
plt.title('morphological image')
plt.imshow(dst,plt.cm.gray)
plt.axis('off')

5、白帽(white-tophat)
函数:skimage.morphology.white_tophat(image, selem=None)
selem表示结构元素,用于设定局部区域的形状和大小。
将原图像减去它的开运算值,返回比结构化元素小的白点

from skimage import io,color
import skimage.morphology as sm
import matplotlib.pyplot as plt
img=color.rgb2gray(io.imread('d:/pic/mor.png'))
dst=sm.white_tophat(img,sm.square(21))  
plt.figure('morphology',figsize=(8,8))
plt.subplot(121)
plt.title('origin image')
plt.imshow(img,plt.cm.gray)
plt.axis('off')
plt.subplot(122)
plt.title('morphological image')
plt.imshow(dst,plt.cm.gray)
plt.axis('off')

6、黑帽(black-tophat)
函数:skimage.morphology.black_tophat(image, selem=None)
selem表示结构元素,用于设定局部区域的形状和大小。
将原图像减去它的闭运算值,返回比结构化元素小的黑点,且将这些黑点反色。

from skimage import io,color
import skimage.morphology as sm
import matplotlib.pyplot as plt
img=color.rgb2gray(io.imread('d:/pic/mor.png'))
dst=sm.black_tophat(img,sm.square(21))  
plt.figure('morphology',figsize=(8,8))
plt.subplot(121)
plt.title('origin image')
plt.imshow(img,plt.cm.gray)
plt.axis('off')
plt.subplot(122)
plt.title('morphological image')
plt.imshow(dst,plt.cm.gray)
plt.axis('off')

1维 2维是指数组, x, y吗?
y 是信号数组,x 是时间 。x可以通过 y 求出来,需要知道信号的频率

看你需要哪种滤波,如果是基于EMD经验模态分解的,可以调用EMD-signal的包
git clone https://github.com/laszukdawid/PyEMD
python setup.py install
安装完使用就好,
如果是卡尔曼滤波可以考虑

def kalman(z_measure,x_last=0,p_last=0,Q=0.018,R=0.0542):
    x_mid = x_last
    p_mid = p_last + Q
    kg = p_mid/(p_mid + R)
    x_now = x_mid + kg*(z_measure - x_mid)
    p_now = (1-kg)*p_mid
    p_last = p_now
    x_last = x_now
    return x_now,p_last,x_last

如果一维信号是声音数据的话,可以直接用librosa,声音一般都是时域转频域,通过傅立叶变换转频谱图处理