echo 0
print "This example shows how astronomical data can be deconvolved using"
print "different deconvolution techniques"

size = 256

pgbeg 0, plotdevice, 1, 1
pgask 0
pgsubp 2,2
pgsch 2
pgenv 1, size, 1, size, 1, -2
pgsitf 0
pglab "", "", "Source distribution"

// Create artificial dataset
image = floatarray(size, size)
psf   = airy(3.5,0.02, 2.2) + gauss(129,129,20)*2

// spread out random stars (500)
for i=1, 500 {
  image[random() * (size-1) + 1, random() * (size-1) + 1] = 1 + random() * 2000
}

// add some noise
for x=1, size {
  for y=1, size {
    image[x,y] += random()*30
  }
}

im = log(smooth(image,1.5))
pggray im, size, size, 1, size, 1, size, max(im), min(im), [0,1,0,0,0,1]

pgenv 1, size, 1, size, 1, -2
pglab "", "", "Convolved with Point Spread Function"
image = convolve(image, psf)
im = log(image)
pggray im, size, size, 1, size, 1, size, max(im), min(im), [0,1,0,0,0,1]

pgenv 1, size, 1, size, 1, -2
pglab "", "", "High pass filtered"
im = image - smooth(image,3)
pggray im, size, size, 1, size, 1, size, max(im), min(im), [0,1,0,0,0,1]

pgenv 1, size, 1, size, 1, -2
pglab "", "", "Deconvolved using Wiener filtering"
im = clip(wien(image, norm(psf, /total), .01),0,-1)
pggray im, size, size, 1, size, 1, size, max(im), min(im), [0,1,0,0,0,1]

pgend
echo 1
