Class: CW::Read
- Inherits:
-
Object
- Object
- CW::Read
- Defined in:
- lib/cw/read.rb
Instance Method Summary collapse
- #calc_coeff(data) ⇒ Object
- #calc_filtered_state ⇒ Object
- #calc_real_state ⇒ Object
- #coeff ⇒ Object
- #cosine ⇒ Object
- #dbg_print(message) ⇒ Object
- #decode_signal ⇒ Object
- #high_avg_compare?(period, avg_x_low, avg_x_high) ⇒ Boolean
-
#initialize(filename) ⇒ Read
constructor
A new instance of Read.
- #input ⇒ Object
- #k ⇒ Object
- #magnitude_filter ⇒ Object
- #n ⇒ Object
- #n_delay_ms ⇒ Object
- #noise_blanked ⇒ Object
- #open_sound_device ⇒ Object
- #per_block_processing ⇒ Object
- #print ⇒ Object
-
#print_char ⇒ Object
def matched_char @cw_encoding.fetch_char @code end.
- #print_space ⇒ Object
- #real_state_change? ⇒ Boolean
- #reset_noise_blanker ⇒ Object
- #store_real_state ⇒ Object
- #w ⇒ Object
Constructor Details
#initialize(filename) ⇒ Read
Returns a new instance of Read.
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 65 66 67 68 69 70 |
# File 'lib/cw/read.rb', line 29 def initialize(filename) @tone = TONE @mag_max = 0 @mag_min = 999999999 @sample_rate = SAMPLE_RATE @n_val = n @coeff = coeff @n_delay_ms = n_delay_ms @filename = filename @code = [] @q1 = 0 @q2 = 0 @magnitude_set_point = 10000 @magnitude_set_point_low = @magnitude_set_point @wpm = 40 @noise_blanking_ms = 6 @last_start_time = 0 @state = Array.new(5) @high = Array.new(3) @low = Array.new(2) @queue = Queue.new @cw_encoding = Encoding.new @print = Print.new @state[START] = 0 @state[VALUE] = :low @state[BLANKING] = false @state[FILTERED] = false @state[PREV] = false @high[START] = 0 @high[PERIOD] = 0 @high[AVG] = 120 @low[START] = 0 @low[PERIOD] = 0 @low[AVG] = 0 @millisecs = 0 @last = 0 @need_space = false puts "@n_val #{@n_val}" puts "@coeff #{@coeff}" puts "@n_delay_ms #{@n_delay_ms}" input end |
Instance Method Details
#calc_coeff(data) ⇒ Object
253 254 255 256 |
# File 'lib/cw/read.rb', line 253 def calc_coeff(data) q0 = @coeff * @q1 - @q2 + data @q2, @q1 = @q1, q0 end |
#calc_filtered_state ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/cw/read.rb', line 166 def calc_filtered_state if real_state_change? reset_noise_blanker end store_real_state if @state[BLANKING] if noise_blanked @state[FILTERED] = true if(@state[VALUE] == :high) @high[START] = @millisecs @high_mag = @magnitude # dbg_print "high: #{@high_mag}\n low: #{@low_mag}\n set point: #{@magnitude_set_point}" @low[PERIOD] = (@millisecs - @low[START]) else @low[START] = @millisecs; @low_mag = @magnitude @high[PERIOD] = @millisecs - @high[START] if @high[PERIOD] < (2 * @high[AVG]) @high[AVG] = ((@high[PERIOD] + @high[AVG] + @high[AVG]) / 3) # now we know avg dit time ( rolling 3 avg) elsif(@high[PERIOD] > (5 * @high[AVG])) @high[AVG] = @high[PERIOD] + @high[AVG]; # if speed decrease fast .. end end end end end |
#calc_real_state ⇒ Object
160 161 162 163 164 |
# File 'lib/cw/read.rb', line 160 def calc_real_state @state[VALUE] = (@magnitude > (@magnitude_set_point * 0.6)) ? :high : :low end |
#coeff ⇒ Object
25 |
# File 'lib/cw/read.rb', line 25 def coeff ; 2 * cosine ; end |
#cosine ⇒ Object
24 |
# File 'lib/cw/read.rb', line 24 def cosine ; Math.cos(w) ; end |
#dbg_print(message) ⇒ Object
121 122 123 124 125 126 127 128 129 |
# File 'lib/cw/read.rb', line 121 def dbg_print if @millisecs > @last @last = @millisecs + 1000 puts puts " " + @mag_min = 999999999 @mag_max = 0 end end |
#decode_signal ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/cw/read.rb', line 195 def decode_signal if(@state[FILTERED]) @state[FILTERED] = false @need_space = true #dbg_print "wpm #{@wpm.to_s}\nhigh period: #{@high[PERIOD]}\nhigh avg: #{@high[AVG]}" if(@state[VALUE] == :low) # we did end a HIGH if high_avg_compare?(@high[PERIOD], 0.6, 2.0) # 0.6 filter out false dits @code << :dot # $stdout.print '.' end if high_avg_compare?(@high[PERIOD], 2.0, 6.0) @code << :dash # $stdout.print '_' if @millisecs % 10 == 0 @wpm = (@wpm + (1200 / ((@high[PERIOD]) / 3))) / 2; # the most precise we can do ;o) # dbg_print "high #{@high[PERIOD]}" end end else # we did end a LOW @need_space = false if high_avg_compare?(@low[PERIOD], 2.0, 4.8) # letter space print_char elsif(high_avg_compare?(@low[PERIOD], 4.8, 6.0)) # word space print_char $stdout.print ' ' end end end # dbg_print "millisecs #{@millisecs}" if @need_space if high_avg_compare?(@millisecs - @low[START], 6.0, 10) @need_space = false if @state[BLANKING] = false end print_char $stdout.print ' ' end end end |
#high_avg_compare?(period, avg_x_low, avg_x_high) ⇒ Boolean
236 237 238 239 |
# File 'lib/cw/read.rb', line 236 def high_avg_compare?(period, avg_x_low, avg_x_high) (period <= (@high[AVG] * avg_x_high).to_i) && (period >= (@high[AVG] * avg_x_low).to_i) end |
#input ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/cw/read.rb', line 83 def input dly = @n_delay_ms open_sound_device nval = @n_val buf = @buf_in bufs = nil block_size = nval * 28 thr = Thread.fork do loop do @queue.push buf.read(block_size) end end loop do loop do bufs = @queue.pop break if @queue.empty? sleep 0.001 end count = 0 28.times do nval.times do temp = bufs[count] + bufs[count + 1] # puts temp calc_coeff temp count += 2 end @millisecs += dly # @millisecs += dly per_block_processing calc_real_state calc_filtered_state decode_signal end end @buf_in.stop $stdout.puts "done." end |
#k ⇒ Object
22 |
# File 'lib/cw/read.rb', line 22 def k ; (0.5 + ((n * @tone / @sample_rate))).to_i ; end |
#magnitude_filter ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/cw/read.rb', line 144 def magnitude_filter if(@magnitude > @magnitude_set_point_low) @magnitude_set_point = (@magnitude_set_point + ((@magnitude - @magnitude_set_point) / 6)) # moving average filter else @magnitude_set_point = @magnitude_set_point_low end # dbg_print "@magnitude: #{@magnitude.to_s}\n @magnitude_set_point: #{@magnitude_set_point.to_s}\n @magnitude_set_point_low = #{@magnitude_set_point_low}" @mag_max = @magnitude if @magnitude > @mag_max @mag_min = @magnitude if @magnitude < @mag_min # dbg_print "@magnitude: #{@magnitude.to_s}\n" + # " @mag_max: #{@mag_max.to_s}\n" + # " @mag_min: #{@mag_min.to_s}\n" end |
#n ⇒ Object
21 |
# File 'lib/cw/read.rb', line 21 def n ; N ; end |
#n_delay_ms ⇒ Object
26 |
# File 'lib/cw/read.rb', line 26 def n_delay_ms ; n * 1000/SAMPLE_RATE; end |
#noise_blanked ⇒ Object
241 242 243 244 245 246 |
# File 'lib/cw/read.rb', line 241 def noise_blanked if((@millisecs - @state[START]) > @noise_blanking_ms) @state[BLANKING] = false return true end end |
#open_sound_device ⇒ Object
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/cw/read.rb', line 72 def open_sound_device soundflower = nil CoreAudio.devices.each do |device| if device.name == 'Soundflower (2ch)' soundflower = device end end @buf_in = soundflower.input_buffer(44100) @buf_in.start end |
#per_block_processing ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/cw/read.rb', line 131 def per_block_processing @magnitude = (@q1 * @q1) + (@q2 * @q2) - @q1 * @q2 * @coeff @magnitude = @magnitude.to_i / 1000000 @magnitude = 0 if @magnitude < MAGNITUDE_CUTOFF # if @magnitude >= 1000000000000.0 # @magnitude = Math.sqrt(magnitude_squared).to_i # else # @magnitude = 0 # end @q1, @q2 = 0, 0; # p @magnitude end |
#print ⇒ Object
27 |
# File 'lib/cw/read.rb', line 27 def print ; @print ; end |
#print_char ⇒ Object
def matched_char
@cw_encoding.fetch_char @code
end
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/cw/read.rb', line 279 def print_char char = @cw_encoding.fetch_char @code @code = [] print.optimum char # if char == ' ' # puts "\n high: #{@high_mag}\n" # end if false # char == '*' # @wpm -= 5 @state[VALUE] = false @state[FILTERED] = false @state[PREV] = false # @awaiting_space = false @low[PERIOD] = 0 @high[PERIOD] = 0 @high[PREV] = 0 @high[AVG] = 0 @low[START] = 0 @millisecs = 0 @last = 0 end end |
#print_space ⇒ Object
248 249 250 251 |
# File 'lib/cw/read.rb', line 248 def print_space @code = [:space] print_char end |
#real_state_change? ⇒ Boolean
263 264 265 266 267 268 269 |
# File 'lib/cw/read.rb', line 263 def real_state_change? # if @real_state != @real_state_prev # $stdout.print 'hi' if @real_state == true # $stdout.print 'low' if @real_state == false # end @state[VALUE] != @state[PREV] end |
#reset_noise_blanker ⇒ Object
258 259 260 261 |
# File 'lib/cw/read.rb', line 258 def reset_noise_blanker @state[BLANKING] = true @state[START] = @millisecs end |
#store_real_state ⇒ Object
271 272 273 |
# File 'lib/cw/read.rb', line 271 def store_real_state @state[PREV] = @state[VALUE] end |
#w ⇒ Object
23 |
# File 'lib/cw/read.rb', line 23 def w ; ((2 * Math::PI) / n) * k ; end |