Wrong syntax for Fourier Series. How to solve?

Viewed 27

    In this question, you will learn how to denoise a signal using the Fourier series.
    The signal you see below is a noisy signal. It is in the form of a function f(x). The variable 
    X contains the x-coordinates of the points and Y contains the height of the function.
    You must find the original signal. The original signal (not noisy) is a function
    g(x)=a0+a1sin(x)+a2sin(2x)+...+a5sin(5x)

    from matplotlib import pyplot as plt
    import numpy as np
    import numpy.random as NPR

    student_no = 2060277 # Change this number to your student number
    npr.seed(student_no) 

    a, b = -np.pi, np.pi
    X = np.linspace(a, b, 200)
    Y = np.zeros_like(X) + npr.randint(-4,4)

    for i in range(3):
     Y = Y + npr.randint(1,5)np.sin(npr.randint(0,6)X)+ npr.normal(0, 0.5, X.shape[0])

    plt.plot(X, Y)
    plt.show()

-----------------------------------------------------------------------------------------------
My code : 

    A = np.array(X)
    B = np.array(Y)

    integrale = 0

    for i in range(199):
     integrale = integrale + (A[i+1]-A[i])*B[i]*np.sin(i)
     a0 = integrale/(2*np.pi)
     an = integrale/np.pi  
     if 5 >= i > 0:
      print("Coefficient a_n :", an)
     elif i==0:
      print("Coefficient a_n :", a0)

The values in the output are really too low, I'm supposed to obtain 5 natural numbers for a_n and 1 for a_0. The graph containing the noisy signal is here. It's determine by the function above My code.

0 Answers
Related