Звезда не активнаЗвезда не активнаЗвезда не активнаЗвезда не активнаЗвезда не активна
 
  • E24 Resistor Series: Defines the standard E24 resistor values and scales them to a wide range of standard resistor values.
  • Helper Functions: Functions to calculate series and parallel resistances, find the closest resistor value, and find resistor combinations.
  • Inverting Amplifier Calculation: Calculates the necessary resistors for an inverting amplifier based on the desired gain, and checks for the best resistor combinations.
  • Non-Inverting Amplifier Calculation: Calculates the necessary resistors for a non-inverting amplifier based on the desired gain, and checks for the best resistor combinations.
  • Main Execution: Takes the desired gain as input, calculates the necessary resistors for both inverting and non-inverting amplifiers, and prints the results, including the calculated gain based on the selected resistors.
#inverting opamp: Uout = -(Rf / Rin)*Uin
#non inverting: Vout = (1 + Rf/Rin)*Vin

import itertools
import numpy as np

# Define E24 series resistors
E24_values = np.array([
    1.0, 1.1, 1.2, 1.3, 1.5, 1.6, 1.8, 2.0, 2.2, 2.4, 2.7, 3.0,
    3.3, 3.6, 3.9, 4.3, 4.7, 5.1, 5.6, 6.2, 6.8, 7.5, 8.2, 9.1
])
# Scale E24 values to standard resistor range
E24_resistors = np.unique([round(r * (10 ** exp), 1) for r in E24_values for exp in range(0, 7)])

# Helper functions to calculate series and parallel resistances
def series_resistance(resistors):
    return sum(resistors)

def parallel_resistance(resistors):
    return 1.0 / sum(1.0 / r for r in resistors)

# Function to find closest E24 resistor value
def closest_resistor(target, resistors):
    return min(resistors, key=lambda x: abs(x - target))

# Function to find resistor combinations
def find_resistor_combination(target_resistance, resistors, max_combinations=2, tolerance=0.1):
    # Check if single resistor is close enough
    closest = closest_resistor(target_resistance, resistors)
    if abs(closest - target_resistance) < tolerance:
        return [closest], "single"

    # Check combinations of up to max_combinations resistors in series
    for n in range(2, max_combinations + 1):
        for combination in itertools.combinations_with_replacement(resistors, n):
            if abs(series_resistance(combination) - target_resistance) < tolerance:
                return list(combination), "series"

    # Check combinations of up to max_combinations resistors in parallel
    for n in range(2, max_combinations + 1):
        for combination in itertools.combinations_with_replacement(resistors, n):
            if abs(parallel_resistance(combination) - target_resistance) < tolerance:
                return list(combination), "parallel"

    return [], "none"

# Function to calculate resistors for inverting amplifier
def calculate_inverting_resistors(gain):
    R1_value = 10.0
    R_f_value = R1_value * gain

    R_f_combination, R_f_type = find_resistor_combination(R_f_value, E24_resistors)
    R1_combination, R1_type = find_resistor_combination(R1_value, E24_resistors)

    final_R_f_value = series_resistance(R_f_combination) if R_f_type == "series" else parallel_resistance(R_f_combination)
    final_R1_value = series_resistance(R1_combination) if R1_type == "series" else parallel_resistance(R1_combination)

    calculated_gain = final_R_f_value / final_R1_value

    return {
        "gain": gain,
        "calculated_gain": calculated_gain,
        "R_f": {"value": final_R_f_value, "combination": R_f_combination, "type": R_f_type},
        "R1": {"value": final_R1_value, "combination": R1_combination, "type": R1_type}
    }

# Function to calculate resistors for non-inverting amplifier
def calculate_non_inverting_resistors(gain):
    R1_value = 10.0
    R_f_value = R1_value * (gain - 1)

    R_f_combination, R_f_type = find_resistor_combination(R_f_value, E24_resistors)
    R1_combination, R1_type = find_resistor_combination(R1_value, E24_resistors)

    final_R_f_value = series_resistance(R_f_combination) if R_f_type == "series" else parallel_resistance(R_f_combination)
    final_R1_value = series_resistance(R1_combination) if R1_type == "series" else parallel_resistance(R1_combination)

    calculated_gain = 1 + (final_R_f_value / final_R1_value)

    return {
        "gain": gain,
        "calculated_gain": calculated_gain,
        "R_f": {"value": final_R_f_value, "combination": R_f_combination, "type": R_f_type},
        "R1": {"value": final_R1_value, "combination": R1_combination, "type": R1_type}
    }

# Input gain
gain = float(input("Enter the desired gain: "))

# Calculate resistors for inverting and non-inverting amplifiers
inverting_result = calculate_inverting_resistors(gain)
non_inverting_result = calculate_non_inverting_resistors(gain)

# Output the result for both inverting and non-inverting configurations
print(f"Desired Gain: {gain}")

print("\nInverting Amplifier:")
print(f"Calculated Gain: {inverting_result['calculated_gain']:.2f}")
print(f"Feedback Resistor (R_f) value: {inverting_result['R_f']['value']} ohms")
print(f"R_f combination: {inverting_result['R_f']['combination']} ({inverting_result['R_f']['type']})")
print(f"Input Resistor (R1) value: {inverting_result['R1']['value']} ohms")
print(f"R1 combination: {inverting_result['R1']['combination']} ({inverting_result['R1']['type']})")

print("\nNon-Inverting Amplifier:")
print(f"Calculated Gain: {non_inverting_result['calculated_gain']:.2f}")
print(f"Feedback Resistor (R_f) value: {non_inverting_result['R_f']['value']} ohms")
print(f"R_f combination: {non_inverting_result['R_f']['combination']} ({non_inverting_result['R_f']['type']})")
print(f"Ground Resistor (R1) value: {non_inverting_result['R1']['value']} ohms")
print(f"R1 combination: {non_inverting_result['R1']['combination']} ({non_inverting_result['R1']['type']})")