FFT bin

Section for discussions related to indicators, use of indicators, and building of trading stategies using indicators.

Moderator: admin

FFT bin

Postby TakisGen » Mon Sep 22, 2014 1:12 pm

Hi,

I am working on a function that returns best frequencies (periods) and pairs of frequencies for the applied stream and i have a question:

Which is the index of the highest frequency in the fft bin?

Is it binSize-1 or binSize/2-1 ?

Here is the code for members of this forum to evaluate. Maybe soneone gets a wild idea :lol:

Code: Select all

----------------------------------------------------------------------------
function fft(s,binSize,windowf,maxF,maxN,zeroR,maxR)
----------------------------------------------------------------------------
   if not windowf or windowf=="" then windowf="none" end
   if not maxF then maxF=999999 end
   if not maxN then maxN=999999 end
   if not zeroR then zeroR=4.236 end
   if not maxR then maxR=0.764 end

   local w={}
   w.none=function(i,binSize) return 1 end
   w.hanning=function(i,binSize) return 0.5*(1-math.cos(2*math.pi*i/(binSize-1.0))) end
   w.hanning0=function(i,binSize) return 0.5*(1+math.cos(2*math.pi*i/(binSize-1.0))) end
   w.hamming=function(i,binSize) local a=0.54; local b=1-a; return a-b*(1-math.cos(2*math.pi*i/(binSize-1.0))) end
   w.barlett=function(i,binSize) local L=binSize-1; return 1-((i-((binSize-1)/2))/(L/2)) end

   local bin=core.makeArray(binSize)
   local i
   local last=s:size()-1
   for i=0,binSize-1 do
      bin[i]=s[last-i]*w[windowf](i,binSize)
   end
   mathex.fft(bin)
   local t={}
   for i=1,binSize-1 do
      local f=math.floor(binSize*2/i)
      if f<=maxF then
         local v=bin[i]
         local t2={f,i,v}
         table.insert(t,t2)
      end
   end
   table.sort(t,function(a,b) return a[3]>b[3] end)
   local tFreq={}
   for k,v in pairs(t) do
      if not table.key(tFreq,v[1]) then
         table.insert(tFreq,v[1])
         if #tFreq==maxN then break end
      end
   end
   local t={}
   for k1,v1 in pairs(tFreq) do
      for k2,v2 in pairs(tFreq) do
         local r=v2/v1-zeroR
         if r<0 then r=r*-1 end
         if r<maxR then
            local tt={v1,v2,r}
            table.insert(t,tt)
         end
      end
   end
   table.sort(t,function(a,b) return a[3]<b[3] end)
   local tPairs={}
   for k,v in pairs(t) do
      local txt=v[1]..":"..v[2]
      if not table.key(tPairs,txt) then
         table.insert(tPairs,txt)
         if #tPairs==maxN then break end
      end
   end
   return bin,tFreq,tPairs
end


Thx
Takis
TakisGen
 
Posts: 34
Joined: Sat May 28, 2011 10:09 am

Re: FFT bin

Postby Valeria » Thu Sep 25, 2014 3:31 am

Hi TakisGen,

If I understand you correctly, you want to get the number of elements of the array with the array size binSize after the call of mathex.fft. If yes, binSize - 1 is correct.
Valeria
 

Re: FFT bin

Postby TakisGen » Thu Sep 25, 2014 11:36 am

Hi Valeria,

No, this is clear to me. What is not clear is which index represents which frequency.
I understand that i=0 contains the dc component and wavelength is rising with each i+1.
Now, as i think, binSize/2-1 should contain information about highest wavelength in the bin as there must be a sampling frequency involved here. Am i wrong?
In short, what frequency is contained in binSize/2 and what in binSize positions?

Thanks for you responce,
Takis
TakisGen
 
Posts: 34
Joined: Sat May 28, 2011 10:09 am

Re: FFT bin

Postby Valeria » Fri Sep 26, 2014 3:42 am

Hi Takis,

Yes, you are right, binSize/2-1 contains information about highest wavelength if fft function counts the right values. The rest values are not written again after binSize/2-1 and the mathex.fft function and have the initial array of values.
Valeria
 


Return to Discussions

Who is online

Users browsing this forum: No registered users and 2 guests