Created
October 10, 2020 01:41
-
-
Save anandijain/18788e952ab2f673e69b0a03ca7855a9 to your computer and use it in GitHub Desktop.
DSP demo with pluto
This file contains 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.3 | |
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 | |
# ╔═╡ d7641af0-e514-11ea-159d-4b0ea0294203 | |
using Plots, SampledSignals, DSP, PlutoUI | |
# ╔═╡ 57b70420-e514-11ea-1a37-11efa5f2726c | |
@bind audio HTML(""" | |
<audio id="player"></audio> | |
<button class="button" id="stopButton">Stop</button> | |
<script> | |
const player = document.getElementById('player'); | |
const stop = document.getElementById('stopButton'); | |
const handleSuccess = function(stream) { | |
const context = new AudioContext({ sampleRate: 44100 }); | |
const analyser = context.createAnalyser(); | |
const source = context.createMediaStreamSource(stream); | |
source.connect(analyser); | |
const bufferLength = analyser.frequencyBinCount; | |
let dataArray = new Float32Array(bufferLength); | |
let animFrame; | |
const streamAudio = () => { | |
animFrame = requestAnimationFrame(streamAudio); | |
analyser.getFloatTimeDomainData(dataArray); | |
player.value = dataArray; | |
player.dispatchEvent(new CustomEvent("input")); | |
} | |
streamAudio(); | |
stop.onclick = e => { | |
source.disconnect(analyser); | |
cancelAnimationFrame(animFrame); | |
} | |
} | |
navigator.mediaDevices.getUserMedia({ audio: true, video: true }) | |
.then(handleSuccess) | |
</script> | |
<style> | |
.button { | |
background-color: darkred; | |
border: none; | |
border-radius: 12px; | |
color: white; | |
padding: 15px 32px; | |
text-align: center; | |
text-decoration: none; | |
display: inline-block; | |
font-size: 16px; | |
font-family: "Alegreya Sans", sans-serif; | |
margin: 4px 2px; | |
cursor: pointer; | |
} | |
</style> | |
""") | |
# ╔═╡ dca970a2-e514-11ea-2975-0be3321223c8 | |
begin | |
aud = Array(audio) | |
plot(domain(SampleBuf(aud, 44100)), SampleBuf(aud, 44100), legend=false, ylims=(-1,1)) | |
end | |
# ╔═╡ e8a895c6-0a98-11eb-018e-0f7b893f0350 | |
@bind cutoff Slider(50:20_000) | |
# ╔═╡ 9e5881e2-0a94-11eb-1415-edbd569e1287 | |
begin | |
hp_resp = Highpass(cutoff; fs=44_100) | |
hp_desg = Butterworth(4) | |
lp_resp = Lowpass(cutoff; fs=44_100) | |
lp_desg = Butterworth(4) | |
hp = filt(digitalfilter(hp_resp, hp_desg), aud) | |
lp = filt(digitalfilter(lp_resp, lp_desg), aud) | |
end | |
# ╔═╡ edcb4148-0a93-11eb-13d2-4bb829db282b | |
begin | |
plot(hp, label="high frequencies") | |
plot!(lp, label="looo") # xlim=(0,20_000)) | |
end | |
# ╔═╡ 2fd6d3d4-0a96-11eb-39e2-8f1d0f8eacdc | |
full_fft, hpfft, lpfft = periodogram.([aud, hp, lp]) | |
# ╔═╡ f5dd1f78-0a93-11eb-3a53-51019f79be01 | |
begin | |
plot(hpfft.freq, pow2db.(hpfft.power), label="hp") | |
plot!(lpfft.freq, pow2db.(lpfft.power), label="lp") | |
plot!(full_fft.freq, pow2db.(full_fft.power), label="ap") | |
end | |
# ╔═╡ Cell order: | |
# ╠═57b70420-e514-11ea-1a37-11efa5f2726c | |
# ╠═d7641af0-e514-11ea-159d-4b0ea0294203 | |
# ╠═dca970a2-e514-11ea-2975-0be3321223c8 | |
# ╠═e8a895c6-0a98-11eb-018e-0f7b893f0350 | |
# ╠═9e5881e2-0a94-11eb-1415-edbd569e1287 | |
# ╠═edcb4148-0a93-11eb-13d2-4bb829db282b | |
# ╠═2fd6d3d4-0a96-11eb-39e2-8f1d0f8eacdc | |
# ╠═f5dd1f78-0a93-11eb-3a53-51019f79be01 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment