import matplotlib.pyplot as plt from numpy import* from scipy.stats import * def graf(a):#группировка данных a.sort() n=len(a) m= int(10+sqrt(n)) d=(max(a)-min(a))/m x=[];y=[] for i in arange(0,m,1): x.append(min(a)+d*i) k=0 for j in a: if min(a)+d*i <=j<min(a)+d*(i+1): k=k+1 y.append(k) h=(1+0.5*m/n)*0.5*d*n*10**(-sum([w*log10(w) for w in y if w!=0])/n) k=h/std (a) mu4=sum ([(w-mean (a))**4 for w in a])/n psi=(std(a))**2/sqrt(mu4) return psi,k b=800#объём контрольной выборки plt.title('Плоскость законов распределения', size=12) plt.xlabel('Коэффициент $\psi$', size=12) plt.ylabel('Энтропийный коэффициент k', size=12) a=uniform.rvs( size=b) psi,k,=graf(a) nr="Равномерное : k=%s,$\psi$=%s "%(round(k,3),round(psi,3)) plt.plot(psi,k,'o', label=nr) a=logistic.rvs( size=b) psi,k,=graf(a) nr="Логистическое:k=%s,$\psi$=%s "%(round(k,3),round(psi,3)) plt.plot(psi,k,'o', label=nr) a=norm.rvs( size=b) psi,k,=graf(a) nr="Нормальное :K=%s,$\psi$i=%s "%(round(k,3),round(psi,3)) plt.plot(psi,k,'o', label=nr) a= erlang.rvs(4,size=b) psi,k,=graf(a) nr=" Эрланга :k=%s,$\psi$=%s "%(round(k,3),round(psi,3)) plt.plot(psi,k, 'o', label=nr) a= pareto.rvs(4,size=b) psi,k,=graf(a) nr="Парето :k=%s,$\psi$=%s "%(round(k,3),round(psi,3)) plt.plot(psi,k, 'o', label=nr) a = cauchy.rvs(size=b) psi,k,=graf(a) nr="Коши :k=%s,$\psi$=%s "%(round(k,3),round(psi,3)) plt.plot(psi,k,'o', label=nr) c = 0.412 a = genlogistic.rvs(c, size=b) psi,k,=graf(a) nr="Логистическое -1:k=%s,$\psi$=%s "%(round(k,3),round(psi,3)) plt.plot(psi,k,'o', label=nr) mu=0.6 a = poisson.rvs(mu, size=b) psi,k,=graf(a) nr="Пуассона:k=%s,$\psi$=%s "%(round(k,3),round(psi,3)) plt.plot(psi,k,'o', label=nr) a= laplace.rvs(size=b) psi,k,=graf(a) nr="Лапласа:k=%s,$\psi$=%s "%(round(k,3),round(psi,3)) plt.plot(psi,k,'o', label=nr) plt.legend(loc='best') plt.grid() plt.show()
from numpy import * import matplotlib.pyplot as plt from scipy.stats import * def graf(a):#группировка данных для определения энтропийной погрешности a.sort() n=len(a) m= int(10+sqrt(n)) d=(max(a)-min(a))/m x=[];y=[] for i in arange(0,m,1): x.append(min(a)+d*i) k=0 for j in a: if min(a)+d*i <=j<min(a)+d*(i+1): k=k+1 y.append(k) h=(1+0.5*m/n)*0.5*d*n*10**(-sum([w*log10(w) for w in y if w!=0])/n) return h def Kalman(a,x,sz,R1): R = R1*R1 # Дисперсия Q = 1e-5 # Дисперсия случайной величины в модели системы # Выделение памяти под массивы: xest1 = zeros(sz) # Априорная оценка состояния xest2 = zeros(sz) # Апостериорная оценка состояния P1 = zeros(sz) # Априорная оценка ошибки P2 = zeros(sz) # Апостериорная оценка ошибки G = zeros(sz) # Коэффициент усиления фильтра xest2[0] = 0.0 P2[0] = 1.0 for k in arange(1, sz): # Цикл по отсчётам времени. xest1[k] = xest2[k-1] # Априорная оценка состояния. P1[k] = P2[k-1] + Q# Априорная оценка ошибки. # После получения нового значения измерения вычисляем апостериорные оценки : G[k] = P1[k] / ( P1[k] + R ) xest2[k] = xest1[k] + G[k] * ( a[k] - xest1[k] ) P2[k] = (1 - G[k]) * P1[k] return xest2,P1 nr="распределением Коши " x =2# Истинное значение измеряемой величины фильтру неизвестно) R1 = 0.1 # Ср . кв . ошибка измерения . sz = 50 # Число итераций. a = cauchy.rvs(x, R1, size=sz) xest2,P1=Kalman(a,x,sz,R1) plt.plot(a, 'k+', label='Зашумлённые измерения') plt.plot(xest2,'b-', label='Апостериорная оценка') plt.axhline(x, color='g', label='Истинное значение') plt.axis([0, sz,-x, 2*x]) plt.legend() plt.xlabel('Номер итерации') plt.ylabel(u'Измеряемая величина') h1=graf(a) h2=graf(xest2) plt.title('Подавление шумов с %s.\n Энтропийная погрешность до ФК $\Delta $1=%s после ФК $\Delta $2=%s '%(nr,round(h1,3),round(h2,3)), size=12) plt.grid() plt.show()
f_ {X} (x) = \ left \ {\ begin {matrix} \ frac {kx_ {k} ^ {m}} {x ^ {k + 1}}, & x \ geq x_ {m} \\ 0, & x <x_ {m} \ end {matrix} \ right.
from numpy import * import matplotlib.pyplot as plt from scipy.stats import * def graf(a):#группировка данных для определения энтропийной погрешности a.sort() n=len(a) m= int(10+sqrt(n)) d=(max(a)-min(a))/m x=[];y=[] for i in arange(0,m,1): x.append(min(a)+d*i) k=0 for j in a: if min(a)+d*i <=j<min(a)+d*(i+1): k=k+1 y.append(k) h=(1+0.5*m/n)*0.5*d*n*10**(-sum([w*log10(w) for w in y if w!=0])/n) return h def Kalman(a,x,sz,R1): R = R1*R1 # Дисперсия Q = 1e-5 # Дисперсия случайной величины в модели системы # Выделение памяти под массивы: xest1 = zeros(sz) # Априорная оценка состояния xest2 = zeros(sz) # Апостериорная оценка состояния P1 = zeros(sz) # Априорная оценка ошибки P2 = zeros(sz) # Апостериорная оценка ошибки G = zeros(sz) # Коэффициент усиления фильтра xest2[0] = 0.0 P2[0] = 1.0 for k in arange(1, sz): # Цикл по отсчётам времени. xest1[k] = xest2[k-1] # Априорная оценка состояния. P1[k] = P2[k-1] + Q# Априорная оценка ошибки. # После получения нового значения измерения вычисляем апостериорные оценки : G[k] = P1[k] / ( P1[k] + R ) xest2[k] = xest1[k] + G[k] * ( a[k] - xest1[k] ) P2[k] = (1 - G[k]) * P1[k] return xest2,P1 nr="распределением Гаусса " x =2# Истинное значение измеряемой величины фильтру неизвестно) R1 = 0.1 # Ср . кв . ошибка измерения . sz = 50 # Число итераций. a=norm.rvs( x, R1, size=sz) xest2,P1=Kalman(a,x,sz,R1) plt.plot(a, 'k+', label='Зашумлённые измерения') plt.plot(xest2,'b-', label='Апостериорная оценка') plt.axhline(x, color='g', label='Истинное значение') plt.axis([0, sz,-x, 2*x]) plt.legend() plt.xlabel('Номер итерации') plt.ylabel(u'Измеряемая величина') h1=graf(a) h2=graf(xest2) plt.title('Подавление шумов с %s.\n Энтропийная погрешность до ФК $\Delta $1=%s после ФК $\Delta $2=%s '%(nr,round(h1,3),round(h2,3)), size=12) plt.grid() plt.show()
from numpy import * import matplotlib.pyplot as plt from scipy.stats import * def graf(a):#группировка данных для определения энтропийной погрешности a.sort() n=len(a) m= int(10+sqrt(n)) d=(max(a)-min(a))/m x=[];y=[] for i in arange(0,m,1): x.append(min(a)+d*i) k=0 for j in a: if min(a)+d*i <=j<min(a)+d*(i+1): k=k+1 y.append(k) h=(1+0.5*m/n)*0.5*d*n*10**(-sum([w*log10(w) for w in y if w!=0])/n) k=h/std (a) mu4=sum ([(w-mean (a))**4 for w in a])/n psi=(std(a))**2/sqrt(mu4) return h def Kalman(a,x,sz,R1): R = R1*R1 # Дисперсия Q = 1e-5 # Дисперсия случайной величины в модели системы # Выделение памяти под массивы: xest1 = zeros(sz) # Априорная оценка состояния xest2 = zeros(sz) # Апостериорная оценка состояния P1 = zeros(sz) # Априорная оценка ошибки P2 = zeros(sz) # Апостериорная оценка ошибки G = zeros(sz) # Коэффициент усиления фильтра xest2[0] = 0.0 P2[0] = 1.0 for k in arange(1, sz): # Цикл по отсчётам времени. xest1[k] = xest2[k-1] # Априорная оценка состояния. P1[k] = P2[k-1] + Q# Априорная оценка ошибки. # После получения нового значения измерения вычисляем апостериорные оценки : G[k] = P1[k] / ( P1[k] + R ) xest2[k] = xest1[k] + G[k] * ( a[k] - xest1[k] ) P2[k] = (1 - G[k]) * P1[k] return xest2,P1 R1 = 0.9 # Ср . кв . ошибка измерения . a=[ 0.203, 0.154, 0.172, 0.192, 0.233, 0.181, 0.219, 0.153, 0.168, 0.132, 0.204, 0.165, 0.197, 0.205, 0.143, 0.201, 0.168, 0.147, 0.208, 0.195, 0.153, 0.193, 0.178, 0.162, 0.157, 0.228, 0.219, 0.125, 0.101, 0.211,0.183, 0.147, 0.145, 0.181,0.184, 0.139, 0.198, 0.185, 0.202, 0.238, 0.167, 0.204, 0.195, 0.172, 0.196, 0.178, 0.213, 0.175, 0.194, 0.178, 0.135, 0.178, 0.118, 0.186, 0.191] sz = len(a) # Число итераций x =0.179# Истинное значение измеряемой величины фильтру неизвестно) xest2,P1=Kalman(a,x,sz,R1) plt.plot(a, 'k+', label='Зашумлённые измерения') plt.plot(xest2,'b-', label='Апостериорная оценка') plt.axhline(x, color='g', label='Истинное значение') plt.axis([0, sz,-x, 2*x]) plt.legend() plt.xlabel('Номер итерации') plt.ylabel('Измеряемая величина') h1=graf(a) nr=" неизвестным распределением" h2=graf(xest2) plt.title('Подавление шумов с %s \n Энтропийная погрешность до ФК $\Delta $1=%s после ФК $\Delta $2=%s '%(nr,round(h1,3),round(h2,3)), size=12) plt.grid() plt.show()
Source: https://habr.com/ru/post/438050/