我想在 ARM Cortex M0 微 Controller 上生成一个正弦信号,该信号可通过形状参数 n 对其形状进行自定义。以下面的图为例。在这里我用函数对不同的曲线进行建模
$y(x) = (1/2*(1 + sin(x)))^n$
对于更高的 n,最大值“较短”,最小值为“
wider
”。对于较低的 n,最大值为“wider
”,最小值为“shorter
”。为了减少内存消耗和提高性能,我只使用整数数学来生成正弦信号。
这是正弦计算的代码:
Input range [0R 2πR] mapped to [0 to 1024].
输出范围 [-1.0 1.0] 映射到 [-16,384 16,384]。
int_fast16_t sin_bam(int angle_bam) {
angle_bam %= 1024;
if (angle_bam < 0)
angle_bam += 1024;
uint_fast16_t sub_angle = angle_bam % (1024u / 2);
int_fast16_t sn = unsigned_mult16x16_into_32(sub_angle, (1024/2 - sub_angle)) / 4;
if (angle_bam & 512) {
sn = -sn;
}
return sn;
}
有没有人知道如何仅使用整数数学来实现这种形状修改?
最佳答案
Does anyone have an idea how to realize such a shape modification only with integer math?
n
could also have a range of 1 to 10 for example$y(x) = (1/2*(1 + sin(x)))^n$
OP 具有正弦逼近函数
sin_bam()
然后使用它来形成 1/2*(1 + sin(x))
这是 hacovercosin(θ) .剩下的就是标度幂函数。#include <inttypes.h>
#define SCALE 16384
int pow_scale(int x, unsigned n) {
unsigned y = SCALE;
while (n>0) {
if (n%2) {
y = unsigned_mult16x16_into_32(x,y)/SCALE; // 16-bit x 16-bit --> 32-bit product.
}
n /= 2;
x = unsigned_mult16x16_into_32(x,x)/SCALE;
}
return x;
}
// x is the angle in 1024 BAM per period
// output is scaled to range [0....SCALE]
int shaped(int x, unsigned n) {
return pow_scale(SCALE/2 + sin_bam(x)/2), n);
}
关于c - 仅使用整数数学对 C 中的正弦信号进行形状修改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47970428/