Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
/*
LED illumination
2017/06/22
edy
FFTの窓関数を利用する
https://ja.wikipedia.org/wiki/%E7%AA%93%E9%96%A2%E6%95%B0
*/
#define PI2 6.2832
#define PI4 12.566
#define PI8 25.133
int ledPin1 = 9;
int ledPin2 = 10;
int ledPin3 = 11;
int Akaike(float x)
{
// Akaike window
// w(x)=0.625-0.5cos(2πx)-0.125cos(4πx) 0<=x<=1
return (int)(255 * (0.625 - 0.5 * cos(PI2 * x) - 0.125 * cos(PI4 * x)));
}
int Akaike2(float x)
{
// Akaike2 window
// w(x)=0 0<=x<0.5
// w(x)=0.625-0.5cos(4πx)-0.125cos(8πx) 0.5<=x<=1
if (x < 0.5) {
return 0;
}
else {
return (int)(255 * (0.625 - 0.5 * cos(PI4 * x) - 0.125 * cos(PI8 * x)));
}
}
int Akaike3(float x)
{
// Akaike3 window
// w(x)=0.625-0.5cos(4πx)-0.125cos(8πx) 0.5<x<=1
// w(x)=0 0<=x<=0.5
if (x < 0.5) {
return (int)(255 * (0.625 - 0.5 * cos(PI4 * x) - 0.125 * cos(PI8 * x)));
}
else {
return 0;
}
}
int INV_Akaike(float x)
{
// w(x)=0.375-0.5cos(2πx)+0.125cos(4πx) 0<=x<=1
return (int)(255 * (0.375 - 0.5 * cos(PI2 * x) + 0.125 * cos(PI4 * x)));
}
int INV_Akaike2(float x)
{
if (x < 0.5) {
return 0;
}
else {
return (int)(255 * (0.375 - 0.5 * cos(PI4 * x) + 0.125 * cos(PI8 * x)));
}
}
int INV_Akaike3(float x)
{
if (x < 0.5) {
return (int)(255 * (0.375 - 0.5 * cos(PI4 * x) + 0.125 * cos(PI8 * x)));
}
else {
return 0;
}
}
int Sawtooth(float x)
{
return (int)(255 * x);
}
int Sawtooth2(float x)
{
return (int)(255 * (1 - x));
}
int Welch(float x)
{
// Welch window
// w(x)=4x(1-x) 0<=x<=1
return (int)(255 * (4 * x * (1 - x)));
}
int Blackman(float x)
{
// Blackman window
//w(x)=0.42-0.5*cos(2πx)+0.08*cos(4πx) 0<=x<=1
return (int)(255 * (0.42 - 0.5 * cos(PI2 * x) + 0.08 * cos(PI4 * x)));
}
int brightness(int select, float x)
{
switch (select) {
case 0:
return Akaike(x);
break;
case 1:
return Akaike2(x);
break;
case 2:
return Akaike3(x);
break;
case 3:
return INV_Akaike(x);
break;
case 4:
return INV_Akaike2(x);
break;
case 5:
return INV_Akaike3(x);
break;
case 6:
return Sawtooth(x);
break;
case 7:
return Sawtooth2(x);
break;
default:
return 0;
break;
}
}
void setup() {
}
void loop() {
float x;
int patternSelect1 = random(8);
int patternSelect2 = random(8);
int patternSelect3 = random(8);
int delayT = random(10, 50);
int loopCount = random(1, 4);
for (int loop = 0 ; loop < loopCount ; loop++) {
for (int i = 0 ; i < 100; i++) {
x = i / 100.0;
analogWrite(ledPin1, brightness(patternSelect1, x));
analogWrite(ledPin2, brightness(patternSelect2, x));
analogWrite(ledPin3, brightness(patternSelect3, x));
delay(delayT);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment