Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Average Peak Value I have a scenario where i have a series of values recorded by

ID: 3562501 • Letter: A

Question

Average Peak Value

I have a scenario where i have a series of values recorded by a datalogger from a sensor. The output of the sensor varies over time, rising and then falling, forming a wave. I would like a way to identify each of the peak values in the output and then find the average. The frequency of the risses and falls does vary slightly as does the amplitude which makes a simple task complicated. Would any onee have any ideas using vba to examine the values and identify each peak value and calculate the average of aall the peaks found. The number of peaks will vary as it depends how long the logger was recording!!!

Explanation / Answer

Confused Slug,

The macro code below is a bit rough & ready, so there are some stylistic changes that could be made (like having a fixed range for input instead of a selection input, etc), but basically I think it does what you want.

Put a button on your sheet that links to the macro, select the input data, then click the button. The macro outputs the peak values in column D (with a header in D1), and the average peak below (with a header at the side in column E) - so if you already have stuff in columns D and E just amend the code.

Note the first and last data values cannot be considered as peak values (with this code anyway).

Option Explicit

Dim c As Long, d As Boolean, i As Long, j As Long, k As Long, p() As Long, r As Range, tp As Long, v() As Double

Sub av_peak()
'
'
ReDim p(0)
ReDim v(0)
'
c = Selection.Cells.Count
'
i = 1
'
For Each r In Selection
'
    ReDim Preserve v(i)
    '
    v(i) = r.Value
    '
    i = i + 1
'
Next r
'
'
d = True
'
k = 1
'
tp = v(1)
'
For i = 2 To c
'
    If v(i) > v(i - 1) Then
    '
        tp = v(i)
        '
        d = False
    '
    Else
    '
        If (v(i) < tp) And (d = False) Then
        '
            ReDim Preserve p(k)
            p(k) = tp
            d = True
            k = k + 1
        '
        End If
        '
    End If
    '
    If v(i) < v(i - 1) Then
    '
        d = True
    '
    End If
    '
'
Next i
'
'
Range("D1") = "Peaks"
'
For j = 1 To UBound(p)
'
Range("D1").Offset(j) = p(j)
'
Next j
'
'
Range("E1").Offset(j + 1) = "Average"
'
Range("D1").Offset(j + 1).Formula = "=Average(D2:D" & j & ")"
'
'
End Sub!!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote