Support

If you have a problem or need to report a bug please email : support@dsprobotics.com

There are 3 sections to this support area:

DOWNLOADS: access to product manuals, support files and drivers

HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects

USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here

NEW REGISTRATIONS - please contact us if you wish to register on the forum

Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright

Linear Interpolation method for hop steps

DSP related issues, mathematics, processing and techniques

Linear Interpolation method for hop steps

Postby HughBanton » Thu Mar 10, 2022 9:45 pm

Here's something I devised sometime last year, it's probably not remotely original. I bumped ito it again the other day and had a "how on earth does this work / what on earth was I thinking??" moment! But I reckon it's quite neat so thought I'd pass it on, might be useful ...

It relates to interpolating DSP processess that have suffered from being hopped, for example envelope generators. Whenever you use 'hop' in DSP code it inevitably generates steps in your output shape - bigger the hop, bigger the steps. Sometimes this doesn't matter, but if it does you can use slewing or interpolating techniques, after the hopped setction, to smooth it out again.

Attached is a very low-cpu-cost method of producing a truly straight-line (linear) interpolation between the steps.

linear_interp_dem.fsm
(18.94 KiB) Downloaded 726 times

In the attached I've tagged on the more traditional slew-rate-limiter method as well, which you can switch to for comparison. The slew-rate-limiter method produces characteristic bumps between each step sample, whereas my linear method makes perfect straight lines.


To help you with unravelling it all ... The variable "diff" calculates the difference between consecutive steps (4096 samples between them in this case); divide this difference by the hop size (4096), and a perfect ramp is restored by simply accumulating the 4096 'diff' values, during the remaining 4095 samples between each hop. You can hopefully see that accumulating 4096 * (1/4096) = 1, thus generating an accurate linear ramp between between adjacent steps.

It's very efficient because all the calculations are done inside the 4096 hopped section, and the ramp restoration (back in real time) is achieved using a solitary 'add' : interpOut + diff.

Keeps me off the streets ... :?

H
User avatar
HughBanton
 
Posts: 265
Joined: Sat Apr 12, 2008 3:10 pm
Location: Evesham, Worcestershire

Re: Linear Interpolation method for hop steps

Postby Spogg » Fri Mar 11, 2022 9:08 am

Wow! :o

I too had that “How on earth does this work” moment when I saw the schematic in action. So I worked it out then re-read your explanation more carefully. This seems SO clever to me that you can achieve that linear interpolation with just one add at sample rate outside the hopped loop.

I also like the fact that when the ramp resets, the reset-to-zero time is determined by the hop size. This means that for a sawtooth LFO controlling audio parameters the reset could be made less sudden and therefore less prone to producing clicks.

You deserve a prize!
User avatar
Spogg
 
Posts: 3358
Joined: Thu Nov 20, 2014 4:24 pm
Location: Birmingham, England

Re: Linear Interpolation method for hop steps

Postby tulamide » Fri Mar 11, 2022 11:44 am

Careful! By comparing it through a scope, you fall to a trap.

The scope only display a few pixels, not nearly all incoming samples. Imagine it like a hop as well. In reality the line will either not look that linear, although better than the staircase, or the dsp module IS calculating each sample instead of only each 4096th.

Also, your code ignores discontinuities. The saw wave would start at -1, after having reached +1. So the two samples at that position would read +1, -1
Your code introduces a delay of 4096 samples, before -1 is reached after +1

Not sure, if these are important points, but they are for sure existing, and I don't think they should be ignored :)

scope.PNG
scope.PNG (8.22 KiB) Viewed 20154 times
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Linear Interpolation method for hop steps

Postby Tepeix » Fri Mar 11, 2022 12:22 pm

That's interesting !

But i saw a little problem, over time your maximum value raise and finish to be greater to 1.
I just added a min max value module to check it.

Well at hop 4096 it make no so much sense to optimize but divide take more cpu than multiply,
Maybe multiply with 0.000244141 ?
Attachments
linear_interp_dem Min max value.fsm
(35.42 KiB) Downloaded 689 times
Tepeix
 
Posts: 361
Joined: Sat Oct 16, 2021 3:11 pm

Re: Linear Interpolation method for hop steps

Postby HughBanton » Fri Mar 11, 2022 5:15 pm

Yes, some valid observations. I remember noticing the accumulating error when I first devised this ... that'll be rounding errors, that will :cry:

