Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

C:\Users\Tianrui\Dropbox\wiener1_template.

m Saturday, May 10, 2014 2:09 PM


% wiener1_template.m
% wiener filter problem
clear;close all;
rand('state', 0); randn('state', 0);
nx = 62; ny = 50;
p = 0.004;
rect2 = zeros(27,27);
%rect([12:16 20:24], 12:16) = 1;
rect2(10:18, 11:17) = 1;
rec = rect2;
rect2 = rect2 / sqrt(p*(1-p)); % scale
bnm = (rand(nx,ny) < p) - p; % bernoulli
fnm = conv2(bnm, rect2, 'same');
vnm = 5 * randn(size(fnm));
gnm = fnm + vnm;
rf = xcorr2(fnm,fnm) / (nx*ny); % empirical autocorrelation estimate
%rs = rf(nx/2:end-nx/2,ny/2:end-ny/2);
Pf = fftshift(fft2(fftshift(rf))); % power spectrum of f[n,m]
Pf = real(Pf);
rv = xcorr2(vnm,vnm) / (nx*ny);
%rvv = rv(nx/2:end-nx/2,ny/2:end-ny/2);
Pv = fftshift(fft2(fftshift(rv)));
Pv = real(Pv);
H = Pf./(Pf+Pv); % wiener filter
%HH = H(nx/2:end-nx/2,ny/2:end-ny/2);
h = fftshift(ifft2(fftshift(H)));
fhat = conv2(gnm, h, 'same'); % wiener filter estimate
fhat = real(fhat);
clf, pl = @(i) subplot(330+i); colormap(1-gray(256))
pl(1), imagesc(1:nx,1:ny,fnm'), axis xy, axis image
xlabel n, ylabel m, title 'True: f[n,m]', colorbar
[ix,iy] = size(rf);
Rf = conv2(rec,rec); %analytically autocorrelation
[fx,fy] = size(Rf);
Rff = zeros(ix,iy);
Rff(nx-(fx-1)/2:nx+(fx-1)/2,ny-(fy-1)/2:ny+(fy-1)/2) = Rf;
pl(2), imagesc(1:ix,1:iy,Rff'), axis xy, axis image
xlabel n, ylabel m, title 'R_f[n,m] true', colorbar
pl(3), imagesc(1:ix, 1:iy, rf'), axis xy, axis image
xlabel n, ylabel m, title 'R_s(n,m) empirical', colorbar
pl(4), imagesc(1:nx,1:ny,gnm'), axis xy, axis image
xlabel n, ylabel m, title 'Noisy: g[n,m]', colorbar
-1-
C:\Users\Tianrui\Dropbox\wiener1_template.m Saturday, May 10, 2014 2:09 PM
pl(5)
[ik,il] = size(H);
imagesc(1:ik, 1:il, Pf'), axis xy, axis image, colorbar
xlabel k, ylabel l, title 'Signal power spectrum: P_f[k,l]'
pl(6)
imagesc(1:ik, 1:il, H'), axis xy, axis image
xlabel k, ylabel l, title 'Wiener filter: H(k,l)', colorbar
pl(8)
imagesc(1:nx, 1:ny, fhat'), axis xy, axis image
xlabel n, ylabel m, title 'Wiener output: f.hat[n,m]', colorbar
f_hat = npls_sps(gnm);
pl(9)
imagesc(1:nx, 1:ny, f_hat'), axis xy, axis image
xlabel n, ylabel m, title 'NPLS output: f.hat[n,m]', colorbar
wnr_temp = (fhat-fnm).^2; %MSE
npls_temp = (f_hat-fnm).^2;
MSE_wnr = sum(wnr_temp(:)) / (nx*ny)
MSE_npls = sum(npls_temp(:)) / (nx*ny)
orient tall, print('fig', '-deps')
-2-

You might also like