Class: PWN::SDR::Decoder::GSM::DemodIQ

Inherits:
Object
  • Object
show all
Defined in:
lib/pwn/sdr/decoder/gsm.rb

Overview

Streaming I/Q energy/burst demod for Base.run_iq.

Instance Method Summary collapse

Constructor Details

#initialize(rate:, protocol:, modulation:, extra: {}) ⇒ DemodIQ

Returns a new instance of DemodIQ.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/pwn/sdr/decoder/gsm.rb', line 10

def initialize(rate:, protocol:, modulation:, extra: {})
  @rate = rate.to_f
  @protocol = protocol
  @modulation = modulation
  @extra = extra
  @floor = nil
  @in_burst = false
  @burst_t0 = nil
  @peak = -200.0
  @burst_n = 0
  @threshold = (extra[:threshold] || 8.0).to_f
  @carry = []
end

Instance Method Details

#feed_iq(samples, rate: nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/pwn/sdr/decoder/gsm.rb', line 24

def feed_iq(samples, rate: nil, &)
  @rate = rate.to_f if rate
  m2 = PWN::SDR::Decoder::DSP.mag_sq(iq: samples)
  # process in ~1 ms hops
  hop = [(@rate / 1000.0).round, 1].max
  i = 0
  while i < m2.length
    win = m2[i, hop]
    break if win.nil? || win.empty?

    ms = win.sum / win.length
    lvl = ms.positive? ? (10.0 * Math.log10(ms)) : -120.0
    @floor = @floor.nil? ? lvl : ((@floor * 0.98) + (lvl * 0.02))
    delta = lvl - @floor
    if delta >= @threshold
      unless @in_burst
        @in_burst = true
        @burst_t0 = Time.now
        @peak = lvl
      end
      @peak = lvl if lvl > @peak
    elsif @in_burst
      @in_burst = false
      @burst_n += 1
      dur_ms = ((Time.now - @burst_t0) * 1000).round
      msg = {
        protocol: @protocol, event: 'burst', source: 'iq',
        burst_no: @burst_n, peak_dbfs: @peak.round(1),
        floor_dbfs: @floor.round(1), delta_db: (@peak - @floor).round(1),
        duration_ms: dur_ms, modulation: @modulation,
        sample_rate: @rate.to_i
      }.merge(@extra)
      msg[:summary] = format(
        '%<p>s IQ-burst #%<n>d peak=%<pk>+.1f dBFS Δ=%<d>.1f dB dur=%<ms>d ms',
        p: @protocol, n: @burst_n, pk: @peak, d: @peak - @floor, ms: dur_ms
      )
      yield msg if block_given?
    end
    i += hop
  end
end