Click here to Skip to main content
16,020,568 members
Home / Discussions / C#
   

C#

 
AnswerRe: C# 2008 express building a query Pin
stopthespying15-Feb-18 6:48
stopthespying15-Feb-18 6:48 
QuestionImplementing a moving average Pin
auting8214-Feb-18 10:12
auting8214-Feb-18 10:12 
AnswerRe: Implementing a moving average Pin
OriginalGriff14-Feb-18 20:41
mveOriginalGriff14-Feb-18 20:41 
GeneralRe: Implementing a moving average Pin
auting8214-Feb-18 23:54
auting8214-Feb-18 23:54 
AnswerRe: Implementing a moving average Pin
Ralf Meier15-Feb-18 0:25
mveRalf Meier15-Feb-18 0:25 
GeneralRe: Implementing a moving average Pin
Rob Philpott15-Feb-18 0:24
Rob Philpott15-Feb-18 0:24 
GeneralRe: Implementing a moving average Pin
OriginalGriff15-Feb-18 0:34
mveOriginalGriff15-Feb-18 0:34 
GeneralRe: Implementing a moving average Pin
auting8215-Feb-18 1:38
auting8215-Feb-18 1:38 
Yes I am a beginner Laugh | :laugh:
Now I made some slight changes and I am getting average of the last 5 numbers, but still my problems is not solved Cry | :((

When I generate my Sensor values in the text box , they appear like this:

TimeStamp AI.1 AI.2 AI.3 AI.4 AI.5 AI.6 AI.7
13:29:05.091 0.726 0.249 0.771 0.294 0.816 0.338 0.861
13:30:40.325 0.817 0.111 0.404 0.698 0.991 0.284 0.962
13:31:19.709 0.768 0.467 0.166 0.865 0.564 0.263 0.915
13:32:00.943 0.558 0.772 0.985 0.198 0.412 0.625 0.594
13:32:31.391 0.206 0.658 0.109 0.560 0.012 0.463 0.720

What happens is that program calculates average of the last five numbers in the text box which means in this case Moving Average=(0.109+0.560+0.012+0.463+0.720)/5=0.373.
While the moving average for example for AI.1 would be:(0.726+0.817+0.758+0.558+0.206)/5=0.615
This is obviously Wrong as I am getting average of the values from different sensors.
I need average of the last five values for Sensor AI.1, AI2,....etc
So I need to make some more changes to get this done.

Here is the code I have changed:

No changes to your method:
C#
private double GetMovingAverage(double newVal)
        {
            // Insert
            if (count != samples.Length) count++;
            samples[index++] = newVal;
            if (index == samples.Length) index = 0;
            // Get average
            double sum = 0.0;
            for (int i = 0; i < count; i++)
            {
                sum += samples[i];
            }
            return sum / (double)(count);
        }


The method was implemented like this:

C#
for (int id = 0; id < maxAI; id++)
            {

                double AnalogSensorValues = sObj[id].GetAnalogValue();
              // double MovingAverageValues= fObj[id].MA_Filter(AnalogSensorValues);


               
                sTxt = AnalogSensorValues.ToString("F3");
             
                textSensorValues.Text += sTxt + "    ";
                txtFilterValues.Text = fTx1;
            
                txtFilterValues.Text = GetMovingAverage(AnalogSensorValues).ToString("F3");
            }


modified 15-Feb-18 7:44am.

GeneralRe: Implementing a moving average Pin
OriginalGriff15-Feb-18 1:53
mveOriginalGriff15-Feb-18 1:53 
GeneralRe: Implementing a moving average Pin
auting8215-Feb-18 4:35
auting8215-Feb-18 4:35 
GeneralRe: Implementing a moving average Pin
OriginalGriff15-Feb-18 4:53
mveOriginalGriff15-Feb-18 4:53 
AnswerRe: Implementing a moving average Pin
Ralf Meier15-Feb-18 21:27
mveRalf Meier15-Feb-18 21:27 
GeneralRe: Implementing a moving average Pin
auting8216-Feb-18 1:25
auting8216-Feb-18 1:25 
GeneralRe: Implementing a moving average Pin
Pete O'Hanlon16-Feb-18 7:53
mvePete O'Hanlon16-Feb-18 7:53 
GeneralRe: Implementing a moving average Pin
Ralf Meier16-Feb-18 9:49
mveRalf Meier16-Feb-18 9:49 
GeneralRe: Implementing a moving average Pin
auting8216-Feb-18 11:40
auting8216-Feb-18 11:40 
GeneralRe: Implementing a moving average Pin
Ralf Meier16-Feb-18 12:42
mveRalf Meier16-Feb-18 12:42 
GeneralRe: Implementing a moving average Pin
Ralf Meier17-Feb-18 0:14
mveRalf Meier17-Feb-18 0:14 
GeneralRe: Implementing a moving average Pin
auting8217-Feb-18 0:36
auting8217-Feb-18 0:36 
AnswerRe: Implementing a moving average Pin
Ralf Meier17-Feb-18 0:53
mveRalf Meier17-Feb-18 0:53 
GeneralRe: Implementing a moving average Pin
auting8217-Feb-18 1:13
auting8217-Feb-18 1:13 
GeneralRe: Implementing a moving average Pin
Ralf Meier17-Feb-18 3:30
mveRalf Meier17-Feb-18 3:30 
AnswerRe: Implementing a moving average Pin
Ralf Meier17-Feb-18 3:50
mveRalf Meier17-Feb-18 3:50 
GeneralRe: Implementing a moving average Pin
auting8217-Feb-18 7:40
auting8217-Feb-18 7:40 
GeneralRe: Implementing a moving average Pin
Ralf Meier17-Feb-18 10:11
mveRalf Meier17-Feb-18 10:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.