Snapshot.jl
← Notebooks/EEG band-power features🏝️ 2/2 cells interactive
.jl source

EEG spectral band-power features

A self-contained EEG feature extractor: a synthetic signal (sum of physiological rhythms) is analysed with a pure-Julia DFT and reduced to the canonical clinical band powers. Everything below the slider runs in your browser as WebAssembly β€” no Julia server, no install. Drag the slider to change the alpha rhythm amplitude and watch the band powers update live.

using PlutoUI

Alpha (8–13 Hz) amplitude:

1.0
@bind alpha_amp Slider(0.0:0.1:2.0, default=1.0, show_value=true)
eeg_band_powers

Pure-Julia EEG band powers: synthesize a 128-sample / 128 Hz signal (delta+theta+ alpha+beta+gamma), take the DFT, and integrate power over the clinical bands.

"""Pure-Julia EEG band powers: synthesize a 128-sample / 128 Hz signal (delta+theta+
alpha+beta+gamma), take the DFT, and integrate power over the clinical bands."""
function eeg_band_powers(alpha_amp::Float64)
    N = 128; fs = 128.0
    x = Vector{Float64}(undef, N)
    for n in 1:N
        t = (n - 1) / fs
        x[n] = 0.5*sin(2Ο€*2*t) + 0.4*sin(2Ο€*6*t) + alpha_amp*sin(2Ο€*10*t) +
               0.3*sin(2Ο€*20*t) + 0.2*sin(2Ο€*40*t)
    end
    half = N Γ· 2
    bands = zeros(Float64, 5)
    for k in 0:half
        re = 0.0; im = 0.0
        for n in 1:N
            ΞΈ = 2Ο€ * k * (n - 1) / N
            re += x[n]*cos(ΞΈ); im -= x[n]*sin(ΞΈ)
        end
        p = re*re + im*im
        f = k * fs / N
        if     f >= 0.5 && f < 4;  bands[1] += p
        elseif f < 8;              bands[2] += p
        elseif f < 13;             bands[3] += p
        elseif f < 30;             bands[4] += p
        elseif f < 45;             bands[5] += p
        end
    end
    return bands
end
relative_alpha

Relative alpha power = alpha-band power / total band power.

"Relative alpha power = alpha-band power / total band power."
function relative_alpha(a::Float64)
    b = eeg_band_powers(a)
    return b[3] / (b[1] + b[2] + b[3] + b[4] + b[5])
end
0.6493506493506496
rel = relative_alpha(alpha_amp)

Relative alpha power: 0.6493506493506496

(alpha-band fraction of total spectral power β€” rises as you increase the alpha amplitude)

Pluto notebooks as lean Therapy components β€” interactive WebAssembly, no server.