fix: noise model minimum sensitivity
This commit is contained in:
1 parent
4627bb78a6
commit
3b8a596ad5
4 files changed
+75
-2
No files matched your search
@@ -51,6 +51,13 @@ data class CalibratedRawNoiseProfile(
|
||||
}
|
||||
}
|
||||
|
||||
/** Lowest integer ISO whose read variance is positive in every plane. */
|
||||
val minimumCompatibleSensitivity: Int = minimumCompatibleReadSensitivity().also { minimum ->
|
||||
require(minimum <= maximumCompatibleSensitivity) {
|
||||
"RAW noise model has no common sensitivity with positive read variance in every plane"
|
||||
}
|
||||
}
|
||||
|
||||
fun evaluate(
|
||||
sensitivity: Int,
|
||||
minimumSensitivityIso: Int = 0,
|
||||
@@ -95,7 +102,7 @@ data class CalibratedRawNoiseProfile(
|
||||
}
|
||||
|
||||
fun compatibleSensitivityAt(sensitivity: Int): Int? =
|
||||
sensitivity.takeIf { it > 0 }?.coerceAtMost(maximumCompatibleSensitivity)
|
||||
sensitivity.takeIf { it > 0 }?.coerceIn(minimumCompatibleSensitivity, maximumCompatibleSensitivity)
|
||||
|
||||
/** MGC's total gain, or the legacy ISO/100 display coordinate for an external `.c`. */
|
||||
fun overallGainAt(sensitivity: Int, minimumSensitivityIso: Int = 0): Double? =
|
||||
@@ -174,6 +181,30 @@ data class CalibratedRawNoiseProfile(
|
||||
val digitalGain: Double,
|
||||
)
|
||||
|
||||
private fun minimumCompatibleReadSensitivity(): Int {
|
||||
var minimum = 1
|
||||
for (plane in 0 until CHANNEL_COUNT) {
|
||||
val quadratic = readQuadraticC[plane]
|
||||
val digital = readDigitalGainD[plane]
|
||||
if (quadratic <= 0.0 || digital > 0.0) continue
|
||||
// Above the external analog limit, digital gain preserves the variance sign.
|
||||
var low = 1
|
||||
var high = minOf(maxAnalogSensitivity ?: Int.MAX_VALUE, maximumCompatibleSensitivity)
|
||||
fun positiveRead(iso: Int): Boolean =
|
||||
(quadratic * iso.toDouble() * iso.toDouble() + digital).toFloat() > 0f
|
||||
require(positiveRead(high)) {
|
||||
"RAW noise model has no sensitivity with positive read variance in plane $plane"
|
||||
}
|
||||
// Evaluate the polynomial itself to exclude exact roots and rounded zeroes.
|
||||
while (low < high) {
|
||||
val middle = low + (high - low) / 2
|
||||
if (positiveRead(middle)) high = middle else low = middle + 1
|
||||
}
|
||||
minimum = maxOf(minimum, low)
|
||||
}
|
||||
return minimum
|
||||
}
|
||||
|
||||
private fun maximumCompatibleReadSensitivity(): Int {
|
||||
var maximum = Int.MAX_VALUE
|
||||
for (plane in 0 until CHANNEL_COUNT) {
|
||||
|
||||
@@ -779,7 +779,7 @@ internal class GlesMgcRawSpatialStacker(
|
||||
"minimumIso=${frames.map { it.minimumSensitivityIso }} " +
|
||||
"maximumAnalogIso=${frames.map { it.maximumAnalogSensitivityIso }} " +
|
||||
"compatibleIso=$compatibleIso " +
|
||||
"compatibleMax=$compatibleMax " +
|
||||
"compatibleMin=${profile.minimumCompatibleSensitivity} compatibleMax=$compatibleMax " +
|
||||
"overallGain=${compatibleIso.mapIndexed { index, iso ->
|
||||
iso?.let {
|
||||
profile.overallGainAt(it, frames[index].minimumSensitivityIso)
|
||||
|
||||
@@ -317,6 +317,40 @@ class CalibratedRawNoiseProfileTest {
|
||||
assertTrue(shotModel.evaluate(uiIso) > shotModel.evaluate(tuningGain) * 400.0)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun lowIsoWithNegativeReadInterceptUsesCommonPositiveBoundary() {
|
||||
val profile = CalibratedRawNoiseProfile(
|
||||
id = "lower-bounded",
|
||||
shotSlopeA = DoubleArray(4) { 1e-6 },
|
||||
shotInterceptB = DoubleArray(4),
|
||||
readQuadraticC = DoubleArray(4) { 1.0 },
|
||||
readDigitalGainD = doubleArrayOf(-10_000.0, -40_000.0, -40_000.0, -10_000.0),
|
||||
maxAnalogSensitivity = 3200,
|
||||
)
|
||||
assertEquals(201, profile.minimumCompatibleSensitivity)
|
||||
assertEquals(Int.MAX_VALUE, profile.maximumCompatibleSensitivity)
|
||||
assertEquals(201, profile.compatibleSensitivityAt(100))
|
||||
assertEquals(3201, profile.compatibleSensitivityAt(3201))
|
||||
assertNull(profile.compatibleSensitivityAt(0))
|
||||
val low = requireNotNull(profile.evaluate(100))
|
||||
val boundary = requireNotNull(profile.evaluate(201))
|
||||
assertTrue(low.readNoise.all { it > 0f })
|
||||
assertTrue(low.readNoise.contentEquals(boundary.readNoise))
|
||||
assertTrue(low.shotNoise.contentEquals(boundary.shotNoise))
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException::class)
|
||||
fun rejectsNonOverlappingChannelReadNoiseIntervals() {
|
||||
CalibratedRawNoiseProfile(
|
||||
id = "disjoint",
|
||||
shotSlopeA = DoubleArray(4) { 1e-6 },
|
||||
shotInterceptB = DoubleArray(4),
|
||||
readQuadraticC = doubleArrayOf(1.0, -1.0, 1.0, 1.0),
|
||||
readDigitalGainD = doubleArrayOf(-40_000.0, 10_000.0, 0.0, 0.0),
|
||||
maxAnalogSensitivity = 3200,
|
||||
)
|
||||
}
|
||||
|
||||
private fun fitLinear(x: DoubleArray, y: DoubleArray): LinearModel {
|
||||
require(x.size == y.size && x.isNotEmpty())
|
||||
val meanX = x.average()
|
||||
|
||||
Reference in new issue
Block a user