Spectral estimation with Control Variate Variance Reduction
In [1]:
using PyPlot
using Statistics
se = include("SpecEst.jl")
at = include("../../AnalysisToolbox.jl")
whf = include("../../WhiteningFilters.jl")
ut = include("../Autocov_Spec_Est_utils.jl")
Out[1]:
In [28]:
function Get_Specs(data_type; N = 10^5, Nen = 25, L = 1500, Nex = 2L+2, flags...)
O = data_type(N; flags...)
X = O.X
spec= O.spec
labels = ["Mesa", "DSP_spec"]
S = Array{Any,1}(undef,2)
cv = false
@time S[1] = se.spec_est(X; estimator = at.spec_GB, Nex, cv, Nen)
@time S[2] = se.spec_est(X; estimator = se.Mesa, Nex, cv, Nen)
SS = copy(S);
S = Array{Any,1}(undef,2)
cv = true
@time S[1] = se.spec_est(X; estimator = at.spec_GB, Nex, cv, Nen)
@time S[2] = se.spec_est(X; estimator = se.Mesa, Nex, cv, Nen)
SS_cv = copy(S);
S = Array{Any,1}(undef,2)
cv = true
whole = true
@time S[1] = se.spec_est(X; estimator = at.spec_GB, Nex, cv, Nen, whole)
@time S[2] = se.spec_est(X; estimator = se.Mesa, Nex, cv, Nen, whole)
SS_cv_whole = copy(S);
return SS, SS_cv, SS_cv_whole, labels, spec
end
function DisplayPlots(SS, SS_cv, SS_cv_whole, labels, spec, Run = 1;
Nex = 2000,
zoombox = [pi - .1,pi + .1,1e-7, 1e-2],
smo = at._smoother(4,5; ty = "bin", two_sided = false)
)
wgrid = at.Θ(Nex)
Spec = spec.(wgrid);
figure(figsize = (12,12))
suptitle( "Run $Run")
for i = 1:2
subplot(2,2,2i-1)
title("estimated spectra using "*labels[i])
semilogy(SS[i].wgrid,SS[i].S[:], "k", label = "Sans CV")
semilogy(SS_cv[i].wgrid,SS_cv[i].S[:], "--g", label = "CV averaged")
semilogy(SS_cv[i].wgrid,at.smooth_spec(SS_cv[i].S[:],smo), ":g", label = "CV ave smo")
semilogy(SS_cv_whole[i].wgrid,SS_cv_whole[i].S[:], "--r", label = "CV whole")
semilogy(SS_cv_whole[i].wgrid,at.smooth_spec(SS_cv_whole[i].S[:],smo), ":r", label = "With CV whl smo")
semilogy(wgrid,Spec,":k",label = "truth")
ylabel("power")
xlabel(L"\omega")
legend()
subplot(2,2,2i)
title("estimated spectra using "*labels[i])
semilogy(SS[i].wgrid,SS[i].S[:], "k", label = "Sans CV")
semilogy(SS_cv[i].wgrid,SS_cv[i].S[:], "--g", label = "CV averaged")
semilogy(SS_cv[i].wgrid,at.smooth_spec(SS_cv[i].S[:],smo), ":g", label = "CV ave smo")
semilogy(SS_cv_whole[i].wgrid,SS_cv_whole[i].S[:], "--r", label = "CV whole")
semilogy(SS_cv_whole[i].wgrid,at.smooth_spec(SS_cv_whole[i].S[:],smo), ":r", label = "With CV whl smo")
semilogy(wgrid,Spec,":k",label = "truth")
axis(zoombox)
ylabel("power")
xlabel(L"\omega"*" (near "*L"\pi)")
legend()
end
end
Out[28]:
Guassian¶
In [20]:
sig =.5
K = 1000
f(x) = x < pi ? K/sqrt(2pi*sig)*exp(-x^2/sig) : K/sqrt(2pi*sig)*exp(-(2pi-x)^2/sig)
spec = x -> f(x) # divide by two if we require X real
N = 10^5
L = 1500
Nex = 2L+2
Nen = 25
SS, SS_cv, SS_cv_whole, labels, spec = Get_Specs(se.Guassian; N, Nen, L, Nex, spec, rl = true);
DisplayPlots(SS, SS_cv, SS_cv_whole, labels, spec, 1; Nex)
In [22]:
SS, SS_cv, SS_cv_whole, labels, spec = Get_Specs(se.Guassian; N, Nen, L, Nex, spec, rl = true);
DisplayPlots(SS, SS_cv, SS_cv_whole, labels, spec, 2; Nex)
In [24]:
SS, SS_cv, SS_cv_whole, labels, spec = Get_Specs(se.Guassian; N, Nen, L, Nex, spec, rl = true);
DisplayPlots(SS, SS_cv, SS_cv_whole, labels, spec, 3; Nex)
ARMA¶
In [34]:
L = 1500
Nex = 2L+2
Nen = 25
zoombox = [3pi/2*.9 - .1,3pi/2*.9 + .1,1e-9, 1e1]
SS, SS_cv, SS_cv_whole, labels, spec = Get_Specs(se.ARMA; N, Nen, L, Nex, rl = true);
DisplayPlots(SS,SS_cv,SS_cv_whole, labels, spec, 1; Nex, zoombox)
In [35]:
SS, SS_cv, SS_cv_whole, labels, spec = Get_Specs(se.ARMA; N, Nen, L, Nex, rl = true);
DisplayPlots(SS,SS_cv,SS_cv_whole, labels, spec, 2; Nex, zoombox)
In [36]:
SS, SS_cv, SS_cv_whole, labels, spec = Get_Specs(se.ARMA; N, Nen, L, Nex, rl = true);
DisplayPlots(SS,SS_cv,SS_cv_whole, labels, spec, 3; Nex, zoombox)
KSE (Second mode)¶
In [307]:
N = 10^5
O = se.KSE(N; rl = true)
X = O.X
Out[307]:
In [308]:
L = 1500
Nex = 2L + 1 + 1
S = Array{Any,1}(undef,2)
cv = false
labels = ["Mesa", "DSP_spec"]
@time S[1] = se.spec_est(X; estimator = at.spec_GB, Nex, cv, Nen, disc)
@time S[2] = se.spec_est(X; estimator = se.Mesa, Nex, cv, Nen, disc)
SS = copy(S);
In [309]:
S = Array{Any,1}(undef,2)
cv = true
Nen = 25 # div(N,Nen) = 4000
disc = 0
labels = ["Mesa", "DSP_spec"]
@time S[1] = se.spec_est(X; estimator = at.spec_GB, Nex, cv, Nen, disc)
# @time S[2] = se.spec_est(X; estimator = se.Mesa, Nex, cv, Nen, disc)
SS_cv = copy(S);
In [310]:
S = Array{Any,1}(undef,2)
cv = true
Nen = 25 # div(N,Nen) = 4000
whole = true
labels = ["Mesa", "DSP_spec"]
@time S[1] = se.spec_est(X; estimator = at.spec_GB, Nex, cv, Nen, whole)
# @time S[2] = se.spec_est(X; estimator = se.Mesa, Nex, cv, Nen, whole)
SS_cv_whole = copy(S);
In [314]:
labels = ["spec_GB", "Mesa"]
wgrid = at.Θ(Nex)
Spec = spec.(wgrid);
figure(figsize = (12,12))
suptitle( "KSE Run 1")
for i = 1:1
subplot(2,2,2i-1)
title("estimated spectra using "*labels[i])
loglog(SS[i].wgrid,SS[i].S[:], "k", label = "Sans CV")
semilogy(SS_cv[i].wgrid,SS_cv[i].S[:], "--g", label = "CV averaged")
semilogy(SS_cv[i].wgrid,at.smooth_spec(SS_cv[i].S[:],smo), ":b", label = "CV ave smo")
semilogy(SS_cv_whole[i].wgrid,SS_cv_whole[i].S[:], "--r", label = "CV whole")
semilogy(SS_cv_whole[i].wgrid,at.smooth_spec(SS_cv_whole[i].S[:],smo), ":r", label = "With CV whl smo")
ylabel("power")
xlabel(L"\omega")
legend()
# subplot(2,2,2i)
# title("estimated spectra using "*labels[i])
# loglog(SS[i].wgrid,SS[i].S[:], "k", label = "Sans CV")
# semilogy(SS_cv[i].wgrid,SS_cv[i].S[:], "--g", label = "CV averaged")
# semilogy(SS_cv[i].wgrid,at.smooth_spec(SS_cv[i].S[:],smo), ":b", label = "CV ave smo")
# semilogy(SS_cv_whole[i].wgrid,SS_cv_whole[i].S[:], "--r", label = "CV whole")
# semilogy(SS_cv_whole[i].wgrid,at.smooth_spec(SS_cv_whole[i].S[:],smo), ":r", label = "With CV whl smo")
# axis([3pi/2*.9 - .2,3pi/2*.9 + .2,1e-8, 1e1])
# ylabel("power")
# xlabel(L"\omega"*" (near "*L"\pi)")
# legend()
end
i = 2
subplot(2,2,2i-1)
title("estimated spectra using "*labels[i])
loglog(SS[i].wgrid,SS[i].S[:], "k", label = "Sans CV")
# semilogy(SS_cv[i].wgrid,SS_cv[i].S[:], "--g", label = "CV averaged")
# semilogy(SS_cv[i].wgrid,at.smooth_spec(SS_cv[i].S[:],smo), ":b", label = "CV ave smo")
# semilogy(SS_cv_whole[i].wgrid,SS_cv_whole[i].S[:], "--r", label = "CV whole")
# semilogy(SS_cv_whole[i].wgrid,at.smooth_spec(SS_cv_whole[i].S[:],smo), ":r", label = "With CV whl smo")
ylabel("power")
xlabel(L"\omega")
legend()
Out[314]: