Last active
March 22, 2018 17:48
-
-
Save jdpigeon/2e92eadb22b7f3be488a27918ba1a62c to your computer and use it in GitHub Desktop.
FFT with Fili
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
// Create FFT object | |
import { Fft } from "fili"; | |
// Helpful functions | |
const nextPow2 = (num) => { | |
let pow = 1; | |
while(pow < num) { | |
pow *= 2 | |
} | |
return pow | |
} | |
const meanFunc = (array) => array.reduce((acc, curr) => acc + curr) / array.length; | |
// const signal = someArrayOfOneChannelEEGData | |
const Fs = 1000; // sampling rate | |
const winSampleLength = signal.length; // length of epoch to be FFT'd | |
const NFFT = nextPow2(winSampleLength); | |
const fft = new Fft(NFFT); | |
// Demean | |
const mean = meanFunc(signal); | |
const demeanedSignal = signal.map((x) => x - mean); | |
// Calculate forward DFT with Hamming window | |
let Y = fft.forward(demeanedSignal, 'hamming'); | |
// Get complex magnitude | |
let magnitude = fft.magnitude(Y); | |
// Extract only positive frequencies & redistribute | |
let PSD = magnitude.slice(0, NFFT/2).map((x)=>2*x) | |
// Create frequency bins array | |
const f = new Array(NFFT / 2).fill(0).map((_, i)=> Fs/2 * ((i / (NFFT / 2)))) | |
// Calc band powers | |
// Delta < 4 | |
const indDelta = f.find((x) => x > 4); | |
const meanDelta = meanFunc(PSD.slice(0, indDelta)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment