【问题标题】:Generating sine wave array in C with noise在 C 中生成带噪声的正弦波阵列
【发布时间】:2015-09-19 17:22:30
【问题描述】:

这里的菜鸟。有人可以给我一个例子,说明我如何生成一个 2kHz 正弦波阵列,C 中的白噪声方差为 0.01?这是我到目前为止所拥有的:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define PI 3.141592653589793

int main() {
    int i;
    double buffer[10000];

    for(i = 0; i < 10000; i++)
    {
        buffer[i] = sin(2000 * (2 * PI) * i / 6000) + sqrt(0.01)rand;
    }

    return 0;
}

【问题讨论】:

  • 你的代码有什么问题?
  • 不确定如何使用 rand 函数添加白噪声:S
  • (除了丢失的随机种子和 sqrt(0.01)rand 的错别字)
  • 好吧,你当然必须用 srand((unsigned)time(0)) 为你的随机数生成器播种,而 sqrt(0.01)rand 应该是 sqrt(0.01) * rand()?
  • “用 srand((unsigned)time(0)) 为随机数生成器播种”是什么意思?

标签: c input signals trigonometry


【解决方案1】:

(供参考)

您首先必须使用srand() 为随机数生成器播种,您应该在每次程序运行时向其传递一个唯一值。

您的代码,为了正确性而修改:

#include <math.h>
#include <stdlib.h>
#include <time.h>

int main() {
    srand((unsigned)time(NULL));

    int i;
    double buffer[10000];

    for(i = 0; i < 10000; i++)
    {
        buffer[i] = sin(2000 * (2 * M_PI) * i / 6000) + sqrt(0.01) * rand();
    }

    /* ... */

    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-18
    • 1970-01-01
    • 1970-01-01
    • 2012-01-08
    • 2018-10-26
    • 1970-01-01
    • 2021-05-13
    • 1970-01-01
    相关资源
    最近更新 更多