{{ message }}

Instantly share code, notes, and snippets.

# shyd/PRECALCULATE_WAVEFORMS.md

Last active Sep 22, 2017
How to precalculate sine with js.

# Waveforms

## precalculated sine with js

Run the following code on JSFiddle with the proper amplitude set:

```var count = 0;
var max_amplitude=4095;
var amplitude=3800;
var offset=(max_amplitude-amplitude)/2;
var a=[];
for (var i=0; i<360; i++) {
var value = (Math.sin(count*3.14159265/180)+1)*amplitude/2+offset;
console.log(count, value.toFixed(0));
a.push(parseInt(value.toFixed(0), 10));
count = (count+1)%360;
}

if (number < 10) {
return "   " + number;
}
if (number < 100) {
return "  " + number;
}
if (number < 1000) {
return " " + number;
}
return number;
}

var res = "";
for (var i=0; i<360; i++) {
res += pad(a[i]) + ", ";
if (i != 0 && (i+1) % 20 == 0) {
res += "\n";
}
}
console.log(res);```

then use the console output for e.g. an ARM MCU like so:

```const uint16_t sine_wave_array[360] = {
2048, 2081, 2114, 2147, 2180, 2213, 2246, 2279, 2312, 2345, 2377, 2410, 2443, 2475, 2507, 2539, 2571, 2603, 2635, 2666,
2697, 2728, 2759, 2790, 2820, 2850, 2880, 2910, 2939, 2969, 2997, 3026, 3054, 3082, 3110, 3137, 3164, 3191, 3217, 3243,
3269, 3294, 3319, 3343, 3367, 3391, 3414, 3437, 3459, 3481, 3503, 3524, 3545, 3565, 3585, 3604, 3623, 3641, 3659, 3676,
3693, 3709, 3725, 3740, 3755, 3769, 3783, 3796, 3809, 3821, 3833, 3844, 3855, 3864, 3874, 3883, 3891, 3899, 3906, 3913,
3919, 3924, 3929, 3933, 3937, 3940, 3943, 3945, 3946, 3947, 3948, 3947, 3946, 3945, 3943, 3940, 3937, 3933, 3929, 3924,
3919, 3913, 3906, 3899, 3891, 3883, 3874, 3864, 3855, 3844, 3833, 3821, 3809, 3796, 3783, 3769, 3755, 3740, 3725, 3709,
3693, 3676, 3659, 3641, 3623, 3604, 3585, 3565, 3545, 3524, 3503, 3481, 3459, 3437, 3414, 3391, 3367, 3343, 3319, 3294,
3269, 3243, 3217, 3191, 3164, 3137, 3110, 3082, 3054, 3026, 2998, 2969, 2939, 2910, 2880, 2850, 2820, 2790, 2759, 2728,
2697, 2666, 2635, 2603, 2571, 2539, 2507, 2475, 2443, 2410, 2377, 2345, 2312, 2279, 2246, 2213, 2180, 2147, 2114, 2081,
2048, 2014, 1981, 1948, 1915, 1882, 1849, 1816, 1783, 1750, 1718, 1685, 1652, 1620, 1588, 1556, 1524, 1492, 1460, 1429,
1398, 1367, 1336, 1305, 1275, 1245, 1215, 1185, 1156, 1126, 1098, 1069, 1041, 1013,  985,  958,  931,  904,  878,  852,
826,  801,  776,  752,  728,  704,  681,  658,  636,  614,  592,  571,  550,  530,  510,  491,  472,  454,  436,  419,
402,  386,  370,  355,  340,  326,  312,  299,  286,  274,  262,  251,  240,  231,  221,  212,  204,  196,  189,  182,
176,  171,  166,  162,  158,  155,  152,  150,  149,  148,  148,  148,  149,  150,  152,  155,  158,  162,  166,  171,
176,  182,  189,  196,  204,  212,  221,  231,  240,  251,  262,  274,  286,  299,  312,  326,  340,  355,  370,  386,
402,  419,  436,  454,  472,  491,  510,  530,  550,  571,  592,  614,  636,  658,  681,  704,  728,  752,  776,  801,
826,  852,  878,  904,  931,  958,  985, 1013, 1041, 1069, 1097, 1126, 1156, 1185, 1215, 1245, 1275, 1305, 1336, 1367,
1398, 1429, 1460, 1492, 1524, 1556, 1588, 1620, 1652, 1685, 1718, 1750, 1783, 1816, 1849, 1882, 1915, 1948, 1981, 2014,
};```

If putting the wave to an DAC make to use a DMA to offload the CPU. Also check the wave for clipping.

## Use it with other MCUs

On Atmel MCUs you can put the array into flash instead on the the stack with the following declaration:

Set the amplitude to one byte with `var amplitude=255;` in the js section to fit into `uint8_t`.

`const uint8_t sine_wave_array[360] PROGMEM = {...}`