I have a bunch of data points (copied from Mathematica below), which I have 'ListPlotted'

I would now like to compute the Fourier series of this function (after doing a Fourier transform I assume? Or could I do it without?)

This video here (https://www.youtube.com/watch?v=VF1tOcEVGyU) describes how to do the Fourier series on Mathematica for a simpler function. Obviously my data is more complicated so I essentially need a modified version of that (with the slider and other paraphernalia if possible). Would anyone be able to help me please?

Many thanks!



EDIT: I've been told this is useful info to include! From the comments down below:

"If I saw correctly, that video is about Fourier integrals. Since you have data points, a discrete FT seems more appropriate. Maybe if you explained what is the underlying problem you wish to solve, someone could give better advice. I know how to do some things with the folded points I showed (e.g. smoothing), but the validity of such operations might be questionable. – Daniel Lichtblau

@DanielLichtblau - thanks - ultimately my goal is to analyse this sound wave from my violin to see what is going on. I might then investigate what happens at a particular transition note, though I still haven’t ironed out the details! I’m basically investigating/modelling the sound wave of my violin to see if I can make any interesting observations/comments.

That's useful information. For one, it indicates that trigs are sensible to use, periodicity is present, etc. It also means you might get advice from people familiar with the acoustics of bowed string instruments.

  • $\begingroup$ I would try generating a time series with a know Fourier series. Then you can check that you procedure for generating a Fourier series works, before you try it on data that you don't (yet) understand. $\endgroup$
    – mikado
    Jun 2, 2021 at 21:23
  • $\begingroup$ In this kind of data analysis, what you do depends on what you want the Fourier series for. Are you estimating a spectrum? Are you interpolating? $\endgroup$
    – John Doty
    Jun 3, 2021 at 13:43
  • $\begingroup$ @JohnDoty - thanks. All I want to do is function fit my data and estimate what it is at varying degrees of accuracy. $\endgroup$
    – rdx
    Jun 3, 2021 at 13:59
  • $\begingroup$ For that kind of thing, I use things like Fit and LinearModelFit. $\endgroup$
    – John Doty
    Jun 3, 2021 at 15:15
  • $\begingroup$ @JohnDoty - would that give me a sum of sine and cosine functions? Please could you demonstrate? $\endgroup$
    – rdx
    Jun 3, 2021 at 15:34

4 Answers 4


I think Daniel Lichtblau's answer is well worth studying (which I am doing as well). However here is a simple way forward that overcomes the irregular spacing of the data.

I have called your data, data, and interpolated it. Then plotted the interpolation and the data.

nn = Length@data;
fun = Interpolation[data];
{x1, x2} = fun[[1, 1]];
Plot[fun[x], {x, x1, x2}, Epilog -> {Red, Point[data]}]

 interpolated data

Then I sample the data to give as many points as the original data but points that are now equally spaced. Then I take the numerical Fourier transform using Fourier.

d2 = Table[fun[x], {x, x1, x2, (x2 - x1)/(nn - 1)}];
ft = Fourier[d2, FourierParameters -> {-1, -1}];
ListLinePlot[Abs[ft[[1 ;; nn/2]]], PlotRange -> All]
ListLinePlot[Arg[ft[[1 ;; nn/2]]], PlotRange -> All]

Modulus plot Phase plot

So there are the values of the Fourier components. I have only plotted half of them as the output from Fourier is symmetric.

Does that help?


The representation in terms of sin and cos functions was requested. This can be done but as there are 182 points there are in total 182 cos terms and a similar number of sin terms. It might be interesting to remove the smaller terms and look for an approximate version of the data.

First I work out the increment of time between points and the period of the data. The period is longer than the length of the data by one increment.

inc = (x2 - x1)/(nn - 1);
t0 = x2 - x1 + inc;

Now for the reconstruction. I have done this rather clumsily I am sure it can be simplified.

recon[t_] := 
     Re[ft[[n]]] Cos[(n - 1) 2 π t/t0] - 
      Im[ft[[n]]] Sin[(n - 1) 2 π t/t0], {n, nn/2 + 1}] +
    Sum[Re[ft[[n]]] Cos[-(nn - n + 1) 2 π t/t0] - 
      Im[ft[[n]]] Sin[-(nn - n + 1) 2 π t/t0], {n, nn/2 + 2, 

Different FourierParameters would require slightly different versions. Now to check if this is correct. I plot the original interpolation and the reconstruction that uses trig functions.

Plot[{fun[x], recon[x - x1]}, {x, x1, x2}]

enter image description here

The plots overlay nicely so this works. We can go further and look at the errors developed by interpolating the data.

Plot[{fun[x] - recon[x - x1]}, {x, x1, x2}, PlotRange -> All]

enter image description here

Note that in the reconstruction I started from time zero not time x1 so have to subtract x1 to get the plots to coincide. The error is about two orders of magnitude smaller than the original data.

If you want to see the trig form. Type in recon[t] where t is a symbol. Here are the first 10 terms

recon[t][[1 ;; 10]]


 0.000776623 + 0.00277817 Cos[7.61234 t] + 0.00270181 Cos[15.2247 t] + 
 0.0181226 Cos[22.837 t] - 0.00306168 Cos[30.4493 t] + 
 0.00115042 Cos[38.0617 t] + 0.0081826 Cos[45.674 t] - 
 0.00974623 Cos[53.2864 t] - 0.00466773 Cos[60.8987 t] - 
 0.00287715 Cos[68.511 t]


Now you could eliminate the smaller contributors to the list of trig functions and get an approximation; I leave that to you...

  • $\begingroup$ (1) I did something similar before I saw this, just to see if a smoothed version of the data looked plausible. This is a bit clunky, but... {times, vals} = Transpose@data; foldtimes = foldTimes[times, per]; newdata = SortBy[Transpose[{foldtimes, vals}], First]; kern = {1, 2, 3, 4, 5, 4, 3, 2, 1}/25; {newtimes, newvals} = Transpose@newdata; smoothvals = ListConvolve[kern, newvals]; smoothtimes = newtimes[[5 ;; -5]]; $\endgroup$ Jun 3, 2021 at 16:43
  • $\begingroup$ (2) Then do: ifunc = Interpolation[Transpose[{smoothtimes, smoothvals}], InterpolationOrder -> 1]; Plot[ifunc[t], {t, .01, per - .01}] and perhaps ft = Fourier[Table[ifunc[t], {t, .01, per - .01, .001}]]; ListPlot[Abs@ft] $\endgroup$ Jun 3, 2021 at 16:44
  • $\begingroup$ (3) Whether this type of smoothing makes sense is a question I cannot answer though. Outside my expertise and also it probably depends on the underlying problem that needs solving. $\endgroup$ Jun 3, 2021 at 16:45
  • $\begingroup$ @Hugh - thanks. That last graph you’ve drawn, if I’ve understood correctly is the components of the Fourier series for example sin(x) + sin (3x)/3 + ... etc. ? $\endgroup$
    – rdx
    Jun 3, 2021 at 16:50
  • $\begingroup$ @rdx Yes these are the magnitude and phase of each Fourier component. You might like to work with the real and imaginary parts of the Fourier components and not take their magnitude and phase. See here for some notes on how Fourier works. $\endgroup$
    – Hugh
    Jun 3, 2021 at 17:12

You could compute the irregularly-spaced periodogram and use that to obtain plausible frequency/period information. For this I use a resource function.

First plot the points.


enter image description here

Now we find the periodogram.

   ResourceFunction["IrregularPeriodogram"][w, data]}, {w, .1, 
   40, .1}]]

