# Help with Fourier Series Expansions numbers not matching up withexcel calculations tia sal22

Discussion in 'MATLAB' started by Rick T, Jan 20, 2010.

1. ### Rick TGuest

Help with Fourier Series Expansions numbers not matching up with excel
calculations tia sal22

Greetings All

I’ve worked out a Fourier series expansion problem in excel (which is
correct) and in matlab but my An and Bn coefficients aren’t matching
up anyone know why? or have any suggestions on adjusting my matlab
code.

Excel Calculations:
http://demos.onewithall.net/discrete_fourier_expansion_coefficients.jpg

matlab see code below:

clear
clc
t1 = tic; %start timer to see how long it takes to process
%
% DFT in a direct implementation loops
%
% Enter Data in y
x=linspace(0,1,10);
wt=2*pi*x;

y=[14 18.7 9 4.1 6.7 6 6.3 8.4 4 2.9];
% Get length of data vector or number of samples
N=length(y);
% Compute Fourier Coefficients

for p=1:N/2+1
An(p)=0;
Bn(p)=0;
for n=1:N
An(p)=An(p)+2/N*y(n)*cos(2*pi*(p-1)*n/N)';
Bn(p)=Bn(p)+2/N*y(n)*sin(2*pi*(p-1)*n/N)';
disp([num2str(2/N*y(n)),'Cos[',num2str((2*pi*(p-1)*n/N)),'*x]
+'...
num2str(2/N*y(n)),'Sin[',num2str((2*pi*(p-1)*n/N)),'*x]
+']);
end
end
An(N/2+1)=An(N/2+1)/2;

% Reconstruct Signal - pmax is number of frequencies used in
increasing order
pmax=5;
for n=1:N
ynew(n)=An(1)/2;
for p=2max
ynew(n)=ynew(n)+An(p)*cos(2*pi*(p-1)*n/N)+Bn(p)*sin(2*pi*(p-1)
*n/N);
%ynew2(n,p)=An(p)*cos(2*pi*(p-1)*n/N);
end;
end;
toc(t1) %stops timer and tells how long it took to run

% Plot Data
plot(y,'o')

% Plot reconstruction over data
hold on
plot(ynew,'r')
hold off

When I check my matlab and excel An and Bn coefficients they don’t
match up.
For my excel code my coefficients are: (which are correct)

My A0 coefficient is 8.01
My An coefficients are:
3.022410928,2.012410924,0.9875890712,-0.02241093140,-0.02000000000
My Bn coefficients are:
2.037572130,4.022534640,2.999133180,0.04141345680,0

For my matlab code my coefficients are:

My A0 coefficients is 8.01
My An coefficients are:
1.24752695783729,-3.20378860391235,-3.15752695783729,-0.00621139608765053,0..00999999999999995
My Bn coefficients are:
3.42495905108121,3.15694809226703,0.0124699004377189,-0.0466770043125478,-7..69078189864538e-16

Anyone have any idea why these values are so different? And how I can
adjust my matlab code to fix it?

If your wondering what I’m trying to do I’m trying to make the signal
periodic using sin and cos values.

Tia sal22

Rick T, Jan 20, 2010

2. ### Ken PledgerGuest

This news group <alt.math.recreational> isn't the best place to
and displaying data". You're likely to get more expert attention in the
group <sci.math.num-analysis>.

Ken Pledger.

Ken Pledger, Jan 20, 2010

3. ### Greg HeathGuest

You have posted this, separately in multiple groups.
See my reply in one of the others.

Greg

Greg Heath, Jan 21, 2010