Search code examples
pythonnumpymatplotlibsignal-processing

Why function sampled in time frequency is not repeating?


I'm reading my lecture and found this figure: sampled signal in frequency domain

So, I get the continuous function after sampling, in frequency domain, should repeat the pattern of the original continuous function. But in my Python code I just get one square where it should be a lot more squares repeating. I'm new to signals (I'm not engineer, it's an additional course) so I'm a little confused with my code. Am I missing something? Sorry if it is something dumb or concept misunderstanding.

def g(t):
    if t == 0:
        return 1
    return np.sin(1.5*np.pi*t) / (1.5*np.pi*t)


if __name__ == '__main__':
    n = 40
    fs = 100  # 0.01
    t = np.arange(-n, n, 1.0/fs)
    y = [g(i) for i in t]

    plt.plot(t, y)
    plt.xlim(-5, 5)
    plt.show()

        # Impulse train
    n = 40
    T = 1.0/2.5
    tn = np.arange(-n, n, T)
    # y2 = [1 for i in range(len(tn))]
    
    nT = tn*T
    y2 = [g(i) for i in nT]

    # Sampled g
    plt.stem(nT, y2, 'r', markerfmt='C3o', use_line_collection=True)
    plt.xlim(-0.9, 0.9)
    plt.show()

    fft_s = fft(y2)
    plt.plot(fftshift(fftfreq(len(fft_s ))),
             fftshift(np.abs(fft_s )))
    plt.show()

Solution

  • There is a subtle but important difference between a Fourier-Transformation (which you have in your figure) and a FFT (which you are computing). The FFT is a discrete (and computationally efficient) version of the Fourier-Transformation. However, since the FFT is a method that is defined over the samples, it goes from -Fs/2 to +Fs/2 where Fs is the sample rate. I believe that you have normalized the frequencies so it goes from -0.5 to 0.5. The effect that you are describing, however, can only be seen at frequencies higher than half the sampling frequency (so just outside of the FFT).

    What the picture that you have provided shows is the effect of sampling on the ‚real‘, i.e. the continuous spectrum. You might see this effect by actually computing the Fourier transformation via the definition and some integral solving function.

    Something that you can see when using the FFT is what happens when the sampling frequency is chosen incorrectly, i.e. ihre sampling frequency is greater than two times the bandwidth of the signal (this is known as the nyquist theorem). In this case, the outer spectrums „move“ so close to the original one that they overlap and cause the desired signal to be incorrect. In this case, reconstruction is no longer possible.