我正在尝试生成一组点,当绘制为图形时,这些点表示 1 个周期的正弦波。要求是:
- 1个周期的正弦波
- 下限 = 29491
- 上限= 36043
- 点数 = 100
- 振幅 = 3276
- 零偏移量 = 32767
代码:
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;
}
我正在生成这些点并将其存储在一个文件中。绘制这些点后,我得到下图。
但我只需要一个周期。我该怎么做?
最佳答案
考虑到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
会是:
y[t] = AMPLITUDE * sin (2 * M_PI * 0.15 * t + 0) + ZERO_OFFSET;
^^^ f = 15 cycles / NUM_POINTS = 0.15 Hz
要有一个完整的循环,从 y[0:t)
开始循环,其中 t
是完成一个完整循环所需的时间或点数(即波长)
关于c++ - C++中的正弦波生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50366146/