Only suitable really for use with poly stuff anyway, where such errors won't be of significance because you don't hold keys down for 10 minutes. (Omg you don't, do you ??) I made yesterdays demo in blue just because it's more forum-friendly that way (- no complication of MIDI keing or anything). Same kind of code.

Nor would I choose to use it with hop(4096) .. ditto! In practice I've used it with envelope code at (64) or (128), where any sample lag, compared with exponential methods, tends to be insignificant.

Btw one other disadvantage of slew-limiter maths is that it can lead to Denorm cpu log-jams ( .. see elsewhere), I can't see that happening with linear interpolation.

Anyways .. here's an Envelope-type version using the same principal. No accumulation error.
Hit <SPACE BAR> to key it ...
linear_interp_dem_3.fsm
(4.21 KiB) Downloaded 709 times

H
User avatar
HughBanton
 
Posts: 265
Joined: Sat Apr 12, 2008 3:10 pm
Location: Evesham, Worcestershire

Re: Linear Interpolation method for hop steps

Postby tulamide » Fri Mar 11, 2022 10:29 pm

HughBanton wrote:Only suitable really for use with poly stuff anyway, where such errors won't be of significance because you don't hold keys down for 10 minutes. (Omg you don't, do you ??) I made yesterdays demo in blue just because it's more forum-friendly that way (- no complication of MIDI keing or anything). Same kind of code.

Nor would I choose to use it with hop(4096) .. ditto! In practice I've used it with envelope code at (64) or (128), where any sample lag, compared with exponential methods, tends to be insignificant.

I thought so, too. That's why I said that I'm not sure how important those points really are. But, all who know me understand, that I couldn't NOT point to it :lol:
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Linear Interpolation method for hop steps

Postby HughBanton » Sat Mar 12, 2022 11:08 am

Had a light-bulb moment this morning, while I was sitting on the :idea: .. stairs.

The drift caused by rounding can be completely eliminated quite simply :
The two outputs, stepOut & interpOut, should always be == immediately before each new level is calculated; therefore it follows that we can write interpOut = count inside the hopped loop and that will clamp the two together. Negligible extra load.

Not needed for envelope processes of course, which will be regularly zero'd automatically.

Here's a revised version of the original with the ramp. This one can be run continuously in blue after all, should you need something similar.
linear_interp_dem_with_clamp.fsm
(19.03 KiB) Downloaded 718 times

H
User avatar
HughBanton
 
Posts: 265
Joined: Sat Apr 12, 2008 3:10 pm
Location: Evesham, Worcestershire

Re: Linear Interpolation method for hop steps

Postby Spogg » Mon Mar 14, 2022 9:50 am

HughBanton wrote: This one can be run continuously in blue after all, should you need something similar.


I confess I didn’t notice the drift when I first laid eyes on this. :oops:
However your fix is perfect, simple and uses virtually no more CPU.

Bravo!!

Edit: I incorporated the point that Tepeix made so the multiplication factor is calculated just once at stage(0) instead of 10 times per second at 44100. As per Tesco… every little helps. :lol:
Attachments
linear_interp_dem_with_clamp div cpu reduced.fsm
3.06
(59.01 KiB) Downloaded 678 times
User avatar
Spogg
 
Posts: 3358
Joined: Thu Nov 20, 2014 4:24 pm
Location: Birmingham, England

Re: Linear Interpolation method for hop steps

Postby adamszabo » Mon Mar 14, 2022 4:51 pm

Wouldnt it be just more simple to write:

float div = 0.000244140625; // 1/1496

and remove the whole division fiasco all together? :D
adamszabo
 
Posts: 667
Joined: Sun Jul 11, 2010 7:21 am

Re: Linear Interpolation method for hop steps

Postby Spogg » Tue Mar 15, 2022 8:58 am

adamszabo wrote:Wouldnt it be just more simple to write:

float div = 0.000244140625; // 1/1496

and remove the whole division fiasco all together? :D


You clearly like typing in huge numbers Adam! :lol:

My thought was that if you change the hop number, the multiplier would have to be re-calculated and entered.
And what about the precision? If you make the division in DSP it takes one sample period just once and handles any rounding internally.
User avatar
Spogg
 
Posts: 3358
Joined: Thu Nov 20, 2014 4:24 pm
Location: Birmingham, England

Next

Return to DSP

Who is online

Users browsing this forum: No registered users and 60 guests