From c565f01be89754c914b6400c8146bd032a979f1e Mon Sep 17 00:00:00 2001 From: dprn Date: Mon, 19 Oct 2020 23:29:16 +0200 Subject: [PATCH] new tests --- experiments/bars.jl | 66 ------------------------------ experiments/interrupted_chirp.jl | 58 ++++++++++++++++++++++++++ experiments/intersecting_chirps.jl | 58 ++++++++++++++++++++++++++ experiments/linear_chirp.jl | 57 +++++++++++++++++++------- experiments/nonlinear_chirp.jl | 56 +++++++++++++++++++++++++ experiments/run_experiments.jl | 46 ++++++++++++++++----- 6 files changed, 250 insertions(+), 91 deletions(-) delete mode 100644 experiments/bars.jl create mode 100644 experiments/interrupted_chirp.jl create mode 100644 experiments/intersecting_chirps.jl create mode 100644 experiments/nonlinear_chirp.jl diff --git a/experiments/bars.jl b/experiments/bars.jl deleted file mode 100644 index 5298dce..0000000 --- a/experiments/bars.jl +++ /dev/null @@ -1,66 +0,0 @@ -# Simple bars for tests - -using ImageFiltering - -# We just generate an STFT container, starting from a sound -rate = 16000. -duration = 2 -samples = round(Int,duration*rate) -y = [ t <= 8*samples/14 ? sin(1000*2*π*t/rate+1000*t/rate*2*π*t/rate) : 0. for t in 1:samples ] - -SS = stft(y, 500, 450, fs = rate); - -function two_bars(p1, p2, km1, km2, N1, N2; λ = 4) - # the bars - U0v1=10*[(((i+10)p1+km1-1 project + +## Save results + +try + mkpath("interrupted-chirp-results") +catch +end + +cd("interrupted-chirp-results") + +save_result(M, W, α, β, γ, χ) + +cd("..") diff --git a/experiments/intersecting_chirps.jl b/experiments/intersecting_chirps.jl new file mode 100644 index 0000000..a9616fa --- /dev/null +++ b/experiments/intersecting_chirps.jl @@ -0,0 +1,58 @@ +# Input sound + +rate = 16000. +duration = 2 +samples = round(Int,duration*rate) + +x1 = [ samples/7 <= t <= 6*samples/14 ? sin(2000*2*π*t/rate + 1000*t/rate*2*π*t/rate) : 0. for t in 1:samples ] +x2 = [ samples/7 <= t <= 6*samples/14 ? sin(6000*2*π*t/rate - 1000*t/rate*2*π*t/rate) : 0. for t in 1:samples ] + + +x = x1+x2 + +## Short time Fourier transform + +m =stft(x, 500, 450, fs = rate, window = hanning) + + +## smoothing + +using ImageFiltering + +λ=2 + +M = STFT(0.01.*ComplexF64.(imfilter(abs.(m.stft),ImageFiltering.Kernel.gaussian((λ,2λ)))), m.freq, m.time, m.width) + + +## Lift + +Lm = lift(M; νMin=-1, νMax=1, N=100) + +## WC evolution + +χ = 20 + +α = 53 +β = 1 +γ = 55 + +τ = χ * step(time(Lm)) + +b = .01 + +k = Kern(normalize(freq(Lm)), slopes(Lm), KernParams(τ, b, 1e-6)); + +W = wc_delay(Lm, α, β, γ, K=k,τdx = χ) |> project + +## Save results + +try + mkpath("intersecting-chirps-results") +catch +end + +cd("intersecting-chirps-results") + +save_result(M, W, α, β, γ, χ) + +cd("..") diff --git a/experiments/linear_chirp.jl b/experiments/linear_chirp.jl index 5177b81..18f78d6 100644 --- a/experiments/linear_chirp.jl +++ b/experiments/linear_chirp.jl @@ -1,28 +1,55 @@ -# Test with linear chirp +# Input sound rate = 16000. duration = 2 samples = round(Int,duration*rate) -y = [ t <= 8*samples/14 ? sin(1000*2*π*t/rate+1000*t/rate*2*π*t/rate) : 0. for t in 1:samples ] -mkpath("linear-chirp-results") -cd("linear-chirp-results") -wavwrite(y, "linear_chirp.wav", Fs = rate) +x = [ samples/7 <= t <= 8*samples/14 ? sin(2000*2*π*t/rate+1000*t/rate*2*π*t/rate) : 0. for t in 1:samples ] + + +## Short time Fourier transform + +m =stft(x, 500, 450, fs = rate, window = hanning) + + +## smoothing + +using ImageFiltering + +λ=2 -@time SS = stft(y, 500, 450, fs = rate) +M = STFT(0.01.*ComplexF64.(imfilter(abs.(m.stft),ImageFiltering.Kernel.gaussian((λ,2λ)))), m.freq, m.time, m.width) + -@time Lm = lift(SS, threshold = 10, N=30) +## Lift -KK=20 -AA=110 -BB=50 -CC=250 +Lm = lift(M; νMin=-0.5, νMax=1.5, N=100) +## WC evolution -τ = KK*step(time(Lm)) -@time k = kernel_computation(normalize(freq(Lm)), slopes(Lm), τ, n = 20); +χ = 20 -@time W = wc_delay(Lm, AA, BB, CC, K=k) +α = 55 +β = 1 +γ = 55 + +τ = χ * step(time(Lm)) + +b = .05 + +k = Kern(normalize(freq(Lm)), slopes(Lm), KernParams(τ, b, 1e-6)); + +W = wc_delay(Lm, α, β, γ, K=k,τdx = χ) |> project + +## Save results + +try + mkpath("linear-chirp-results") +catch +end + +cd("linear-chirp-results") -save_result(W, AA, BB, CC, KK) +save_result(M, W, α, β, γ, χ) +cd("..") diff --git a/experiments/nonlinear_chirp.jl b/experiments/nonlinear_chirp.jl new file mode 100644 index 0000000..bfdf627 --- /dev/null +++ b/experiments/nonlinear_chirp.jl @@ -0,0 +1,56 @@ + +# Input sound + +rate = 16000. +duration = 2 +samples = round(Int,duration*rate) + +x = [ samples/7 <= t <= 8*samples/14 ? sin(3000*2*π*t/rate+2pi*150*sin(2*pi*t/rate)) : 0. for t in 1:samples ] + +## Short time Fourier transform + +m =stft(x, 500, 450, fs = rate, window = hanning) + + +## smoothing + +using ImageFiltering + +λ=2 + +M = STFT(0.01.*ComplexF64.(imfilter(abs.(m.stft),ImageFiltering.Kernel.gaussian((λ,2λ)))), m.freq, m.time, m.width) + + +## Lift + +Lm = lift(M; νMin=-1.5, νMax=1.5, N=100) + +## WC evolution + +χ = 20 + +α = 53 +β = 1 +γ = 55 + +τ = χ * step(time(Lm)) + +b = .2 + +k = Kern(normalize(freq(Lm)), slopes(Lm), KernParams(τ, b, 1e-6)); + +W = wc_delay(Lm, α, β, γ, K=k,τdx = χ) |> project + +## Save results + +try + mkpath("nonlinear-chirp-results") +catch +end + +cd("nonlinear-chirp-results") + +save_result(M, W, α, β, γ, χ) + +cd("..") + diff --git a/experiments/run_experiments.jl b/experiments/run_experiments.jl index b1fb2a5..88f3721 100644 --- a/experiments/run_experiments.jl +++ b/experiments/run_experiments.jl @@ -1,25 +1,51 @@ using Dates -function save_result(m, α, β, γ, τdx; rate = 16000.) +function save_result(m_in, m_out, α, β, γ, τdx; rate = 16000.) str = string(Dates.format(now(),"yyyy-mm-dd"), " at ", Dates.format(now(),"HH"),"h",Dates.format(now(),"MM")," - a=",α,", b=",β,", g=",γ,", tdx=",τdx) - save(string(str," - STFT.png"), show_stft(m)) - save(string(str," - Wave.png"), show_istft(m)) + save(string(str," - input - STFT.png"), show_stft(m_in)) + save(string(str," - input - Wave.png"), show_istft(m_in)) - sound = istft(project(m)) - sound = 2*(sound .- minimum(sound))/(maximum(sound)-minimum(sound)).-1 - wavwrite(sound, string(str," - sound.wav"), Fs = rate) + save(string(str," - output - STFT.png"), show_stft(m_out)) + save(string(str," - output - Wave.png"), show_istft(m_out)) + + normalize(sound) = 2*(sound .- minimum(sound))/(maximum(sound)-minimum(sound)).-1 + + sound_in = istft(m_in) |> normalize + wavwrite(sound_in, string(str," - input - sound.wav"), Fs = rate) + + sound_out = istft(m_out) |> normalize + wavwrite(sound_out, string(str," - output - sound.wav"), Fs = rate) end using WAV +using DSP.Windows import Pkg Pkg.activate("../../WCA1") using WCA1 -# There is some kind of bug, for which we need to -# precompile `WCA1` before loading `Images` using Images -# include("linear_chirp.jl") -include("bars.jl") \ No newline at end of file +# TESTS + +try + mkpath("results") +catch +end + +cd("results") + +println("=== LINEAR CHIRP ===") +include("linear_chirp.jl") + +println("=== INTERRUPTED CHIRP ===") +include("interrupted_chirp.jl") + +println("=== INTERSECTING CHIRPS ===") +include("intersecting_chirps.jl") + +println("=== NON-LINEAR CHIRP ===") +include("nonlinear_chirp.jl") + +cd("..")