You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This will print the SNR of the frequencies at every stage of the prewhitening (including the process of refining the frequency grid). The console output will look similar to
The function stopcrit_scargle_snr below is meant to return True or False depending on whether the stopping criterion is met or not. As the function's name suggests, the halt happens when the SNR value drops below crit_value and the function returns the tuple(True,value).
defstopcrit_scargle_snr(times,signal,modelfunc,allparams,pergram,crit_value,width=6.):
""" Stop criterium based on signal-to-noise ratio. """width=width/2.argmax=np.argmax(pergram[1]) #1ampls=pergram[1]
start=max(0,pergram[0][argmax]-width)
stop=min(pergram[0][argmax]+width,pergram[0][-1])
ifstart==0:
stop+=width-pergram[0][argmax]
ifstop==pergram[0][-1]:
start=pergram[0][-1]-pergram[0][argmax]+widthampls=ampls[(start<=pergram[0]) & (pergram[0]<=stop)]
value=pergram[1][argmax]/ampls.mean()
returnvalue<crit_value,value
The problem is, at each iteration, the frequency ν returned by iterative_prewhitening(prewhiteningorder_snr=True) is such that maximizes the SNR in the periodogram, while the frequency ν' used by stopcrit_scargle_snr to compute the SNR is such that maximizes the amplitudein the periodogram (see #1 above).
This implies that ν is not always equal to ν' and, therefore, the SNR in the output does not necessarily correspond to the associated frequency in the same output. For instance, the frequencies stored in params['freq'] in the example above do not necessarily relate to the SNR stored in params['stopcrit'].
Solution
We have to modify the function stopcrit_scargle_snr so that it calculates the SNR of the frequency selected by the prewhitening process and not always default to the frequency with the highest periodogram's amplitude.
Below is an example of such new code:
defNEW_stopcrit_scargle_snr(times,signal,modelfunc,allparams,pergram,crit_value,width=6.):
""" Stop criterium based on signal-to-noise ratio. """width=width/2.freqs=pergram[0] # frequency gridampls=pergram[1] # periodogram's amplitudesfreq=allparams['freq'][-1] # Last frequency in the iterative prewhiteningind=np.abs(freqs-freq).argmin() # Get grid's indice closest to the frequency# Clip the frequency gridstart=max(0,freqs[ind]-width)
stop=min(freqs[ind]+width,freqs[-1])
ifstart==0:
stop+=width-freqs[ind]
ifstop==freqs[-1]:
start=freqs[-1]-freqs[ind]+widthclipped_ampls=ampls[(start<=freqs) & (freqs<=stop)]
value=ampls[ind]/clipped_ampls.mean()
returnvalue<crit_value,value
Testing the solution
We will just repeat the code in section Example using NEW_stopcrit_scargle_snr instead of stopcrit_scargle_snr.
Note as well that this time the prewhitening returns 88 frequencies instead of 124.
Final note <--------------------------
Even if the parameter prewhiteningorder_snr=True is used, the extracted frequencies will not necessarily come sorted by SNR, as it can be seen in the output for params['stopcrit'] above. I think this happens because the noise level is calculated with the residuals after each iteration, meaning that subsequent frequencies may have their SNR increased because the windows function of the previous frequency has been removed.
I may have stumbled on the same bug Siemen and Dominic brought up during the STARBUCKS meetings and decided to post it here.
The bug shows up when the following two setups are used together in the prewhitening function
iterative_prewhitening
:prewhiteningorder_snr=True
.stopcrit_scargle_snr
.Example
The best way to visualize this issue is to print the SNR value at each step of the prewhitening by inserting the line of code
below line 276 in
find_frequency
(which is called byiterative_prewhitening
). The change should look likeNow, load a light curve of choice or use the one attached here:
Finally, run the code below:
This will print the SNR of the frequencies at every stage of the prewhitening (including the process of refining the frequency grid). The console output will look similar to
This shows:
params['stopcrit']
Explanation
The function
stopcrit_scargle_snr
below is meant to returnTrue
orFalse
depending on whether the stopping criterion is met or not. As the function's name suggests, the halt happens when the SNRvalue
drops belowcrit_value
and the function returns the tuple(True,value)
.The problem is, at each iteration, the frequency ν returned by
iterative_prewhitening(prewhiteningorder_snr=True)
is such that maximizes the SNR in the periodogram, while the frequency ν' used bystopcrit_scargle_snr
to compute the SNR is such that maximizes the amplitude in the periodogram (see#1
above).This implies that ν is not always equal to ν' and, therefore, the SNR in the output does not necessarily correspond to the associated frequency in the same output. For instance, the frequencies stored in
params['freq']
in the example above do not necessarily relate to the SNR stored inparams['stopcrit']
.Solution
We have to modify the function
stopcrit_scargle_snr
so that it calculates the SNR of the frequency selected by the prewhitening process and not always default to the frequency with the highest periodogram's amplitude.Below is an example of such new code:
Testing the solution
We will just repeat the code in section Example using
NEW_stopcrit_scargle_snr
instead ofstopcrit_scargle_snr
.the console output will look similar to
This shows:
params['stopcrit']
Note as well that this time the prewhitening returns 88 frequencies instead of 124.
Final note <--------------------------
Even if the parameter
prewhiteningorder_snr=True
is used, the extracted frequencies will not necessarily come sorted by SNR, as it can be seen in the output forparams['stopcrit']
above. I think this happens because the noise level is calculated with the residuals after each iteration, meaning that subsequent frequencies may have their SNR increased because the windows function of the previous frequency has been removed.Useful links
iterative_prewhitening
stopcrit_scargle_snr
lc_tic374944608.zip
The text was updated successfully, but these errors were encountered: