stingray icon indicating copy to clipboard operation
stingray copied to clipboard

Fix: ensure impulse response returns at least one bin when width < dt…

Open Sudarshan-21 opened this issue 8 months ago • 2 comments

Bugfix-for-issue #851: Ensure Non-Zero Impulse Response by robustly handling computation for Small Widths in simple_ir

Summary

This pull request addresses an edge case in the simple_ir method where passing a small width value—specifically, one smaller than self.dt—results in a zero-length impulse response. This occurs because the number of bins is computed using integer division, which truncates the result to zero for width < self.dt. Along with a fine-tuning of rounding off of the (width/ self.dt), initially, we were truncating it to the nearest lower int, but for robust handling of all cases math.ceil() can be used.

🔧 Fix

Original Code:

h_ones = np.ones(int(width / self.dt)) * intensity

Updated Code:

import math
h_zeros = np.zeros(math.ceil(start / self.dt))
h_ones = np.ones(math.ceil(width / self.dt)) * intensity

This ensures that for any non-zero width, there will always be at least one non-zero bin in the impulse response.

Bug Description

The impulse response was being generated using:

int(width / self.dt)

If the width is smaller than self.dt, this computation yields zero bins, effectively omitting the impulse response entirely, despite the user intending to generate a small impulse.

Sample Input

self.dt = 1.0
start = 3.0
width = 0.6
intensity = 1.0

Current Output:

[0. 0. 0.]

Expected Output:

[0. 0. 0. 1.]

The expected behavior is that even a short width (e.g., 0.6) should produce a visible impulse response (at least one non-zero value) after the delay.

🔍 Fix and Rationale

📊 Output Comparison Table

Input (width) dt Method num_bins Output
0.6 1.0 int(width / dt) 0 [0. 0. 0.]
0.6 1.0 ceil(width / dt) 1 [0. 0. 0. 1.]

This change ensures accuracy across a broader range of valid width values.

❓ Open Question for Reviewers

I would like to know if the rounding off to a lower integer is intentional or is it fine that we round off the (width/self.dt) to the next larger integer.

✅ Benefits

  • Fixes a silent bug for narrow impulse widths
  • Enhances robustness of the impulse generation
  • More faithful representation of user-specified widths

✅ Checklist

  • [x] Fixes the edge case for small widths
  • [x] Preserves compatibility for width >= dt
  • [x] Tested with sample inputs and verified output consistency

Let me know if further refinements or configuration support is required!

Sudarshan-21 avatar Apr 08 '25 11:04 Sudarshan-21

Hey @Sudarshan-21 , thanks for the pr. But can u write a concise detail about the bugfix. It looks like u have use LLMs to write the description. Please see #891

ankitkhushwaha avatar Apr 08 '25 15:04 ankitkhushwaha

Sure, @ankitkhushwaha, I was facing difficulty in generating the tables in .md, so I took help from a LLM. I have now edited it manually, can you please review it. Also, I have a "question for the reviewers" section, which I would like the maintainers' opinion on. It would be helpful if you can give your input.

Sudarshan-21 avatar Apr 08 '25 17:04 Sudarshan-21