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

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

  1. Rick T

    Rick T Guest

    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=2:pmax
    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
    #1
    1. Advertisements

  2. Rick T

    Greg Heath Guest

    clear all, clc

    Aexcel =[ 8.01, 3.022410928, 2.012410924, 0.9875890712, ...
    -0.02241093140,-0.02000000000]';
    Bexcel =[0, 2.037572130, 4.022534640, 2.999133180, ...
    0.04141345680, 0 ]';
    tic
    y = [14 18.7 9 4.1 6.7 6 6.3 8.4 4 2.9]';
    N = length(y);
    C = fft(y)/N;
    A = real(C);
    B = imag(C);
    toc
    % Single-Sided (f >= 0)
    As = [A(1); 2*A(2:N/2); A(N/2+1)];
    Bs = [B(1); 2*B(2:N/2); B(N/2+1)];

    summary = [Aexcel As Bexcel Bs]
    toc

    % ans =
    % 8.0100 8.0100 0 0
    % 3.0224 3.0224 2.0376 -2.0376
    % 2.0124 2.0124 4.0225 -4.0225
    % 0.9876 0.9876 2.9991 -2.9991
    % -0.0224 -0.0224 0.0414 -0.0414
    % -0.0200 -0.0100 0 0
    %
    % elapsed_time = 0

    % Therefore Aexcel(N/2+1) is a factor of two
    % too large and Bexcel:)) has the wrong sign

    Hope this helps.

    Greg
     
    Greg Heath, Jan 21, 2010
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.