<jupyter_start><jupyter_text>Math stuff, that is usefull for my education  Fourier series graph calculator  How it worksSine-cosine form of fourier series of $f(x)$ at $[a, b]$:$f_N(x) = \frac{a_0}{2} + \sum_{i=1}^{N}\big(a_i \cos \big (\frac{2\pi}{b-a} ix\big) + b_i \sin \big (\frac{2\pi}{b-a} ix\big)\big)$, where- $a_i = \frac{2}{b-a} \int_{a}^{b}f(x) \cos \big( \frac{2\pi}{b-a} i x\big)dx$- $b_i = \frac{2}{b-a} \int_{a}^{b}f(x) \sin \big( \frac{2\pi}{b-a} i x\big)dx$ Note- $a_0 = \frac{2}{b-a} \int_{a}^{b}f(x)dx$- $b_0 = 0$<jupyter_code># Define function
def fn(x):
    return x**3 + 2*x**2
#@title <b><font color="red" size="">←</font> Run</font></b>


import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import square
from scipy.integrate import quad
import scipy.special as special
import math #* //import all function from math
from math import cos, sin
from matplotlib import figure
from ipywidgets import interact


@interact
def fourier_factor(start_point=-np.pi, end_point=np.pi, n=(2, 71)):
    line_lenght = end_point - start_point
    x = np.arange(start_point, end_point, 0.001) #x axis has been chosen from –π to +π, 

    y = fn(x)
    
    # Define integrands
    fc = lambda x:fn(x)*cos((2*np.pi / line_lenght)*i*x)  # i :dummy index
    fs = lambda x:fn(x)*sin((2*np.pi / line_lenght)*i*x)
    An=[]
    Bn=[]

    sum=0

    for i in range(n):
        an = quad(fc,start_point,end_point)[0]*(2./line_lenght)
        An.append(an)
        bn = quad(fs,start_point,end_point)[0]*(2./line_lenght)
        Bn.append(bn) #//putting value in array Bn

    sum += An[0]/2

    for i in range(1, n):
        sum += An[i]*np.cos((2*np.pi / line_lenght)*i*x) + Bn[i]*np.sin((2*np.pi / line_lenght)*i*x)

    plt.plot(x,sum,'g')
    plt.plot(x,y,'r--')
    plt.title(f"fourier series for f ({n} iterations)")
    plt.show()<jupyter_output><empty_output><jupyter_text>Fourier transform (DCT-2) graph calculator  How it worksDCT - Discrete cosine transforms. Here we are using the second DCT type. In case of the normalized DCT:$f = \begin{cases}\sqrt{\frac{1}{4N}} & \text{ if } k=0 \\\sqrt{\frac{1}{2N}} & \text{ if } k \neq 0 \end{cases}$In this case, the DCT “base functions” $\phi_k[n] = 2f \cos \big( \frac{\pi (2n+1)k}{2N} \big)$become orthonormal:$\sum_{n=0}^{N-1}\phi_k[n]\phi_l[n]=\delta_{lk}$<jupyter_code># Define function
def fun(x):
    return np.exp(-x) * special.gamma(x)
#@title <b><font color="red" size="">←</font> Run</font></b>

from scipy.fft import dct, idct
import matplotlib.pyplot as plt


@interact
def fft(start_point=-np.pi, end_point=np.pi, N=(2, 150)):
    t = np.linspace(start_point, end_point, N, endpoint=False)
    x = fun(t)
    y = dct(x, norm='ortho')
    window = np.zeros(N)
    window[:20] = 1
    yr = idct(y*window, norm='ortho')
    sum(abs(x-yr)**2) / sum(abs(x)**2)
    plt.plot(t, x, '-bx')
    plt.plot(t, yr, 'ro')
    window = np.zeros(N)
    window[:15] = 1
    yr = idct(y*window, norm='ortho')
    sum(abs(x-yr)**2) / sum(abs(x)**2)
    plt.plot(t, yr, 'g+')
    plt.legend(['x', '$x_{20}$', '$x_{15}$'])
    plt.grid()
    plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20323912353127976, 'fraction_numerical': 0.020959034614163225, 'mean_word_length': 3.5985401459854014, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '476890', 'license': 'MIT', 'n_tokens_mistral': 1384, 'n_tokens_neox': 1338, 'n_words': 304, 'path': 'fourier.ipynb', 'repo_name': 'nstu-fami-90x/math_experiments', 'size': '3149'}