function [w,MSE]=filtro_wiener(x,d,M)

N=length(x);

rx=xcorr(x,'biased');
rxd=xcorr(x,d,'biased');

Rx=toeplitz(rx(N:N+M-1));
p=rxd(N:-1:N-(M-1));
w=Rx\p;

rd=xcorr(d,'biased');
MSE=rd(N)-w'*p;