enter image description here

This gives a pretty good idea of the maximum frequency (I plotted to 100 actually, and saw the next peaks were getting smaller, so likely to correspond to higher harmonics). Compute this frequency and corresponding period.

freq = 
 NArgMax[{ResourceFunction["IrregularPeriodogram"][w, data[[All, 1]], 
    data[[All, 2]]], 16 <= w <= 29}, w]

(* Out[808]= 24.1185 *)

per = 2 Pi/freq

(* Out[809]= 0.260514 *)

We can fold the time axis by the period to get a better view of the data.

foldTimes[times_, period_] := period*FractionalPart[times/period]

ListPlot[Transpose[{foldTimes[data[[All, 1]], per], data[[All, 2]]}]]

enter image description here

--- edit ---

I can show a few ways one might take this further, or attack it differently.

First, one can reinterpolate on a regular grid. I will do this after a bit of smoothing.

{times, vals} = Transpose@data;
foldtimes = foldTimes[times, per];
newdata = SortBy[Transpose[{foldtimes, vals}], First];
n = 5;
kern = Join[Range[n],Reverse[Range[n-1]]]/n^2;
{newtimes, newvals} = Transpose@newdata;
smoothvals = ListConvolve[kern, newvals];
smoothtimes = newtimes[[n ;; -n]];

