[Python] What's causing this weird jump in these phase graphs of Fourier coefficients?

Discussão em 'Python' iniciado por Stack, Abril 18, 2025.

  1. Stack

    Stack Membro Participativo

    from an assignment in signal processing in Python. It's all done in Python (through Google Colab) with the libraries cmath, numpy, and matplotlib alone.

    Signal a is a window signal, and I calculate its Fourier coefficients as ak. In the same vein, bk is the Fourier coefficients of signal b.

    (the following code is patched together since it's in a notebook style):


    import numpy as np
    import cmath
    import matplotlib.pyplot as plt

    D=1000
    j = complex(0, 1)
    pi = np.pi
    N = 2 * D + 1

    # initializing signal a[n]
    a=np.zeros(2*D+1)
    for i in range(-99,100):
    a[i+D] = 1

    # function to do DFST (either inverse or regular)
    def fourier_series_transform(data, D, inverse=False):
    j = complex(0, 1)
    pi = np.pi
    N = 2 * D + 1

    # Allocate result array
    result = np.zeros(N, dtype=complex)

    if inverse:
    # Inverse transform: reconstruct time-domain signal from bk
    for n in range(-D, D + 1):
    for k in range(-D, D + 1):
    result[n + D] += data[k + D] * cmath.exp(j * 2 * pi * k * n / N)
    else:
    # Forward transform: compute bk from b[n]
    for k in range(-D, D + 1):
    for n in range(-D, D + 1):
    result[k + D] += (1 / N) * data[n + D] * cmath.exp(-j * 2 * pi * k * n / N)

    return result

    # getting ak from a
    ak = fourier_series_transform(a, D)


    here's pics of a[n] and ak (which is the Dirichlet kernel):

    [​IMG]

    [​IMG]

    # initialization of bk
    bk = np.zeros(N, dtype=complex)

    # defining bk - n=100 - which means the signal b[n] is moved 100 units to the right
    for k in range(-D, D + 1):
    bk[k + D] = ak[k + D] * cmath.exp(-j * 2 * pi * k * 100 / N)

    # getting the original signal b[n]
    b = fourier_series_transform(bk, D, inverse=True)


    bk is given to me as bk=ake^(-jk2*pi/N * 100), here are pics of bk, b[n]:

    [​IMG]

    [​IMG]

    ### here starts the problem:
    #plot of all the coefficients
    plt.plot(np.arange(-D, D+1), ak.real, label="real part $a_k$", color='blue')
    plt.plot(np.arange(-D, D+1), ak.imag, label="imaginary part $a_k$", color='turquoise')

    plt.plot(np.arange(-D, D+1), bk.real, label="real part $b_k$", color='orange')
    plt.plot(np.arange(-D, D+1), bk.imag, label="imaginary part $b_k$", color='red')

    plt.grid(True)
    plt.title("plot of freq of $a_k$ and $b_k$")
    plt.xlim(-250, 250)
    #plt.ylim(-0.0006, 0.0004)
    plt.legend()
    plt.show()

    #phase
    plt.plot(np.arange(-D, D+1), np.arctan2(ak.imag, ak.real), label="phase $a_k$", color='turquoise')
    plt.plot(np.arange(-D, D+1), np.arctan2(bk.imag, bk.real), label="phase $b_k$", color='orange')

    plt.grid(True)
    plt.title("plot of phases of $a_k$ and $b_k$")
    plt.xlim(-250, 250)
    plt.yticks(
    [-pi, -pi/2, 0, pi/2, pi],
    [r"$-\pi$", r"$-\frac{\pi}{2}$", "0", r"$\frac{\pi}{2}$", r"$\pi$"]
    )
    plt.legend()
    plt.show()


    here are the pics i get of the real and imaginary parts of ak, bk and later their phase (the imaginary part of ak=0):

    [​IMG]

    [​IMG]

    as you can see before -190 and after 190 if the unit digit is 1 (i.e., 841, -561, -191...) it does a jump which, according to my math isn't suposed to happen, this problem presist for the rest of the sampling area (meaning from [-1000,-190], and [190,1000]).

    i would love to understand why it's happening, i dont belive it's a problem with my math but i might be wrong about that.

    Continue reading...

Compartilhe esta Página