Code covered by the BSD License  

Highlights from
Simple real Fourier series approximation

5.0

5.0 | 5 ratings Rate this file 46 Downloads (last 30 days) File Size: 3.39 KB File ID: #31013
image thumbnail

Simple real Fourier series approximation

by

 

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

| Watch this File

File Information
Description

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.

MATLAB release MATLAB 7.11 (R2010b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (11)
25 Feb 2013 Chihan  
22 Sep 2012 Kevin Hammonds

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?

12 Aug 2012 Tillmann

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

14 Jul 2012 Miguel Angel

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.

13 Jul 2012 Miguel Angel

Very fast and useful.
Thank you very much.

13 Jul 2012 Miguel Angel  
21 Nov 2011 Christiaan Tol

very helpful, thanks! I used it for my project

10 Nov 2011 Matt Tearle

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

10 Nov 2011 Matt Tearle

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

26 Oct 2011 Jong Kim

Could you explain how to produce frequency from your code?

26 Oct 2011 Jong Kim

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

Contact us