-
Notifications
You must be signed in to change notification settings - Fork 0
/
filters.py
104 lines (90 loc) · 3.15 KB
/
filters.py
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
99
100
101
#!/usr/bin/env python
# python
# Title: filters.py
#
# Author: Andrew Collins
#
# Description: Houses filter related functions.
#
# Non standard libraries used:
from .Resistors.resistor_vals import resistor_ratio
from math import *
__all__ = ['filtcalc']
# Function name: filtcalc
#
# Description: Calculates resistor values for an RC filter given a set of capacitor values,
# and a cutoff frequency.
#
# Inputs: none
#
# Outputs: none
#
def filtcalc() :
# Receive cap values from user in nano-farads
capstr = input("Please enter available capacitor values in nF (space separated): ")
# Receive resistor family string
family_str = input("Please enter the IEC family of resistors: ")
# Split these values into a list of integers
c = map(float, capstr.split(' '))
# Non-mapped cap array for later
cp = []
# Get the desired cutoff frequency in Hz
fc = int(input("Please enter cutoff frequency (Hz): "))
RT = []
vals = []
print ("Valid resistor values are: ")
print()
# Generate the valid list of resistor values
for element in c :
result = 0
k = 0
# Calculate the float resistor value for
# the current cap
x = 1/(fc * element * 2 * pi * 10**(-9))
# Loop until valid result produced, or rounding
# is too agressive.
while (result == 0 and k < 4) :
# Create empty list to store R values
R = [[],[],[]]
# Round this value, amount of rounding
# starts from 4th most significant digit
# and progresses up with each unsuccessful
# loop.
y = round(x, -int(log10(x)-(4-k)))
# Feed into resistor finder function
temp = resistor_ratio(family_str, 1, y)
# Loop through the returned items
j = 0
found = 0
for item in temp :
# If a part of the returned tuple is
# populated, then load in the values
if (len(item)) :
# Only take the second part of each
# item, as the first part is the
# value we fed in.
found = 1
for part in item :
R[j].append(part[1])
j = j + 1
# Check if there was values found
if (found == 1) :
cp.append(element)
# Append the current array, to the total
# list of values.
RT.append(R)
# Set results flag to stop loop
result = 1
# Append the calculated and rounded values
vals.append([x, y])
k = k + 1
for item in range(0, len(RT)) :
print("The capacitor value ", cp[item],
"nF, produced value ",
vals[item][0], " and when rounded to ",
vals[item][1],
" can be produced by values :")
print("Single Resistor: ", RT[item][0])
print("Series Combo : ", RT[item][1])
print("Parallel Combo : ", RT[item][2])
print()