Skip to content

Commit

Permalink
add chebychef filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Uwe Fechner committed Nov 19, 2024
1 parent 8bca4b8 commit cf9fa0f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
13 changes: 12 additions & 1 deletion examples/test_lowpass.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ dt = 0.05
sim_time = 4.0
N = Int(sim_time / dt)

# Design the filter
butter = create_filter(cut_off_freq; order=4, dt)
cheby1 = create_filter(cut_off_freq; order=4, type=:Cheby1, dt)

# Create an array of measurements
measurements = zeros(N)
for i in Int(N/2):N
Expand All @@ -31,6 +35,13 @@ for i in 1:N
results_butter[i] = apply_filter(butter, measurements[i], buffer, i)
end

# Apply the Cheby1 filter
buffer = zeros(N)
results_cheby1 = zeros(N)
for i in 1:N
results_cheby1[i] = apply_filter(cheby1, measurements[i], buffer, i)
end

# Plot the results
plot((1:N)*dt, [measurements, results, results_butter]; xlabel="Time (s)", ylabel="Amplitude",
plot((1:N)*dt, [measurements, results, results_butter, results_cheby1]; xlabel="Time (s)", ylabel="Amplitude",
fig="Exponential Moving Average and Butterworth Filters")
8 changes: 6 additions & 2 deletions src/DiscreteFilters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ Design a Butterworth filter with the given cut-off frequency.
- `order`: The order of the filter, optionally, default: 4
- `dt`: The sampling time in seconds
"""
function create_filter(cut_off_freq; order=4, dt)
Filters.digitalfilter(Filters.Lowpass(cut_off_freq; fs=1/dt), Filters.Butterworth(order))
function create_filter(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

"""
Expand Down

0 comments on commit cf9fa0f

Please sign in to comment.