# Juliaでオーディオ信号処理の練習¶

## ワウペダル¶

In [1]:
#パターン1: BiQuadモデル可変ローパスフィルタ
a::Vector
b::Vector
uz::Vector
yz::Vector

a = [1.0, 0.0, 0.0]
b = [0.0, 0.0, 0.0]
uz = [0.0, 0.0]
yz = copy(uz)
new(a, b, uz, yz)
end
end

function setCoefficients(sys::BiQuadLPF, fc, Q, fs)
ωc = 2π*fc/fs
α = sin(ωc)/2Q
a1 = 1 + α
a2 = -2*cos(ωc)/a1
a3 = (1 - α)/a1
b1 = (1 - cos(ωc))/2.0/ a1
b2 = b1*2
b3 = b1
sys.a[2] = a2
sys.a[3] = a3
sys.b[1] = b1
sys.b[2] = b2
sys.b[3] = b3
end

y = sys.b[1]*u
for i in [1, 2]
y += sys.uz[i] * sys.b[i+1] - sys.yz[i] * sys.a[i+1]
end

sys.uz[2] = sys.uz[1]
sys.uz[1] = u
sys.yz[2] = sys.yz[1]
sys.yz[1] = y

return y
end

#mは，カットオフ周波数を時間変化させるモジュレーション信号
function process!(sys::BiQuadLPF, u::Vector, m::Vector, f0, Q, fs)
@assert length(u) == length(m)

for i in 1:length(u)
fc = f0 + m[i]
setCoefficients(sys, fc, Q, fs)
u[i] = process(sys, u[i])
end
end

function process(sys::BiQuadLPF, u::Vector, m::Vector, f0, Q, fs)
uc = copy(u)
process!(sys, uc, m, f0, Q, fs)

return uc
end

Out[1]:
process (generic function with 2 methods)
In [2]:
#パターン2: StateVariableモデル可変ローパスフィルタ
struct StateVariableLPF
s::Vector

StateVariableLPF() = new([0.0, 0.0])
end

function process(sys::StateVariableLPF, u, g, Q)
den = 1 + g/Q + g^2
G1 = g/den
S1 = (sys.s[1] - g*sys.s[2])/den
G2 = g*G1
S2 = g*S1 + sys.s[2]

ξ = u - 1.0/Q * (G1*u + S1) - (G2*u + S2)
v = g*ξ
y1 = v + sys.s[1]
sys.s[1] = v + y1

v = g*y1
y2 = v + sys.s[2]
sys.s[2] = v + y2

return y2
end

function process!(sys::StateVariableLPF, u::Vector, m::Vector, f0, Q, fs)
@assert length(u) == length(m)

for i in 1:length(u)
fc = f0 + m[i]
g = π*fc/fs
u[i] = process(sys, u[i], g, Q)
end
end

function process(sys::StateVariableLPF, u::Vector, m::Vector, f0, Q, fs)
uc = copy(u)
process!(sys, uc, m, f0, Q, fs)

return uc
end

Out[2]:
process (generic function with 4 methods)
In [3]:
#オーディオファイルIOパッケージ
using LibSndFile

In [4]:
#サンプルオーディオデータ

To enable for the whole notebook select "Trust Notebook" from the "File" menu. You can also trust this cell by re-running it. You may also need to re-run using SampledSignals if the module is not yet loaded in the Julia kernel, or SampledSignals.embed_javascript() if the Julia module is loaded but the javascript isn't initialized.