Пример #1
0
def test_sawtooth_wave():
    s = fourier_series(x, (x, 0, pi))
    assert s.truncate(4) == \
        pi/2 - sin(2*x) - sin(4*x)/2 - sin(6*x)/3
    s = fourier_series(x, (x, 0, 1))
    assert s.truncate(4) == \
        S.Half - sin(2*pi*x)/pi - sin(4*pi*x)/(2*pi) - sin(6*pi*x)/(3*pi)
Пример #2
0
def test_FourierSeries_finite():

    assert fourier_series(sin(x)).truncate(1) == sin(x)
    # assert type(fourier_series(sin(x)*log(x))).truncate() == FourierSeries
    # assert type(fourier_series(sin(x**2+6))).truncate() == FourierSeries
    assert fourier_series(sin(x) * log(y) * exp(z), (x, pi, -pi)).truncate() == sin(
        x
    ) * log(y) * exp(z)
    assert fourier_series(sin(x) ** 6).truncate(oo) == -15 * cos(2 * x) / 32 + 3 * cos(
        4 * x
    ) / 16 - cos(6 * x) / 32 + Rational(5, 16)
    assert fourier_series(sin(x) ** 6).truncate() == -15 * cos(2 * x) / 32 + 3 * cos(
        4 * x
    ) / 16 + Rational(5, 16)
    assert fourier_series(sin(4 * x + 3) + cos(3 * x + 4)).truncate(oo) == -sin(
        4
    ) * sin(3 * x) + sin(4 * x) * cos(3) + cos(4) * cos(3 * x) + sin(3) * cos(4 * x)
    assert fourier_series(sin(x) + cos(x) * tan(x)).truncate(oo) == 2 * sin(x)
    assert fourier_series(cos(pi * x), (x, -1, 1)).truncate(oo) == cos(pi * x)
    assert fourier_series(
        cos(3 * pi * x + 4) - sin(4 * pi * x) * log(pi * y), (x, -1, 1)
    ).truncate(oo) == -log(pi * y) * sin(4 * pi * x) - sin(4) * sin(3 * pi * x) + cos(
        4
    ) * cos(
        3 * pi * x
    )
Пример #3
0
def test_FourierSeries_2():
    p = Piecewise((0, x < 0), (x, True))
    f = fourier_series(p, (x, -2, 2))

    assert f.term(3) == (2 * sin(3 * pi * x / 2) / (3 * pi) -
                         4 * cos(3 * pi * x / 2) / (9 * pi**2))
    assert f.truncate() == (2 * sin(pi * x / 2) / pi - sin(pi * x) / pi -
                            4 * cos(pi * x / 2) / pi**2 + Rational(1, 2))
Пример #4
0
def test_FourierSeries_2():
    p = Piecewise((0, x < 0), (x, True))
    f = fourier_series(p, (x, -2, 2))

    assert f.term(3) == (2*sin(3*pi*x / 2) / (3*pi) -
                         4*cos(3*pi*x / 2) / (9*pi**2))
    assert f.truncate() == (2*sin(pi*x / 2) / pi - sin(pi*x) / pi -
                            4*cos(pi*x / 2) / pi**2 + Rational(1, 2))
