Created
March 29, 2021 14:37
-
-
Save jerlich/76d3c69bbd4046317b00ad9fc489ddba to your computer and use it in GitHub Desktop.
An answer to https://dsp.stackexchange.com/questions/74078/hypothesis-testing-with-binary-pattern/74137#74137
This file contains hidden or 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
### A Pluto.jl notebook ### | |
# v0.12.21 | |
using Markdown | |
using InteractiveUtils | |
# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error). | |
macro bind(def, element) | |
quote | |
local el = $(esc(element)) | |
global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : missing | |
el | |
end | |
end | |
# ╔═╡ 1301ad3c-9098-11eb-377b-655794eedeb7 | |
using Distributions, DSP, StatsPlots, StatsBase, PlutoUI | |
# ╔═╡ b63ef62c-9097-11eb-0d6a-b9c24d0b13f4 | |
import Pkg | |
# ╔═╡ bd82f474-9097-11eb-0d2f-3fbe5cdcbf76 | |
Pkg.add("DSP") | |
# ╔═╡ 843f6622-909a-11eb-1a0f-195649300b81 | |
md""" | |
flipfrac = $( @bind flipfrac Slider(0:0.05:1, show_value=true) ) | |
""" | |
# ╔═╡ c4af9504-9097-11eb-2661-397f406c4ad7 | |
noisy = let ff = flipfrac | |
n=1000 | |
flip_frac = ff | |
raw_a = repeat([1,0,0], n) | |
flipind = rand(n*3) .< flip_frac | |
noisy_a = copy(raw_a) | |
noisy_a[flipind] .= sample([0,1], sum(flipind)) | |
noisy_a = noisy_a .- mean(noisy_a) | |
end | |
# ╔═╡ c6a4901e-9097-11eb-0e9c-c92acb4bf00c | |
begin | |
bins = 6 | |
xc = xcorr(noisy,noisy); mid = Int(length(xc)/2+0.5) | |
end; md"Data xcorr" | |
# ╔═╡ 0f4161b4-9099-11eb-0b23-5f155bdc1cb6 | |
nulldist = let | |
boots = 1000 | |
B = fill(NaN, (boots, length(-bins:bins))) | |
for bx = 1:boots | |
newseq = sample(noisy, length(noisy), replace=false) | |
B[bx,:] = xcorr(newseq, newseq)[mid-bins:mid+bins] | |
end | |
B | |
end; md"Generate shuffled sequence" | |
# ╔═╡ 87621054-9098-11eb-02d8-cf064f1fe551 | |
begin | |
plot(-6:6,xc[mid-bins:mid+bins], marker=:circle, title="Noisy 1-0-0 Sequence") | |
plot!(-6:6,nulldist',color=:grey, alpha=0.5, legend=:false) | |
end | |
# ╔═╡ 135e0552-909b-11eb-246b-f143c2db75bb | |
@bind A Slider(0:0.05:1) | |
# ╔═╡ Cell order: | |
# ╠═b63ef62c-9097-11eb-0d6a-b9c24d0b13f4 | |
# ╠═bd82f474-9097-11eb-0d2f-3fbe5cdcbf76 | |
# ╠═1301ad3c-9098-11eb-377b-655794eedeb7 | |
# ╟─843f6622-909a-11eb-1a0f-195649300b81 | |
# ╟─c4af9504-9097-11eb-2661-397f406c4ad7 | |
# ╟─c6a4901e-9097-11eb-0e9c-c92acb4bf00c | |
# ╟─0f4161b4-9099-11eb-0b23-5f155bdc1cb6 | |
# ╠═87621054-9098-11eb-02d8-cf064f1fe551 | |
# ╠═135e0552-909b-11eb-246b-f143c2db75bb |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment