Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Jorge Eduardo Pérez Pérez <perez.jorge@ur.edu.co> |
To | <statalist@hsphsun2.harvard.edu> |
Subject | st: Rescaling Fast Fourier Transform |
Date | Tue, 4 Jan 2011 17:34:20 -0500 |
Dear Statalist: I'm trying to implement in Stata the Ideal Band Pass Filter for time series described in Corbae and Ouliaris (2006) "Extracting Cycles from Nonstationary data"Econometric theory and Practice: Frontiers of Analysis and Applied Research (Cambridge and New York: Cambridge University Press), pp 167-177 At some point, I have to compute a Discrete Fourier Transform. I translated a function -dft- to do this into Mata: complex matrix dft( transmorphic matrix x) { real matrix yr, yi, dftr, dfti, range real scalar n, i, ae complex matrix y, df y=C(x) yr=Re(y) yi=Im(y) n=rows(x) dftr=J(n,1,.) dfti=J(n,1,.) range=range(1,n,1) for (i=0; i<=n-1; i++) { ae=-2*pi()/n dftr[i+1]=colsum(yr:*cos((range:-1):*ae:*i)-yi:*sin((range:-1):*ae:*i)) dfti[i+1]=colsum(yr:*sin((range:-1):*ae:*i)+yi:*cos((range:-1):*ae:*i)) } df=conj(C(dftr,dfti)) return(df) } For vectors with length equal to a power of 2, this function yields the same results as the built-in -fft- function (That uses the Fast Fourier Transform algorithm), up to rounding error: mata : x=range(1,4,1) : dft(x) 1 +--------------------+ 1 | 10 | 2 | -2 - 2i | 3 | -2 + 9.7972e-16i | 4 | -2 + 2i | +--------------------+ : fft(x) 1 +-----------+ 1 | 10 | 2 | -2 - 2i | 3 | -2 | 4 | -2 + 2i | +-----------+ However, if the length of the vector is not a power of 2, I have to use zero padding. i.e: -fft(ftpad(x))- . How do I scale the results obtained from -fft(ftpad(x))- to be the same as the ones obtained from -dft- ? Thank you. _______________________ Jorge Eduardo Pérez Pérez * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/