[Python] why is there a complex argument in the array here trying to do DTFS?

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

  1. Stack

    Stack Membro Participativo

    I have a window signal, which i calculate it's Fourier coefficients but then in the output i get a small complex value [3 order of magnitude less in the origin and the same order of magnitude in the edges of my sampling (from -1000 to 1000)] where the output should be purely real, if it was just a floating point approx error it wouldn't have the pattern of sin like i found, so what could cause this?

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

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

    D=1000

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


    This code creates the following window signal:

    [​IMG]

    and then to get the fourier coefficients:

    j=complex(0,1)
    pi=np.pi
    N=2001

    ak = np.zeros(N, dtype=complex)

    for k in range(-D, D + 1):
    for n in range(-D, D + 1):
    ak[k + D] += (1/N) * a[n + D] * cmath.exp(-j * 2 * pi * k * n / N)


    which gives me the following image:

    [​IMG]

    when i do the math in paper i get the coeficients to be:

    [​IMG]

    where omega0 is 2*pi/N and a[n]=u(n+100)-u(n-100) (where u(n) is the Heaveside function).

    but it's weird as when i plot the imaginary part of ak i get that it's (at a very good approximation) equal to 1/N *sin(201*pi/N where N=2001 and is my entire sampling area (from -1000 to 1000 as i mentioned), which if it was just a rounding error because of floating point it wouldn't be in this form.

    here it's the plot of the imaginary part of ak, not freq of ak or bk.

    [​IMG]

    Continue reading...

Compartilhe esta Página