python - Fourier transform in scipy and Parseval's identity -
while working on fft in python, i'm running problems normalization of function , calculating squared sum of coefficients.
my code follows:
import pandas pd import numpy np import scipy.fftpack # sample points n = 1024 # sample spacing t = 1.0/512.0 # example function x = np.linspace(0.0, n*t, n) y = 1.5 * np.sin(5*2*np.pi*x) + 3 * np.sin(6*2*np.pi*x) df = pd.dataframe({'x': x, 'y': y}) sumy = sum(abs(df['y'])**2) df['fft (y)'] = scipy.fftpack.fft(df['y'])/(n/2) df['fft (freq)'] = scipy.fftpack.fftfreq(n, t) sumffty = sum(abs(df['fft (y)'])**2) print sumy, sumffty
according parseval's identity, sums should equal; however, they're off factor of 256 (sumffty being smaller one). missing?
parseval's theorem states total power in 2 domains (space/time , frequency) equal. statement, dft this. notice, in spite of 1/n on right, not mean power, total power. reason 1/n normalization convention dft.
put in python, means time/space signal sig
, parseval equivalence may stated as:
np.sum(np.abs(sig)**2) == np.sum(np.abs(np.fft.fft(sig))**2)/sig.size
below complete example, starting toy cases (one , 2 dimensional arrays filled 1 1s). note used .size
property of numpy.ndarray, returns total number of elements in array. hope helps!
import numpy np # 1-d, 4 elements: ones_1d = np.array([1.,1.,1.,1.]) ones_1d_f = np.fft.fft(ones_1d) # compute total power in space , frequency domains: space_power_1d = np.sum(np.abs(ones_1d)**2) freq_power_1d = np.sum(np.abs(ones_1d_f)**2)/ones_1d.size print 'space_power_1d = %f'%space_power_1d print 'freq_power_1d = %f'%freq_power_1d
i'm sorry couldn't use example directly--i'm not pandas user , couldn't understand perfectly.
Comments
Post a Comment