|
10 | 10 | from stingray import lightcurve |
11 | 11 | import stingray.utils as utils |
12 | 12 | from stingray.utils import fftshift, fft2, ifftshift, fft |
| 13 | +from stingray.fourier import get_flux_iterable_from_segments, bispectrum_and_bicoherence_from_iterable |
13 | 14 |
|
14 | 15 | __all__ = ["Bispectrum"] |
15 | 16 |
|
@@ -445,3 +446,100 @@ def plot_phase(self, axis=None, save=False, filename=None): |
445 | 446 | else: |
446 | 447 | plt.savefig(filename) |
447 | 448 | return plt |
| 449 | + |
| 450 | + |
| 451 | +def bispectrum_and_bicoherence_from_time_array(times, gti, segment_size, dt): |
| 452 | + """Calculate the bispectrum and the bicoherence from an array of times. |
| 453 | +
|
| 454 | + Parameters |
| 455 | + ---------- |
| 456 | + times : float `np.array` |
| 457 | + Array of times in the reference band |
| 458 | + gti : [[gti00, gti01], [gti10, gti11], ...] |
| 459 | + common good time intervals |
| 460 | + segment_size : float |
| 461 | + length of segments |
| 462 | + dt : float |
| 463 | + Time resolution of the light curves used to produce periodograms |
| 464 | +
|
| 465 | + Returns |
| 466 | + ------- |
| 467 | + freqs : `np.array` |
| 468 | + The frequency in each row or column |
| 469 | + bispectrum: 2-d `np.array` |
| 470 | + The unnormalized bispectrum |
| 471 | + bicoherence: 2-d `np.array` |
| 472 | + The bicoherence, calculated with the normalization from |
| 473 | + Kim & Powers (1979), IEEE Trans. Plasma Sci., PS-7, 120 |
| 474 | + """ |
| 475 | + |
| 476 | + n_bin = np.rint(segment_size / dt).astype(int) |
| 477 | + dt = segment_size / n_bin |
| 478 | + |
| 479 | + flux_iterable = get_flux_iterable_from_segments( |
| 480 | + times, gti, segment_size, n_bin |
| 481 | + ) |
| 482 | + |
| 483 | + return bispectrum_and_bicoherence_from_iterable(flux_iterable, dt) |
| 484 | + |
| 485 | + |
| 486 | +def bispectrum_and_bicoherence_from_events(events, segment_size, dt): |
| 487 | + """Calculate the bispectrum and the bicoherence from an input event list. |
| 488 | +
|
| 489 | + Parameters |
| 490 | + ---------- |
| 491 | + events : `EventList` |
| 492 | + The input event list |
| 493 | + segment_size : float |
| 494 | + length of segments |
| 495 | + dt : float |
| 496 | + Time resolution of the light curves used to produce periodograms |
| 497 | +
|
| 498 | + Returns |
| 499 | + ------- |
| 500 | + freqs : `np.array` |
| 501 | + The frequency in each row or column |
| 502 | + bispectrum: 2-d `np.array` |
| 503 | + The unnormalized bispectrum |
| 504 | + bicoherence: 2-d `np.array` |
| 505 | + The bicoherence, calculated with the normalization from |
| 506 | + Kim & Powers (1979), IEEE Trans. Plasma Sci., PS-7, 120 |
| 507 | + """ |
| 508 | + |
| 509 | + times = events.time |
| 510 | + gti = events.gti |
| 511 | + |
| 512 | + return bispectrum_and_bicoherence_from_time_array(times, gti, segment_size, dt) |
| 513 | + |
| 514 | + |
| 515 | +def bispectrum_and_bicoherence_from_lightcurve(lightcurve, segment_size): |
| 516 | + """Calculate the bispectrum and the bicoherence from an array of times. |
| 517 | +
|
| 518 | + Parameters |
| 519 | + ---------- |
| 520 | + lightcurve : `Lightcurve` |
| 521 | + Input light curve |
| 522 | + segment_size : float |
| 523 | + length of segments |
| 524 | + dt : float |
| 525 | + Time resolution of the light curves used to produce periodograms |
| 526 | +
|
| 527 | + Returns |
| 528 | + ------- |
| 529 | + freqs : `np.array` |
| 530 | + The frequency in each row or column |
| 531 | + bispectrum: 2-d `np.array` |
| 532 | + The unnormalized bispectrum |
| 533 | + bicoherence: 2-d `np.array` |
| 534 | + The bicoherence, calculated with the normalization from |
| 535 | + Kim & Powers (1979), IEEE Trans. Plasma Sci., PS-7, 120 |
| 536 | + """ |
| 537 | + dt = lightcurve.dt |
| 538 | + n_bin = np.rint(segment_size / dt).astype(int) |
| 539 | + |
| 540 | + flux_iterable = get_flux_iterable_from_segments( |
| 541 | + lightcurve.time, lightcurve.gti, segment_size, |
| 542 | + n_bin, fluxes=lightcurve.counts |
| 543 | + ) |
| 544 | + |
| 545 | + return bispectrum_and_bicoherence_from_iterable(flux_iterable, dt) |
0 commit comments