Sha256: 0be57915eed002fe91a34da46005f4b9a463e3149f13dee774d447c93b493cdd

Contents?: true

Size: 970 Bytes

Versions: 1

Compression:

Stored size: 970 Bytes

Contents

class Radio
  
  class PSK31
    
    class BitDetect
      
      AVG_SAMPLES = 50.freeze
      CHANGE_DELAY = 5
      
      def initialize
        @averages = Array.new 21
        reset
      end
      
      def reset
        @averages.fill 0.0
        @phase = 0
        @peak = 0
        @next_peak = 0
        @change_at = 0
      end
      
      def call sample_x, sample_y
        yield if @phase == @peak
        @peak = @next_peak if @phase == @change_at
        energy = sample_x**2 + sample_y**2
        @averages[@phase] = (1.0-1.0/AVG_SAMPLES)*@averages[@phase] + (1.0/AVG_SAMPLES)*energy
        @phase += 1
        if @phase > 15
          @phase = 0
          max = -1e10
          for i in 0...16
            energy = @averages[i]
            if energy > max
              @next_peak = i
              @change_at = (i + CHANGE_DELAY) & 0x0F
              max = energy
            end
          end
        end
      end
      
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
radio-0.0.1 lib/radio/psk31/bit_detect.rb