C ++中的正弦波生成 [英] sine wave generation in c++

查看:104
本文介绍了C ++中的正弦波生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试生成一组点,当绘制为图形时,这些点代表 1 个周期的正弦波.要求是:

  • 1 个周期的正弦波
  • 下限 = 29491
  • 上限 = 36043
  • 点数 = 100
  • 振幅 = 3276
  • 零偏移 = 32767

代码:

int main(){ofstream 输出文件;outfile.open("data.dat",ios::trunc | ios::out);for(int i=0;i<100;i++){输出文件<

我正在生成点并将其存储在文件中.绘制这些点后,我会得到以下图表.

但我只需要一个周期.我该怎么做?

解决方案

考虑到正弦波的公式:

<块引用>

y(t) = A * sin(2 * PI * f * t + shift)

哪里:

A = 幅度,函数从零的峰值偏差.
f = 普通频率,振荡次数(周期)
t = 时间
shift = 相移

应该是:

y[t] = AMPLITUDE * sin (2 * M_PI * 0.15 * t + 0) + ZERO_OFFSET;^^^ f = 15 个周期/NUM_POINTS = 0.15 Hz

要获得一个完整的循环,从 y[0:t) 开始循环,其中 t 是获得完整循环所需的时间或点数(即波长)

I am trying to generate a set of points, which when plotted as a graph represent a sine wave of 1 cycle. The requirements are :

  • a sine wave of 1 cycle
  • lower limit = 29491
  • upper limit = 36043
  • no of points = 100
  • Amplitude = 3276
  • zero offset = 32767

Code :

int main()
{
    ofstream outfile;
    outfile.open("data.dat",ios::trunc | ios::out);
    for(int i=0;i<100;i++)
    {
        outfile << int(3276*sin(i)+32767) << "\n";
    }
    outfile.close();
    return 0;
}

I am generating and storing the points in a file. When these points are plotted I get the following graph.

But I only need one cycle. How can I do this?

解决方案

taking into the formula of sine wave:

y(t) = A * sin(2 * PI * f * t + shift)

where:

A = the amplitude, the peak deviation of the function from zero.
f = the ordinary frequency, the number of oscillations (cycles)
t = time
shift = phase shift

would be:

y[t] = AMPLITUDE * sin (2 * M_PI * 0.15 * t + 0) + ZERO_OFFSET;
                                   ^^^ f = 15 cycles / NUM_POINTS = 0.15 Hz

To have one full-cycle, loop from y[0:t) where t is the time or number of points it takes to have a full cycle (i.e. wavelength)

这篇关于C ++中的正弦波生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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