<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 CrossCorrelation
This Tutorial is intended to give a demostration of How to make a CrossCorrelation Object in Stingray Library.
Step1:  CrossCorrelation Example
1. Create two light curves
There are two ways to create a Lightcurve.<br>
1) Using an array of time stamps and an array of counts.<br>
2) From the Photon Arrival times.
In this example, Lightcurve is created using arrays of time stamps and counts.
Generate an array of relative timestamps that's 10 seconds long, with dt = 0.03125 s, and make two signals in units of counts. The signal is a sine wave with amplitude = 300 cts/s, frequency = 2 Hz, phase offset of pi/2 radians, and mean = 1000 cts/s. We then add Poisson noise to the light curve.
Step2:  Now let's turn noisy_1 and noisy_2 into Lightcurve objects. This way we have two Lightcurves to calculate CrossCorrelation.
Step3:  2. Create a CrossCorrelation Object from two Light curves created above
To create a CrossCorrelation Object from LightCurves, simply pass both Lightvurves created above into the CrossCorrelation.
Step4:  Now, Cross Correlation values are stored in attribute corr, which is called below.
Step5:  3. Plot Cross Correlation for Different lags
To visulaize correlation for different values of time lags, simply call plot function on cs.
Step6:  Given the Phase offset of pi/2 between two lightcurves created above, and freq=1 Hz, time_shift should be close to 0.25 sec. Small error is due to time resolution.
Step7:  Modes of Correlation
You can also specify an optional argument on modes of cross-correlation. <br>
There are three modes 
Step8:  The number of data points in corr and largest lightcurve are same in this mode.
Step9:  Creating CrossCorrelation with full mode now using same data as above.
Step10:  Full mode does a full cross-correlation.
Step11:  Another Example
You can also create CrossCorrelation Object by using Cross Correlation data. This can be useful in some cases when you have correlation data and want to calculate time shift for max. correlation. You need to specify time resolution for correlation(default value of 1.0 seconds is used otherwise).
Step12:  Yet another Example with longer Lingcurve
I will be using same lightcurves as in the example above but with much longer duration and shorter lags.<br>
Both Lightcurves are chosen to be more or less same with a certain phase shift to demonstrate Correlation in a better way.
Again Generating two signals this time without poission noise so that time lag can be demonstrated. For noisy lightcurves, accurate calculation requires interpolation.
Step13:  Converting noisy signals into Lightcurves.
Step14:  Now, creating CrossCorrelation Object by passing lc1 and lc2 into the constructor.
Step15:  time_shift is very close to 0.25 sec, in this case.
AutoCorrelation
Stingray has also separate class for AutoCorrelation. AutoCorrealtion is similar to crosscorrelation but involves only One Lightcurve.i.e. Correlation of Lightcurve with itself.
AutoCorrelation is part of stingray.crosscorrelation module. Following line imports AutoCorrelation.
Step16:  To create AutoCorrelation object, simply pass lightcurve into AutoCorrelation Constructor.<br> Using same Lighrcurve created above to demonstrate AutoCorrelation.
Step17:  time_Shift for AutoCorrelation is always zero. Since signals are maximally correlated at zero lag.
Step18:  Another Example
Another example is demonstrated using a Lightcurve with Poisson Noise.
Step19:  AutoCorrelation also supports {full,same,valid} modes similar to CrossCorrelation
<ASSISTANT_TASK:>
Python Code:
import numpy as np
from stingray import Lightcurve
from stingray.crosscorrelation import CrossCorrelation
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager
%matplotlib inline
font_prop = font_manager.FontProperties(size=16)
Explanation: CrossCorrelation
This Tutorial is intended to give a demostration of How to make a CrossCorrelation Object in Stingray Library.
End of explanation
dt = 0.03125  # seconds
exposure = 10.  # seconds
freq = 1   # Hz
times = np.arange(0, exposure, dt)  # seconds
signal_1 = 300 * np.sin(2.*np.pi*freq*times) + 1000  # counts/s
signal_2 = 300 * np.sin(2.*np.pi*freq*times + np.pi/2) + 1000  # counts/s
noisy_1 = np.random.poisson(signal_1*dt)  # counts
noisy_2 = np.random.poisson(signal_2*dt)  # counts
Explanation: CrossCorrelation Example
1. Create two light curves
There are two ways to create a Lightcurve.<br>
1) Using an array of time stamps and an array of counts.<br>
2) From the Photon Arrival times.
In this example, Lightcurve is created using arrays of time stamps and counts.
Generate an array of relative timestamps that's 10 seconds long, with dt = 0.03125 s, and make two signals in units of counts. The signal is a sine wave with amplitude = 300 cts/s, frequency = 2 Hz, phase offset of pi/2 radians, and mean = 1000 cts/s. We then add Poisson noise to the light curve.
End of explanation
lc1 = Lightcurve(times, noisy_1)
lc2 = Lightcurve(times, noisy_2)
len(lc1)
fig, ax = plt.subplots(1,1,figsize=(10,6))
ax.plot(lc1.time, lc1.counts, lw=2, color='blue')
ax.plot(lc1.time, lc2.counts, lw=2, color='red')
ax.set_xlabel("Time (s)", fontproperties=font_prop)
ax.set_ylabel("Counts (cts)", fontproperties=font_prop)
ax.tick_params(axis='x', labelsize=16)
ax.tick_params(axis='y', labelsize=16)
ax.tick_params(which='major', width=1.5, length=7)
ax.tick_params(which='minor', width=1.5, length=4)
plt.show()
Explanation: Now let's turn noisy_1 and noisy_2 into Lightcurve objects. This way we have two Lightcurves to calculate CrossCorrelation.
End of explanation
cr = CrossCorrelation(lc1, lc2)
Explanation: 2. Create a CrossCorrelation Object from two Light curves created above
To create a CrossCorrelation Object from LightCurves, simply pass both Lightvurves created above into the CrossCorrelation.
End of explanation
cr.corr[:10]
# Time Resolution for Cross Correlation is same as that of each of the Lightcurves
cr.dt
Explanation: Now, Cross Correlation values are stored in attribute corr, which is called below.
End of explanation
cr.plot(labels = ['Time Lags (seconds)','Correlation'])
Explanation: 3. Plot Cross Correlation for Different lags
To visulaize correlation for different values of time lags, simply call plot function on cs.
End of explanation
cr.time_shift #seconds
Explanation: Given the Phase offset of pi/2 between two lightcurves created above, and freq=1 Hz, time_shift should be close to 0.25 sec. Small error is due to time resolution.
End of explanation
cr.mode
Explanation: Modes of Correlation
You can also specify an optional argument on modes of cross-correlation. <br>
There are three modes : 1) same   2) valid   3) full 
Visit following ink on more details on mode : https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.signal.correlate.html
Default mode is 'same' and it gives output equal to the size of larger lightcurve and is most common in astronomy. You can see mode of your CrossCorrelation by calling mode attribute on the object.
End of explanation
cr.n
Explanation: The number of data points in corr and largest lightcurve are same in this mode.
End of explanation
cr1 = CrossCorrelation(lc1, lc2, mode = 'full') 
cr1.plot()
cr1.mode
Explanation: Creating CrossCorrelation with full mode now using same data as above.
End of explanation
cr1.n
Explanation: Full mode does a full cross-correlation.
End of explanation
cs = CrossCorrelation()
cs.corr = np.array([ 660,  1790,  3026,  4019,  5164,  6647,  8105,  7023, 6012, 5162])
time_shift, time_lags, n = cs.cal_timeshift(dt=0.5)
time_shift
cs.plot( ['Time Lags (seconds)','Correlation'])
Explanation: Another Example
You can also create CrossCorrelation Object by using Cross Correlation data. This can be useful in some cases when you have correlation data and want to calculate time shift for max. correlation. You need to specify time resolution for correlation(default value of 1.0 seconds is used otherwise).
End of explanation
dt = 0.0001  # seconds
exposure = 50.  # seconds
freq = 1       # Hz
times = np.arange(0, exposure, dt)  # seconds
signal_1 = 300 * np.sin(2.*np.pi*freq*times) + 1000 * dt # counts/s
signal_2 = 200 * np.sin(2.*np.pi*freq*times + np.pi/2) + 900 * dt  # counts/s
Explanation: Yet another Example with longer Lingcurve
I will be using same lightcurves as in the example above but with much longer duration and shorter lags.<br>
Both Lightcurves are chosen to be more or less same with a certain phase shift to demonstrate Correlation in a better way.
Again Generating two signals this time without poission noise so that time lag can be demonstrated. For noisy lightcurves, accurate calculation requires interpolation.
End of explanation
lc1 = Lightcurve(times, signal_1)
lc2 = Lightcurve(times, signal_2)
len(lc1)
fig, ax = plt.subplots(1,1,figsize=(10,6))
ax.plot(lc1.time, lc1.counts, lw=2, color='blue')
ax.plot(lc1.time, lc2.counts, lw=2, color='red')
ax.set_xlabel("Time (s)", fontproperties=font_prop)
ax.set_ylabel("Counts (cts)", fontproperties=font_prop)
ax.tick_params(axis='x', labelsize=16)
ax.tick_params(axis='y', labelsize=16)
ax.tick_params(which='major', width=1.5, length=7)
ax.tick_params(which='minor', width=1.5, length=4)
plt.show()
Explanation: Converting noisy signals into Lightcurves.
End of explanation
cs = CrossCorrelation(lc1, lc2)
print('Done')
cs.corr[:50]
# Time Resolution for Cross Correlation is same as that of each of the Lightcurves
cs.dt
cs.plot( ['Time Lags (seconds)','Correlation'])
cs.time_shift #seconds
Explanation: Now, creating CrossCorrelation Object by passing lc1 and lc2 into the constructor.
End of explanation
from stingray.crosscorrelation import AutoCorrelation
Explanation: time_shift is very close to 0.25 sec, in this case.
AutoCorrelation
Stingray has also separate class for AutoCorrelation. AutoCorrealtion is similar to crosscorrelation but involves only One Lightcurve.i.e. Correlation of Lightcurve with itself.
AutoCorrelation is part of stingray.crosscorrelation module. Following line imports AutoCorrelation.
End of explanation
lc = lc1
ac = AutoCorrelation(lc)
ac.n
ac.corr[:10]
ac.time_lags
Explanation: To create AutoCorrelation object, simply pass lightcurve into AutoCorrelation Constructor.<br> Using same Lighrcurve created above to demonstrate AutoCorrelation.
End of explanation
ac.time_shift
ac.plot()
Explanation: time_Shift for AutoCorrelation is always zero. Since signals are maximally correlated at zero lag.
End of explanation
dt = 0.001  # seconds
exposure = 20.  # seconds
freq = 1   # Hz
times = np.arange(0, exposure, dt)  # seconds
signal_1 = 300 * np.sin(2.*np.pi*freq*times) + 1000  # counts/s
noisy_1 = np.random.poisson(signal_1*dt)  # counts
lc = Lightcurve(times, noisy_1)
Explanation: Another Example
Another example is demonstrated using a Lightcurve with Poisson Noise.
End of explanation
ac = AutoCorrelation(lc, mode = 'full')
ac.corr
ac.time_lags
ac.time_shift
ac.plot()
Explanation: AutoCorrelation also supports {full,same,valid} modes similar to CrossCorrelation
End of explanation
<END_TASK>