import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import UnivariateSpline

frequencies = np.array([20, 40, 100, 200, 400, 1000, 2000, 4000, 
                        10000, 20000, 40000, 100000, 200000, 400000])

Cp1_values = np.array([61, 130, 300, 460, 590, 630, 630, 580, 360, 180, 66])
Cp2_values = np.array([140, 230, 480, 600, 650, 670, 670, 670, 660, 650, 580, 380, 185, 68])
Cp3_values = np.array([88, 220, 480, 590, 650, 650, 650, 650, 580, 430, 245, 74, 24, 7.5])
Cp4_OOC_values = np.array([48, 96, 230, 230, 245, 250, 255, 255, 250, 245, 240, 200, 130, 52])

plt.figure(figsize=(12, 8))

def plot_smooth_curve(frequencies, Cp_values, label, color, start_idx=0):
    if start_idx > 0:
        freq = frequencies[start_idx:]
        Cp = Cp_values
    else:
        freq = frequencies
        Cp = Cp_values

    log_freq = np.log10(freq)
    
    spline = UnivariateSpline(log_freq, Cp, s=0.03)
    
    log_freq_smooth = np.linspace(log_freq.min(), log_freq.max(), 500)
    freq_smooth = 10**log_freq_smooth
    Cp_smooth = spline(log_freq_smooth)
    
    plt.plot(freq_smooth, Cp_smooth, label=label, color=color)
    
    plt.scatter(freq, Cp, color=color, edgecolor='k')

# Аппроксимация и построение кривых для каждого набора данных
plot_smooth_curve(frequencies, Cp1_values, label="2.1", color='blue', start_idx=3)
plot_smooth_curve(frequencies, Cp2_values, label="2.2", color='green')
plot_smooth_curve(frequencies, Cp3_values, label="2.3", color='red')
plot_smooth_curve(frequencies, Cp4_OOC_values, label="2.4", color='purple')

plt.xscale('log')

plt.xlabel("Частота (Гц)", fontsize=14)
plt.ylabel("$2U_{m \\, {вых}}$ (мВ)", fontsize=14)
plt.title("Зависимость $2U_{m \\, {вых}}$ от частоты", fontsize=16)

plt.legend(fontsize=12)
plt.grid(True, which="both", ls="--", linewidth=0.5)

plt.ylim(bottom=0)

plt.tight_layout()

plt.savefig('images/AchX.png', dpi=300)