Rebinning Updates!
Updates on Rebinning in Stingray.jl
Welcome back! Over the past couple of weeks, we have been working hard on expanding the capabilities of Stingray.jl. One of the key features we've recently added is data rebinning. Rebinning is an essential part of time series analysis, especially when dealing with noisy light curves or power spectra where we want to trade off some resolution for a higher signal-to-noise ratio.
In this post, we'll walk through the two new rebinning functions we've introduced: rebin_data for linear rebinning and rebin_data_log for logarithmic rebinning.
Linear Rebinning with rebin_data
The rebin_data function allows you to linearly rebin your data array to a new, coarser resolution.
You can choose to either sum or average the data points falling into the new bins using the method keyword argument.
It also automatically propagates uncertainties if you provide them!Here is a quick example of how you can use it to rebin a light curve or a signal:
using Stingray using Plots # Generate some noisy synthetic data x = collect(0.0:0.01:10.0) y = sin.(2 * pi * x) .+ 0.2 * randn(length(x)) # Rebin the data to a new resolution of 0.5s, averaging the points xbin, ybin, ybinerr, step_size = rebin_data(x, y, 0.5, method=:mean, dx=0.01) # Plot the results p1 = plot(x, y, label="Original Data", xlabel="Time (s)", ylabel="Signal", alpha=0.5, marker=:circle, markersize=2, seriestype=:scatter) plot!(p1, xbin, ybin, label="Rebinned Data (dx=0.5s)", linewidth=3, color=:red)
As you can see, the higher resolution but noisy signal is nicely smoothed out in the rebinned array.
TIP:
The rebin_data function carefully handles bins that fall partially into the new bin edges by calculating their fractional contribution. This matches the behavior of Python's Stingray!
Logarithmic Rebinning with rebin_data_log
When working with power spectra, it's often more useful to rebin logarithmically. Logarithmic rebinning increases the bin width by a constant factor f for each subsequent bin. This means higher frequencies (which often have more noise and less signal) are binned more aggressively than lower frequencies, leading to roughly equal signal-to-noise across the spectrum.
The rebin_data_log function implements this in Stingray.jl:
This function returns the new bin midpoints (xlog), the rebinned power (ylog), the propagated errors (ylogerr), and the number of original samples combined into each new bin (nsamples). As
expected, the higher frequency bins are significantly wider in linear space, which cleans up the high-frequency noise beautifully on a log-log plot.
What's Next?
Both of these functions fully support propagating errors via quadrature and properly handle Complex numbers, which is essential when rebinning cross-spectra. We're excited to have these core utilities merged!
Stay tuned for more updates as we continue porting spectral timing capabilities to Julia!