Пример #5
0
def test_FourierSeries():
    fo, fe, fp = _get_examples()

    assert fourier_series(1, (-pi, pi)) == 1
    assert (
        Piecewise((0, x < 0), (pi, True)).fourier_series((x, -pi, pi)).truncate()
    ) == fp.truncate()
    assert isinstance(fo, FourierSeries)
    assert fo.function == x
    assert fo.x == x
    assert fo.period == (-pi, pi)

    assert fo.term(3) == 2 * sin(3 * x) / 3
    assert fe.term(3) == -4 * cos(3 * x) / 9
    assert fp.term(3) == 2 * sin(3 * x) / 3

    assert fo.as_leading_term(x) == 2 * sin(x)
    assert fe.as_leading_term(x) == pi ** 2 / 3
    assert fp.as_leading_term(x) == pi / 2

    assert fo.truncate() == 2 * sin(x) - sin(2 * x) + (2 * sin(3 * x) / 3)
    assert fe.truncate() == -4 * cos(x) + cos(2 * x) + pi ** 2 / 3
    assert fp.truncate() == 2 * sin(x) + (2 * sin(3 * x) / 3) + pi / 2

    fot = fo.truncate(n=None)
    s = [0, 2 * sin(x), -sin(2 * x)]
    for i, t in enumerate(fot):
        if i == 3:
            break
        assert s[i] == t

    def _check_iter(f, i):
        for ind, t in enumerate(f):
            assert t == f[ind]
            if ind == i:
                break

    _check_iter(fo, 3)
    _check_iter(fe, 3)
    _check_iter(fp, 3)

    assert fo.subs(x, x ** 2) == fo

    raises(ValueError, lambda: fourier_series(x, (0, 1, 2)))
    raises(ValueError, lambda: fourier_series(x, (x, 0, oo)))
    raises(ValueError, lambda: fourier_series(x * y, (0, oo)))
Пример #6
0
def test_FourierSeries():
    fo, fe, fp = _get_examples()

    assert fourier_series(1, (-pi, pi)) == 1
    assert (Piecewise((0, x < 0), (pi, True)).
            fourier_series((x, -pi, pi)).truncate()) == fp.truncate()
    assert isinstance(fo, FourierSeries)
    assert fo.function == x
    assert fo.x == x
    assert fo.period == (-pi, pi)

    assert fo.term(3) == 2*sin(3*x) / 3
    assert fe.term(3) == -4*cos(3*x) / 9
    assert fp.term(3) == 2*sin(3*x) / 3

    assert fo.as_leading_term(x) == 2*sin(x)
    assert fe.as_leading_term(x) == pi**2 / 3
    assert fp.as_leading_term(x) == pi / 2

    assert fo.truncate() == 2*sin(x) - sin(2*x) + (2*sin(3*x) / 3)
    assert fe.truncate() == -4*cos(x) + cos(2*x) + pi**2 / 3
    assert fp.truncate() == 2*sin(x) + (2*sin(3*x) / 3) + pi / 2

    fot = fo.truncate(n=None)
    s = [0, 2*sin(x), -sin(2*x)]
    for i, t in enumerate(fot):
        if i == 3:
            break
        assert s[i] == t

    def _check_iter(f, i):
        for ind, t in enumerate(f):
            assert t == f[ind]
            if ind == i:
                break

    _check_iter(fo, 3)
    _check_iter(fe, 3)
    _check_iter(fp, 3)

    assert fo.subs(x, x**2) == fo

    raises(ValueError, lambda: fourier_series(x, (0, 1, 2)))
    raises(ValueError, lambda: fourier_series(x, (x, 0, oo)))
    raises(ValueError, lambda: fourier_series(x*y, (0, oo)))
def test_fourier_series_square_wave():
    """Test if fourier_series approximates discontinuous function correctly."""
    square_wave = Piecewise((1, x < pi), (-1, True))
    s = fourier_series(square_wave, (x, 0, 2 * pi))

    assert s.truncate(3) == 4 / pi * sin(x) + 4 / (3 * pi) * sin(3 * x) + \
        4 / (5 * pi) * sin(5 * x)
    assert s.sigma_approximation(4) == 4 / pi * sin(x) * sinc(pi / 4) + \
        4 / (3 * pi) * sin(3 * x) * sinc(3 * pi / 4)
