# How to implement a filtered derivative

**URL:** <https://discourse.julialang.org/t/how-to-implement-a-filtered-derivative/109441>\
**Category:** Signal and Image Processing\
**Tags:** question\
**Created:** [January 30, 2024, 10:49am UTC](https://discourse.julialang.org/t/how-to-implement-a-filtered-derivative/109441 "2024-01-30T10:49:38Z")\
**Posts on this page:** 10\
**Page:** 2

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 31, 2024, 7:28pm UTC](https://discourse.julialang.org/t/how-to-implement-a-filtered-derivative/109441/21 "2024-01-31T19:28:33Z")

</div>

> [@A\_C](#):
>
> So I dont think you can get an unique implementation.

That’s true if you specify it as a differential equation as in the original post, but if it is specified as a rational transfer function H(z) as in the [later post](https://discourse.julialang.org/t/how-to-implement-a-filtered-derivative/109441/13) then the discrete-time function is specified uniquely. It depends on what @ufechner7 is comparing to.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [January 31, 2024, 7:35pm UTC](https://discourse.julialang.org/t/how-to-implement-a-filtered-derivative/109441/22 "2024-01-31T19:35:32Z")

</div>

The point is, that the Simulink component I try to port to Julia is a hybrid that is working both in continues and discrete time… Which is a pain, you cannot easily know what it is really doing, and the documentation mixes both implementations…  
Sorry for posting a continues time problem in the first place, this is not what I am looking for…

---

<div class="post-metadata">

**Author:** ![A\_C](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@A\_C](https://discourse.julialang.org/u/A_C)\
**Post date:** [February 1, 2024, 3:33am UTC](https://discourse.julialang.org/t/how-to-implement-a-filtered-derivative/109441/23 "2024-02-01T03:33:05Z")

</div>

Yes the discrete time version posted later will be unique. I was comparing to the continuous time one.

---

<div class="post-metadata">

**Author:** ![A\_C](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@A\_C](https://discourse.julialang.org/u/A_C)\
**Post date:** [February 1, 2024, 3:41am UTC](https://discourse.julialang.org/t/how-to-implement-a-filtered-derivative/109441/24 "2024-02-01T03:41:02Z")

</div>

If you have access to MATLAB you can select the block and press `Ctrl+U` to look under the mask. Most probably it would be using [Variant](https://in.mathworks.com/help/simulink/slref/variantsubsystemvariantmodelvariantassemblysubsystem.html) subsystem somewhere and in that will implement the continuous and discrete versions.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [February 1, 2024, 9:55am UTC](https://discourse.julialang.org/t/how-to-implement-a-filtered-derivative/109441/25 "2024-02-01T09:55:08Z")

</div>

Just for completeness my current, verified implementation:

```julia
using PyPlot

# this implements the function (1/T)(z - 1)/(z + Ts/T -1)
function filtered_derivative(Ω; Ts=0.01, T=0.1)
    dΩ = zeros(length(Ω))
    a = [Ts/T - 1]
    b = [1, -1] / T
    for n in eachindex(Ω)
        if n > 1
            dΩ[n] = b[1] * Ω[n] + b[2] * Ω[n-1] - a[1] * dΩ[n-1]
        end
    end
    dΩ
end

N = 200
Ts = 0.01
Ω = zeros(N)
for i in 101:200
    Ω[i] = 1
end
dΩ = filtered_derivative(Ω; Ts)
P = zeros(N)
PyPlot.step((1:N)*Ts, dΩ)
grid(true)

```

Thanks a lot @A_C and @stevengj for all your input!

---

<div class="post-metadata">

**Author:** ![artkuo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/artkuo/32/28577_2.png) [@artkuo](https://discourse.julialang.org/u/artkuo)\
**Post date:** [February 2, 2024, 1:00am UTC](https://discourse.julialang.org/t/how-to-implement-a-filtered-derivative/109441/26 "2024-02-02T01:00:14Z")

</div>

Looks like you’ve got it working, but here’s some context. First, the original continuous-time equations @ufechner7 provided are not a filtered derivative. They are equivalent to transfer function  
 \frac{K\_d}{T\_d} \frac{1}{(T\_d s+1)}  
which is a first-order low-pass filter. Almost certainly you want  
 \frac{K\_d s}{(T\_d s+1)}   
which is what your reference shows in block diagram (a), but their implementation (b) forgets the s in numerator, which means derivative. (T\_d s+1) in denominator is a low-pass filter, so you need both to get a filtered derivative. The frequency response asymptotes to K\_d/T\_d at high frequencies, which you can understand by taking s \to\infty. Here s is the Laplace transform operator, or kind of a derivative operator, or a complex frequency for a one-sided Fourier transform.

How did they get the discrete-time implementation? Any sampling of a continuous-time system will be an approximation, and it’s a matter of what you prioritize. Here they discretize a derivative as backward rectangular rule. So each s \gets (1 - z^{-1})/T\_s, i.e. current sample minus previous sample and divide by sample period. Then multiply by whatever you need get the high-frequency gain above.

All this is from discrete-time control systems. @stevengj was able to figure it out because it’s math, but you can look up some textbooks if you want background for designing filters. (Personally I think this IIR implementation is standard and am skeptical there’s much to gain from FIR.)

BTW @ufechner7 I believe your implementation is still missing something, because your reference also shows a signal limiter, so you won’t truly reproduce their system until you put it in.

---

<div class="post-metadata">

**Author:** ![A\_C](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@A\_C](https://discourse.julialang.org/u/A_C)\
**Post date:** [February 2, 2024, 5:43pm UTC](https://discourse.julialang.org/t/how-to-implement-a-filtered-derivative/109441/27 "2024-02-02T17:43:03Z")

</div>

The implementation in block diagram (b) seems correct to me. The transfer function can be written as:  
y(s)=\frac{K\_{d}s}{1+sT\_{d}}u(s)=\left(\frac{K\_{d}}{T\_{d}}\right)\left(\frac{1}{1+\frac{1}{sT\_{d}}}\right)u(s),  
which is what the block diagram (b) implements. The code that I have posted above actually implements the backward euler discretization of the block diagram along with saturation.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [February 2, 2024, 6:39pm UTC](https://discourse.julialang.org/t/how-to-implement-a-filtered-derivative/109441/28 "2024-02-02T18:39:11Z")

</div>

> [@artkuo](#):
>
> because your reference also shows a signal limiter, so you won’t truly reproduce their system until you put it in.

Thanks for your comments!

I am only porting a model to Julia that does not make use of the limiters, so I don’t need them. Another question is if this filter should be part of a package, DSP.jl or another package… I considered to create a Filters.jl package, but for now the code is so simple that it is not really worth creating a package for it…

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [February 2, 2024, 6:56pm UTC](https://discourse.julialang.org/t/how-to-implement-a-filtered-derivative/109441/29 "2024-02-02T18:56:35Z")

</div>

> [@artkuo](#):
>
> but you can look up some textbooks if you want background for designing filters.

I learned this all at university, but that is long time ago… From an engineering point of view it would be nice to have a set of often used filters available in a package that you can use without understanding the math…

---

<div class="post-metadata">

**Author:** ![artkuo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/artkuo/32/28577_2.png) [@artkuo](https://discourse.julialang.org/u/artkuo)\
**Post date:** [February 3, 2024, 10:51pm UTC](https://discourse.julialang.org/t/how-to-implement-a-filtered-derivative/109441/30 "2024-02-03T22:51:18Z")

</div>

> [@ufechner7](#):
>
> if this filter should be part of a package, [DSP.jl](https://juliahub.com/ui/Packages/DSP) or another package

Might be nice to have some convenience filters within DSP.jl, as non-exported but public functions. It’s already pretty close, because you can do `digitalfilter(Highpass(1/T, 1/Ts), Butterworth(1))`. The “filtered derivative” is not exactly a Butterworth, which I believe is usually digitized with bilinear (trapezoidal) transform rather than backward difference.

But all first-order filters are pretty similar, perhaps differing slightly in cut-off and pass-band gain. The filter could even be designed in analog but implemented digitally with `analogfilter`. (I’ve not verified any of the above.)

[Previous page](https://discourse.julialang.org/t/how-to-implement-a-filtered-derivative/109441.md?page=1)
