question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow specifying expected output length when resampling

See original GitHub issue

Hi Alexandre 😃

Consider this scenario:

We have a machine learning model that takes in and outputs audio at a high sample rate. For some reasons (amongst other things execution time) the model uses an internal sample rate that is lower than the input-output. The output shape is expected to be the same as the input shape.

class ResampleWrapper(nn.Module):
    """
    This class downsamples audio before it's passed to the audio denoiser model,
    and upsamples the audio back to the original sample rate before returning.
    """

    def __init__(
        self,
        model: nn.Module,
        input_sample_rate: int = 48_000,
        internal_sample_rate: int = 32_000,
    ):
        super().__init__()
        self.model = model
        self.input_sample_rate = input_sample_rate
        self.internal_sample_rate = internal_sample_rate

        self.downsampler = ResampleFrac(
            self.input_sample_rate, self.internal_sample_rate
        )
        self.upsampler = ResampleFrac(self.internal_sample_rate, self.input_sample_rate)

    def forward(self, x):
        """
        :param x: tensor with shape (batch_size, num_channels, num_samples)
        :return: tensor with shape (batch_size, num_channels, num_samples)
        """
        x = self.downsampler(x)
        x = self.model(x)
        x = self.upsampler(x)
        return x

Depending on the exact length of the input, downsampling and then upsampling will often give an output that is off by one sample in length.

In librosa this kind of issue can be solved by setting the fix parameter or by using fix_length manually.

I imagine that julius.ResampleFrac could provide a parameter called something like expected_length in its forward function that customizes the slice end offset in the end so that the result has the given length 😄

Would you like a pull request that adds this feature?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
adefossezcommented, Jul 24, 2021

Hey @iver56 , yes definitely length of the sequence can become problematic, especially with non trivial sample rates and when trying to align with other transforms. I would be happy to accept a PR that allows to chose a custom length between floor(length * new_sr / old_sr), ceil(length * new_sr / old_sr). It might be as easy as changing this line https://github.com/adefossez/julius/blob/main/julius/resample.py#L122 but it is possible in some cases one need to do extra padding on the right too 3 lines above.

0reactions
iver56commented, Aug 3, 2021

Yeah, I agree, it’s fine 😃 I was just confused initially. Maybe it’s a good idea to add an example of this use case (downsample, do something, then upsample) to the documentation?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Time Series Data Analysis — Resample | by James Ho
When it comes to time series analysis, resampling is a critical technique that allows you to flexibly define the resolution of the data...
Read more >
How To Resample and Interpolate Your Time Series Data ...
We can use this function to transform our monthly dataset into a daily dataset by calling resampling and specifying the preferred frequency ...
Read more >
Resample operation - WaveBeans
Resample allows you to change the sample rate of the stream, for the cases ... It is not expected that the length of...
Read more >
Resample DataArray on only one of TWO time dimensions
Issue: I have a DataArray with two time dimensions (initialization time and a forecast time. When I try to resample over just one...
Read more >
Resampling | Understand How Color Works in Photoshop
With Resample Image turned on, you can change the pixel dimensions, the document size, or the resolution. You also have the ability to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found