# How to do DC Offset using LUA?

hi, how do i recreate the DC Offset feature using LUA, please?

Especially this:

it seems it’s destructive and can result in really snappy sinewave drums etc. i just need to know how to replicate this. is there a way of opening up what DC Offset does, somehow, for api functions? @taktik

Just calculate offset for all sample values.

``````local offset = 0
for i = 1, #SAMPLE do
offset = offset + SAMPLE[i]
end
offset = offset / #SAMPLE
``````

in the next step, you have to subtract this ‘offset’ value from each value in the sample.

try it out… that’s not what the “adjust dcoffset” does.

here’s a gif of running the script as per your in structions, then clicking on dc offset adjust button. notice how it keeps fluctuating / undulating somehow. something is introduced here on every run.

Then one more way, but I haven’t tried it, so no guarantee
apply this algorithm when traversing the sample data.

``````local newData = {0}
for i = 2, #SAMPLE do
newData[#newData + 1] = SAMPLE[i] - SAMPLE[i -1] + R * newData[i - 1]
end
return newData
``````

where R is the lowest frequency in the sample.
It is calculated like this.

``````R = 1 - (pi * 2 * lowest_frequency / samplerate)
``````

if you don’t know the frequency.
use this
(-3dB @ 40Hz): R = 1-(250 / samplerate)
(-3dB @ 30Hz): R = 1-(190 / samplerate)
(-3dB @ 20Hz): R = 1-(126 / samplerate)

I’ll be surprised what it will do

1 Like

@martblek this is exactly what was looked for. it works and it has that waviness. so now i can rotate a waveform updates, apply massive amounts of dcoffset to cause it to crunch in a really digital clippy way.
thanks much!