Adding values in Array

walterf25

Expert
Licensed User
Longtime User
Hi all, i was wondering if anyone can help me by showing me how to add all the values placed in an array, i have an array(512) and i want to get the sum of all those values placed in the array, i know how to do it in visual studio but it won't work in B4A, does anyone know?

thanks,
Walter
 

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
Hi all, i was wondering if anyone can help me by showing me how to add all the values placed in an array, i have an array(512) and i want to get the sum of all those values placed in the array, i know how to do it in visual studio but it won't work in B4A, does anyone know?

thanks,
Walter

Can you post your non-working code?
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Here it is

I'm basically trying to implement a moving average filter, i'm getting the data from an analog to digital converter, i need to use this function to average out the data point and get rid of all the noise on top of the signal, here's what i got so far.

B4X:
Sub MovingAVG()
       ' Dim rndm As New Random 'used to generate random data
        Dim someData(512) As Byte 'place to store random data
       ' rndm.NextBytes(someData) 'generate 1000 values randomly
      Dim maDuration As Int 
    maDuration =  30 'length of moving average
   
       
      Dim theAvg As Int 'storage for values and average
        For x = 0 To samp_buf.Length   - 1 'iterate through random data
            ma.InsertAt(0, samp_buf(x)) 'insert into list newest will be at index 0
         Log(ma.get(0))
            theAvg = 0 'calculate the average
            For y = 0 To ma.Size  - 1
                theAvg = ma.Get(y) 'this where i'm stuck, i can't figure out how to add all the values in the array
'in Visual Studio you can just use theAVG += ma(y) and it will work.
            
            Log("values " & ma.Get(y))
            Next
            theAvg = theAvg / ma.Size  'divide by the count
         Log("theAVG " & theAvg)
            'Debug.WriteLine(theAvg.ToString)
         'Log("Average " & theAvg)
            If ma.size = maDuration Then 'do we have a durations worth of data?
                ma.RemoveAt(ma.size - 1) 'remove the oldest
            End If
        Next
'      For i = 0 To ma.Size - 1
'      Log("ma " & ma.Get(i))
'      Next
    End Sub

any help will be greatly appreciated

Thanks,
Walter
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
I tried that but it always gives me zero
Are you sure? Then perhaps there is a problem with your variables, because the following code logged correctly:

B4X:
Sub MovingAVG()
    Dim sampleSize As Int, maDuration As Int
    sampleSize=500
     Dim sampleData(samplesize) As Byte
    maDuration=30
    For k=0 To sampleSize-1
    sampleData(k)=Rnd(0,256)
    Next
    Dim ma As List
    ma.Initialize 
    Dim theAVG As Int 
    For x = 0 To sampleData.Length   - 1 
            ma.InsertAt(0, sampleData(x)) 
            theAVG = 0 
            For y = 0 To ma.Size  - 1
                theAVG = theAVG+ma.Get(y)
            Next
            theAVG = theAVG / ma.Size 
            Log("theAVG " & theAVG)
            If ma.size = maDuration Then 
                ma.RemoveAt(ma.size - 1) 
            End If
     Next
End Sub
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
summing arrays

I see that you are generating random numbers, I'm actually using the data taken from an analog to digital converter, but I don't see why that would make a difference I will have to double check my code and report back, thanks everyone for your help.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Summing Arrays

Ok , i got it working, it turns out that the data i'm loading into the arrays are double, and i had theAVG variable declared as integer, all i did was changed it to double and wala, works like a charm, thanks everyone for all your help.

Cheers,
Walter
 
Upvote 0
Top