Computational Vision
U. Minn. Psy 5036W

Assignment #1: Ideal pattern detector

In this assignment, you will simulate an ideal observer called the signal-known-exactly ideal (SKE).  This observer has a built-in template that matches the signal that it is looking for. The signal is embedded in "white gaussian noise".   "white" means the pixels are not correlated with each other--intuitively this means that you can't reliably predict what one  pixel's value is from any of the others.
In the absence of any internal noise, this ideal detector behaves as one would expect a linear neuron to behave when a target signal pattern exactly matches its synaptic weight pattern. There are some neurons in the the primary cortex of the visual system called "simple cells". These cells can be modeled as ideal detectors for the patterns that match their receptive fields. In actual practice, neurons are not noise-free, and not perfectly linear.

Before you get started

When doing calculations on images, it is useful to be able to easily go back and forth between matrix and vector representations of images. So in the exercises below, you have a signal which is represented as a size x size matrix. But in some of the calculations you'd like to treat the signal as a vector. How do you get back and forth? The answer is with Flatten[] and Partition[]. Here is an example:

m = {{1,2,3},{4,5,6},{7,8,9}}
v=Flatten[m]
Partition[v,3]

The target

The target is given to you  below. It is a very low contrast sine-wave grating that is oriented at 45 deg, which is displayed below as deviations about a mean nominal gray-level of 128. The x-y dimensions are 16 x 16, the amplitude is 8, and the mean is 0. We will first define both the target signal and the noise to have zero means so that when we add the noise as contrast noise, the mean doesn't change. Then when we display the image, we will map 0 to a specific graylevel (e.g. ~255/2).
On an 8-bit display, graylevels go from 0 to 255. We use the option PlotRange->{-1,1} to map our signal values {-1,1} to pixel intensity values L, {0,255}. Contrast of a grating is defined as δL/(Subscript[L, 0]), where  (Subscript[L, 0]) is the mean intensity, and δL is the amplitude of the grating. We have chosen our units so that contrast = amplitude of the grating (as used below). Zero maps to graylevel (Subscript[L, 0])= 128 (rounding up). A signal amplitude of 0.0625 maps to 0.0626*128 = 8. So the contrast, δL/(Subscript[L, 0])= 0.0626*128/128 = 0.0625.

(Sidenote:If you were actually doing psychophysics, it would be important to check to make sure that the computer monitor can actually supply the graylevel differences you want.E.g.if the amplitude gets below 1/128, an 8-bit display card won't be able to deliver--and human contrast thresholds can be lower than this. Another note,is that the monitor's intensity isn't equal,nor even linearly related to requested graylevel. This problem can be fixed by doing "gamma correction", which involves setting the lookup table in your computer's graphics card to compensate for the particular display you may be using.)

Grating[x_,y_,fx_,fy_] := Cos[2 Pi (fx x + fy y)];

size = 16;
amplitude = 0.0625; (* 8/128 = 0.0625% contrast*)
signal = Table[amplitude*Grating[x,y,1/16,1/16],
{x,0,size-1},{y,0,size-1}];

ListDensityPlot[signal,Mesh->False,
	Frame->False,PlotRange->{-1,1}];

Problem 1: The noise

Define a function noise that will generate a sizexsize array of Gaussian noise with standard deviation of sigma=48/128 = 0.375, and a mean of 0.  Use the standard library package

<<Statistics`ContinuousDistributions`

Add the signal to the noise to generate an observation vector called observation, and use ListDensityPlot to display a sample observation.

Problem 2-a: Ideal's theoretical d'

Use the formula:

dprime := Sqrt[s.s]/sigma

to calculate the ideal's d' using Mathematica. This formula assumes that the signal has an average value of zero (not 128), so you will have to take that into account.

Problem 2-b: Write a function to calculate d' from a two-alternative forced-choice experiment from a z-score of the proportion correct.

Answer 2-b. I'll give you the answer to this one. Here is the answer using the inverse of a standard mathematical function called Erf[].

z[p_] := Sqrt[2] InverseErf[1 - 2 p];

d' for a 2AFC task is given by the formula:

dprime := -Sqrt[2] Z(Pc)

where Z(*) is the z-score for Pc , the proportion correct. (See the supplementary class notes on SDT.).

dprime[x_] := N[-Sqrt[2] z[x]]

dprime[.76]

Problem 3: Ideal's simulated d' in a two-alternative forced-choice task (2AFC)

Generate ntrials = 100 observation pairs of signal+noise, and  noise. For each pair compute an ideal decision variable (i.e. the output of a cross-correlator, which is the dot product of the observation with the signal). Pick which observation has the higher value of the decision variable. Decide whether this decision is the right answer or not. Total up the proportion of right answers and compute d'. Repeat this four times (total of 400 trials) to get four estimates of d'. How does the average of these four compare to your result from the closed form prediction above?

Problem 4: What would d' be if the template was wrong? Run your simulation above using  the template below:

size = 16;
amplitude = 8;
Grating[x_,y_,fx_,fy_] := 
	N[amplitude Cos[2 Pi (fx x + fy y)]];

wrongtemplate = Table[Grating[x,y,1/16,0],
	{x,0,size-1},{y,0,size-1}];