xxxxxxxxxx
51
var fft = new FFTJS(256);
var input = new Array(256);
var sampling_rate = 50;
var freq_step = 50/256;
var out = fft.createComplexArray();
var ble = new Orphe(0);
function setup() {
// ORPHE CORE Init
ble.setup();
input.fill(0);
ble.onStartNotify = function () {
loop();
};
ble.gotAcc = function (_acc) {
input.push(_acc.x);
while (input.length > 256) {
input.shift();
}
};
createCanvas(20 + 256 + 20, 400);
noLoop();
}
function draw() {
background(200);
if (input.length >= 256) {
const data = fft.toComplexArray(input);
fft.transform(out, data);
noFill();
beginShape();
for (let i = 0; i < out.length / 2; i++) {
text(i * freq_step, 20 + i, height - 10);
vertex(20 + i, -pow(out[i], 2) + height - 20);
}
endShape();
fill(0);
textSize(7);
textAlign(CENTER);
for (let i = 0; i < out.length / 2; i += 25) {
text((i * freq_step).toFixed(1), 20 + i, height - 10);
line(20 + i, height - 18, 20 + i, height - 25);
}
text(`Frequency Step: ${freq_step}`, 10, 10);
}
}