Пример #8
0
def test_fourier_series_square_wave():
    """Test if fourier_series approximates discontinuous function correctly."""
    square_wave = Piecewise((1, x < pi), (-1, True))
    s = fourier_series(square_wave, (x, 0, 2*pi))

    assert s.truncate(3) == 4 / pi * sin(x) + 4 / (3 * pi) * sin(3 * x) + \
        4 / (5 * pi) * sin(5 * x)
    assert s.sigma_approximation(4) == 4 / pi * sin(x) * sinc(pi / 4) + \
        4 / (3 * pi) * sin(3 * x) * sinc(3 * pi / 4)
Пример #9
0
def test_FourierSeries__add__sub():
    assert fo + fo == fo.scale(2)
    assert fo - fo == 0
    assert -fe - fe == fe.scale(-2)

    assert (fo + fe).truncate() == 2*sin(x) - sin(2*x) - 4*cos(x) + cos(2*x) \
        + pi**2 / 3
    assert (fo - fe).truncate() == 2*sin(x) - sin(2*x) + 4*cos(x) - cos(2*x) \
        - pi**2 / 3

    assert isinstance(fo + 1, Add)

    raises(ValueError, lambda: fo + fourier_series(x, (x, 0, 2)))
Пример #10
0
def test_FourierSeries__add__sub():
    assert fo + fo == fo.scale(2)
    assert fo - fo == 0
    assert -fe - fe == fe.scale(-2)

    assert (fo + fe).truncate() == 2*sin(x) - sin(2*x) - 4*cos(x) + cos(2*x) \
        + pi**2 / 3
    assert (fo - fe).truncate() == 2*sin(x) - sin(2*x) + 4*cos(x) - cos(2*x) \
        - pi**2 / 3

    assert isinstance(fo + 1, Add)

    raises(ValueError, lambda: fo + fourier_series(x, (x, 0, 2)))
Пример #11
0
def test_FourierSeries_finite():

    assert fourier_series(sin(x)).truncate(1) == sin(x)
    # assert type(fourier_series(sin(x)*log(x))).truncate() == FourierSeries
    # assert type(fourier_series(sin(x**2+6))).truncate() == FourierSeries
    assert fourier_series(sin(x)*log(y)*exp(z),(x,pi,-pi)).truncate() == sin(x)*log(y)*exp(z)
    assert fourier_series(sin(x)**6).truncate(oo) == -15*cos(2*x)/32 + 3*cos(4*x)/16 - cos(6*x)/32 \
           + Rational(5, 16)
    assert fourier_series(sin(x) ** 6).truncate() == -15 * cos(2 * x) / 32 + 3 * cos(4 * x) / 16 \
           + Rational(5, 16)
    assert fourier_series(sin(4*x+3) + cos(3*x+4)).truncate(oo) ==  -sin(4)*sin(3*x) + sin(4*x)*cos(3) \
           + cos(4)*cos(3*x) + sin(3)*cos(4*x)
    assert fourier_series(sin(x)+cos(x)*tan(x)).truncate(oo) == 2*sin(x)
    assert fourier_series(cos(pi*x), (x, -1, 1)).truncate(oo) == cos(pi*x)
    assert fourier_series(cos(3*pi*x + 4) - sin(4*pi*x)*log(pi*y) , (x, -1, 1)).truncate(oo) == -log(pi*y)*sin(4*pi*x)\
           - sin(4)*sin(3*pi*x) + cos(4)*cos(3*pi*x)
Пример #12
0
def constructTrajectory_FourierSeriesY():
    dc = 0.8
    ht = 3.5
    T = 1.0

    t_current = symbols('t_current')
    fy1 = -18
    fy2 = -18 + 0 + 16*ht/(T-dc)**2*(t_current-dc)**2 - 32*ht/(T-dc)**3*(t_current-dc)**3 + 16*ht/(T-dc)**4*(t_current-dc)**4 
    
    print("Calculating Piecewise Fourier Series for Y")
    FourierY = Piecewise((fy1,t_current <= dc),(fy2, t_current<=1))
    print("\n")
    print("Fourier piecewise for Y is",FourierY)
    FourierYSeries = fourier_series(FourierY,(t_current,0,1))
    q = FourierYSeries.truncate(100)
    print("FourierY Series calculated:  ",q)
   
    lq = np.array(range( 0 , 100))
    for i in lq:

        currentY = q.subs(t_current,i/100)
        plt.scatter( i/100 , currentY )

    plt.show()
