From 527f09653fc6be8d7eec16aa9be027533f769f33 Mon Sep 17 00:00:00 2001 From: Uwe Fechner Date: Wed, 4 Dec 2024 18:13:07 +0100 Subject: [PATCH] cleanup --- examples/test_butter.jl | 6 ++--- examples/test_butter2.jl | 50 ---------------------------------------- src/DiscreteFilters.jl | 17 ++++++++++++++ 3 files changed, 20 insertions(+), 53 deletions(-) delete mode 100644 examples/test_butter2.jl diff --git a/examples/test_butter.jl b/examples/test_butter.jl index 393149b..61e13d0 100644 --- a/examples/test_butter.jl +++ b/examples/test_butter.jl @@ -19,12 +19,12 @@ for i in Int(N/2):N end # apply the filter -buffer = zeros(N) results = zeros(N) +tfilter = DF2TFilter(butter) for i in 1:N - results[i] = apply_filter(butter, measurements[i], buffer, i) + results[i] = apply_filter(tfilter, measurements[i], i) end -@time apply_filter(butter, measurements[N], buffer, N) +@time apply_filter(tfilter, measurements[N], N) # Plot the step response p = plot((1:N)*dt, [measurements, results]; xlabel="Time (s)", ylabel="Amplitude", diff --git a/examples/test_butter2.jl b/examples/test_butter2.jl deleted file mode 100644 index 5e1ebcf..0000000 --- a/examples/test_butter2.jl +++ /dev/null @@ -1,50 +0,0 @@ -using DiscreteFilters, ControlPlots, DSP, ControlSystemsBase, Printf, LaTeXStrings -include("plotting.jl") - -function create_filter2(cut_off_freq; order=4, type=:Butter, dt) - if type == :Butter - return (Filters.digitalfilter(Filters.Lowpass(cut_off_freq; fs=1/dt), Filters.Butterworth(order))) - elseif type == :Cheby1 - return (Filters.digitalfilter(Filters.Lowpass(cut_off_freq; fs=1/dt), Filters.Chebyshev1(order, 0.01))) - end -end -function apply_filter2(butterF, measurement, index) - results = zeros(1) - measurements = ones(1) * measurement - @views filt!(results[1:1], butterF, measurements) - return results[1] -end - -# Define the cut-off frequency in Hz -cut_off_freq = 2.0 - -# Define the sampling and simulation time in seconds -dt = 0.05 -sim_time = 4.0 -N = Int(sim_time / dt) - -# Design the filter -butter = create_filter2(cut_off_freq; order=4, dt) - -# Create an array of measurements (step signal) -measurements = zeros(N) -for i in Int(N/2):N - measurements[i] = 1.0 -end - -# apply the filter -results = zeros(N) -tfilter = DSP.Filters.DF2TFilter(butter) -for i in 1:N - results[i] = apply_filter2(tfilter, measurements[i], i) -end -@time apply_filter2(tfilter, measurements[N], N) - -# Plot the step response -p = plot((1:N)*dt, [measurements, results]; xlabel="Time (s)", ylabel="Amplitude", - labels=["Input" "Output"], fig="Forth order Butterworth Filter") -display(p) - -# Plot the frequency response -bo = tf(butter, dt) -bode_plot(bo; hz=true, from=0.5, to=1.5, title="4th order Butterworth Filter") \ No newline at end of file diff --git a/src/DiscreteFilters.jl b/src/DiscreteFilters.jl index 8be82aa..27459bb 100644 --- a/src/DiscreteFilters.jl +++ b/src/DiscreteFilters.jl @@ -65,6 +65,23 @@ function apply_filter(butter, measurement, buffer, index) res = filt(butter, buffer[1:index]) return res[index] end +""" + apply_filter(tfilter::DF2TFilter, measurement, index) + +Apply the filter to the measurement. + +# Arguments +- `tfilter`: The filter, created with `create_filter` and converted to a DF2TFilter +- `measurement`: The measurement value +- `buffer`: The buffer to store the measurements +- `index`: The index of the measurement +""" +function apply_filter(tfilter::DF2TFilter, measurement, index) + results = zeros(1) + measurements = ones(1) * measurement + @views filt!(results[1:1], tfilter, measurements) + return results[1] +end function apply_delay(measurement, buffer, index; delay=1) buffer[index] = measurement