I have this function which is SMA (Simple Moving Average). The result in the array I display as graph in ZedGraph and now it will start from 0 to 1956. I want the graph to start from frameSize / 2 in this case example it will be 300 / 2 so 150 so the graph should start from 150 to 2016.
I don’t want to make the graph to grow I mean the array should stay length as 1956 I just want it to be pushed by 150 indexes from the beginning so it will start from index 150 instead of 0.
So this is the SMA function:
private static double[] smaDoubles(int frameSize, int[] data)
{
int padding = frameSize / 2;
double sum = 0;
double[] avgPoints = new double[(padding + data.Length) - frameSize + 1];
for (int counter = padding; counter <= data.Length - frameSize; counter++)
{
int innerLoopCounter = 0;
int index = counter;
while (innerLoopCounter < frameSize)
{
sum = sum + data[index];
innerLoopCounter += 1;
index += 1;
}
avgPoints[counter] = sum / frameSize;
sum = 0;
}
return avgPoints;
}
In the for loop counter = padding before it was counter = 0 so the result of that is in the image here.
The green one is the SMA from this function. And the green start from 150 but ends at 1956 and it should end at 2106. When I moved it to start from 150 I want the whole graph to move as one unit by 150 so it will start from 150 and end in 2106. The red graph should stay the same all
How can I do it?
Now as it is in the image the graph end by 300 from the right edge.
This is the function as it is now i changed the line: double[] avgPoints = new double[data.Length – frameSize + 1]; this is how it was original so i changed it to this one now.
And the function get frameSize as 3 and data as [10] and im getting the same exception:
private static double[] smaDoubles(int frameSize, int[] data)
{
int padding = frameSize / 2;
double sum = 0;
double[] avgPoints = new double[data.Length - frameSize + 1];
for (int counter = padding; counter <= data.Length - padding; counter++)//for (int counter = padding; counter <= data.Length - frameSize; counter++)
{
int innerLoopCounter = 0;
int index = counter;
while (innerLoopCounter < frameSize)
{
// if (index < data.Length)
sum = sum + data[index];
innerLoopCounter += 1;
index += 1;
}
avgPoints[counter] = sum / frameSize;
sum = 0;
}
return avgPoints;
}
Are you sure:
for (int counter = padding; counter <= data.Length - frameSize; counter++)shouldn’t be:
for (int counter = padding; counter <= data.Length - padding; counter++)And then your function that computes the moving average should go from counter – padding to counter + padding instead of from counter to counter + frameSize.
To debug this type of problem, it’s often helpful to try it which a much smaller data set where you can compute the expected result by hand and see if your algorithm matches your expectations. I don’t believe that your algorithm is necessarily calculating what you think it’s calculating here. Try it with 10 data elements and a window size of 3 to see if you’re getting the results that you expect.
Note, the first code line actually contains 2 logic errors, one of which is not necessarily apparent until you try the code 2nd line. The error is preserved for illustrative purposes