Module: WavSpectrumAnalyzer::WaveLetApp

Extended by:
Common
Defined in:
lib/wavspa/wavlet/main.rb,
lib/wavspa/wavlet/preset.rb

Constant Summary collapse

PRESET_TABLE =
{
  "default" => {
    :sigma           => 24.0,
    :threshold       => 0.01,
    :unit_time       => 10 * 10,
    :scale_mode      => :LOGSCALE,
    :transform_mode  => :POWER,
    :output_width    => 240,
    :range           => [200.0, 8000.0],
    :ceil            => -10.0,
    :floor           => -90.0,
    :luminance       => 3.5,
    :col_step        => 1,
  },

  "cd" => {
    :sigma           => 24.0,
    :threshold       => 0.01,
    :unit_time       => 5 * 10,
    :scale_mode      => :LOGSCALE,
    :transform_mode  => :POWER,
    :output_width    => 480,
    :range           => [50.0, 22050.0],
    :ceil            => -10.0,
    :floor           => -90.0,
    :luminance       => 3.5,
    :col_step        => 1,
  },
}

Class Method Summary collapse

Methods included from Common

down_step, draw_freq_line, draw_time_line, fpos, linear_scaler, log_scaler, time_str, up_step

Class Method Details

.load_param(param) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/wavspa/wavlet/main.rb', line 21

def load_param(param)
  @transform_mode = param[:transform_mode]
  @unit_time      = param[:unit_time]
  @sigma          = param[:sigma]
  @threshold      = param[:threshold]
  @output_width   = param[:output_width]
                 
  @freq_range     = param[:range]
  @ceil           = param[:ceil]
  @floor          = param[:floor]
  @luminance      = param[:luminance]
  @col_step       = param[:col_step]
                 
  @scale_mode     = param[:scale_mode]
  @logscale       = (param[:scale_mode] == :LOGSCALE)
  @basis_freq     = param[:basis_freq] || 440.0 
  @grid_step      = param[:grid_step] || ((@logscale)? 2.0: 2000.0)
                 
  @lo_freq        = @freq_range[0]
  @hi_freq        = @freq_range[1]
  @freq_width     = (@hi_freq - @lo_freq)
                 
  @log_step       = (@hi_freq / @lo_freq) ** (1.0 / @output_width)
  @log_base       = Math.log(@log_step)
end

.main(input, param, output) ⇒ Object



48
49
50
51
52
53
54
55
56
57
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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/wavspa/wavlet/main.rb', line 48

def main(input, param, output)
  load_param(param)

  wav = WavFile.open(input)
  wl  = Wavelet.new

  wl.frequency       = wav.sample_rate
  wl.sigma           = @sigma
  wl.gabor_threshold = @threshold
  wl.range           = (@lo_freq .. @hi_freq)
  wl.scale_mode      = @scale_mode
  wl.width           = @output_width


  wl.put_in("s%dle" % wav.sample_size, wav.read)

  row   = 0
  usize = (wav.sample_rate * @unit_time) / 1000
  nblk  = (wav.data_size / wav.block_size) / usize

  fb    = FrameBuffer.new(nblk,
                          @output_width,
                          :column_step => @col_step,
                          :margin_x => ($draw_freq_line)? 50:0,
                          :margin_y => ($draw_time_line)? 30:0,
                          :ceil => @ceil,
                          :floor => @floor,
                          :luminance => @luminance)

  if $verbose
    STDERR.print <<~EOT
      - input data
        #{input}
          data size:       #{wav.data_size}
          channel num:     #{wav.channel_num}
          sample rate:     #{wav.sample_rate} Hz
          sample size:     #{wav.sample_size} bits
          data rate:       #{wav.bytes_per_sec} bytes/sec

      - WAVELET parameter
          sigma:           #{@sigma}
          gabor threshold: #{@threshold}
          unit time:       #{@unit_time} ms

      - OUTPUT
          width:           #{fb.width}px
          height:          #{fb.height}px
          freq range:      #{@lo_freq} - #{@hi_freq}Hz
          scale mode:      #{@scale_mode}
          plot mode :      #{@transform_mode}
          ceil:            #{@ceil}
          floor:           #{@floor}

    EOT
  end

  if wav.channel_num >= 2
    error("error: multi chanel data is not supported " \
          "(support only monoral data).")
  end
 
  until row >= nblk
    STDERR.printf("\rtransform #{row + 1}/#{nblk}", row) if $verbose
    wl.transform(row * usize)

    if @transform_mode == :POWER
      fb.draw_power(row, wl.power)
    else
      fb.draw_amplitude(row, wl.amplitude)
    end

    row += 1
  end

  STDERR.printf(" ... done\n") if $verbose

  STDERR.printf("write to #{output} ... ") if $verbose

  draw_freq_line(fb) if $draw_freq_line
  draw_time_line(fb, wav, usize) if $draw_time_line

  png = PNG.encode(fb.width, fb.height, fb.to_s, :pixel_format => :RGB)
  IO.binwrite(output, png)

  STDERR.printf("done\n") if $verbose
end