{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Juliaでオーディオ信号処理の練習\n", "## ワウペダル" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "process (generic function with 2 methods)" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#パターン1: BiQuadモデル可変ローパスフィルタ\n", "struct BiQuadLPF\n", " a::Vector\n", " b::Vector\n", " uz::Vector\n", " yz::Vector\n", "\n", " function BiQuadLPF()\n", " a = [1.0, 0.0, 0.0]\n", " b = [0.0, 0.0, 0.0]\n", " uz = [0.0, 0.0]\n", " yz = copy(uz)\n", " new(a, b, uz, yz)\n", " end\n", "end\n", "\n", "function setCoefficients(sys::BiQuadLPF, fc, Q, fs)\n", " ωc = 2π*fc/fs\n", " α = sin(ωc)/2Q\n", " a1 = 1 + α\n", " a2 = -2*cos(ωc)/a1\n", " a3 = (1 - α)/a1\n", " b1 = (1 - cos(ωc))/2.0/ a1\n", " b2 = b1*2\n", " b3 = b1\n", " sys.a[2] = a2\n", " sys.a[3] = a3\n", " sys.b[1] = b1\n", " sys.b[2] = b2\n", " sys.b[3] = b3\n", "end\n", "\n", "function process(sys::BiQuadLPF, u)\n", " y = sys.b[1]*u\n", " for i in [1, 2]\n", " y += sys.uz[i] * sys.b[i+1] - sys.yz[i] * sys.a[i+1]\n", " end\n", " \n", " sys.uz[2] = sys.uz[1]\n", " sys.uz[1] = u\n", " sys.yz[2] = sys.yz[1]\n", " sys.yz[1] = y\n", " \n", " return y\n", "end\n", "\n", "#mは,カットオフ周波数を時間変化させるモジュレーション信号\n", "function process!(sys::BiQuadLPF, u::Vector, m::Vector, f0, Q, fs)\n", " @assert length(u) == length(m)\n", " \n", " for i in 1:length(u)\n", " fc = f0 + m[i]\n", " setCoefficients(sys, fc, Q, fs)\n", " u[i] = process(sys, u[i])\n", " end\n", "end\n", "\n", "function process(sys::BiQuadLPF, u::Vector, m::Vector, f0, Q, fs)\n", " uc = copy(u)\n", " process!(sys, uc, m, f0, Q, fs)\n", " \n", " return uc\n", "end" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "process (generic function with 4 methods)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#パターン2: StateVariableモデル可変ローパスフィルタ\n", "struct StateVariableLPF\n", " s::Vector\n", "\n", " StateVariableLPF() = new([0.0, 0.0])\n", "end\n", "\n", "function process(sys::StateVariableLPF, u, g, Q)\n", " den = 1 + g/Q + g^2\n", " G1 = g/den\n", " S1 = (sys.s[1] - g*sys.s[2])/den\n", " G2 = g*G1\n", " S2 = g*S1 + sys.s[2]\n", " \n", " ξ = u - 1.0/Q * (G1*u + S1) - (G2*u + S2)\n", " v = g*ξ\n", " y1 = v + sys.s[1]\n", " sys.s[1] = v + y1\n", " \n", " v = g*y1\n", " y2 = v + sys.s[2]\n", " sys.s[2] = v + y2\n", " \n", " return y2\n", "end\n", "\n", "function process!(sys::StateVariableLPF, u::Vector, m::Vector, f0, Q, fs)\n", " @assert length(u) == length(m)\n", " \n", " for i in 1:length(u)\n", " fc = f0 + m[i]\n", " g = π*fc/fs\n", " u[i] = process(sys, u[i], g, Q)\n", " end\n", "end\n", "\n", "function process(sys::StateVariableLPF, u::Vector, m::Vector, f0, Q, fs)\n", " uc = copy(u)\n", " process!(sys, uc, m, f0, Q, fs)\n", " \n", " return uc\n", "end" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#オーディオファイルIOパッケージ\n", "using LibSndFile" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
To enable for the whole notebook select \"Trust Notebook\" from the\n", " \"File\" menu. You can also trust this cell by re-running it. You may\n", " also need to re-run `using SampledSignals` if the module is not yet\n", " loaded in the Julia kernel, or `SampledSignals.embed_javascript()`\n", " if the Julia module is loaded but the javascript isn't initialized.
\n", "To enable for the whole notebook select \"Trust Notebook\" from the\n", " \"File\" menu. You can also trust this cell by re-running it. You may\n", " also need to re-run `using SampledSignals` if the module is not yet\n", " loaded in the Julia kernel, or `SampledSignals.embed_javascript()`\n", " if the Julia module is loaded but the javascript isn't initialized.
\n", "To enable for the whole notebook select \"Trust Notebook\" from the\n", " \"File\" menu. You can also trust this cell by re-running it. You may\n", " also need to re-run `using SampledSignals` if the module is not yet\n", " loaded in the Julia kernel, or `SampledSignals.embed_javascript()`\n", " if the Julia module is loaded but the javascript isn't initialized.
\n", "