Skip to content

Commit 70ff6ab

Browse files
Merge pull request #77 from baggepinnen/highpass
allow `FirstOrder` to operate as highpass instead of lowpass
2 parents 24a442f + 1751cfd commit 70ff6ab

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/Blocks/continuous.jl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,22 @@ function Derivative(; name, k=1, T, x_start=0.0)
6161
end
6262

6363
"""
64-
FirstOrder(; name, k=1, T, x_start=0.0)
64+
FirstOrder(; name, k=1, T, x_start=0.0, lowpass=true)
6565
66-
A first-order filter with a single real pole in `s = -T` and gain `k`. The transfer function
66+
A first-order filter with a single real pole in `s = -T` and gain `k`. If `lowpass=true` (default), the transfer function
6767
is given by `Y(s)/U(s) = `
6868
```
6969
k
7070
───────
7171
sT + 1
7272
```
73+
and if `lowpass=false`, by
74+
```
75+
sT + 1 - k
76+
──────────
77+
sT + 1
78+
```
79+
7380
7481
# Parameters:
7582
- `k`: Gain
@@ -82,15 +89,15 @@ sT + 1
8289
8390
See also [`SecondOrder`](@ref)
8491
"""
85-
function FirstOrder(; name, k=1, T, x_start=0.0)
92+
function FirstOrder(; name, k=1, T, x_start=0.0, lowpass=true)
8693
T > 0 || throw(ArgumentError("Time constant `T` has to be strictly positive"))
8794
@named siso = SISO()
8895
@unpack u, y = siso
8996
sts = @variables x(t)=x_start
9097
pars = @parameters T=T k=k
9198
eqs = [
9299
D(x) ~ (k*u - x) / T
93-
y ~ x
100+
lowpass ? y ~ x : y ~ k*u - x
94101
]
95102
extend(ODESystem(eqs, t, sts, pars; name=name), siso)
96103
end

test/Blocks/continuous.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ end
4343
@testset "PT1" begin
4444
pt1_func(t, k, T) = k * (1 - exp(-t / T)) # Known solution to first-order system
4545

46-
k, T = 1.0, 0.1
46+
k, T = 1.2, 0.1
4747
@named c = Constant(; k=1)
4848
@named pt1 = FirstOrder(; k=k, T=T)
4949
@named iosys = ODESystem(connect(c.output, pt1.input), t, systems=[pt1, c])
@@ -52,6 +52,15 @@ end
5252
sol = solve(prob, Rodas4())
5353
@test sol.retcode == :Success
5454
@test sol[pt1.output.u] pt1_func.(sol.t, k, T) atol=1e-3
55+
56+
# Test highpass feature
57+
@named pt1 = FirstOrder(; k=k, T=T, lowpass=false)
58+
@named iosys = ODESystem(connect(c.output, pt1.input), t, systems=[pt1, c])
59+
sys = structural_simplify(iosys)
60+
prob = ODEProblem(sys, Pair[], (0.0, 100.0))
61+
sol = solve(prob, Rodas4())
62+
@test sol.retcode == :Success
63+
@test sol[pt1.output.u] k .- pt1_func.(sol.t, k, T) atol=1e-3
5564
end
5665

5766
@testset "PT2" begin

0 commit comments

Comments
 (0)