使用高斯混合模型查找波形的精确幅度

问题描述

我正在尝试将波形分解为高斯混合。我正在生成波形的直方图并将这些数据放入高斯混合模型中。我能够找到组件,但我无法正确拟合其幅度。请帮助我。

import matplotlib.pyplot as plt
import scipy.io as sio
import numpy as np
from sklearn.mixture import GaussianMixture


def gaussian(x,m,s,scale):
    sqrt_2pi = np.sqrt(2*np.pi)
    return scale * 1/(s*sqrt_2pi) * np.exp(-0.5*((x-m)/s)**2)

gaswin1 =  gaussian(np.arange(0,100),50,40,1)
gaswin2 = 1.5* gaussian(np.arange(0,70,10,1)
gaswin = gaswin1 + gaswin2
plt.plot(gaswin)

x = np.arange(0,100)
data = []
num_presision = 100
max_y = np.max(gaswin)
for x_,y_ in zip(x,gaswin):
      data += [x_]*int(y_/max_y*num_presision)
      #print(data) 
sy = np.array(data) 
#plt.hist(sy,bins=50,color='orange',label='histogram of generated data')
num_clusters = 2
gmm = GaussianMixture(num_clusters)
gmm.fit(X=np.expand_dims(sy,1))

# sort results in the order of the means
params = zip(gmm.means_.ravel(),gmm.covariances_.ravel(),gmm.weights_)
sorted_params = sorted([ (m,v,w) for m,w in params],key=lambda p:p[0])

# plot the results
gy_list = []
ty = np.zeros(len(gaswin))
for m,w in sorted_params:
    gy = gaussian(x,np.sqrt(v),w)
    gy_list.append(gy)
    ty += gy

plt.plot(ty,':',color='red',linewidth=3,label='GMM fitted curve')
for k,gy in enumerate(gy_list):
    plt.plot(gy,label='%d-th component' % k)

GMM fitted curve

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)