Пример #13
0
from sympy import fourier_series, pi, cos
from sympy.abc import x
from sympy.utilities.lambdify import lambdify, implemented_function
from sympy import Function
from sympy import *

s = fourier_series(x**2, (x, -pi, pi))
res = s.truncate(n=9)

print(res)
lam_f = lambdify(x, res)
print(lam_f(4))

plot(res)
Пример #14
0
def constructTrajectory_FourierSeriesX():
    dc = 0.8
    dt = 0.025
    T = 1.0
    stroke = 12.0
    ht= 3.5



    a3 = (-4.0*(stroke/2.0+stroke/dc*dt)-2.0*stroke/dc*(T-dc-2.0*dt))/(T-dc-2.0*dt)**3.0;
    a2 = -3.0/2.0*a3*(T-dc-2.0*dt)
    a1 = -stroke/dc
    a0 = -(stroke/2.0+stroke/dc*dt)

    t_current = symbols('t_current')

    fx1 = (stroke/2.0 + stroke/dc*(-1* t_current))
    fx2 = a0 + a1*(t_current-(dc+dt)) + a2*(t_current-(dc+dt))**2.0 + a3*(t_current-(dc+dt))**3.0; #stride 
    fx3 = (stroke/2.0 + stroke/dc*(-1* t_current))

    print("Calculating Piecewise Fourier Series for X")
    FourierX = Piecewise( (fx1,t_current<=dc+dt) ,  (fx2 ,t_current<=T-dt ) , (fx3 , t_current<=1)   )

    print("\n")
    print("Fourier piecewise for X is    ",FourierX)
    print("\n")
   
    FourierXSeries = fourier_series( FourierX , (t_current,0,1)  )
    p = FourierXSeries.truncate(1000)
    print("FourierX Series Calculated: ", p)

    # lp = np.array(range( 0 , 100))
    # for i in lp:

    #     currentX = p.subs(t_current,i/100)
    #     plt.scatter( i/100 , currentX )

    # plt.show()


    fy1 = -18
    fy2 = -18 + 0 + 16*ht/(T-dc)**2*(t_current-dc)**2 - 32*ht/(T-dc)**3*(t_current-dc)**3 + 16*ht/(T-dc)**4*(t_current-dc)**4 
    
    print("Calculating Piecewise Fourier Series for Y")
    FourierY = Piecewise((fy1,t_current <= dc),(fy2, t_current<=1))
    print("\n")
    print("Fourier piecewise for Y is",FourierY)
    FourierYSeries = fourier_series(FourierY,(t_current,0,1))
    q = FourierYSeries.truncate(1000)
    print("FourierY Series calculated:  ",q)
   
    lq = np.array(range( 0 , 100))
    for i in lq:
        a=0
        currentX = p.subs(t_current,i/100)
        currentY = q.subs(t_current,i/100)
        plt.scatter( currentX , currentY )
        
        print("loop = ",i)

    plt.show()
Пример #15
0
def _get_examples():
    fo = fourier_series(x, (x, -pi, pi))
    fe = fourier_series(x ** 2, (-pi, pi))
    fp = fourier_series(Piecewise((0, x < 0), (pi, True)), (x, -pi, pi))
    return fo, fe, fp
