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
- 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