File Exchange

## Simple real Fourier series approximation

version 1.0.0.1 (3.39 KB) by

Computes coefficients for the real Fourier series approximation to a data set.

Updated

Includes two functions: Fseries.m and Fseriesval.m
[a,b] = Fseries(X,Y,n) fits an nth-order Fourier expansion of the form
y = a_0/2 + Sum_k[ a_k cos(kx) + b_k sin(kx) ]
to the data in the vectors X & Y, using a least-squares fit.

Y = Fseriesval(a,b,X) evaluates the Fourier series defined by the coefficients a and b at the values in the vector X.

Extra arguments allow for rescaling of X data and sin-only or cosine-only expansions.

Example:
% Generate data
x = linspace(0,2,41)';
y = mod(2*x,1);
% Use FSERIES to fit
[a,b,yfit] = Fseries(x,y,10);
% Evaluate on finer grid
xfine = linspace(0,2)';
yfine = Fseriesval(a,b,xfine);
% Visualize results
plot(x,y,'x',x,yfit,'o',xfine,yfine)

This generates the attached image of a 10-term Fourier series approximation of a sawtooth wave.

goc3

Inbar Frenkel

Paul Barratt

Neil Fu

Teresa Lo

### Teresa Lo (view profile)

Thank you so much!!!!!!!!!!!!

Giorgio Fagioli

### Giorgio Fagioli (view profile)

Perfect code and very useful since already built-in functions of matlab do not allow fitting beyond eight terms. Thank you!

Zuowei Zhu

### Zuowei Zhu (view profile)

Very useful tool, thanks for your work!

Matt Tearle

### Matt Tearle (view profile)

@Amir Zakaria: Thanks for the feedback. I just had a look at what the Curve Fitting app is doing at its "Fourier" option includes the fundamental frequency as one of the fit parameters. So it's fitting a_k cos(w*k*x), where the coefficients a_k *and* the frequency w are parameters. My function is intended for just plain Fourier series expansion (a_k cos(k*x)). If you call Fseries with the scaling option set to false, and run the Curve Fitting app with w forced to 1 (you can set bounds on the parameters with "Fit Options"), you get the same values. Hope that helps.

Amir Zakaria

### Amir Zakaria (view profile)

I found this very useful, but when i compare the same number of coefficients for example 5 using this function and using cftool, i have different values. can someone explain why?

Zachary Hugo

ding

very good tool

Moisés

Chihan

Kevin Hammonds

### Kevin Hammonds (view profile)

Does anyone know if there is a way to extract a_o , a_n, and b_n from the command line or the script in the more general cos and sin terms?

Tillmann

### Tillmann (view profile)

Really great!
But, how can I visualize more than one period?

Miguel Angel

### Miguel Angel (view profile)

I have a question: I've used your code to fit a Fourier series to a set of data (t,x). However, when I apply the same found coefficients to a different vector of time t', I do not obtain the same function (I expected that were the case). What can be occuring? Thank you so much in advance.

Miguel Angel

### Miguel Angel (view profile)

Very fast and useful.
Thank you very much.

Miguel Angel

Christiaan Tol

### Christiaan Tol (view profile)

very helpful, thanks! I used it for my project

Matt Tearle

### Matt Tearle (view profile)

The expansion is in terms of sin/cos(kx) for k = 0:n, so the frequencies are simply k/L (for k from 0 to n), where L = max(x) - min(x). The units would be in inverse units of x. In the example given in the description, you can see the dominant contribution from the 4th sine term, which corresponds to a frequency of 4/(2-0) = 2 Hz (if x represented time in seconds).

Matt Tearle

### Matt Tearle (view profile)

Thanks for the feedback, too. Glad it could help.

Jong-Hwan Kim

### Jong-Hwan Kim (view profile)

Could you explain how to produce frequency from your code?

Jong-Hwan Kim

### Jong-Hwan Kim (view profile)

this is a very good tool. I really like it. It helps my project. Thanks