PIXELBANKv9.1.0
Menu

Noise Level at a Target SNR

Problem Statement

Given a schedule of alpha_bar values, find the timestep whose signal-to-noise ratio is closest to a target SNR — the operation behind picking where to start editing/inpainting in the middle of a trajectory.

Background

The per-step SNR is SNR_t = alpha_bar_t / (1 - alpha_bar_t), monotonically decreasing in t. To place an operation at a desired noise level you pick the timestep whose SNR is nearest the target:

t⋆=arg⁡min⁡t  ∣SNRt−target∣t^\star = \arg\min_t \; \lvert \text{SNR}_t - \text{target} \rvert

Ties go to the smaller timestep. A schedule value of exactly 1.0 has infinite SNR; treat its distance to any finite target as infinite so it is never chosen unless it is the only option.

Your Task

Implement:

def timestep_for_snr(alpha_bar, target_snr):

Return the chosen timestep index (an int).

Input Format

  • alpha_bar: list of cumulative products.
  • target_snr (float): the desired SNR, >= 0.

Output Format

  • A single int index.

Sample

print(timestep_for_snr([0.9, 0.5, 0.1], 1.0))

Output:

1

Example:

Input:
print(timestep_for_snr([0.9, 0.5, 0.1], 1.0))
Output:
1
Reasoning:
  • Compute the Signal-to-Noise Ratio (SNR) for each timestep tt using the formula SNRt=αtˉ1−αtˉ\text{SNR}_t = \frac{\alpha_{\bar{t}}}{1 - \alpha_{\bar{t}}}, where αtˉ\alpha_{\bar{t}} is the value at index tt.
  • For t=0t=0, α0ˉ=0.9\alpha_{\bar{0}} = 0.9, so SNR0=0.91−0.9=0.90.1=9.0\text{SNR}_0 = \frac{0.9}{1 - 0.9} = \frac{0.9}{0.1} = 9.0.
  • For t=1t=1, α1ˉ=0.5\alpha_{\bar{1}} = 0.5, so SNR1=0.51−0.5=0.50.5=1.0\text{SNR}_1 = \frac{0.5}{1 - 0.5} = \frac{0.5}{0.5} = 1.0.
  • For t=2t=2, α2ˉ=0.1\alpha_{\bar{2}} = 0.1, so SNR2=0.11−0.1=0.10.9≈0.111\text{SNR}_2 = \frac{0.1}{1 - 0.1} = \frac{0.1}{0.9} \approx 0.111.
  • Calculate the absolute difference between each SNR and the target SNR of 1.01.0: ∣9.0−1.0∣=8.0|9.0 - 1.0| = 8.0, ∣1.0−1.0∣=0.0|1.0 - 1.0| = 0.0, and ∣0.111−1.0∣≈0.889|0.111 - 1.0| \approx 0.889.
  • Select the timestep with the minimum difference; since 0.00.0 is the smallest, the chosen index is 11.
  • The final output is 1

Constraints:

  • 1 <= len(alpha_bar) <= 100000, target_snr >= 0.
  • SNR_t = alpha_bar_t / (1 - alpha_bar_t); a value of 1.0 has infinite SNR.
  • Ties in the distance go to the smaller index.
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.