Search code examples
pythonarraysnumpyresolution

logarithmic rebinning of 2D array


I have a 1D ray containing data that looks like this (48000 points), spaced by one wavenumber (R = 1 cm-1). The shape of the x and y array is (48000, 1), I want to rebin both in a similar way

xarr=[50000,9999,9998,....,2000]
yarr=[0.1,0.02,0.8,0.5....0.1] 

I wish to decrease the spatial resolution, lets say R= 10 cm-1), so I want ten times less points (4800), from 50000 to 2000. And do the same for the y array

How to start?

I try by taking the natural log of the wavelength scale, then re-bin this onto a new log of wavelength scale generate using np.linspace()

xi=np.log(xarr[0])
xf=np.log(xarr[-1])
xnew=np.linspace(xi, xf, num=4800)

now I need to recast the y array into this xnew array, I am thinking of using rebin, a 2D rebin, but not sure how to use this. Any suggestions?


Solution

  • I tried this and it seems to work!

    import numpy as np
    import scipy.stats as stats
    
    #irregular x and y arrays
    yirr= np.random.randint(1,101,10)
    xirr=np.arange(10)
    
    nbins=5
    bin_means, bin_edges, binnumber = stats.binned_statistic(xirr,yirr, 'mean', bins=nbins)
    
    yreg=bin_means # <== regularized yarr
    
    xi=xirr[0]
    xf=xirr[-1]
    xreg=np.linspace(xi, xf, num=nbins)
    
    print('yreg',yreg)
    print('xreg',xreg) # <== regularized xarr
    

    If anyone can find an improvement or see a problem with this, please post! I'll try it on my logarithmically scaled data now