Пример #16
0
##def sin(x):
##    return sin(x)
#t_list=[]
#for x in xrange:
#    print(x)
#    fx = f(x)
#    fx_sum=0
#    for n in count:
#        if n == 0:
#            print(f(x))
#            a0=integrate.quad(f,a=-pi,b=pi,args=x)[0]*(1/pi)
#
#            fx_sum += a0*cos(n*x)
#        else:
#            a=integrate.quad(f*cos(n*x),-pi,pi,args=x)*(1/pi)
#            asum=a*cos(n)
#            b=integrate.quad(f*sin(n*x),-pi,pi,args=x)*(1/pi)
#            fx_sum +=b*sin(x)
#    t_list.append(fx_sum)
#
#print(t_list)
from sympy import fourier_series, pi
from sympy.abc import x
from sympy.plotting import plot

s = fourier_series(pi + x, (x, -pi, pi))
print(type(s))
s.scale(2).truncate()
sp = plot(s)
#plt.show()
print(s)
Пример #17
0
# Fprma de calcular series de fourier truncadas de orde n , ampliadas y trasladadas.


from sympy import fourier_series, pi, plot
from sympy.abc import x
f = x**2
sn = fourier_series(f, (x, -pi, pi))
sn.shift(5).truncate(7)
sn.shiftx(3).truncate(7)
sn.scale(5).truncate(7)
sn.scalex(7).truncate(7)



# Series de fourier y graficadoras
import numpy as np
from sympy import fourier_series, pi, plot
from sympy.abc import x 
f = x
fser = fourier_series(f,(x,-pi, pi))
fser1 = fser.truncate(3)
fser2 = fser.truncate(7)
fser3 = fser.truncate(14)

p = plot(f,fser1, fser2, fser3, (x,-pi, pi), show = False, legend = True)
p[0].line_color = (0,0,0)
p[0].label = 'x'
p[1].line_color = (1,0,0)
p[1].label = 'n=3'
p[2].label = 'n=7'
p[3].label = 'n=14'
Пример #18
0
from sympy import fourier_series, pi, plot
from sympy.abc import x
f = x
s = fourier_series(f, (x, -pi, pi))
s1 = s.truncate(n = 3)
s2 = s.truncate(n = 5)
s3 = s.truncate(n = 7)
p = plot(f, s1, s2, s3, (x, -pi, pi), show=False, legend=True)

p[0].line_color = (0, 0, 0)
p[0].label = 'x'
p[1].line_color = (0.7, 0.7, 0.7)
p[1].label = 'n=3'
p[2].line_color = (0.5, 0.5, 0.5)
p[2].label = 'n=5'
p[3].line_color = (0.3, 0.3, 0.3)
p[3].label = 'n=7'

p.show()
Пример #19
0
def _get_examples():
    fo = fourier_series(x, (x, -pi, pi))
    fe = fourier_series(x**2, (-pi, pi))
    fp = fourier_series(Piecewise((0, x < 0), (pi, True)), (x, -pi, pi))
    return fo, fe, fp
Пример #20
0
from sympy import (symbols, pi, Piecewise, sin, cos, Rational, oo,
                   fourier_series, Add)
from sympy.series.fourier import FourierSeries
from sympy.utilities.pytest import raises

x, y, z = symbols('x y z')

fo = fourier_series(x, (x, -pi, pi))
fe = fourier_series(x**2, (-pi, pi))
fp = fourier_series(Piecewise((0, x < 0), (pi, True)), (x, -pi, pi))


