Skip to content

Commit

Permalink
Use Polynomials 1.0 API (JuliaDSP#361)
Browse files Browse the repository at this point in the history
* Use Polynomials 1.0 API

Instead of using the compatibility interface provided by Polynomials.jl for
backwards compatibility, I have changed calls to Polynomials.jl from DSP.jl so
that they use the new API.

Related to JuliaDSP#359.

* Remove redundant `using` statement for Polynomials.jl
  • Loading branch information
galenlynch authored May 17, 2020
1 parent f5e871f commit f53fe27
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/Filters/Filters.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Filters
using ..Unwrap
using Polynomials, ..Util
using Polynomials.PolyCompat
using ..Util
using Polynomials: Polynomial, coeffs, roots, fromroots

import Base: *
using LinearAlgebra: I, mul!, rmul!
Expand Down
24 changes: 12 additions & 12 deletions src/Filters/coefficients.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ Base.promote_rule(::Type{ZeroPoleGain{Z1,P1,K1}}, ::Type{ZeroPoleGain{Z2,P2,K2}}
#

struct PolynomialRatio{T<:Number} <: FilterCoefficients
b::Poly{T}
a::Poly{T}
b::Polynomial{T}
a::Polynomial{T}

PolynomialRatio{Ti}(b::Poly, a::Poly) where {Ti<:Number} =
new{Ti}(convert(Poly{Ti}, b/a[end]), convert(Poly{Ti}, a/a[end]))
PolynomialRatio{Ti}(b::Polynomial, a::Polynomial) where {Ti<:Number} =
new{Ti}(convert(Polynomial{Ti}, b/a[end]), convert(Polynomial{Ti}, a/a[end]))
end
"""
PolynomialRatio(b, a)
Expand All @@ -63,16 +63,16 @@ H(z) = \\frac{\\verb!b[1]! + \\ldots + \\verb!b[n]! z^{-n+1}}{\\verb!a[1]! + \\l
`b` and `a` may be specified as `Polynomial` objects or
vectors ordered from highest power to lowest.
"""
PolynomialRatio(b::Poly{T}, a::Poly{T}) where {T<:Number} = PolynomialRatio{T}(b, a)
PolynomialRatio(b::Poly, a::Poly) = PolynomialRatio(promote(b, a)...)
PolynomialRatio(b::Polynomial{T}, a::Polynomial{T}) where {T<:Number} = PolynomialRatio{T}(b, a)
PolynomialRatio(b::Polynomial, a::Polynomial) = PolynomialRatio(promote(b, a)...)

# The DSP convention is highest power first. The Polynomials.jl
# convention is lowest power first.
function PolynomialRatio{T}(b::Union{Number,Vector{<:Number}}, a::Union{Number,Vector{<:Number}}) where {T}
if all(iszero, b) || all(iszero, a)
throw(ArgumentError("filter must have non-zero numerator and denominator"))
end
PolynomialRatio{T}(Poly(reverse(b)), Poly(reverse(a)))
PolynomialRatio{T}(Polynomial(reverse(b)), Polynomial(reverse(a)))
end
PolynomialRatio(b::Union{T,Vector{T}}, a::Union{S,Vector{S}}) where {T<:Number,S<:Number} =
PolynomialRatio{promote_type(T,S)}(b, a)
Expand All @@ -83,9 +83,9 @@ PolynomialRatio(f::PolynomialRatio{T}) where {T} = PolynomialRatio{T}(f)
Base.promote_rule(::Type{PolynomialRatio{T}}, ::Type{PolynomialRatio{S}}) where {T,S} = PolynomialRatio{promote_type(T,S)}

function PolynomialRatio{T}(f::ZeroPoleGain) where T<:Real
b = f.k*poly(f.z)
a = poly(f.p)
PolynomialRatio{T}(Poly(real(b.a)), Poly(real(a.a)))
b = f.k * fromroots(f.z)
a = fromroots(f.p)
PolynomialRatio{T}(Polynomial(real(coeffs(b))), Polynomial(real(coeffs(a))))
end
PolynomialRatio(f::ZeroPoleGain{Z,P,K}) where {Z,P,K} =
PolynomialRatio{promote_type(real(Z),real(P),K)}(f)
Expand All @@ -108,7 +108,7 @@ ZeroPoleGain(f::PolynomialRatio{T}) where {T} =
Coefficients of the numerator of a PolynomialRatio object, highest power
first, i.e., the `b` passed to `filt()`
"""
coefb(f::PolynomialRatio) = reverse(f.b.a)
coefb(f::PolynomialRatio) = reverse(coeffs(f.b))
coefb(f::FilterCoefficients) = coefb(PolynomialRatio(f))

"""
Expand All @@ -117,7 +117,7 @@ coefb(f::FilterCoefficients) = coefb(PolynomialRatio(f))
Coefficients of the denominator of a PolynomialRatio object, highest power
first, i.e., the `a` passed to `filt()`
"""
coefa(f::PolynomialRatio) = reverse(f.a.a)
coefa(f::PolynomialRatio) = reverse(coeffs(f.a))
coefa(f::FilterCoefficients) = coefa(PolynomialRatio(f))

#
Expand Down
4 changes: 2 additions & 2 deletions src/Filters/filt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ function filt!(out::AbstractVector, f::DF2TFilter{PolynomialRatio{T},Vector{S}},
si = f.state
# Note: these are in the Polynomials.jl convention, which is
# reversed WRT the usual DSP convention
b = f.coef.b.a
a = f.coef.a.a
b = coeffs(f.coef.b)
a = coeffs(f.coef.a)
n = length(b)
if n == 1
mul!(out, x, b[1])
Expand Down
4 changes: 2 additions & 2 deletions src/Filters/response.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
function freqz(filter::FilterCoefficients, w::Number)
filter = convert(PolynomialRatio, filter)
ejw = exp(-im * w)
polyval(filter.b, ejw) ./ polyval(filter.a, ejw)
filter.b(ejw) ./ filter.a(ejw)
end

function freqz(filter::ZeroPoleGain, w::Number)
Expand Down Expand Up @@ -88,7 +88,7 @@ or frequencies `w` in radians/sample.
function freqs(filter::FilterCoefficients, w::Number)
filter = convert(PolynomialRatio, filter)
s = im * w
polyval(filter.b, s) ./ polyval(filter.a, s)
filter.b(s) ./ filter.a(s)
end

function freqs(filter::ZeroPoleGain, w::Number)
Expand Down
8 changes: 6 additions & 2 deletions test/filter_conversion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,13 @@ end
@test Biquad(2.0, 0.0, 0.0, 0.0, 0.0)*2 == Biquad(4.0, 0.0, 0.0, 0.0, 0.0)
@test convert(Biquad{Float64}, f1) == convert(Biquad, f1)
f = PolynomialRatio(Float64[1.0], Float64[1.0])
empty!(f.b.a)
empty!(f.a.a)

# I don't understand why this tests is necessary, it's impossible to make a
# PolynomialRatio with empty coefficients in the first place.
empty!(f.b.coeffs)
empty!(f.a.coeffs)
@test_throws ArgumentError convert(Biquad, f)

@test_throws ArgumentError convert(SecondOrderSections, ZeroPoleGain([0.5 + 0.5im, 0.5 + 0.5im], [0.5 + 0.5im, 0.5 - 0.5im], 1))
@test_throws ArgumentError convert(SecondOrderSections, ZeroPoleGain([0.5 + 0.5im, 0.5 - 0.5im], [0.5 + 0.5im, 0.5 + 0.5im], 1))

Expand Down

0 comments on commit f53fe27

Please sign in to comment.