Skip to content

Commit cb13b8b

Browse files
committed
add DSP example
1 parent 376b6be commit cb13b8b

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

docs/src/tutorials/noise.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,48 @@ plot(figy, figu, plot_title = "DC Motor with Discrete-time Speed Controller")
8888
You may, e.g.
8989
- Use [`ExponentialFilter`](@ref) to add exponential filtering using `y(k) ~ (1-a)y(k-1) + a*u(k)`, where `a` is the filter coefficient and `u` is the signal to be filtered.
9090
- Add moving average filtering using `y(k) ~ 1/N sum(i->u(k-i), i=0:N-1)`, where `N` is the number of samples to average over.
91+
- Construct a filter transfer function using [`DiscreteTransferFunction`](@ref). If ControlSystemsBase is loaded, `ControlSystemsBase.TransferFunction` objects can be used to construct a [`DiscreteTransferFunction`](@ref). For advanced filter design, see [DSP.jl: Filter design](https://docs.juliadsp.org/stable/filters/#Filter-design), example below.
92+
93+
### Filter design using DSP.jl
94+
Here, we design a fourth-order Butterworth low-pass filter with a cutoff frequency of 100 Hz and apply it to a noisy signal. The filter is designed using DSP.jl and converted to a transfer function from ControlSystemsBase.jl. The transfer function can be passed to the [`DiscreteTransferFunction`](@ref) block. We also design an [`ExponentialFilter`](@ref) for comparison.
95+
```@example NOISE
96+
using ControlSystemsBase: tf
97+
using ControlSystemsBase.DSP: digitalfilter, Lowpass, Butterworth
98+
fs = 1000
99+
cutoff = 100
100+
z = ShiftIndex(Clock(1/fs))
101+
F_dsp = digitalfilter(Lowpass(cutoff; fs), Butterworth(4))
102+
F_tf = tf(F_dsp)
103+
@mtkmodel FilteredNoise begin
104+
@components begin
105+
sine = Sine(amplitude=1, frequency=10)
106+
noise = NormalNoise(sigma = 0.1, additive = true)
107+
filter1 = ExponentialFilter(a = 0.2)
108+
filter2 = DiscreteTransferFunction(F_tf; z)
109+
end
110+
@variables begin
111+
x(t) = 0 # Dummy variable to work around a bug for models without continuous-time state
112+
end
113+
@equations begin
114+
connect(sine.output, noise.input)
115+
connect(noise.output, filter1.input, filter2.input)
116+
D(x) ~ 0 # Dummy equation
117+
end
118+
end
119+
@named m = FilteredNoise()
120+
m = complete(m)
121+
ssys = structural_simplify(IRSystem(m))
122+
prob = ODEProblem(ssys, [
123+
m.filter1.y(z-1) => 0;
124+
[m.filter2.y(z-k) => 0 for k = 1:4];
125+
[m.filter2.u(z-k) => 0 for k = 1:4];
126+
m.noise.y(z-1) => 0;
127+
], (0.0, 0.1))
128+
sol = solve(prob, Tsit5())
129+
plot(sol, idxs=m.noise.y, label="Noisy signal", c=1)
130+
plot!(sol, idxs=m.filter1.y, label="Exponential filtered signal", c=2)
131+
plot!(sol, idxs=m.filter2.y, label="Butterworth filtered signal", c=3)
132+
```
91133

92134
## Colored noise
93135
Colored noise can be achieved by filtering white noise through a filter with the desired spectrum. No components are available for this yet.

0 commit comments

Comments
 (0)