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:
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
print(timestep_for_snr([0.9, 0.5, 0.1], 1.0))
1
- Compute the Signal-to-Noise Ratio (SNR) for each timestep using the formula , where is the value at index .
- For , , so .
- For , , so .
- For , , so .
- Calculate the absolute difference between each SNR and the target SNR of : , , and .
- Select the timestep with the minimum difference; since is the smallest, the chosen index is .
- 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 of1.0has infinite SNR.- Ties in the distance go to the smaller index.