# Easiest way to implement a Z-1 unit delay function? (resolved)

**URL:** <https://forum.juce.com/t/easiest-way-to-implement-a-z-1-unit-delay-function-resolved/30168>\
**Category:** General JUCE discussion\
**Created:** [November 4, 2018, 6:25am UTC](https://forum.juce.com/t/easiest-way-to-implement-a-z-1-unit-delay-function-resolved/30168 "2018-11-04T06:25:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikej](https://avatars.discourse-cdn.com/v4/letter/m/a9a28c/32.png) [@mikej](https://forum.juce.com/u/mikej)\
**Post date:** [November 4, 2018, 6:25am UTC](https://forum.juce.com/t/easiest-way-to-implement-a-z-1-unit-delay-function-resolved/30168/1 "2018-11-04T06:25:39Z")

</div>

I am working on rebuilding some distortion units from Reaktor based on Infinite Linear Oversampling which is a technique to reduce aliasing. It involves utilizing the integrals of the distortion equations with unit delays. Here’s an example of a schematic:  
[https://www.native-instruments.com/forum/attachments/ilo-tanh-png.54931/](https://www.native-instruments.com/forum/attachments/ilo-tanh-png.54931/)

I’m wondering what the simplest way to write a function that delays output by one unit.

I read Jordan Harris’s posts (who designed the State Variable Filter I’m currently enjoying very much incidentally) [here](https://forum.juce.com/t/unit-delay-z/14694/5), but I’m not sure I follow his technique.

Here’s what I’m wondering if might be the same idea:

```
double output = nullptr;

inline double getUnitDelay(float& input) {
return output;
input = output;
}

```

So in principle, it takes an input, but it doesn’t return that input. It copies it to another variable called output, which I think needs to be initialized as nullptr so that there is something in it (even nullptr) for the first sample request. Not sure if this can be folded into the function.

Since C++ is order sensitive (I think), this function returns the output from the prior sample each time it is run.

Then for example, it can be used in equations like this:

`integral - getUnitDelay(integral) ... ;`

Would that work? Is there a better way to do it?

Thanks as always

---

<div class="post-metadata">

**Author:** ![ttg](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ttg/32/14456_2.png) [@ttg](https://forum.juce.com/u/ttg)\
**Post date:** [November 4, 2018, 11:01am UTC](https://forum.juce.com/t/easiest-way-to-implement-a-z-1-unit-delay-function-resolved/30168/2 "2018-11-04T11:01:50Z")

</div>

if this is the exact code notice there is huge control flow issue.  
notice `return output;` would basically mean your code flow ends there. input would never be updated by output.

if you’re coming from modular/dsp environments you should remember:

- serial/parallel logic should be explicit. sometimes requiring “boiler-plate” coding…
- do not expect things to be like circuits… a process callback will run circular for you but you’ll need to make sure all your variables and states are progressing and saved correctly.
- multi-input state requires multiple variables. for example z-1 state **MUST** have a variable for each input. (so if you have stereo input… that means 2 variables each one for each input).
- understand the concept of reference / pointer better (from your code it seems you’re still figuring it out).

If I get your intentions, it should look like this:

again, reminding that your `output` variable / this code would only be good for single channel.  
if you get multiple inputs… you’d need to have multiple variables (or in modernized coding maybe a class with instance per input).

```auto
double output = 0.0; // notice this IS NOT a pointer! it cannot reference nullptr

inline double getUnitDelay(float& input) {
    const double prevOut = output;
    input = output;
    return prevOut;
}

```

---

<div class="post-metadata">

**Author:** ![basteln](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@basteln](https://forum.juce.com/u/basteln)\
**Post date:** [November 4, 2018, 4:56pm UTC](https://forum.juce.com/t/easiest-way-to-implement-a-z-1-unit-delay-function-resolved/30168/3 "2018-11-04T16:56:44Z")

</div>

> [@ttg](#):
>
> ```auto
> double output = 0.0; // notice this IS NOT a pointer! it cannot reference nullptr 
> 
> inline double getUnitDelay(float& input) {
> const double prevOut = output;
> input = output;
> return prevOut;
> }
> 
> ```

With this implementation, the value of `output` never changes and always stays at `0.0`. The function always returns the same value that it assigns to `input`. I don’t think that’s what mikem intends to do.

I suggest not taking the `input` by reference, but rather just by value:

```auto
double output = 0.0;

inline double getUnitDelay(float input) {
    const double prevOut = output;
    output = input;
    return prevOut;
}

```

To execute something more complex after a return, [ScopeGuard](https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Scope_Guard) is a good pattern. Similar to JUCE’s `ScopedValueSetter `. But in this simple case, the local `prevOut` is better IMO.

---

<div class="post-metadata">

**Author:** ![ttg](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ttg/32/14456_2.png) [@ttg](https://forum.juce.com/u/ttg)\
**Post date:** [November 4, 2018, 5:19pm UTC](https://forum.juce.com/t/easiest-way-to-implement-a-z-1-unit-delay-function-resolved/30168/4 "2018-11-04T17:19:52Z")

</div>

> [@basteln](#):
>
> I don’t think that’s what mikem intends to do.

since we’re with snippets of code I also don’t know how other parts interact with this code. so I’ve tried to do minimal changes but I guess your suggestion might be more of what @mikem had in-mind.

---

<div class="post-metadata">

**Author:** ![mikej](https://avatars.discourse-cdn.com/v4/letter/m/a9a28c/32.png) [@mikej](https://forum.juce.com/u/mikej)\
**Post date:** [November 5, 2018, 4:16am UTC](https://forum.juce.com/t/easiest-way-to-implement-a-z-1-unit-delay-function-resolved/30168/7 "2018-11-05T04:16:00Z")

</div>

I was told on KVR it is pointless to try to do this as a function, and I should just hardcode what I want to do into the class using variables as storage. So for example, here’s what I’ve come up with to demonstrate the idea:

```auto
private:
double integralZMemory = 0;
double inputZMemory = 0;
double integralZDelayed;
double inputZDelayed;

```

```auto
double Overdrive::getOutput(double &input, "other parameters") {

double integral = ...;
integralZDelayed = integralZMemory;
integralZMemory = integral;

inputZDelayed = inputZMemory;
inputZMemory = input;

double output = integralZDelayed + inputZDelayed;
return output;

```

If I did that correctly, it would continuously output the integral delayed by 1 sample + the input delayed by 1 sample. I would never want that. But just for illustration purposes. Makes sense?

---

<div class="post-metadata">

**Author:** ![mikej](https://avatars.discourse-cdn.com/v4/letter/m/a9a28c/32.png) [@mikej](https://forum.juce.com/u/mikej)\
**Post date:** [November 5, 2018, 6:26am UTC](https://forum.juce.com/t/easiest-way-to-implement-a-z-1-unit-delay-function-resolved/30168/8 "2018-11-05T06:26:16Z")

</div>

Or even simpler …

```auto
private:
double integral_1 = 0;
double input_1 = 0;

```

```auto
double Overdrive::getOutput(double &input, "other parameters") {

double integral = ...;

double output = integral_1 + input_1;

integral_1 = integral;
input_1 = input;

return output;

```

I think that’s it. Problem solved. Now I just have to write the actual code. 🙂
