You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
RingBuffer.Resample repeats source elements unevenly because of banker's rounding, and its docs promise linear interpolation it doesn't do #88
The XML doc on RingBuffer<T>.Resample (Containers/RingBuffer.cs:237) says it "preserves the data pattern by applying simple linear interpolation". It also says that when upsampling, new values are created between existing ones.
The implementation (RingBuffer.cs:274-275) does not interpolate. It picks the nearest existing element:
Nearest-neighbour is unavoidable for a generic T. The problem is Math.Round, which defaults to MidpointRounding.ToEven. For common ratios the mapping often lands exactly on .5, and every such tie goes to the even index. Some source elements then appear three times while their neighbours appear once, which distorts the resampled shape.
In the 5→9 case, element 2 appears three times while elements 1 and 3 each appear once.
The existing test ResampleWithSpecificValues_CorrectlyInterpolates (Containers.Test/RingBufferTests.cs:221-235) has a comment saying it expects 10, 15, 20, 25, 30. The actual output is 10, 10, 20, 30, 30. The test passes only because it asserts indices 0, 2 and 4.
Why it matters
Resample is offered for resampling time series and changing sample rates. A caller who relies on the documented interpolation, or at least on even duplication of samples, gets a distorted signal with no error.
Suggested fix / acceptance criteria
Make the mapping evenly distributed. Either use Math.Round(oldIndex, MidpointRounding.AwayFromZero), or sample from the centre: index = (int)((i + 0.5) * oldCount / length), clamped to [0, oldCount - 1].
Correct the XML doc to describe nearest-neighbour selection, since T is generic.
Fix the misleading test comment. Add tests that assert the full output for 5→9 (no source element appears more than ⌈9/5⌉ = 2 times) and for 2→3.
Priority: Medium. Resample quietly distorts the signal shape at common ratios, its XML doc promises interpolation it doesn't do, and an existing test hides the problem by asserting only indices 0, 2 and 4.
Area / suggested assignment:Containers/RingBuffer.csResample and Containers.Test/RingBufferTests.cs. @matt-edmondson
Notes: Centre sampling, (int)((i + 0.5) * oldCount / length), gives the evenest spread and avoids floating-point ties altogether. Assert the full output arrays in the new tests.
Activity
matt-edmondson commentedon Sep 28, 2026
Triage
Resamplequietly distorts the signal shape at common ratios, its XML doc promises interpolation it doesn't do, and an existing test hides the problem by asserting only indices 0, 2 and 4.Containers/RingBuffer.csResampleandContainers.Test/RingBufferTests.cs. @matt-edmondsonNotes: Centre sampling,
(int)((i + 0.5) * oldCount / length), gives the evenest spread and avoids floating-point ties altogether. Assert the full output arrays in the new tests.Generated by Claude Code