Limho
Limho

Reputation: 3

Array insertion in a for loop C coding

    for(t=0; t<=1; t+=1e-4)
    {
        input [n] = 2 + sin(w*t);
    }

Hi I am new to C coding. I'm trying to program a Moving Average filter to be used in DSP controller to continuously calculate average of a waveform.

In this step, I would like to sample data from a sine wave with a step-size of 1e-4, and save them into an buffer. The buffer size should be 1000. But in this case, "t" is not an integer, so how can I be able to do that? Thanks for your help!!!

Upvotes: 0

Views: 56

Answers (2)

Lee Daniel Crocker
Lee Daniel Crocker

Reputation: 13196

Never use floating point values as loop indices. Use integers:

double t = 0.0;
for (int i = 0; i < 1000; i += 1) {
    t = i / 1000.0;
    input[i] = 2.0 + sin(w * t);
}

Upvotes: 0

Daniel
Daniel

Reputation: 51

Maybe you want to use microseconds so you can sample with integer numbers. You just have to adjust the formula so the result is in concordance.

Upvotes: 1

Related Questions