Search code examples
arraysidlsmoothingbinning

Reducing the size of an array by averaging points within the array (IDL)


While I am sure there is an answer, and this question is very low-level (but it's always the easy things that trip you up), my main issue is trying to word the question.

Say I have the following arrays:

time=[0,1,2,3,4,5,6,7,8,9,10,11] ;in seconds
data=[0,1,2,3,4,5,6,7,8,9,10,11] 

The 'time' array is in bins of '1s', but instead I would like the array to be in bins of '2s' where the data is then the mean:

time=[0,2,4,6,8,10] ;in seconds
data=[0.5,2.5,4.5,6.5,8.5,10.5] 

Is there (and I am sure there is) an IDL function to implement this in IDL? my actual data array is:

DATA          DOUBLE    = Array[15286473]

so I would rather use an existing, efficient, solution than unnecessarily creating my own.

Cheers, Paul

NB: I can change the time array to what I want by interpolating the data (INTERPOL)

IDL> x=[0,1,2,3,4,5,6,7,8,9,10]
IDL> x_new=interpol(x,(n_elements(x)/2)+1.)
IDL> print, x_new                          
      0.00000      2.00000      4.00000      6.00000      8.00000      10.0000

The issue is just with the data array


Solution

  • I think you need rebin: http://www.exelisvis.com/docs/REBIN.html

    congrid provides similar functionality. If rebin does not solve your problem, this should work:

    step = 2
    select = step * indgen(floor(n_elements/step))
    new_time = (smooth(time, step))[select]
    new_data = (smooth(data, step))[select]
    

    You might want to set /edge_truncate for smooth, based on your requirements. Also, won't interpol work for you?