Created
July 26, 2015 17:53
-
-
Save r9y9/31121dc6487c762cada0 to your computer and use it in GitHub Desktop.
Re-design MelGeneralizedCepstrums
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# NOTE: requires julia v0.4 | |
import Base: eltype, size, length, getindex | |
import SPTK | |
### Generic interface ### | |
abstract SpectralEnvelopeDef | |
# subtypes of SpectralEnvelopeDef should have field `order` | |
get_order(s::SpectralEnvelopeDef) = s.order | |
function estimate(s::SpectralEnvelopeDef, x::AbstractArray) | |
error("should provide an estimator") | |
end | |
### Common types that characterize spectral envelope paramters ### | |
abstract FrequencyForm | |
type MelFrequency <: FrequencyForm | |
end | |
type LinearFrequency <: FrequencyForm | |
end | |
abstract LogForm | |
type GeneralizedLog <: LogForm | |
end | |
type StandardLog <: LogForm | |
end | |
type AllPoleLog <: LogForm | |
end | |
### Definitions for spectral envelope paramters ### | |
immutable MelGeneralizedCepstrum{F,L} <: SpectralEnvelopeDef | |
order | |
α | |
γ | |
function MelGeneralizedCepstrum(order, α, γ) | |
abs(α) < 1 || throw(ArgumentError("|α| < 1 is supported")) | |
(-1 <= γ <= 0) || throw(ArgumentError("-1 <= γ <= 0 is supported")) | |
new(order, α, γ) | |
end | |
end | |
typealias MelFrequencyCepstrum{L} MelGeneralizedCepstrum{MelFrequency,L} | |
typealias LinearFrequencyCepstrum{L} MelGeneralizedCepstrum{LinearFrequency,L} | |
typealias GeneralizedLogCepstrum{F} MelGeneralizedCepstrum{F,GeneralizedLog} | |
typealias StandardLogCepstrum{F} MelGeneralizedCepstrum{F,StandardLog} | |
typealias AllPoleLogCepstrum{F} MelGeneralizedCepstrum{F,AllPoleLog} | |
typealias GeneralizedCepstrum MelGeneralizedCepstrum{LinearFrequency,GeneralizedLog} | |
typealias MelCepstrum MelGeneralizedCepstrum{MelFrequency,StandardLog} | |
typealias LinearCepstrum MelGeneralizedCepstrum{LinearFrequency,StandardLog} | |
typealias AllPoleCepstrum MelGeneralizedCepstrum{LinearFrequency,AllPoleLog} | |
# Generic fallback | |
function MelGeneralizedCepstrum(order, α, γ) | |
F = (α == zero(α)) ? LinearFrequency : MelFrequency | |
L = GeneralizedLog | |
if γ == zero(γ) | |
L = StandardLog | |
elseif γ == -one(γ) | |
L = AllPoleLog | |
end | |
MelGeneralizedCepstrum{F,L}(order, α, γ) | |
end | |
function Base.call(::Type{GeneralizedCepstrum}, order, γ) | |
MelGeneralizedCepstrum{LinearFrequency,GeneralizedLog}(order, 0.0, γ) | |
end | |
function Base.call(::Type{MelCepstrum}, order, α) | |
MelGeneralizedCepstrum{MelFrequency,StandardLog}(order, α, 0.0) | |
end | |
function Base.call(::Type{LinearCepstrum}, order) | |
MelGeneralizedCepstrum{LinearFrequency,StandardLog}(order, 0.0, 0.0) | |
end | |
function Base.call(::Type{AllPoleCepstrum}, order) | |
MelGeneralizedCepstrum{LinearFrequency,AllPoleLog}(order, 0.0, -1.0) | |
end | |
allpass_alpha(c::MelGeneralizedCepstrum) = c.α | |
glog_gamma(c::MelGeneralizedCepstrum) = c.γ | |
# LPC, LSP and PARCOR | |
immutable LinearPredictionCoef <: SpectralEnvelopeDef | |
order | |
end | |
immutable LineSpectralPair <: SpectralEnvelopeDef | |
order | |
end | |
immutable PartialAutoCorrelation <: SpectralEnvelopeDef | |
order | |
end | |
### State ### | |
type SpectralEnvelopeState{S<:SpectralEnvelopeDef,T,N} <: AbstractArray{T,N} | |
def::S | |
data::Array{T,N} | |
has_loggain::Bool | |
gain_normalized::Bool | |
function SpectralEnvelopeState(s::S, data::Array{T,N}, | |
has_loggain::Bool, gain_normalized::Bool) | |
new(s, data, has_loggain, gain_normalized) | |
end | |
end | |
function SpectralEnvelopeState{S<:SpectralEnvelopeDef,T,N}(s::S, data::Array{T,N}, | |
has_loggain::Bool, | |
gain_normalized::Bool) | |
SpectralEnvelopeState{S,T,N}(s, data, has_loggain, gain_normalized) | |
end | |
eltype(s::SpectralEnvelopeState) = eltype(s.data) | |
size(s::SpectralEnvelopeState) = size(s.data) | |
size(s::SpectralEnvelopeState) = size(s.data) | |
length(s::SpectralEnvelopeState) = length(s.data) | |
getindex(s::SpectralEnvelopeState, i::Real) = getindex(s.data, i) | |
getindex(s::SpectralEnvelopeState, i::Real...) = getindex(s.data, i...) | |
has_loggain(s::SpectralEnvelopeState) = s.has_loggain | |
gain_normalized(s::SpectralEnvelopeState) = s.gain_normalized | |
def(s::SpectralEnvelopeState) = s.def | |
rawdata(s::SpectralEnvelopeState) = s.data | |
# Generic fallback | |
function estimate(mgc::MelGeneralizedCepstrum, x::AbstractArray) | |
println("using mgcep") | |
α = allpass_alpha(mgc) | |
γ = glog_gamma(mgc) | |
order = get_order(mgc) | |
rawdata = SPTK.mgcep(x, order, α, γ) | |
gain_normalized = (γ == zero(γ) || γ == -one(γ)) | |
SpectralEnvelopeState(mgc, rawdata, true, gain_normalized) | |
end | |
function estimate(mgc::GeneralizedCepstrum, x::AbstractArray) | |
println("using gcep") | |
γ = glog_gamma(mgc) | |
order = get_order(mgc) | |
data = SPTK.gcep(x, order, γ) | |
gain_normalized = (γ == zero(γ) || γ == -one(γ)) | |
SpectralEnvelopeState(mgc, data, true, gain_normalized) | |
end | |
function estimate(mgc::MelCepstrum, x::AbstractArray) | |
println("using mcep") | |
order = get_order(mgc) | |
data = SPTK.mcep(x, order) | |
SpectralEnvelopeState(mgc, data, true, true) | |
end | |
function estimate(mgc::LinearCepstrum, x::AbstractArray) | |
println("using mgcep(α=0.0 and γ=0.0)") | |
order = get_order(mgc) | |
data = SPTK.mgcep(x, order, 0.0, 0.0) | |
SpectralEnvelopeState(mgc, data, true, true) | |
end | |
function estimate(mgc::AllPoleCepstrum, x::AbstractArray) | |
println("lpc cep") | |
order = get_order(mgc) | |
data = SPTK.mgcep(x, order, 0.0, -1.0) | |
SpectralEnvelopeState(mgc, data, true, true) | |
end | |
function estimate(lpc::LinearPredictionCoef, x::AbstractArray) | |
println("lpc") | |
order = get_order(lpc) | |
data = SPTK.lpc(x, order) | |
SpectralEnvelopeState(lpc, data, false, true) | |
end | |
### Conversions | |
function lpc2lsp{T<:LinearPredictionCoef}(state::SpectralEnvelopeState{T}; | |
kargs...) | |
@assert !has_loggain(state) | |
lpcdef = def(state) | |
data = rawdata(state) | |
data = SPTK.lpc2lsp(data) | |
ka = Dict{Symbol,Any}(kargs) | |
loggain = haskey(ka, :loggain) ? ka[:loggain] : false | |
newdef = LineSpectralPair(get_order(lpcdef)) | |
SpectralEnvelopeState(newdef, data, loggain, gain_normalized(state)) | |
end | |
### Examples | |
let | |
srand(98765) | |
x = rand(1024) | |
# mgcep | |
mgc = MelGeneralizedCepstrum(20, 0.41, -0.01) | |
state = estimate(mgc, x) | |
dump(state) | |
# gcep | |
mgc = GeneralizedCepstrum(20, -0.01) | |
state = estimate(mgc, x) | |
dump(state) | |
# mcep | |
mgc = MelCepstrum(20, 0.41) | |
state = estimate(mgc, x) | |
dump(state) | |
# uels | |
mgc = LinearCepstrum(20) | |
state = estimate(mgc, x) | |
dump(state) | |
# lpc-cepstrum | |
mgc = AllPoleCepstrum(20) | |
state = estimate(mgc, x) | |
dump(state) | |
# lpc | |
lpc = LinearPredictionCoef(20) | |
lpcstate = estimate(lpc, x) | |
dump(lpcstate) | |
# lsp | |
state = lpc2lsp(lpcstate) | |
dump(state) | |
# lsp (with loggain) | |
state = lpc2lsp(lpcstate, loggain=true) | |
dump(state) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment