-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrcf
More file actions
executable file
·99 lines (69 loc) · 2.09 KB
/
rcf
File metadata and controls
executable file
·99 lines (69 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
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
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
#!/usr/bin/perl -w
# RCF.pl - jzu@free.fr 2010 - WTFPL
# Computes the third value of a first-order RC filter, given two of them
# Example: 0.000001 Farad (1u) and 1000 Ohm (1k) give about 159 (Hz)
use strict;
my %h = ("-12" => "p", # pico
"-9" => "n", # nano
"-6" => "u", # micro
"-3" => "m", # milli
"0" => "", #
"3" => "k", # kilo
"6" => "M" # mega
);
# Function expansion
# Takes a number + optional 10^3 multiplier (Mkmunp)
# Issues a float
sub expansion($) {
my $value = shift;
my $unit;
($value =~ m/[0-9]$/) and
return $value;
$unit = $value;
$unit =~ s/.*(.)$/$1/;
chop $value;
($unit eq 'M') and $value *= 1000000;
($unit eq 'k') and $value *= 1000;
($unit eq 'm') and $value /= 1000;
($unit eq 'u') and $value /= 1000000;
($unit eq 'n') and $value /= 1000000000;
($unit eq 'p') and $value /= 1000000000000;
return $value;
}
# Function fmt
# Takes a float on input
# Issues a rounded number (2 significant digits) + multiplier (Mkmunp)
sub fmt ($) {
my $value = shift;
my ($sci, $rnd, $norm, $i, $j);
# First, determine the magnitude of the number
for ($i = 6; $i > -13; $i--) {
if (int ($value / (10**$i)) > 0) {
$sci = sprintf ("%.1f*10**$i", $value / (10**$i));
last;
}
}
$rnd = eval $sci;
# Then, round the number accordingly, and prepare for 10^3 multipliers
for ($j = 6; $j > -13; $j -= 3) {
if (int ($rnd / (10**$j)) > 0) {
if ($i % 3 == 0) {
$norm = sprintf ("%.1f%s", $rnd/(10**$j), $h{$j});
}
else {
$norm = sprintf ("%.0f%s", $rnd/(10**$j), $h{$j});
}
return $norm;
}
}
}
# Main code : F=1/(2PiRC), R=1/(2PiFC), C=1/(2PiRF)
if ((defined $ARGV [0]) and
(defined $ARGV [1]) and
($ARGV [0] =~ /^(\d+\.?\d*|\.\d+)[Mkmunp]?$/) and
($ARGV [1] =~ /^(\d+\.?\d*|\.\d+)[Mkmunp]?$/)) {
my $result = 1 / (6.283 * expansion ($ARGV [0]) * expansion ($ARGV [1]));
print $result . " (" . fmt ($result) . ")\n";
exit 0;
}
print STDERR "Usage: rcf num[Mkmunp] num[Mkmunp] \n";