# Discrete Fourier transformation from ASCII (for noob)

I am not familiar with Mathematica (I am a physicist programming in Fortran) and my knowledge of it is very limited, unfortunately.

I would like to perform the Fourier trasform of a lightcurve which I have as an ASCII table (time, photon counts).

I can import the table:

Data = Import["/path/light_1band.dat", "Table"]


and define the arrays of variables:

time = Table[Data[[i]][[1]], {i, 1, Dimensions[Data][[1]]}]
counts = Table[Data[[i]][[2]], {i, 1, Dimensions[Data][[1]]}]


which I can plot:

ListPlot[Data]


and it looks fine.

Now I would like to perform the Fourier transformation of this light curve and my guess is that I should use the discrete Fourier transformation (because I don't have an analytical expression for my light-curve). The final goal is to have two arrays consisting in the Real and Imaginary integrals as a function of a range of frequencies (v) chosen by me so that I can use them to calculate the phaselag as:

phaselag(v) = ATAN(-Imaginary(v),Real(v))

and then the timelag as:

timelag(v) = phaselag(v)/ 2*pie*v

I am kind of lost because it seems that mathematica can do so much more than what I need, however I don't how to get those integrals which would be already enough form me.

If I just do:

Fourier[Data]


I end up with a table of complex numbers which I don't know what they represents and if I plot it:

ListLinePlot[Abs[Fourier[Data]]]


the plot doesn't help me to understand what I get. I also tried to perform the integrals by myself because I am not sure that the command 'Fourier' can give me what I need but my pitiful tentative:

ftw[(w_)?NumberQ, t_, v_] :=  Total[v*Exp[I w t]]


is far from working (and I struggle a lot with mathematica language)

I am aware that most of my problems are related to my ignorance of how mathematica works (and also partially on how Fourier transformation works), but if you understood what I need to obtain, any suggestion will be appreciated !

Can you help me ?

EDIT

Thanks for the replies. I forgot to mention that data is equally spaced in time (in 300 bins).

First column is time in seconds and second column are photons counts. Notice that the absolute values of time are not (scientifically) relevant because in the end I will compare the Fourier results of this light curve (obtained in one energy band) with the results obtained on different curves in other energy bands and I am interested in the delays among them.

Also, if relevant, I would analyze these curves in a positive frequency range (10^5-10^2 Hz). I am studying more about Fourier transformation and its definition on Mathematica in the links you shared, thank you!

Here is the data:

 0.3450333333E+01               0
0.1035100000E+02               0
0.1725166667E+02               0
0.2415233333E+02               0
0.3105300000E+02               0
0.3795366667E+02               0
0.4485433333E+02               0
0.5175500000E+02               0
0.5865566666E+02               0
0.6555633333E+02               0
0.7245699999E+02               0
0.7935766666E+02               0
0.8625833332E+02               1
0.9315900000E+02               0
0.1000596667E+03               0
0.1069603333E+03               0
0.1138610000E+03               0
0.1207616666E+03               0
0.1276623334E+03               0
0.1345630000E+03               0
0.1414636666E+03               0
0.1483643333E+03               0
0.1552650000E+03               0
0.1621656666E+03               0
0.1690663334E+03               0
0.1759670000E+03               0
0.1828676666E+03               0
0.1897683334E+03               0
0.1966690000E+03               0
0.2035696666E+03               0
0.2104703334E+03               0
0.2173710000E+03               0
0.2242716666E+03               0
0.2311723334E+03               0
0.2380730000E+03               0
0.2449736666E+03               0
0.2518743334E+03               0
0.2587750000E+03               0
0.2656756666E+03               0
0.2725763333E+03               0
0.2794769999E+03               0
0.2863776666E+03               0
0.2932783333E+03               0
0.3001789999E+03               0
0.3070796666E+03               0
0.3139803333E+03               1
0.3208809999E+03               0
0.3277816667E+03               0
0.3346823333E+03               0
0.3415829999E+03               1
0.3484836667E+03               1
0.3553843333E+03               0
0.3622849999E+03               1
0.3691856667E+03               0
0.3760863333E+03               0
0.3829869999E+03               1
0.3898876667E+03               0
0.3967883333E+03               0
0.4036889999E+03               1
0.4105896667E+03               1
0.4174903333E+03               3
0.4243909999E+03               0
0.4312916667E+03               0
0.4381923333E+03               0
0.4450929999E+03               0
0.4519936667E+03               0
0.4588943333E+03               1
0.4657950000E+03               0
0.4726956667E+03               0
0.4795963333E+03               2
0.4864970000E+03               1
0.4933976667E+03               0
0.5002983333E+03               0
0.5071990000E+03               0
0.5140996667E+03               1
0.5210003333E+03               0
0.5279009999E+03               1
0.5348016667E+03               0
0.5417023333E+03               0
0.5486029999E+03               0
0.5555036667E+03               0
0.5624043333E+03               0
0.5693049999E+03               0
0.5762056667E+03               1
0.5831063333E+03               0
0.5900069999E+03               0
0.5969076667E+03               1
0.6038083333E+03               3
0.6107089999E+03               4
0.6176096667E+03               1
0.6245103333E+03               1
0.6314109999E+03               0
0.6383116667E+03               0
0.6452123333E+03               1
0.6521129999E+03               2
0.6590136667E+03               2
0.6659143333E+03               3
0.6728149999E+03               3
0.6797156667E+03               5
0.6866163333E+03               6
0.6935169999E+03               7
0.7004176667E+03               4
0.7073183333E+03               5
0.7142189999E+03               4
0.7211196666E+03               6
0.7280203333E+03              11
0.7349209999E+03               5
0.7418216666E+03               4
0.7487223333E+03               3
0.7556229999E+03               8
0.7625236666E+03               6
0.7694243333E+03               8
0.7763250000E+03               5
0.7832256666E+03              14
0.7901263333E+03             111
0.7970270000E+03             387
0.8039276666E+03             682
0.8108283333E+03            1120
0.8177290000E+03            1461
0.8246296666E+03            1788
0.8315303333E+03            1985
0.8384310000E+03            2167
0.8453316666E+03            2308
0.8522323333E+03            2394
0.8591329999E+03            2447
0.8660336666E+03            2465
0.8729343333E+03            2503
0.8798349999E+03            2629
0.8867356666E+03            2629
0.8936363333E+03            2566
0.9005369999E+03            2518
0.9074376666E+03            2519
0.9143383333E+03            2498
0.9212390000E+03            2513
0.9281396666E+03            2380
0.9350403333E+03            2548
0.9419410000E+03            2368
0.9488416666E+03            2366
0.9557423333E+03            2317
0.9626430000E+03            2386
0.9695436666E+03            2383
0.9764443332E+03            2404
0.9833450000E+03            2222
0.9902456666E+03            2355
0.9971463334E+03            2160
0.1004047000E+04            2292
0.1010947666E+04            2112
0.1017848334E+04            2116
0.1024749000E+04            2098
0.1031649666E+04            2060
0.1038550333E+04            2101
0.1045451000E+04            1941
0.1052351666E+04            1989
0.1059252334E+04            1848
0.1066153000E+04            1819
0.1073053666E+04            1740
0.1079954333E+04            1643
0.1086855000E+04            1635
0.1093755666E+04            1583
0.1100656334E+04            1511
0.1107557000E+04            1510
0.1114457666E+04            1436
0.1121358333E+04            1289
0.1128259000E+04            1238
0.1135159666E+04            1263
0.1142060334E+04            1183
0.1148961000E+04            1124
0.1155861666E+04            1055
0.1162762333E+04             996
0.1169663000E+04             956
0.1176563666E+04             965
0.1183464334E+04             818
0.1190365000E+04             835
0.1197265666E+04             801
0.1204166333E+04             735
0.1211067000E+04             698
0.1217967666E+04             619
0.1224868334E+04             631
0.1231769000E+04             624
0.1238669666E+04             522
0.1245570333E+04             515
0.1252471000E+04             503
0.1259371666E+04             501
0.1266272334E+04             500
0.1273173000E+04             463
0.1280073666E+04             440
0.1286974333E+04             418
0.1293875000E+04             407
0.1300775667E+04             384
0.1307676334E+04             374
0.1314577000E+04             359
0.1321477666E+04             359
0.1328378334E+04             326
0.1335279000E+04             288
0.1342179667E+04             259
0.1349080334E+04             226
0.1355981000E+04             214
0.1362881666E+04             173
0.1369782334E+04             144
0.1376683000E+04             121
0.1383583667E+04             139
0.1390484334E+04             106
0.1397385000E+04             100
0.1404285666E+04              70
0.1411186334E+04              63
0.1418087000E+04              52
0.1424987667E+04              50
0.1431888334E+04              51
0.1438789000E+04              39
0.1445689666E+04              44
0.1452590334E+04              30
0.1459491000E+04              42
0.1466391667E+04              19
0.1473292334E+04              25
0.1480193000E+04              17
0.1487093666E+04              28
0.1493994334E+04              17
0.1500895000E+04              13
0.1507795667E+04              14
0.1514696334E+04               9
0.1521597000E+04               7
0.1528497666E+04               4
0.1535398334E+04               1
0.1542299000E+04               7
0.1549199667E+04               8
0.1556100334E+04               1
0.1563001000E+04               6
0.1569901666E+04               6
0.1576802334E+04               2
0.1583703000E+04               0
0.1590603667E+04               1
0.1597504334E+04               3
0.1604405000E+04               1
0.1611305666E+04               1
0.1618206334E+04               0
0.1625107000E+04               2
0.1632007667E+04               1
0.1638908334E+04               1
0.1645809000E+04               1
0.1652709666E+04               1
0.1659610334E+04               0
0.1666511000E+04               0
0.1673411667E+04               1
0.1680312334E+04               1
0.1687213000E+04               0
0.1694113666E+04               0
0.1701014334E+04               0
0.1707915000E+04               0
0.1714815667E+04               0
0.1721716334E+04               0
0.1728617000E+04               0
0.1735517666E+04               0
0.1742418334E+04               0
0.1749319000E+04               0
0.1756219667E+04               0
0.1763120334E+04               0
0.1770021000E+04               0
0.1776921666E+04               0
0.1783822334E+04               0
0.1790723000E+04               0
0.1797623667E+04               0
0.1804524334E+04               0
0.1811425000E+04               0
0.1818325666E+04               0
0.1825226334E+04               0
0.1832127000E+04               0
0.1839027667E+04               0
0.1845928334E+04               0
0.1852829000E+04               0
0.1859729666E+04               0
0.1866630334E+04               0
0.1873531000E+04               0
0.1880431667E+04               0
0.1887332334E+04               0
0.1894233000E+04               0
0.1901133666E+04               0
0.1908034334E+04               0
0.1914935000E+04               0
0.1921835667E+04               0
0.1928736334E+04               0
0.1935637000E+04               0
0.1942537666E+04               0
0.1949438333E+04               0
0.1956339000E+04               0
0.1963239667E+04               0
0.1970140333E+04               0
0.1977041000E+04               0
0.1983941666E+04               0
0.1990842333E+04               0
0.1997743000E+04               0
0.2004643667E+04               0
0.2011544333E+04               0
0.2018445000E+04               0
0.2025345666E+04               0
0.2032246333E+04               0
0.2039147000E+04               0
0.2046047667E+04               0
0.2052948333E+04               0
0.2059849000E+04               0
0.2066749666E+04               0

• Welcome to mathematica.SE! Could you please give us also few lines fo your data as an example?
– Johu
Sep 25 '18 at 9:54
• Possible duplicate of What do the X and Y axis stand for in the Fourier transform domain?
– Johu
Sep 25 '18 at 9:57
• May I suggest putting (a small subset of) light_1band.dat in Pastebin? Sep 25 '18 at 10:28
• Is your data equally spaced in time? If so, then Fourier, which implements the DFT is the tool you need. The answer Johu links to is good, and here is another that discusses the meaning of the output of Fourier mathematica.stackexchange.com/a/33625/1783 Sep 25 '18 at 13:06

Just to get you started i'll give you an example session, that shows some tricks and how you can start getting the data manipulation (magnitude/phase) you want from the data.

Let's start by setting the active directory to the directory where the Mathematica notebook is located (you can also use any other absolute path), assuming our data file is located here, too:

SetDirectory[NotebookDirectory[]]


Now let's import the data:

data = Import["light_1band.dat", "Table"];
Dimensions[data]
data//Short


{300,2}

{{3.45033,0},{10.351,0},{17.2517,0},{24.1523,0},<<293>>,{2052.95,0},{2059.85,0},{2066.75,0}}

Checking the dimension and a shortened version of the data (for the purpose of easier output inspection) makes it easy to know if we have imported the correct data. Let's also plot the data:

ListPlot[data]


Now to get the two columns (time and count) separately we can use a trick by just imagining the imported data as a 300x2 matrix and transposing that matrix to get a 2x300 matrix which we can assign to two different variables:

{time, counts} = Transpose[data];


The times seem to be perfectly evenly spaced

Differences[time]//Short


{6.90067,6.90067,6.90067,<<293>>,6.90067,6.90067,6.90067}

Let's verify this quickly:

Length[Union[Differences[time], SameTest -> (Abs[#1 - #2] < 10^(-5) &)]] == 1


True

Here we checked if all the neighbouring time differences are close to within 10^-5 to the same value, which seems to be the case.

Next we'll do a discrete fourier transform of the counts (not the times, because we already checked they are evenly spaced and otherwise this would do a 2D Fourier transform of {time,counts}, which is not what we want):

fcounts = Fourier[counts];
fcounts//Short


{7012.67 +0. I,<<298>>,-6411.28-681.684 I}

We see they are indeed complex numbers. Those encode both the magnitude and phase in each frequency band (including negative frequencies, but that's not important for now). We can easily convert those to a more easily plottable magnitude/phase representation by Mapping (the /@ is infix notation for Map) AbsArg over the complex values:

{magnitudes, phases} = Transpose[AbsArg /@ fcounts];


Now we can plot both the magnitudes and the phases separately:

ListLinePlot[magnitudes]
ListLinePlot[phases]


Hope this gives you a quicker start and some basis to explore and consult the online help to get more information on the used functions or more questions to ask here.

• this is tremendously helpful @Thies Heidecke Sep 25 '18 at 19:11
• Many thanks for the explication !! It is a very good starting point and it answers my doubts as a noob of Fourier transform in Mathematica
– Syph
Sep 26 '18 at 12:04