Home:ALL Converter>How to compute Fourier coefficients with MATLAB

How to compute Fourier coefficients with MATLAB

Ask Time:2013-02-23T00:28:28         Author:Rick

Json Formatter

I'm trying to compute the Fourier coefficients for a waveform using MATLAB. The coefficients can be computed using the following formulas:

enter image description here

enter image description here

T is chosen to be 1 which gives omega = 2pi.

However I'm having issues performing the integrals. The functions are are triangle wave (Which can be generated using sawtooth(t,0.5) if I'm not mistaking) as well as a square wave.

I've tried with the following code (For the triangle wave):

    function [ a0,am,bm ] = test( numTerms )
            b_m = zeros(1,numTerms);
            w=2*pi;
            for i = 1:numTerms
                    f1 = @(t) sawtooth(t,0.5).*cos(i*w*t);
                    f2 = @(t) sawtooth(t,0.5).*sin(i*w*t);
                    am(i) = 2*quad(f1,0,1);
                    bm(i) = 2*quad(f2,0,1);
            end
    end

However it's not getting anywhere near the values I need. The b_m coefficients are given for a triangle wave and are supposed to be 1/m^2 and -1/m^2 when m is odd alternating beginning with the positive term.

The major issue for me is that I don't quite understand how integrals work in MATLAB and I'm not sure whether or not the approach I've chosen works.

Edit: To clairify, this is the form that I'm looking to write the function on when the coefficients have been determined:

enter image description here

Here's an attempt using fft:

    function [ a0,am,bm ] = test( numTerms )
            T=2*pi;
            w=1;
            t = [0:0.1:2];
            f = fft(sawtooth(t,0.5));
            am = real(f);
            bm = imag(f);
            func = num2str(f(1));
            for i = 1:numTerms
                    func = strcat(func,'+',num2str(am(i)),'*cos(',num2str(i*w),'*t)','+',num2str(bm(i)),'*sin(',num2str(i*w),'*t)');
            end
            y = inline(func);
            plot(t,y(t));
    end

Author:Rick,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/15028793/how-to-compute-fourier-coefficients-with-matlab
macduff :

Looks to me that your problem is what sawtooth returns the mathworks documentation says that:\n\nsawtooth(t,width) generates a modified triangle wave where width, a scalar parameter between 0 and 1, determines the point between 0 and 2π at which the maximum occurs. The function increases from -1 to 1 on the interval 0 to 2πwidth, then decreases linearly from 1 to -1 on the interval 2πwidth to 2π. Thus a parameter of 0.5 specifies a standard triangle wave, symmetric about time instant π with peak-to-peak amplitude of 1. sawtooth(t,1) is equivalent to sawtooth(t).\n\nSo I'm guessing that's part of your problem.\nAfter you responded I looked into it some more. Looks to me like it's the quad function; not very accurate! I recast the problem like this:\n function [ a0,am,bm ] = sotest( t, numTerms )\n bm = zeros(1,numTerms);\n am = zeros(1,numTerms);\n % 2L = 1\n L = 0.5;\n for ii = 1:numTerms\n am(ii) = (1/L)*quadl(@(x) aCos(x,ii,L),0,2*L);\n bm(ii) = (1/L)*quadl(@(x) aSin(x,ii,L),0,2*L);\n end\n ii = 0;\n a0 = (1/L)*trapz( t, t.*cos((ii*pi*t)/L) ); \n % now let's test it\n y = ones(size(t))*(a0/2);\n for ii=1:numTerms\n y = y + am(ii)*cos(ii*2*pi*t);\n y = y + bm(ii)*sin(ii*2*pi*t);\n end\n figure; plot( t, y);\n end\n\n function a = aCos(t,n,L)\n a = t.*cos((n*pi*t)/L);\n end\n\n function b = aSin(t,n,L)\n b = t.*sin((n*pi*t)/L);\n end\n\nAnd then I called it like:\n[ a0,am,bm ] = sotest( t, 100 );\n\nand I got:\n\nSweetness!!!\nAll I really changed was from quad to quadl. I figured that out by using trapz which worked great until the time vector I was using didn't have enough resolution, which led me to believe it was a numerical issue rather than something fundamental. Hope this helps!",
2013-02-22T17:07:15
yy