def test_FourierSeries():
    assert fourier_series(1, (-pi, pi)) == 1
    assert (Piecewise((0, x < 0), (pi, True)).fourier_series(
        (x, -pi, pi)).truncate()) == fp.truncate()
    assert isinstance(fo, FourierSeries)
    assert fo.function == x
    assert fo.x == x
    assert fo.period == (-pi, pi)

    assert fo.term(3) == 2 * sin(3 * x) / 3
    assert fe.term(3) == -4 * cos(3 * x) / 9
    assert fp.term(3) == 2 * sin(3 * x) / 3

    assert fo.as_leading_term(x) == 2 * sin(x)
    assert fe.as_leading_term(x) == pi**2 / 3
    assert fp.as_leading_term(x) == pi / 2

    assert fo.truncate() == 2 * sin(x) - sin(2 * x) + (2 * sin(3 * x) / 3)
    assert fe.truncate() == -4 * cos(x) + cos(2 * x) + pi**2 / 3
            "Enter 1 to enter interval in terms of pi or it's multiples,Otherwise enter 0"
        ))
    if try1 == 1:
        interval1 = int(
            input(
                "Enter the multiple of pi that you want,Eg:2 represents 2*pi"))
        interval2 = int(
            input(
                "Enter the multiple of pi that you want,Eg:2 represents 2*pi"))
        interval1 = interval1 * pi
        interval2 = interval2 * pi
    else:
        interval1 = float(input("Enter the Starting point of the interval"))
        interval2 = float(input("Enter the Ending point of the interval"))

    s = fourier_series(expr1, (x, interval1, interval2))

    truncval = int(
        input("enter the number of terms desired in the fourier series"))

    v = s.truncate(truncval)

    print("FOURIER SERIES OF THE ENTERED FUNCTION\n\n\n")

    print(v)
    x1 = [i for i in np.arange(interval1, interval2, 0.02)]
    y1 = [(v.evalf(subs={x: j}))
          for j in np.arange(interval1, interval2, 0.02)]

    x2 = [i for i in np.arange(interval1, interval2, 0.0001)]
    y2 = [(expr1.evalf(subs={x: j}))
Пример #22
0
from sympy import fourier_series, pi, Piecewise
from sympy.abc import t
from sympy.plotting import plot

f = Piecewise((0, t < -pi), (-1, t < -pi / 2), (1, t < pi / 2), (-1, t < pi),
              (0, True))
T = 2 * pi

s = fourier_series(f, (t, -T / 2, T / 2))
F = s.truncate(5)
print(F)

n = [10, 50, 200]
p = plot(s.truncate(n[0]), (t, -10, 10),
         title="For 10 terms",
         line_color='firebrick',
         show=False)
p.xlabel = '$t$'
p.ylabel = '$F(t)$'
p.show()
p = plot(s.truncate(n[1]), (t, -10, 10),
         title="For 50 terms",
         line_color='darkblue',
         show=False)
p.xlabel = '$t$'
p.ylabel = '$F(t)$'
p.show()
p = plot(s.truncate(n[2]), (t, -10, 10),
         title="For 200 terms",
         line_color='green',
         show=False)
Пример #23
0
from sympy import (symbols, pi, Piecewise, sin, cos, sinc, Rational,
                   oo, fourier_series, Add)
from sympy.series.fourier import FourierSeries
from sympy.utilities.pytest import raises

x, y, z = symbols('x y z')

fo = fourier_series(x, (x, -pi, pi))
fe = fourier_series(x**2, (-pi, pi))
fp = fourier_series(Piecewise((0, x < 0), (pi, True)), (x, -pi, pi))


def test_FourierSeries():
    assert fourier_series(1, (-pi, pi)) == 1
    assert (Piecewise((0, x < 0), (pi, True)).
            fourier_series((x, -pi, pi)).truncate()) == fp.truncate()
    assert isinstance(fo, FourierSeries)
    assert fo.function == x
    assert fo.x == x
    assert fo.period == (-pi, pi)

    assert fo.term(3) == 2*sin(3*x) / 3
    assert fe.term(3) == -4*cos(3*x) / 9
    assert fp.term(3) == 2*sin(3*x) / 3

    assert fo.as_leading_term(x) == 2*sin(x)
    assert fe.as_leading_term(x) == pi**2 / 3
    assert fp.as_leading_term(x) == pi / 2

    assert fo.truncate() == 2*sin(x) - sin(2*x) + (2*sin(3*x) / 3)
    assert fe.truncate() == -4*cos(x) + cos(2*x) + pi**2 / 3