Skip to content

Commit

Permalink
data: real fft
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny committed Oct 24, 2020
1 parent c5c836c commit 0dd117f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
17 changes: 10 additions & 7 deletions src/data.mjs
Expand Up @@ -41,12 +41,16 @@ export default class Data {

sample(freq) {
let presence = true;
let noise = Math.random() * 0.4;

// E6=1318
// A1=55
if (!freq) {
freq = Math.pow(2, Math.random() * 4.7) * 55;
presence = Math.random() > 0.5;
if (!presence) {
noise = 1;
}
}
const f = freq / this.sampleRate;
const phase = Math.random();
Expand All @@ -70,18 +74,17 @@ export default class Data {
this.normalize(this.fftIn);
}

const noise = Math.random() * 0.5;
for (let t = 0; t < this.fftIn.length; t++) {
this.fftIn[t] = this.fftIn[t] * (1 - noise) + Math.random() * noise;
}
this.normalize(this.fftIn);

this.fft.realTransform(this.fftOut, this.fftIn);
for (let i = 0; i < this.fftSize; i += 2) {
this.fftOut[i >>> 1] = Math.sqrt(
this.fftOut[i] ** 2 + this.fftOut[i + 1] ** 2) * (1 - noise) +
Math.random() * noise;
}

return {
freq: presence ? freq : 440,
presence,
fft: this.fftOut.slice(0, this.fftSize),
fft: this.fftOut.slice(0, this.fftSize >>> 1),
};
}
}
2 changes: 1 addition & 1 deletion src/train.mjs
Expand Up @@ -34,7 +34,7 @@ async function main() {
const name = '2048-32x4-1';

model.add(tf.layers.dense({
inputShape: [ data.fftSize ],
inputShape: [ data.fftSize >>> 1 ],
units: 32,
activation: 'relu',
}));
Expand Down

0 comments on commit 0dd117f

Please sign in to comment.