Skip to content

Keep missing values missing in resample_sig - #615

Merged
tompollard merged 2 commits into
MIT-LCP:mainfrom
TheNetherWatcher:resample-mark-nan
Oct 31, 2026
Merged

tompollard merged 2 commits into
MIT-LCP:mainfrom
TheNetherWatcher:resample-mark-nan

Conversation

@TheNetherWatcher

@TheNetherWatcher TheNetherWatcher commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to @tompollard's note on #604.

resample_sig fills NaN values before resampling, because the FFT would otherwise spread them over the whole signal. But the filled values were then returned as if they were real data, so the output had no trace of where the input was missing.

Now output samples near a NaN input sample are set back to NaN. An output sample at position t (in input samples) is NaN if any input sample within half an output spacing of t is NaN (and within at least one input sample, so neighbours count when upsampling). The filling and resampling are unchanged, so samples away from the gaps keep the same values.

For example, record 100 at 360 Hz with its first 50 samples set to NaN, resampled to 250 Hz, gives NaN for the first 35 output samples (positions below 50) and finite values after. Signals without NaN give the same output as before.

This changes the output for signals with NaN, which is why it's a separate PR. The two tests from #604 now expect NaN at the filled positions.

Tests: new tests check which output samples are NaN when upsampling, and when downsampling with a single NaN and with a gap between two output samples; they fail without the change. pytest -m "not network" passes (109 tests), and the docs build passes.

@tompollard tompollard left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for following up on this so quickly!

One issue when downsampling: the check only looks at the input samples on either side of each output position, so a gap that falls between two output samples disappears. For example, at 1000 Hz → 100 Hz, a single NaN at sample 500 gives one NaN output, but an 8-sample gap at 501–508 gives none.

Marking an output sample if any NaN lies within half an output spacing of it (and at least one input sample, so upsampling is unchanged) fixes this:

half = max(fs / fs_target / 2, 1)
n_nan = np.concatenate([[0], np.cumsum(nan_mask.reshape((-1,)))])
lo = np.clip(np.floor(resampled_t - half).astype(int) + 1, 0, len(t))
hi = np.clip(np.ceil(resampled_t + half).astype(int), 0, len(t))
resampled_x[n_nan[hi] - n_nan[lo] > 0] = np.nan

With this, your tests still pass and upsampling marks the same samples, while the 8-sample gap gives 2 NaN outputs. Could you add a downsampling test for that case too?

@TheNetherWatcher

Copy link
Copy Markdown
Contributor Author

Thanks, good catch! I've switched to your version, and added a downsampling test at 1000 Hz → 100 Hz: a NaN at sample 500 gives output 50, and the gap at 501–508 gives outputs 50 and 51. The gap case fails with the previous version. I also rebased onto main now that #604 is merged, and updated the description and changelog. Tests pass (109).

NaN values are filled before resampling, but the filled values were
returned as if they were real data. Output samples at or next to a NaN
input sample are now NaN.
Only the input samples on either side of each output position were
checked, so a gap between two output samples was lost when
downsampling. Mark an output sample if a NaN lies within half an output
spacing of it (and at least one input sample).
@TheNetherWatcher

Copy link
Copy Markdown
Contributor Author

btw, sorry for incresing your load on halloween today, @tompollard 🤣

@tompollard

Copy link
Copy Markdown
Member

btw, sorry for incresing your load on halloween today, @tompollard 🤣

No problem at all, I've enjoyed working on this. Thanks for all the fixes!

@TheNetherWatcher

Copy link
Copy Markdown
Contributor Author

will work on some more things, tomorrow my time

@tompollard

Copy link
Copy Markdown
Member

Thanks @TheNetherWatcher, I appreciate it.

@tompollard

Copy link
Copy Markdown
Member

Also, side note, love the design here: https://www.bharatkaurav.com/

@tompollard
tompollard merged commit 4aa1e5b into MIT-LCP:main Oct 31, 2026
18 checks passed
@TheNetherWatcher

Copy link
Copy Markdown
Contributor Author

i really like cyberpunk themes, so i love the game cyberpunk 2077, blade runner 2049 and so on stuff

@TheNetherWatcher
TheNetherWatcher deleted the resample-mark-nan branch November 1, 2026 06:13
@tompollard

Copy link
Copy Markdown
Member

same! i'm midway through the game, and the original bladerunner movie is one of my favorites.

@TheNetherWatcher

Copy link
Copy Markdown
Contributor Author

nice! the original is a classic, 2049 did a great job living up to it. how are you finding johnny silverhand so far?
also, phantom liberty is worth it if you haven't got to it yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants