I am not from a signal processing background, kindly excuse me for my vocabulary.

When I compute the power spectrum of a periodic function, where the "real" space variables are generated by np.linspace, I can choose whether or not I want to include the last point. Over integer multiples of a cycle, excluding the last point produces samples for a periodic function and including it produces samples for an aperiodic function, giving rise to "spectral leaking" in the Fourier transform of the function (1).

In the plots of the power spectrum for first harmonic cosine and sine functions, one can notice that the cosine function has spectral leakage that confers the zero mode with a non-zero value, while this is not the case with the sine function. Why is this the case?

import numpy as np
import matplotlib.pyplot as plt

theta = np.linspace(0,2*np.pi,100, endpoint=False)
phi = np.linspace(0,2*np.pi,100, endpoint=True)

ct = np.cos(theta)
cp = np.cos(phi)

st = np.sin(theta)
sp = np.sin(phi)

fct = np.fft.fft(ct)
fcp = np.fft.fft(cp)
fst = np.fft.fft(st)
fsp = np.fft.fft(sp)

plt.plot(np.abs(fct), label='endpoint=False')
plt.plot(np.abs(fcp), label='endpoint=True')
plt.title('Cosine Power Spectrum')

plt.plot(np.abs(fst), label='endpoint=False')
plt.plot(np.abs(fsp), label='endpoint=True')
plt.title('Sine Power Spectrum')

enter image description here enter image description here


1 Answer 1


The DFT is periodic in both domains (time and frequency). It's easiest to see the "discontinuity" that causes the spectral leakage if we plot two periods of the signal

enter image description here

At sample time $n=100$ we are duplicating a sample, which generates the leakage.

For the sine wave the duplicated sample is 0, so it doesn't not affect the mean (or DC part of the spectrum). For the cosine the duplicated sample is a 1, so it does indeed result in non-zero DC component.

Spectral leakage occurs whenever the frequency of a sine wave isn't an integer multiple of the FFT's basis frequency (which is sample rate divided by FFT length). The exact shape is the sum of two Dirichlet kernels and a phase term which depend on amount of "misalignment" of the sine wave frequency and it's pahse.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.