Module: NumRu::GAnalysis::LombScargle

Defined in:
lib/numru/ganalysis/lomb_scargle.rb

Class Method Summary collapse

Class Method Details

.derive_phase(t, f, dim, rank, mask = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/numru/ganalysis/lomb_scargle.rb', line 58

def derive_phase(t,f,dim,rank,mask=nil)
  multiple_f = !f.is_a?(Numeric)
  if multiple_f
    o2t = 4*Math::PI * f.newdim(0) * t.newdim(-1)  # 2 omega t
    mask = mask.newdim(-1) if mask
    nf = f.length
  else
    o2t = 4*Math::PI * f * t  # 2 omega t
  end
  with_miss = (mask && mask.count_false > 0)
  if dim >= 1
    o2t = o2t.newdim!( *([0]*dim) )
  end
  if dim < rank-1
    o2t = o2t.newdim!( *([dim+1]*(rank-1-dim)) )
  end
  if with_miss
    # sampling is not common for all grid points
    expander = NArray.float( *mask.shape )
    o2t += expander    # duplicates o2t for all grid points
    if multiple_f
      expander = NArray.byte(*([1]*rank+[nf])) # duplicates mask for all f
      mask += expander
    end
    o2t = NArrayMiss.to_nam(o2t,mask)
  end
  ph = 0.5* Misc::EMath::atan2( Misc::EMath.sin(o2t).sum(dim),
                               -Misc::EMath.cos(o2t).sum(dim)).newdim(dim)
  ot_ph = o2t*0.5 + ph  # omega*t + ph
  [ph, ot_ph]
end

.lomb_scargle(y, dim, t, f) ⇒ Object

Lomb-Scargle periodgram

ARGUMENTS

  • y [NArray or NArrayMiss] : data

  • dim [Integer] : the dimension to apply LS

  • t [1D NArray] : “time” (the coordinate values along dim)

  • f [Numric or 1D NArray] : frequency. If 1D NArray, the frequency dimension is added to the end of outputs

OUTPUTS

  • a : coef of cos

  • b : coef of sin (sqrt(a**2+b**2) is the amplitude)

  • reconst : reconstructed y (fitting result)

  • cos [NArray or NArrayMiss]: cos(2*pi*f*t + ph) (useful if you want to reconstruct for a part of f)

  • sin [NArray or NArrayMiss]: sin(2*pi*f*t + ph) (useful if you want to reconstruct for a part of f)

  • cc [NArray or NArrayMiss]: normarization factor sum(cos**2) (needed to derive power spectrum)

  • ss [NArray or NArrayMiss]: normarization factor sum(sin**2) (needed to derive power spectrum)

  • ph [NArray or NArrayMiss]: phase (for each f and, if with miss, for each grid point)



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/numru/ganalysis/lomb_scargle.rb', line 36

def lomb_scargle(y, dim, t, f)
  rank = y.rank
  if y.respond_to?(:get_mask)
    mask = y.get_mask
  else
    mask = nil
  end
  ph, ot_ph = derive_phase(t,f,dim,rank,mask)
  cos = Misc::EMath.cos( ot_ph )
  sin = Misc::EMath.sin( ot_ph )
  cc = (cos*cos).sum(dim)
  ss = (sin*sin).sum(dim)
  #p "%%%", (cos*sin).sum(dim), cc, ss
  if !f.is_a?(Numeric)
    y = y.newdim(-1)
  end
  a = (y*cos).sum(dim) / cc
  b = (y*sin).sum(dim) / ss
  reconst = (a.newdim(-2)*cos).sum(-1) + (b.newdim(-2)*sin).sum(-1)
  [a,b,reconst,cos,sin,cc,ss,ph]
end