Reputation: 1
Sine wave generated using calculation during runtime works well but using precalculated values messes it up? Why? The pattern generated by using the function although does flicker a little but when using the table it gets too distorted and switches between 3-4 different partial sinewave patterns. I generated the table using this website and configuration shown in this screenshot.
const int32_t sinTable[1024] = {
0, 201, 402, 603, 804, 1005, 1206,
1407, 1608, 1809, 2009, 2210, 2410, 2611,
2811, 3012, 3212, 3412, 3612, 3811, 4011,
....... //shortend the table for the sake of simplicity
-4609, -4410, -4210, -4011, -3811, -3612, -3412,
-3212, -3012, -2811, -2611, -2410, -2210, -2009,
-1809, -1608, -1407, -1206, -1005, -804, -603,
-402, -201 };
#include <Arduino.h>
#include <I2S.h>
#define I2S_SAMPLE_RATE (44100)
#define I2S_SAMPLE_BITS 16
#define PIN_LRC 33
#define PIN_BCLK 26
#define PIN_DOUT 25
#include "tables.h"
int count = 0;
void setup() {
Serial.begin(115200);
I2S.setSckPin(PIN_BCLK);
I2S.setFsPin(PIN_LRC);
I2S.setDataPin(PIN_DOUT);
if (!I2S.begin(I2S_PHILIPS_MODE, I2S_SAMPLE_RATE, I2S_SAMPLE_BITS)) {
Serial.println("Failed to initialize I2S!");
while (1)
; // do nothing
}
}
int16_t GenerateSineWave() {
double rad = 2 * M_PI * 1000 * count++ / I2S_SAMPLE_RATE;
int16_t sineVal = 32767 * sin(rad);
// int16_t sineVal = 32767 * (rad);
return sineVal;
}
void loop() {
//Original Code: This works perfectly
I2S.write(GenerateSineWave());
I2S.write(GenerateSineWave());
//Alternative code to save calculation time. This doesn't work and create random wave patterns
if (count > 1023) count = 0;
I2S.write(sinTable[count]);
count++;
}
Upvotes: 0
Views: 172