# Wiener filter to estimate constant signal

I want to compare the performance of a Wiener Filter and the Kalman filer to estimate the value of a constant $d$ using mesurements corrupted by a white noise. That is, my measurements are of the form $$x(n) = d + v(n)$$ where $v(n)$ have a normal distribution with mean $0$ and known variance $\sigma^2$.

Using the Kalman filter, I could put it on State Space form $$d(n+1) = d(n)$$ $$x(n) = d(n) + v(n)$$ and solve the problem. But I am having difficults to set the problem so I can solve with the Wiener filter. My desided signal is the constant signal $x$. My filter input is the measurements $x$. But I have doubts if I am doing something wrong, because my estimated signal is not a really good estimative of the desired response. I used the following matlab code:

function test()
n = 0:511;
d = 10 * ones(1,512);
v = 0.5*randn(1,512);
x = d + v;
w = WienerFIRFilter(x, d, 12);
y = filter(w', 1, x);

plot(x)
hold on
plot(y, 'r')
end


The function WienerFIRFilter is defined as following

function w=WienerFIRFilter(u,d,M)
aux = xcorr(d,u,'biased');
p = aux(1,(length(aux)+1)/2:((length(aux)+1)/2)+M-1);
[U, R] = corrmtx(u,M-1);
w=inv(R)*p';
end Am I doing something wrong?

• tangent: Do not use inv! Use the blackslash operator (mldivide or mrdivide). Oct 10, 2016 at 11:58

You can use this code

 function w=WienerFIRFilter(input,desired,M)
auto_corr=xcorr(input,M,'unbiased');
r=auto_corr(M+1:end); % positive lags only
R=toeplitz(r); %correlation matrix
cross_corr=xcorr(input,desired,M,'unbiased');
p=(cross_corr(M+1:end));
w=inv(R)*p'
end

• Thanks for your answer. But can you tell me what am I doing wrong? It is not correct, since I've made it by evaluating the mean, but I cant see what is wrong in my algorithm. Oct 10, 2016 at 19:52
• Your'e welcome @Giiovanna.
– msm
Oct 11, 2016 at 0:59