Getting the Average Value of Each Wave in a List of Waves

#pragma rtGlobals=1     // Use modern global access method.

// Demonstration of getting the average values of a set of waves on a per-wave basis.
// For a demo, execute:
//  DemoWaveAverages()
//
// The actual averaging is done in GetWaveAverages. This is the only function
// you will need once you have come up with a wave containing a list of wave
// references for the waves whose average values you want.
//
// The GetWaveAveragesFromTable function illustrates how to get such a
// list wave from a table.

// GetWaveAverages(listWave, outputName)
// listWave contains a list of input waves.
// outputName is the desired name for the output wave.
// Returns a wave containing the average values of each waves.
Function/WAVE GetWaveAverages(listWave, outputName)
    Wave /WAVE listWave // Contains list of numeric waves to be averaged
    String outputName       // Desired name for output wave
   
    // Make the output wave
    Variable numWaves = numpnts(listWave)
    Make /O /D /N=(numWaves) $outputName
    Wave output = $outputName
   
    // Fill it with the averages
    Variable i = 0
    for(i=0; i<numWaves; i+=1)
        Wave input = listWave[i]
        output[i] = mean(input)
    endfor
   
    return output   // Return output wave to caller
End

// GetWaveAveragesFromTable(tableName, outputName)
// tableName is the name of an existing table.
// outputName is the desired name for the output wave.
// Returns a wave containing the average values of each 1D, real, numeric wave in the table.
Function/WAVE GetWaveAveragesFromTable(tableName, outputName)
    String tableName
    String outputName       // Desired name for output wave
   
    if (WinType(tableName) != 2)
        Abort "Expected table name"
    endif
   
    String info = TableInfo(tableName, -2)
    Variable numColumns = NumberByKey("COLUMNS",info)   // Number of columns in table
    numColumns -= 1     // Ignore the Point column
   
    // This is guaranteed to be big enough
    Make /FREE /WAVE /N=(numColumns) listWave
   
    // Get list of 1D real numeric waves in table
    Variable numWaves = 0
    Variable index = 0
    do
        Wave/Z w = WaveRefIndexed(tableName, index, 1)  // Data columns only
        if (!WaveExists(w))
            break               // No more waves
        endif
        index += 1
       
        if (WaveDims(w) > 1)    // Ignore multi-dimensional waves
            continue
        endif
       
        if (WaveType(w) & 1)    // Ignore complex waves
            continue
        endif
       
        if (WaveType(w,1) != 1) // Ignore text waves, wave waves and DFREF waves
            continue
        endif
       
        listWave[numWaves] = w      // Add to list of waves whose averages we want
        numWaves += 1
    while(1)
       
    // Get wave containing averages
    Wave averageWave = GetWaveAverages(listWave, outputName)
   
    // Since listWave is a free wave and there are no more references to it,
    // Igor will kill it automatically when the function ends.
   
    return averageWave
End

Function DemoWaveAverages()
    // Create sample data and table
    DoWindow /F DemoTable
    if (V_flag == 0)
        Edit /W=(5,44,809,254) /N=DemoTable
        Variable i
        for(i=0; i<5; i+=1)
            String name = "wave" + num2istr(i)
            Make/O/N=3 $name = i + p
            AppendToTable $name
        endfor
    else
        // We don't want to average the output wave
        RemoveFromTable averages
    endif
   
    // Get a list of waves whose averages we want
    Make /O /FREE /WAVE listWave = {wave0,wave1,wave2,wave3,wave4}
   
    // Get wave containing averages
    Wave averageWave = GetWaveAveragesFromTable("DemoTable", "averages")
    AppendToTable /W=DemoTable averageWave
   
    Print averageWave
   
    // Since listWave is a free wave and there are no more references to it,
    // Igor will kill it automatically when the function ends.
End

Forum

Support

Gallery

Igor Pro 8

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More