从特定数据计算锯齿波和三角波 [英] Calculate saw and triangle wave from specific data

查看:521
本文介绍了从特定数据计算锯齿波和三角波的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要计算一个三角形和锯齿波,但它有点复杂,因为我的模型和我能够处理的数据(但也许我只是困惑)。

I need to calculate a triangle and saw wave but it is a little complicate because of my model and the data I'm able to work with (but maybe I'm just confused).

我可以计算我的正弦波,但我不是真的使用帧计数器。我做的是,计算一个 theta_increment 变量,我可以使用下一次我需要计算一个样本。这样工作:

I'm able to calculate my sine wave but I'm not really using a frame counter. What I do is, calculate a theta_increment variable which I can use the next time I need to calculate a sample. This works like this:

float x = note.frequency / AppSettings::sampleRate;
float theta_increment = 2.0f * M_PI * x;
float value = 0;

if(waveType == SINE){
    value = sin(note.theta) * fixedAmplitude;
}

现在我有currend框架/样本的值我存储 theta_increment 在我的 note.theta 成员内,所以我可以使用它下一个样本:

Now that I have the value of the currend frame/sample I store theta_increment inside my note.theta member so I can use it for the next sample:

note.theta += theta_increment;

我已经查看了大量的如何计算锯齿或三角形的例子, 't搞定了。这是我最后一次尝试,但它不工作,给我很多毛刺:

I've looked at tons of examples on how I should calculate a saw or a triangle but I can't figure it out. (I only have the data mentioned above at my disposal) This is my last attempt but it's not working and giving me tons of glitches:

value = 1.0f - (2.0f * ((float)note.theta / (float)44100));


推荐答案

如果你有一个循环生成你的值,

If you have a loop generating your values like this:

for (size_t frame=0; frame!=n_frames; ++frame) {
  float pos = fmod(frequency*frame/sample_rate,1.0);
  value[frame] = xFunc(pos)*fixedAmplitude;
}

然后,您可以将这些函数用于不同类型的wave:

Then you can use these functions for different types of waves:

float sinFunc(float pos)
{
  return sin(pos*2*M_PI);
}

float sawFunc(float pos)
{
  return pos*2-1;
}

float triangleFunc(float pos)
{
  return 1-fabs(pos-0.5)*4;
}

基本思想是你想要一个值(pos)到1.0。

The basic idea is that you want a value (pos) that goes from 0.0 to 1.0 over each cycle. You can then shape this however you want.

对于正弦波,sin()函数执行作业,您只需乘以2 * PI即可转换0.0至1.0范围转换为0.0至2 * PI范围。

For a sine wave, the sin() function does the job, you just need to multiply by 2*PI to convert the 0.0 to 1.0 range into a 0.0 to 2*PI range.

对于锯齿波,只需将0.0至1.0范围转换为-1.0至1.0范围。乘以2,减去1即可。

For a sawtooth wave, you just need to convert the 0.0 to 1.0 range into a -1.0 to 1.0 range. Multiplying by two and subtracting one does that.

对于三角波,您可以使用绝对值函数导致方向突然变化。首先,我们通过减去-0.5将0.0到1.0的范围映射到-0.5到0.5的范围。然后我们通过取绝对值使它成为0.5到0.0到0.5的形状。通过乘以4,我们将其转换成2.0到0.0到2.0的形状。最后通过从一个减去它,我们得到一个-1.0到1.0到-1.0的形状。

For a triangle wave, you can use the absolute value function to cause the sudden change in direction. First we map the 0.0 to 1.0 range into a -0.5 to 0.5 range by subtracting -0.5. Then we make this into a 0.5 to 0.0 to 0.5 shape by taking the absolute value. By multiplying by 4, we convert this into a 2.0 to 0.0 to 2.0 shape. And finally by subtracting it from one, we get a -1.0 to 1.0 to -1.0 shape.

这篇关于从特定数据计算锯齿波和三角波的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