switch to int32_t and add modulo period for input values
parent
4e0097be1c
commit
b60e0a6c46
|
|
@ -1,15 +1,17 @@
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
template<uint32_t period, int16_t max_val>
|
template<int32_t period, int32_t max_val>
|
||||||
int16_t sine(uint32_t t) {
|
int32_t sine(int32_t t) {
|
||||||
// 0 <= t < period is 0 <= t_2pi < 2 pi
|
// 0 <= t < period is 0 <= t_2pi < 2 pi
|
||||||
float t_2pi = 2 * M_PI / static_cast<float>(period) * t;
|
t %= period;
|
||||||
return static_cast<int16_t>(sinf(t_2pi) * max_val);
|
float t_2pi = 2 * M_PI / static_cast<float>(period) * t;
|
||||||
|
return static_cast<int32_t>(sinf(t_2pi) * max_val);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<uint32_t period, int16_t max_val>
|
template<int32_t period, int32_t max_val>
|
||||||
int16_t square(uint32_t t) {
|
int32_t square(int32_t t) {
|
||||||
|
t %= period;
|
||||||
if (t <= period / 2) {
|
if (t <= period / 2) {
|
||||||
return -max_val;
|
return -max_val;
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -17,11 +19,12 @@ int16_t square(uint32_t t) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<uint32_t period, int16_t max_val>
|
template<int32_t period, int32_t max_val>
|
||||||
int16_t sawtooth(uint32_t t) {
|
int32_t sawtooth(int32_t t) {
|
||||||
// f(t) = a t + y
|
// f(t) = a t + y
|
||||||
// f(0) = -max_val = y
|
// f(0) = -max_val = y
|
||||||
// f(period) = a * period - max_val = max_val
|
// f(period) = a * period - max_val = max_val
|
||||||
// a = 2 * max_val / period
|
// a = 2 * max_val / period
|
||||||
|
t %= period;
|
||||||
return 2 * max_val * t / period - max_val;
|
return 2 * max_val * t / period - max_val;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue