Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Finding Fourier coefficients and the series in Matlab

2,303 views
Skip to first unread message

Hemant

unread,
Oct 26, 2009, 12:49:02 PM10/26/09
to
Hi I was wondering if I can get some help on how to find the Fourier coefficients Ak, Bk, Ck for a periodic Function in MATLAB. After finding the coef's I would like to use fourier series approximation to get back the function.
Is there such a function that does just this.
Thanks...

Sean Larkin

unread,
Oct 26, 2009, 1:16:05 PM10/26/09
to
"Hemant " <san...@hotmail.com> wrote in message <hc4jtu$k99$1...@fred.mathworks.com>...

> Hi I was wondering if I can get some help on how to find the Fourier coefficients Ak, Bk, Ck for a periodic Function in MATLAB. After finding the coef's I would like to use fourier series approximation to get back the function.
> Is there such a function that does just this.
> Thanks...

I pretty sure there isn't a MATLAB function that does this. You're going to have to solve for these in a for-loop using the Fourier equations. I say this because I remember doing this in a partial diff eq. class. It was not that difficult. In fact, if I can find my code later today I'll post it here... but I haven't seen that code in years.

Matt Fetterman

unread,
Oct 26, 2009, 1:31:20 PM10/26/09
to
"Sean Larkin" <slarki...@sbcglobal.net> wrote in message <hc4lgl$4om$1...@fred.mathworks.com>...

> "Hemant " <san...@hotmail.com> wrote in message <hc4jtu$k99$1...@fred.mathworks.com>...
> > Hi I was wondering if I can get some help on how to find the Fourier coefficients Ak, Bk, Ck for a periodic Function in MATLAB. After finding the coef's I would like to use fourier series approximation to get back the function.
> > Is there such a function that does just this.
> > Thanks...
>
> I pretty sure there isn't a MATLAB function that does this. You're going to have to ?solve for these in a for-loop using the Fourier equations. I say this because I ??>remember doing this in a partial diff eq. class. It was not that difficult. In fact, if I can >find my code later today I'll post it here... but I haven't seen that code in years.

How about the fft function. Then after you take the fft choose the largest coefficients.

Hemant

unread,
Oct 26, 2009, 9:38:01 PM10/26/09
to
> I pretty sure there isn't a MATLAB function that does this. You're going to have to solve for these in a for-loop using the Fourier equations. I say this because I remember doing this in a partial diff eq. class. It was not that difficult. In fact, if I can find my code later today I'll post it here... but I haven't seen that code in years.

Thanks Sean,
I originally did it the way you have suggested and have favourable results with it, however the problem is that when I try to plot it, Matlab complains that the array is SYMBOLIC and it doesnt like that for plotting especially when the x axis is time and it is of type DOUBLE.
I need to figure out a way to convert the sym to Double.
Any suggestion??

Nasser M. Abbasi

unread,
Oct 26, 2009, 9:51:38 PM10/26/09
to

"Hemant " <san...@hotmail.com> wrote in message
news:hc5itp$bh2$1...@fred.mathworks.com...

look into ezplot, or to convert sym to double, use double() ?

may be if you post the final expression you can't plot, someone can help you
plot it.

--Nasser


Steven Lord

unread,
Oct 27, 2009, 9:55:26 AM10/27/09
to

"Hemant " <san...@hotmail.com> wrote in message
news:hc5itp$bh2$1...@fred.mathworks.com...

>> I pretty sure there isn't a MATLAB function that does this. You're going
>> to have to solve for these in a for-loop using the Fourier equations. I
>> say this because I remember doing this in a partial diff eq. class. It
>> was not that difficult. In fact, if I can find my code later today I'll
>> post it here... but I haven't seen that code in years.
>
> Thanks Sean,
> I originally did it the way you have suggested and have favourable results
> with it, however the problem is that when I try to plot it, Matlab
> complains that the array is SYMBOLIC and it doesnt like that for plotting
> especially when the x axis is time and it is of type DOUBLE.

Of course. Where should PLOT place the marker if the x coordinate is
2*t*cos(t), where t is a symbolic variable?

> I need to figure out a way to convert the sym to Double.
> Any suggestion??

If your symbolic expression doesn't contain any symbolic variables, use
DOUBLE. If it does, use SUBS (and perhaps DOUBLE after that.)

--
Steve Lord
sl...@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


Hemant

unread,
Oct 28, 2009, 5:35:03 AM10/28/09
to
"Steven Lord" <sl...@mathworks.com> wrote in message <hc6u2u$smv$1...@fred.mathworks.com>...


Please see the code.

clear all, close all, clc
syms A0 Ak Bk Ck dt fn k t T0 W0 Y % declaring variables
%DEFINE FUNCTION
% fn=t;
%FIND FOURIER COEF's
W0=1; %define W0=1 fundamental frequency
T0=(2*pi); %Period of the function
AK=(2/T0)*int(t*cos(k*t*W0),t,-(T0/2),(T0/2));
BK=((2/T0)*int(t*(sin(k*t*W0)),t,-(T0/2),(T0/2)));
CK=(1/T0)*int(t*exp(-j*k*W0*t),t,-(T0/2),(T0/2));
%SIMPLIFY THE COEF's
Ak=simplify (AK)
Bk= round(BK)
Ck=simplify (CK)
%INITIALIZE VARIABLES
dt=T0/100;
%CALCULATE A0
A0=(1/T0)*int(t),t,-(T0/2),(T0/2);
t=[-pi:dt:pi]; %the variable limits and resolution
Y=zeros(size(t));
%PLOT FUNCTION
for k=[1:2:21];
y=A0/2+Ak*cos(k*t*W0)+Bk*sin(k*t*W0);
%double(y);
%EZPLOT(y);
% Y=Y+y
end

%plot(t,Y,'LineWidth',2);
axis([-4 4 -4 4]);
grid;
xlabel('time, s');
ylabel('v1(t), (V)');

Steven Lord

unread,
Oct 28, 2009, 9:28:24 AM10/28/09
to

"Hemant " <san...@hotmail.com> wrote in message
news:hc9387$17r$1...@fred.mathworks.com...

> "Steven Lord" <sl...@mathworks.com> wrote in message
> <hc6u2u$smv$1...@fred.mathworks.com>...

*snip*

> Please see the code.
>
> clear all, close all, clc
> syms A0 Ak Bk Ck dt fn k t T0 W0 Y % declaring variables
> %DEFINE FUNCTION
> % fn=t;
> %FIND FOURIER COEF's
> W0=1; %define W0=1 fundamental frequency
> T0=(2*pi); %Period of the function
> AK=(2/T0)*int(t*cos(k*t*W0),t,-(T0/2),(T0/2));
> BK=((2/T0)*int(t*(sin(k*t*W0)),t,-(T0/2),(T0/2)));
> CK=(1/T0)*int(t*exp(-j*k*W0*t),t,-(T0/2),(T0/2));
> %SIMPLIFY THE COEF's
> Ak=simplify (AK)
> Bk= round(BK)

This doesn't work -- BK is a function of t and k, so there's no way to tell
how to round that symbolic expression until after you've substituted values
for k and t into it.

> Ck=simplify (CK)
> %INITIALIZE VARIABLES
> dt=T0/100;
> %CALCULATE A0
> A0=(1/T0)*int(t),t,-(T0/2),(T0/2);

I don't think this is parenthesized the way you intended.

> t=[-pi:dt:pi]; %the variable limits and resolution
> Y=zeros(size(t));
> %PLOT FUNCTION
> for k=[1:2:21];
> y=A0/2+Ak*cos(k*t*W0)+Bk*sin(k*t*W0);

Some or all of A0, Ak, and Bk are functions of the symbolic variables k and
t -- you should SUBS values for those symbolic variables into those
coefficients, and use .* to perform elementwise multiplication, when
computing y. If you do so, I believe Y will be either a double array or a
sym array that can be evaluated to return a double array using DOUBLE and
plotted using PLOT.

*snip*

0 new messages