Created
November 1, 2011 08:15
-
-
Save opamp/1330136 to your computer and use it in GitHub Desktop.
デシベルと比を変換する簡易スクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
#-*- encoding:utf-8 -*- | |
# 1.9じゃないとちゃんと動かないようだよ。 | |
if ARGV.size != 2 then | |
puts "dBconv -{n|v|w} n[dB|x]" | |
exit 0 | |
end | |
dB = 0 | |
nx = 0 | |
todB = false | |
m = 'n' # or v or w | |
ARGV.each do |x| | |
if x =~ /dB$/ then | |
dB = x[0..(x.size() - 3)].to_f | |
if dB <= 0 then | |
puts "input number is invalid." | |
exit 1 | |
end | |
todB = false | |
end | |
if x=~ /x$/ then | |
nx = x[0..(x.size() -2)].to_f | |
todB = true | |
end | |
if x=~ /^-/ then | |
m = 'n' if x[1] == 'n' | |
m = 'v' if x[1] == 'v' | |
m = 'w' if x[1] == 'w' | |
end | |
end | |
if todB == true then | |
dB = Math::log10 nx | |
if m == 'n' then | |
puts "=#{dB}" | |
elsif m == 'v' then | |
puts "=#{dB*20}" | |
elsif m == 'w' then | |
puts "=#{dB*10}" | |
end | |
else | |
if m == 'n' then | |
nx = 10.0 ** dB | |
elsif m == 'v' then | |
nx = 10.0 ** (dB / 20) | |
elsif m == 'w' then | |
nx = 10.0 ** (dB / 10) | |
end | |
puts "=#{nx}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment