# How to modify the Kalman filter to the result was similar to the envelope of the original signal in Matlab?

Took the implementation of the Kalman filter from these articles: https://clck.ru/FSbT2, https://habr.com/ru/post/166693/
My task in the condition of a signal, which looks like this:
``````for x = 1:N
A(x) = exp((-(x-500)^2)/50000);
y(x) = A(x)*cos(2*3.14*0.01*x)+normrnd(0,sigmaPsi);
end``````

After applying Kalman filter result, almost repeats the original signal (as in the problem of the above sources):

I need to schedule a Kalman filter was not the closest to the original signal and its envelope. Accordingly, it is necessary to slightly change the algorithm of Kalman filter:
``````for t=1:(N-1)
eOpt(t+1)=sqrt((sigmaEtaFilter^2)*(eOpt(t)^2+sigmaPsi^2)/(sigmaEtaFilter^2+eOpt(t)^2+sigmaPsi^2)); %minimization of error values
msum = msum + eOpt(t+1);
sum = sum + (eOpt(1))^2;
K(t+1)=(eOpt(t+1))^2/sigmaEtaFilter^2; %the expression for error
xOpt(t+1)=(xOpt(t))*(1-K(t+1))+K(t+1)*z(t+1);
end;``````

How can I do that ? For a long time trying to figure it out, but to no avail.
Program code :
``````N = 1000;
sigmaPsi=0.05; %the real error (error model)
sigmaEtaModel=0.5; %measurement error of the device
sigmaEtaFilter = 0.8;
k=1:N;
x=k;
for x = 1:N
A(x) = exp((-(x-500)^2)/50000);
y(x) = A(x)*cos(2*3.14*0.01*x)+normrnd(0,sigmaPsi);
z(x)=y(x)+normrnd(0,sigmaEtaModel);
end
%kalman filter
xOpt(1)=z(1); %a good approximation for the true coordinates
eOpt(1)=sigmaEtaFilter; %variance
msum = eOpt(1);
sum = (eOpt(1))^2;
for t=1:(N-1)
eOpt(t+1)=sqrt((sigmaEtaFilter^2)*(eOpt(t)^2+sigmaPsi^2)/(sigmaEtaFilter^2+eOpt(t)^2+sigmaPsi^2)); %minimization of error values
msum = msum + eOpt(t+1);
sum = sum + (eOpt(1))^2;
K(t+1)=(eOpt(t+1))^2/sigmaEtaFilter^2; %the expression for error
xOpt(t+1)=(xOpt(t))*(1-K(t+1))+K(t+1)*z(t+1);
end;
sr = msum/N;
vdisp = sum/N - sr^2;

hold all;
plot(k,xOpt,'--','linewidth',2.5);
plot(k,A,'linewidth',1.5);
plot(k,y,'LineWidth',1.5);
title('filtering Results');
xlabel('Time (s)');
ylabel('Coordinate (m)');
legend('values of the Kalman filter', 'Envelop','Original signal');``````
March 19th 20 at 09:05
``````for x = 1:N