Skip to content

Instantly share code, notes, and snippets.

@dzonesasaki dzonesasaki/dft.bas

Last active Aug 29, 2015
Embed
What would you like to do?
DFT on OpenOffice Calc
Sub FourierTrans
Dim oSelFile As Object
Dim oSelSheet As Object
Dim uiLenStrm As Integer
Dim uiLenCoef As Integer
Dim uiLenOut As Integer
Dim fsumBuff As Double
oSelFile = ThisComponent
rem // get stream
rem // signalのシートのA列に信号,B1に信号の長さを入れておく.spectのシートを作っておく
oSelSheet = oSelFile.Sheets.getByName("signal")
uiLenStrm = oSelSheet.getCellByPosition(1, 0 ).Value
Dim mfStream( uiLenStrm+1) as Double
for uilp = 0 to uiLenStrm
mfStream(uilp) = oSelSheet.getCellByPosition(0, uilp ).Value
next uilp
rem // calculation Descrete Fourier Transform
Dim mfResultR( uiLenStrm+1) as Double
Dim mfResultI( uiLenStrm+1) as Double
for uilpA = 0 to uiLenStrm-1
fsumBuffR = 0.0
fsumBuffI = 0.0
for uilpB = 0 to uiLenStrm-1
fsumBuffR = fsumBuffR + mfStream(uilpB)*cos(2*PI*uilpA*uilpB/uiLenStrm)
fsumBuffI = fsumBuffI + mfStream(uilpB)*sin(2*PI*uilpA*uilpB/uiLenStrm)
next uilpB
mfResultR(uilpA)=fsumBuffR
mfResultI(uilpA)=fsumBuffI
next uilpA
oSelSheet = oSelFile.Sheets.getByName("spect")
for uilpA = 0 to uiLenStrm-1
oSelSheet.getCellByPosition(0, uilpA ).Value = (uilpA)/uiLenStrm
oSelSheet.getCellByPosition(1, uilpA ).Value = mfResultR(uilpA)
oSelSheet.getCellByPosition(2, uilpA ).Value = mfResultI(uilpA)
oSelSheet.getCellByPosition(3, uilpA ).Value = mfResultR(uilpA)*mfResultR(uilpA)+mfResultI(uilpA)*mfResultI(uilpA)
rem oSelSheet.getCellByPosition(4, uilpA ).Value = atan(mfResultI(uilpA)/mfResultR(uilpA) + (mfResultI(uilpA)*mfResultR(uilpA))=0)
rem oSelSheet.getCellByPosition(4, uilpA ).Value = atan(mfResultI(uilpA)/mfResultR(uilpA) )
next uilpA
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.