Skip to content

Instantly share code, notes, and snippets.

@standarddeviant
Last active May 24, 2018 22:46
Show Gist options
  • Save standarddeviant/f5cc4ff014176c030f9b46cd2335e1b4 to your computer and use it in GitHub Desktop.
Save standarddeviant/f5cc4ff014176c030f9b46cd2335e1b4 to your computer and use it in GitHub Desktop.
Plotting function for FilterCoefficient types from DSP.jl
import Plots
function Plots.plot(fc::FilterCoefficients, w=linspace(0, pi, 500); fs=2,
domag=true, dophase=true,
doimp=false, impn=500,
dostep=false, stepn=500, size=(800, 600))
# calculate plottable vectors
if domag || dophase
h = freqz(fc, w)
end
if doimp
ir = impz(fc, impn)
end
if dostep
sr = stepz(fc, stepn)
end
# make vector of plots
theplots = Plots.Plot[]
hz = w ./ pi .* (fs/2)
if domag
mag = 20*log10.(abs.(h))
ylims = [maximum(mag)-70, maximum(mag)+10]
push!(theplots, plot(hz, mag, lw=2, c=:blue, ylims=ylims, ylabel="dB (mag)"))
end
if dophase
phase = unwrap(angle.(h))
push!(theplots, plot(hz, phase, lw=2, c=:red, ylabel="rad (phase)"))
end
if doimp
push!(theplots, plot((0:impn-1)./fs, ir, lw=2, c=:green, ylabel="imp resp"))
end
if dostep
push!(theplots, plot((0:stepn-1)./fs, sr, lw=2, c=:purple, ylabel="step resp"))
end
# add a title
plot!(theplots[1], title="filter response")
# add xlabel to the last plot for space saving
plot!(theplots[end], xlabel="Hz (freq-plots) or Seconds (time-plots)")
plot(theplots..., layout=(sum([domag, dophase, doimp, dostep]), 1), legend=false, size=size)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment