""" Illustrate correspondence between DCT bins and cosine signals. """ from scipy.fftpack import idct from numpy import cos, arange, zeros, pi import matplotlib.pyplot as plt from numpy.testing import assert_allclose N = 8 # only even numbers k = arange(N) ###################################### Type 1 plt.figure(1) sig = cos(2*pi*k/N) plt.plot(k, sig, '.-') plt.plot(k+N, sig, '.-') spectrum = zeros(N//2+1) spectrum[1] = 1 plt.plot(k[:N//2+1], idct(spectrum, 1)/2, 'o-') plt.margins(0, 0.1) assert_allclose(sig[:N//2+1], idct(spectrum, 1)/2, rtol=1e-05, atol=1e-08) ###################################### Type 2 plt.figure(2) sig = cos(2*pi*(k/N + 0.5/N)) # half-sample shift left plt.plot(k, sig, '.-') plt.plot(k+N, sig, '.-') spectrum = zeros(N//2) spectrum[1] = 1 plt.plot(k[:N//2], idct(spectrum, 2)/2, 'o-') plt.margins(0, 0.1) assert_allclose(sig[:N//2], idct(spectrum, 2)/2, rtol=1e-05, atol=1e-08) plt.show()