为运行 IOS 的设备生成正弦波的最有效方法是什么。出于练习的目的,假设频率为 440Hz,采样率为 44100Hz,样本数为 1024。
一个普通的 C 实现看起来像。
#define SAMPLES 1024
#define TWO_PI (3.14159 * 2)
#define FREQUENCY 440
#define SAMPLING_RATE 44100
int main(int argc, const char * argv[]) {
float samples[SAMPLES];
float phaseIncrement = TWO_PI * FREQUENCY / SAMPLING_RATE;
float currentPhase = 0.0;
for (int i = 0; i < SAMPLES; i ++){
samples[i] = sin(currentPhase);
currentPhase += phaseIncrement;
}
return 0;
}
为了利用 Accelerate Framework 和 vecLib vvsinf 函数,可以将循环更改为仅执行加法。
#define SAMPLES 1024
#define TWO_PI (3.14159 * 2)
#define FREQUENCY 440
#define SAMPLING_RATE 44100
int main(int argc, const char * argv[]) {
float samples[SAMPLES] __attribute__ ((aligned));
float results[SAMPLES] __attribute__ ((aligned));
float phaseIncrement = TWO_PI * FREQUENCY / SAMPLING_RATE;
float currentPhase = 0.0;
for (int i = 0; i < SAMPLES; i ++){
samples[i] = currentPhase;
currentPhase += phaseIncrement;
}
vvsinf(results, samples, SAMPLES);
return 0;
}
但是,就效率而言,我应该尽可能地应用 vvsinf 函数吗?
我不太了解 Accelerate 框架,不知道我是否也可以替换循环。我可以使用 vecLib 或 vDSP 函数吗?
就此而言,是否可以使用完全不同的算法来用正弦波填充缓冲区?