We plot this.

ListPlot[Transpose[{smoothtimes, smoothvals}]]

enter image description here

Now interpolate and plot.

ifunc = Interpolation[Transpose[{smoothtimes, smoothvals}], 
   InterpolationOrder -> 1];
Plot[ifunc[t], {t, .01, per - .01}]

enter image description here

Not great, but okay. I'll compute the (regular) Fourier transform, threshold out the small values, and show a new plot based on inverting the thresholded (thresheld?) FT.

ft = Fourier[Table[ifunc[t], {t, .01, per - .01, .001}]];
ListPlot[Re@InverseFourier[Threshold[ft, .03]]]

enter image description here

Not bad. Now we can get some useful data from the Fourier transform. First find where the (first) largest element is.

TakeLargest[Abs[ft] -> "Index", 1]

(* Out[1054]= {6} *)

Using the fraction where this appears along the time axis, and the point spacing can get a crude approximation of the frequency.

(6/(Length[ft] - 1))/.001

(* Out[1058]= 25.1046 *)

We can also use the inverted FT of the thresholded FT to create yet another smooth approximation.

equalspacedtimes = Range[.01, per - .01, .001];
newfunc = 
   Transpose[{equalspacedtimes, Re@InverseFourier[Threshold[ft, .03]]}]];
Plot[newfunc[t], {t, .01, per - .01}]

enter image description here

I'll stop at this. Time permitting, I might show a different way to attack this.

--- end edit ---

  • 1
    $\begingroup$ Thanks @Daniel Lichtblau - I get almost all of what you are saying. I think what you have done is determined the periodicity of the function and then simplified the data? Now, can we use this to do the Fourier series on? Thanks! $\endgroup$
    – rdx
    Jun 2, 2021 at 22:32
  • 2
    $\begingroup$ It's not (quite) a Fourier series that I computed. A modification of the code in that resource function would give you an irregular Fourier transform though. Also you can try to refine the period using values near what I found. Once you have that you might get a better (though still irregular) FT from the folded time series. $\endgroup$ Jun 2, 2021 at 23:40
  • 1
    $\begingroup$ Ah ok. Would it be possible for you to show me what you mean and go all the way to the final FS please? Even if that FS is inaccurate and has to be cut at a certain point - e.g. is possible to add a 'slider' as in the example video? I'm still slightly new to Mathematica and would be really grateful for your help. Thanks! $\endgroup$
    – rdx
    Jun 3, 2021 at 6:46
  • $\begingroup$ @DanielLichtblau How do you find functions like this (if you are not the author)? $\endgroup$
    – bbgodfrey
    Jun 3, 2021 at 13:46
  • 1
    $\begingroup$ @DanielLichtblau - thanks - ultimately my goal is to analyse this sound wave from my violin to see what is going on. I might then investigate what happens at a particular transition note, though I still haven’t ironed out the details! I’m basically investigating/modelling the sound wave of my violin to see if I can make any interesting observations/comments. $\endgroup$
    – rdx
    Jun 3, 2021 at 16:47

I want to show a different approach to this.The motivation, for me, is that I have of late been trying to learn more about wrangling with irregularly spaced data. Here is one method, dating to the early m90's or so, for interpolating onto a regular grid.

The Whittaker-Shannon interpolation method uses the cardinal sine (sinc) function to evaluate a band-limited periodc function based on its values at evenly spaced coordinates. We can invert that, or rather use the Moore-Penrose pseudoinverse, to obtain the regularly spaced values from the original data. This is done implicitly with the LeastSquares function. What I will show is a reasonable way to approximat, so that a sparse matrix can be used.

First set up a regular grid. I'll use 100 points.

{times, vals} = Transpose@data;
{mintime, maxtime} = MinMax[times];
steps = 100;
stepsize = (maxtime - mintime)/steps;
uniformtimes = Range[mintime, maxtime, stepsize];

