def townsend_lombscargle_value(times, mags, omega):
cos_omegat = npcos(omega*times)<EOL>sin_omegat = npsin(omega*times)<EOL>xc = npsum(mags*cos_omegat)<EOL>xs = npsum(mags*sin_omegat)<EOL>cc = npsum(cos_omegat*cos_omegat)<EOL>ss = npsum(sin_omegat*sin_omegat)<EOL>cs = npsum(cos_omegat*sin_omegat)<EOL>tau = nparctan(<NUM_LIT:2>*cs/(cc - ss))/(<NUM_LIT:2>*omega)<EOL>ctau = npcos(omega*tau)<EOL>stau = npsin(omega*tau)<EOL>leftsumtop = (ctau*xc + stau*xs)*(ctau*xc + stau*xs)<EOL>leftsumbot = ctau*ctau*cc + <NUM_LIT>*ctau*stau*cs + stau*stau*ss<EOL>leftsum = leftsumtop/leftsumbot<EOL>rightsumtop = (ctau*xs - stau*xc)*(ctau*xs - stau*xc)<EOL>rightsumbot = ctau*ctau*ss - <NUM_LIT>*ctau*stau*cs + stau*stau*cc<EOL>rightsum = rightsumtop/rightsumbot<EOL>pval = <NUM_LIT:0.5>*(leftsum + rightsum)<EOL>return pval<EOL>
This calculates the periodogram value for each omega (= 2*pi*f). Mags must
be normalized to zero with variance scaled to unity.