| 1 |
//--------------------------------------------------------------------- |
| 2 |
// name: pca.ck |
| 3 |
// desc: principle component analysis utility |
| 4 |
// |
| 5 |
// version: need chuck version 1.5.0.0 or higher |
| 6 |
// sorting: part of ChAI (ChucK for AI) |
| 7 |
// |
| 8 |
// uncomment for PCA API: |
| 9 |
// PCA.help(); |
| 10 |
// |
| 11 |
// author: Yikai Li |
| 12 |
// Ge Wang (https://ccrma.stanford.edu/~ge/) |
| 13 |
// date: Winter 2023 |
| 14 |
//--------------------------------------------------------------------- |
| 15 |
|
| 16 |
// prepare |
| 17 |
float input[10][10], output[10][3]; |
| 18 |
for( 0 => int i; i < 10; i++ ) |
| 19 |
{ |
| 20 |
for( 0 => int j; j < 10; j++ ) |
| 21 |
{ |
| 22 |
// put noise in |
| 23 |
Math.randomf() => input[i][j]; |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
// dimension reduce input (e.g., 10x10) to output (e.g., 10x3) |
| 28 |
// PCA.reduce() is a static method... |
| 29 |
// can be called with or without PCA instance |
| 30 |
PCA.reduce( input, 3, output ); |
| 31 |
|
| 32 |
// print |
| 33 |
for ( 0 => int i; i < 10; i++ ) |
| 34 |
{ |
| 35 |
for ( 0 => int j; j < 3; j++ ) |
| 36 |
{ |
| 37 |
// prin t |
| 38 |
chout <= output[i][j] <= ","; |
| 39 |
} |
| 40 |
chout <= IO.newline(); |
| 41 |
} |