# Oversampling = random amplitude spikes?

**URL:** <https://forum.juce.com/t/oversampling-random-amplitude-spikes/52785>\
**Category:** General JUCE discussion\
**Created:** [August 27, 2022, 6:08pm UTC](https://forum.juce.com/t/oversampling-random-amplitude-spikes/52785 "2022-08-27T18:08:16Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![cubestar](https://avatars.discourse-cdn.com/v4/letter/c/43a26b/32.png) [@cubestar](https://forum.juce.com/u/cubestar)\
**Post date:** [August 27, 2022, 6:08pm UTC](https://forum.juce.com/t/oversampling-random-amplitude-spikes/52785/1 "2022-08-27T18:08:17Z")

</div>

Hello! I am running into an issue where using oversampling seems to introduce random spikes in amplitude for single samples (this does not happen when not using oversampling). I tried upgrading to Juce 7 and have created a simple test file to demonstrate. I am guessing that I am using the oversampler incorrectly?

I would upload the example file, but I am not allowed to attach yet in the forum. Thanks!

Example output:

```auto
0.00244384
0.00182456
0.0012077
0.000595403
-1.46199e-05
-0.000629048
-0.00125011
-0.00187474

```

In processor.h private:

```auto
juce::dsp::Oversampling< **float** > oversamplingModule;

```

At the top of processor.cpp:

```auto
, // Oversampling
oversamplingModule(2, 1, juce::dsp::Oversampling< **float** >::FilterType::filterHalfBandPolyphaseIIR)

```

In prepare to play:

```auto
    juce::dsp::ProcessSpec spec;
    spec.sampleRate = sampleRate;
    spec.maximumBlockSize = samplesPerBlock;
    spec.numChannels = 2;
    
    oversamplingModule.reset();
    oversamplingModule.initProcessing (static_cast<size_t> (samplesPerBlock));

```

Here’s the entire process block:

```auto
    juce::ScopedNoDenormals noDenormals;
    auto totalNumInputChannels = getTotalNumInputChannels();
    auto totalNumOutputChannels = getTotalNumOutputChannels();

    for (auto i = totalNumInputChannels; i < totalNumOutputChannels; ++i)
        buffer.clear (i, 0, buffer.getNumSamples());

    // Blocks for oversampling
    juce::dsp::AudioBlock<float> block(buffer);
    juce::dsp::AudioBlock<float> upSampledBlock(block);
    upSampledBlock = oversamplingModule.processSamplesUp(block);
    
    for (int channel = 0; channel < totalNumInputChannels; ++channel)
    {
        for (int sample = 0; sample < upSampledBlock.getNumSamples(); ++sample)
        {
        if (channel == 1) {
           DBG(upSampledBlock.getSample(1, sample));
        }
        }

    }

```

---

<div class="post-metadata">

**Author:** ![cubestar](https://avatars.discourse-cdn.com/v4/letter/c/43a26b/32.png) [@cubestar](https://forum.juce.com/u/cubestar)\
**Post date:** [August 27, 2022, 6:47pm UTC](https://forum.juce.com/t/oversampling-random-amplitude-spikes/52785/3 "2022-08-27T18:47:50Z")

</div>

I just got access to upload, so here’s the test file!  
[ClipTestDummy.zip](https://forum.juce.com/uploads/short-url/r7pYqIYg8otOsBrHIkGoRmAOEZr.zip) (65.7 KB)

---

<div class="post-metadata">

**Author:** ![Toddler-Boy](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/toddler-boy/32/2007_2.png) [@Toddler-Boy](https://forum.juce.com/u/Toddler-Boy)\
**Post date:** [August 28, 2022, 12:01am UTC](https://forum.juce.com/t/oversampling-random-amplitude-spikes/52785/4 "2022-08-28T00:01:28Z")

</div>

You’re using the same oversampling object for both channels. You need one object per channel.

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [August 28, 2022, 1:41am UTC](https://forum.juce.com/t/oversampling-random-amplitude-spikes/52785/5 "2022-08-28T01:41:26Z")

</div>

That’s not required with the oversampler, it can deal with stereo and more channels internally. But it of course needs to be initialized to use the needed number of channels.

From the docs :

> A processor that performs multi-channel oversampling.

---

<div class="post-metadata">

**Author:** ![Fandusss](https://avatars.discourse-cdn.com/v4/letter/f/a4c791/32.png) [@Fandusss](https://forum.juce.com/u/Fandusss)\
**Post date:** [August 28, 2022, 1:54am UTC](https://forum.juce.com/t/oversampling-random-amplitude-spikes/52785/6 "2022-08-28T01:54:29Z")

</div>

Is this just a case of ISP’s being generated?

---

<div class="post-metadata">

**Author:** ![cubestar](https://avatars.discourse-cdn.com/v4/letter/c/43a26b/32.png) [@cubestar](https://forum.juce.com/u/cubestar)\
**Post date:** [August 28, 2022, 2:55am UTC](https://forum.juce.com/t/oversampling-random-amplitude-spikes/52785/7 "2022-08-28T02:55:07Z")

</div>

I thought that was the case but was willing to give reFX’s answer a shot!

---

<div class="post-metadata">

**Author:** ![cubestar](https://avatars.discourse-cdn.com/v4/letter/c/43a26b/32.png) [@cubestar](https://forum.juce.com/u/cubestar)\
**Post date:** [August 28, 2022, 2:55am UTC](https://forum.juce.com/t/oversampling-random-amplitude-spikes/52785/8 "2022-08-28T02:55:39Z")

</div>

Sorry - I don’t know what ISP is!

---

<div class="post-metadata">

**Author:** ![Fandusss](https://avatars.discourse-cdn.com/v4/letter/f/a4c791/32.png) [@Fandusss](https://forum.juce.com/u/Fandusss)\
**Post date:** [August 28, 2022, 3:33am UTC](https://forum.juce.com/t/oversampling-random-amplitude-spikes/52785/9 "2022-08-28T03:33:43Z")

</div>

Intersample peak

---

<div class="post-metadata">

**Author:** ![cubestar](https://avatars.discourse-cdn.com/v4/letter/c/43a26b/32.png) [@cubestar](https://forum.juce.com/u/cubestar)\
**Post date:** [August 28, 2022, 4:51pm UTC](https://forum.juce.com/t/oversampling-random-amplitude-spikes/52785/10 "2022-08-28T16:51:30Z")

</div>

Well these are values coming from the input so I don’t know how that would be happening?

---

<div class="post-metadata">

**Author:** ![cubestar](https://avatars.discourse-cdn.com/v4/letter/c/43a26b/32.png) [@cubestar](https://forum.juce.com/u/cubestar)\
**Post date:** [August 28, 2022, 5:04pm UTC](https://forum.juce.com/t/oversampling-random-amplitude-spikes/52785/11 "2022-08-28T17:04:29Z")

</div>

FYI - I did just try making separate L and R instances of the oversampler and the result is the same.

---

<div class="post-metadata">

**Author:** ![lcapozzi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/lcapozzi/32/10960_2.png) [@lcapozzi](https://forum.juce.com/u/lcapozzi)\
**Post date:** [August 28, 2022, 5:27pm UTC](https://forum.juce.com/t/oversampling-random-amplitude-spikes/52785/12 "2022-08-28T17:27:52Z")

</div>

That’s what I use on all of our products, without suffering these spikes.

In PluginProcessor.h, we have the oversampler as a private member:  
std::unique\_ptr\<dsp::Oversampling\> oversampling;

Then, PluginProcessor.cpp:

In the constructor:  
oversampling.reset (new dsp::Oversampling (2, log2(2), dsp::Oversampling::filterHalfBandPolyphaseIIR, false)); //change log2(2) with your oversampling factor

In prepareToPlay:  
oversampling-\>initProcessing(static\_cast\<size\_t\> (samplesPerBlock));

In process(dsp::ProcessContextReplacing context):

```auto
ScopedNoDenormals noDenormals;
    
    auto&& inBlock = context.getInputBlock();
    auto&& outBlock = context.getOutputBlock();
    
    jassert (inBlock.getNumChannels() == outBlock.getNumChannels());
    jassert (inBlock.getNumSamples() == outBlock.getNumSamples());
    
    auto numSamples = inBlock.getNumSamples();
    auto numChannels = inBlock.getNumChannels();
        
    // Upsampling
    dsp::AudioBlock<float> oversampledBlock;
    
    setLatencySamples (audioCurrentlyOversampled ? roundToInt (oversampling->getLatencyInSamples()) : 0);
    
    // Upsampling
    if (audioCurrentlyOversampled)
    {
        oversampledBlock = oversampling->processSamplesUp (context.getInputBlock());
    }
    
    auto pluginContext = audioCurrentlyOversampled ? dsp::ProcessContextReplacing<float> (oversampledBlock)
    : context;
    
    
    auto&& plugInBlock = pluginContext.getInputBlock();
    auto&& plugOutBlock = pluginContext.getOutputBlock();
    
    jassert (plugInBlock.getNumChannels() == plugOutBlock.getNumChannels());
    jassert (plugInBlock.getNumSamples() == plugOutBlock.getNumSamples());
    
    numSamples = plugInBlock.getNumSamples();
    numChannels = plugInBlock.getNumChannels();
    
    const bool isMono = numChannels == 1;
    const bool isStereo = numChannels > 1;
    
    //Plugin Processing
    for (size_t chan = 0; chan < numChannels; ++chan)
    {
        for (size_t i = 0; i < numSamples; ++i)
        {
            const float nrDry = plugInBlock.getChannelPointer(chan)[i];
            //my dsp process goes here
            plugOutBlock.getChannelPointer(chan)[i] = blabla; //my dsp process output goes in the out block here
        }
    }
    
    // Downsampling
    if (audioCurrentlyOversampled)
        oversampling->processSamplesDown (context.getOutputBlock());
        
}

```

Hope this helps.

Luca

---

<div class="post-metadata">

**Author:** ![cubestar](https://avatars.discourse-cdn.com/v4/letter/c/43a26b/32.png) [@cubestar](https://forum.juce.com/u/cubestar)\
**Post date:** [August 28, 2022, 5:48pm UTC](https://forum.juce.com/t/oversampling-random-amplitude-spikes/52785/13 "2022-08-28T17:48:27Z")

</div>

Thank you, I will try this 🙂

---

<div class="post-metadata">

**Author:** ![chkn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chkn/32/3862_2.png) [@chkn](https://forum.juce.com/u/chkn)\
**Post date:** [August 29, 2022, 6:46am UTC](https://forum.juce.com/t/oversampling-random-amplitude-spikes/52785/14 "2022-08-29T06:46:23Z")

</div>

Just out of the blue, the signal after oversampling, especially after using IIR filters (which also move the phase) might look ripply, but could be correct. In the values you provided in the first post I can’t see a spike (did not check the file which you have provided)

---

<div class="post-metadata">

**Author:** ![cubestar](https://avatars.discourse-cdn.com/v4/letter/c/43a26b/32.png) [@cubestar](https://forum.juce.com/u/cubestar)\
**Post date:** [August 29, 2022, 5:43pm UTC](https://forum.juce.com/t/oversampling-random-amplitude-spikes/52785/15 "2022-08-29T17:43:37Z")

</div>

The expected range is -1 to 1, with oversampling enabled I was getting single samples out of range like the -1.46199 in the example.

> [@cubestar](#):
>
> ```auto
> 0.00244384
> 0.00182456
> 0.0012077
> 0.000595403
> -1.46199e-05
> -0.000629048
> -0.00125011
> -0.00187474
> 
> ```

---

<div class="post-metadata">

**Author:** ![danielrudrich](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/danielrudrich/32/3735_2.png) [@danielrudrich](https://forum.juce.com/u/danielrudrich)\
**Post date:** [August 29, 2022, 5:55pm UTC](https://forum.juce.com/t/oversampling-random-amplitude-spikes/52785/16 "2022-08-29T17:55:59Z")

</div>

The e-05 means you have to multiply the value by 10^-5, the value actually is -0.0000146199

---

<div class="post-metadata">

**Author:** ![cubestar](https://avatars.discourse-cdn.com/v4/letter/c/43a26b/32.png) [@cubestar](https://forum.juce.com/u/cubestar)\
**Post date:** [August 29, 2022, 8:18pm UTC](https://forum.juce.com/t/oversampling-random-amplitude-spikes/52785/17 "2022-08-29T20:18:49Z")

</div>

Thank you so much for that - I totally missed that bit. That fixes it.

Thanks again!