This is the code one would use for the full W-S interpolation:

sincvals = 
  Table[Sinc[2*Pi*(times[[j]] - uniformtimes)/stepsize], {j, 
uniformvalues = LeastSquares[sincvals, vals];

Instead I will use the 7 nearest regular points for each original one, and create s SparseArray accordingly.

neartimes = Nearest[uniformtimes -> "Index"];
sincvalset[time_, j_, n_] := Module[
  {neighbors = neartimes[time, n]},
  Thread[Thread[{j, neighbors}] -> 
    Sinc[2*Pi*(time - uniformtimes[[neighbors]])/stepsize]]

sincvals = 
   Flatten@Table[sincvalset[times[[j]], j, 7], {j, Length[times]}]];

uniformvalues = LeastSquares[sincvals, vals];

Plotting along with the original data shows a plausible set of values.

ListPlot[{data, Transpose[{uniformtimes, uniformvalues}]}]

enter image description here

Now we can use the regular FT and have a look at larger frequencies.

ft = Fourier[uniformvalues];
ListPlotTake[Abs[ft], Floor[Length[ft]/2]], PlotRange -> All]

enter image description here

This time I see that there are some later values almost as large as the early peak. In particular there is a good-sized value at the 20th position. I use this to estimate the frequency, on grounds that the large value at 4 is probably a subharmonic of the main frequency.

freq = (20/(Length[ft] - 1))/stepsize
per = 2*Pi/freq

(* Out[1216]= 24.3647

Out[1217]= 0.257881 *)

I use foldTimes from my other response, to plot this modified point set.

ListPlot[Transpose[{foldTimes[uniformtimes, per], uniformvalues}]]

enter image description here

So this shows another way to get and use the regular Fourier transform from the irregularly spaced data.

  • $\begingroup$ Thank you - this has been very useful and I wish I could accept it in addition to Hugh's! $\endgroup$
    – rdx
    Jun 4, 2021 at 17:03

First, establish the range of the independent variable:

minX = Min[data[[All, 1]]];
maxX = Max[data[[All, 1]]];

Define a function to scale the independent variable to 2 Pi:

scaledX[x_] = 2 Pi (x - minX)/(maxX - minX);

Define a function to construct your sines and cosines. Here, n is the number of frequencies you want (not including zero, which is a special case):

fourierBasis[n_] := Flatten[{1, Table[{Cos[i scaledX[x]], Sin[i scaledX[x]]}, {i, 1, n}]}]

Do the fit:

fit[x_] = Fit[data, fourierBasis[20], x]
(* Long, tedious, function of x *)

Show the result:

Show[ListPlot[data], Plot[fit[x], {x, minX, maxX}]]

enter image description here

A defect in this is that representing the function as a Fourier series assumes periodicity, which means that the data points at the edges affect the fit near the opposite edges. This may be ameliorated somewhat by adjusting scaledX to map a wider range to 2 Pi. But for this kind of thing, I personally prefer Chebyshev series to Fourier.

  • $\begingroup$ can Mathematica tell me what that blue line is? i.e. what is the algebraic representation of that function? $\endgroup$
    – rdx
    Jun 3, 2021 at 19:51
  • $\begingroup$ Yes. It's the output of Fit[data, fourierBasis[20], x], used to define fit[x_]. I didn't show it because it's long and tedious ツ $\endgroup$
    – John Doty
    Jun 3, 2021 at 20:00
  • $\begingroup$ Thanks! Would you also know how to show the algebraic representation of the line that Hugh has drawn (another answer) - which shows the Fourier coefficients? $\endgroup$
    – rdx
    Jun 3, 2021 at 20:06
  • $\begingroup$ when I just tried, it doesn't give me a nice(wish) function of f but instead gives me a string starting (1, cos[16.9513x(0.06683+x)) etc ?? $\endgroup$
    – rdx
    Jun 3, 2021 at 20:19
  • 1
    $\begingroup$ You want an expression in a different language? Maybe Fit[...]//Cform yielding 0.0009345428575273403 + 0.0030644487101273076*Cos(7.654393327948233*(-0.30383 + x)) + ...? $\endgroup$
    – John Doty
    Jun 4, 2021 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.