4

为运行 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 函数吗?

就此而言,是否可以使用完全不同的算法来用正弦波填充缓冲区?

4

2 回答 2

5

鉴于您正在计算以固定增量增加的相位参数的正弦,使用递归方程实现信号生成通常要快得多,如本文“如何在软件中创建振荡器”帖子本文中的更多内容所述“ DSP Trick: Sinusoidal Tone Generator”帖子,都在dspguru上:

y[n] = 2*cos(w)*y[n-1] - y[n-2]

请注意,此递归方程可能会受到数值舍入误差累积的影响,您应避免一次计算太多样本(您的选择SAMPLES == 1024应该没问题)。y[0]在获得前两个值和y[1](初始条件)后,可以使用此递推方程。由于您生成的初始阶段为 0,因此它们很简单:

samples[0] = 0;
samples[1] = sin(phaseIncrement); 

或更一般地,具有任意初始阶段(对于经常重新初始化递推方程以避免我前面提到的数值舍入误差累积特别有用):

samples[0] = sin(initialPhase);
samples[1] = sin(initialPhase+phaseIncrement); 

然后可以使用以下方法直接实现递归方程:

float scale = 2*cos(phaseIncrement);
// initialize first 2 samples for the 0 initial phase case
samples[0] = 0;
samples[1] = sin(phaseIncrement);     
for (int i = 2; i < SAMPLES; i ++){
    samples[i] = scale * samples[i-1] - samples[i-2];
}

请注意,可以通过计算具有适当相对相移的多个音调(每个音调具有相同的频率,但在样本之间具有较大的相位增量),然后对结果进行交织以获得原始音调(例如计算sin(4*w*n)、和) sin(4*w*n+w),来对该实现进行矢量化。然而,这将使实现变得更加模糊,而收益相对较小。sin(4*w*n+2*w)sin(4*w*n+3*w)

或者,可以通过使用 来实现等式vDsp_deq22

// setup dummy array which will hold zeros as input
float nullInput[SAMPLES];
memset(nullInput, 0, SAMPLES * sizeof(float));

// setup filter coefficients
float coefficients[5];
coefficients[0] = 0;
coefficients[1] = 0;
coefficients[2] = 0;
coefficients[3] = -2*cos(phaseIncrement);
coefficients[4] = 1.0;

// initialize first 2 samples for the 0 initial phase case
samples[0] = 0;
samples[1] = sin(phaseIncrement); 
vDsp_deq22(nullInput, 1, coefficients, samples, 1, SAMPLES-2);
于 2016-01-25T02:58:48.150 回答
1

如果需要效率,您可以预加载 440hz (44100 / 440) 正弦波形查找表并在其周围循环,无需进一步映射或预加载 1hz (44100 / 44100) 正弦波形查找表并循环通过跳过样本以达到 440hz,就像您通过增加相位计数器所做的那样。使用查找表应该比计算 sin() 更快。

方法 A(使用 440hz 正弦波):

#define SAMPLES 1024
#define FREQUENCY 440
#define SAMPLING_RATE 44100
#define WAVEFORM_LENGTH (SAMPLING / FREQUENCY)

int main(int argc, const char * argv[]) {
    float waveform[WAVEFORM_LENGTH];
    LoadSinWaveForm(waveform);

    float samples[SAMPLES] __attribute__ ((aligned));
    float results[SAMPLES] __attribute__ ((aligned));

    for (int i = 0; i < SAMPLES; i ++){
        samples[i] = waveform[i % WAVEFORM_LENGTH];
    }

    vvsinf(results, samples, SAMPLES);

    return 0;
}

方法 B(使用 1hz 正弦波):

#define SAMPLES 1024
#define FREQUENCY 440
#define TWO_PI (3.14159 * 2)
#define SAMPLING_RATE 44100
#define WAVEFORM_LENGTH SAMPLING_RATE // since it's 1hz

int main(int argc, const char * argv[]) {
    float waveform[WAVEFORM_LENGTH];
    LoadSinWaveForm(waveform);

    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] = waveform[floor(currentPhase) % WAVEFORM_LENGTH];
        currentPhase += phaseIncrement;
    }

    vvsinf(results, samples, SAMPLES);

    return 0;
}

请注意:

  • 由于假设您的频率始终正确划分采样率,因此方法 A 容易受到频率不准确的影响,这是不正确的。这意味着您可能会得到 441hz 或 440hz 的故障。

  • 方法 B 随着频率升高并接近奈奎斯特频率时容易出现混叠,但如果合成​​合理的低频(例如您的示例中的频率),它是性能、质量和内存消耗之间的良好折衷。

于 2016-02-24T11:34:10.207 回答