Hello all,

I'm writing a code that generates sine waves, adds random noise, and then cleans it up by averaging those values. I've got it so it averages every 3 values using a sliding point window, however I need to write it as an array and to tell the truth, I'm stumped. I understand what to do...I just can't think of how to implement that in C.

Code:

/* Alexander DeTrano
This program generates normal sine waves then creates noise using random
numbers and the "cleans up the wave" by a series of averages and then plots
them on screen. When N = 40000 a high pitched noise is created intitally and
then when the wave with noise is added, a static is picked up, then the
averaged values clean it up and reduce some static.
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#define N 40000
double r, pi, f = 1500, w, t ;
int i; int n=N;
void create_sine(double y[], int n)
{
for ( i = 0; i < n; ++i )
{
t = i/8000.0;
y[i] = sin(w*t);
}
}
void print_array(double y[], int n)
{
for ( i=0; i<n; ++i )
{
printf("%f\n",y[i]);
}
}
void add_noise(double y[], int n)
{
for( i=0; i<n; ++i)
{
r = 2*(rand()/(RAND_MAX+1.0)) - 1;
t = i/8000.0;
y[i]=r + sin(w*t);
}
}
void avgm( double y[], int n, int m)
/* this is my old function using constant values for the moving point window.
* Basically a0 a1 and a2 need to go inside an array that's size m and have
them averaged similar to how they are averaged here */
{ double a0=0, a1=0, a2=0;
for (i=0;i<n;++i)
{
a0=a1;
a1=a2;
a2=y[i];
y[i]=(a0+a1+a2)/3.0;
}
}
int main( void)
{
int seed; double y[N]; int m;
void create_sine(double y[], int n);
void print_array(double y[], int n);
void add_noise(double y[], int n);
void avgm(double y[], int n, int m);
pi = 4*atan(1);
w = 2*pi*f;
seed = time(0);
srand(seed);
create_sine( y, N);
print_array( y, N);
add_noise( y, N);
print_array( y, N);
avgm( y, N, 3);
print_array( y, N);
return 0;
}

Any help would be greatly appreciated!

Cheers,

Alexander