[ 3 / biz / cgl / ck / diy / fa / ic / jp / lit / sci / vr / vt ] [ index / top / reports ] [ become a patron ] [ status ]
2023-11: Warosu is now out of extended maintenance.

/sci/ - Science & Math


View post   

File: 50 KB, 480x360, 1381493305804.jpg [View same] [iqdb] [saucenao] [google]
6088407 No.6088407[DELETED]  [Reply] [Original]

Hey /sci/,

What's the moving average of a group of numbers?

I'm writing a C program that calculates the moving average (running average) of a given set of numbers:

I input say 5 numbers from a file given a "window width" and then I output 5 numbers.

What is a window width and a moving average?

Thanks

>> No.6088409

Moving average is the average of your last n samples, where n is the window. As you get a new data point, the nth oldest drops out of your window, and the new one is added.

>> No.6088416

>>6088409

So suppose I have a file consisting of these five numbers:

1 2 4 8 4

and the window width is 2.

this means it outputs:

1.5 3 6 ?

>> No.6088417

>>6088407
>I'm writing a C program

Why the hell are you writing in C instead of C++?

>> No.6088423

>>6088417

It's fun. I'm writing in C90, too.

>> No.6088428

>>6088416
So in that sequence of numbers if I set the window to 3 the output would be:

3.5 7 8
?

>> No.6088437

>I'm writing a C program that calculates the moving average

Clearly you're not writing that program, because you're too stupid to understand what a moving average is. If such a simple homework exercise is too hard for you, then drop out.

>> No.6088438

>>6088428

I was thinking:

7/3 14/3 16/3

>> No.6088440

>>6088437
please explain it

>> No.6088443

>>6088440
nevermind i just googled it

>> No.6088445

>>6088407
>What is a window width and a moving average?

A moving average is a discrete low pass filter on the data set and the width is how many point it considers

ifstream file ("data.txt");
unsigned width, last_loc;
double avg=0, data;
cout<<"Enter the window width";
cin>>width;
if(!width) cerr<<"you're such a faggot!\n";
double* lastpoints = new double[width];
for(last_loc;last_loc<width;last_loc++) lastpoints[last_loc]=0;
while(file.good()){
file>>data;
avg+=(data-lastpoints[last_loc])/width;
lastpoints[last_loc]=data;
last_loc++;
last_loc%=width;//wrap around
cout<<"Data: "<<data<<"\nNew average: "<<avg<<"\n";
}
delete [] lastpoints;

>> No.6088449

>>6088445

I was writing it in C, but this works.