% wiener filter 2d
clear;
for k=1:8
    for l=1:8
        xp(k,l)=1+cos((2*pi/8)*(k+l));
    end
end
xp=round(xp*255*0.5);
x=[xp ]; %input
x=double(x);abs(fft2(x))
%
len = 3; ang = 90;
im_r = fspecial('motion', len, ang);
blurred = imfilter(x, im_r, 'conv', 'circular');
n_var = 0.2;
rng('default');
Noise=sqrt(n_var) * randn(8,8)
y = Noise+blurred
% 
A=conj(fft2(im_r,8,8)) .*fft2(im_r,8,8);
B=conj(fft2(Noise,8,8)) .*fft2(Noise,8,8);
C=conj(fft2(x,8,8)) .*fft2(x,8,8);
w=conj(fft2(im_r,8,8))./(A + B./C);
rs=real(ifft2(fft2(y,8,8) .* w))
sum((y(:)-x(:)).^2) % least squares error
sum((x(:)-rs(:)).^2) % least squares error


 



