//@version=4
study("Low Frequency Fourier Transform", overlay = false, shorttitle="LFFT")
//This Study uses the Real Discrete Fourier Transform algorithm to generate 3 sinusoids possibly indicative of future price.
//I got information about this RDFT algorithm from "
www.dspguide.com/ch8/5.htm
" and "
www.dspguide.com/ch8/6.htm
".
//Declaration of const
pi = acos(-1)
//Declaration of user-defined variables
N = input(defval=64, title="Sample Time Window", type=input.integer, minval=2, maxval=600, confirm=false, step=1, options=[2,4,8,16,32,64,128,256,512,1024,2048,4096])
//Real part of the Frequency Domain Representation
ReX(k) =>
sum = 0.0
for i=0 to N-1
sum := sum + close*cos(2*pi*k*i/N)
return = sum
//Imaginary part of the Frequency Domain Representation
ImX(k) =>
sum = 0.0
for i=0 to N-1
sum := sum + close*sin(2*pi*k*i/N)
return = -sum
//Get sinusoidal amplitude from frequency domain
ReX_(k) =>
case = 0.0
if(k!=0 and k!=N/2)
case := 2*ReX(k)/N
if(k==0)
case := ReX(k)/N
if(k==N/2)
case := ReX(k)/N
return = case
//Get sinusoidal amplitude from frequency domain
ImX_(k) =>
return = -2*ImX(k)/N
//Get full Fourier Transform
x(i, N) =>
sum1 = 0.0
sum2 = 0.0
for k=0 to N/2
sum1 := sum1 + ReX_(k)*cos(2*pi*k*i/N)
for k=0 to N/2
sum2 := sum2 + ImX_(k)*sin(2*pi*k*i/N)
return = sum1+sum2
//Get single constituent sinusoid
sx(i, k) =>
sum1 = ReX_(k)*cos(2*pi*k*i/N)
sum2 = ImX_(k)*sin(2*pi*k*i/N)
return = sum1+sum2
//Plot 3 Low-Freq Sinusoids
plot(sx(0,1), color=#00ff00)
plot(sx(0,2), color=#0000ff)
plot(sx(0,3), color=#ff0000)