The calculations of mean, variance, and standard deviation build off each other. For
hardware efficiency, the calculation logic is shared as shown.
Mean
The equation to calculate the precise mean pixel value requires large internal word
lengths and expensive division logic.
Instead of using this equation, the block calculates the mean by a series of four
accumulators that compute the mean of a segment of pixels. First, the block finds the sum
of a window of 64 pixels, and normalizes.
Then it accumulates 64 of the previous windows, and normalizes.
A third accumulator sums 64 of the 64×64 windows, and normalizes the same
way.
The fourth accumulator sums 64 of the 64×64×64 windows and
normalizes.
Each valid pixel is accumulated as it arrives. Its location within a line or frame
does not affect the accumulation logic.
When vEnd
is received, the block promotes any remaining data in the
four levels of mean calculation to calculate the final output. If an accumulator counter
is not at 64 when vEnd
arrives, that level normalizes by the actual
value of the counter. The constants for this multiplication are stored in a lookup table
(LUT). The four accumulators share a single LUT and multiplier. The values in the LUT are
in ufix18_en17
data type so that the multiplier fits into a DSP block
on an FPGA.
This method of mean calculation is accurate when the number of pixels in the frame
aligns vEnd
with the final accumulator rollover. This alignment occurs
at level 2 when the frame contains a multiple of 64 pixels, and fewer than
642(4096) pixels. It occurs at level 3 when the frame
contains a multiple of 4096 pixels. It occurs at level 4 when the frame contains a
multiple of 643 pixels. This method is also accurate when the
frame has fewer than 64 pixels, because only the first accumulator is needed.
However, when the number of pixels in the frame does not fit these conditions, the
block must normalize the final accumulation before the counter reaches 64. This
normalization introduces an error in the calculation at subsequent levels. The figure
shows the normalization error introduced in the mean calculation by image sizes under 4096
pixels. The spikes occur where an image size is just over a multiple of 64 pixels.
For images larger than 4096 pixels, the same effect occurs at multiples of 4096
pixels, and at multiples of 643 pixels.
Variance
The block calculates the variance of the input pixels by using this equation:
The mean and the mean of the squared input are calculated in parallel. The block
calculates the mean of squares using the same approximation method used to calculate the
mean, as described in the previous section.
Standard Deviation
The block calculates the square root of the variance by using a pipelined
bit-set-and-check algorithm. This algorithm computes the square root by using addition and
shifts, rather than multipliers. For an N-bit input, the result has
N bits of accuracy.
This method is hardware efficient for general inputs. If your data has known
characteristics that allow for a more efficient square root implementation, you can
disable the calculation in this block and construct your own logic from HDL-supported
blocks.