SlideShare une entreprise Scribd logo
1  sur  19
Fourier Series Example
MATLAB Code

%      ***** MATLAB Code Starts Here *****
%

%FOURIER_SERIES_01_MAT

%

fig_size = [232 84 774 624];

x = [0.1 0.9 0.1]; % 1 period of x(t)

x = [x x x x]; % 4 periods of x(t)

tx = [-2 -1 0 0 1 2 2 3 4 4 5 6]; % time points for x(t)

figure(1),plot(tx,x),grid,xlabel('Time (s)'),ylabel('Amplitude'),...

title('Periodic Signal x(t)'),axis([-2 6 0 1]),...

set(gcf,'Position',fig_size)

%

a0 = 0.5; % DC component of Fourier Series

ph0 = 0;

n = [1 3 5 7 9]; % Values of n to be evaluated

an = -3.2 ./ (pi * n).^2; % Fourier Series coefficients

mag_an = abs(an);

ph_an = -180 * ones(1,length(n));

%

n = [0 n];

mag_an = [a0 mag_an]; % Including a0 with a_n

ph_an = [ph0 ph_an];

%

figure(2),clf,subplot(211),plot(n,mag_an,'o'),grid,xlabel('Harmonic
Number'),...
ylabel('Magnitude'),title('Fourier Series Magnitude'),axis([0 10 0
0.6]),...

set(gcf,'Position',fig_size)

%

subplot(212),plot(n,ph_an,'o'),grid,xlabel('Harmonic Number'),...

ylabel('Phase (deg)'),title('Fourier Series Phase'),axis([0 10 -200 0]),...

set(gcf,'Position',fig_size)

%

w0 = pi; % Fundamental Frequency

t = [-2:0.002:6]; % time vector for approximations

%

x1 = 0; % approximation with DC + 1 term

for i = 1:2

x1 = x1 + mag_an(i)*cos(n(i)*w0*t + ph_an(i)*pi/180);

end

%

x2 = x1; % approximation with DC + 2 terms

i = 3;

x2 = x2 + mag_an(i)*cos(n(i)*w0*t + ph_an(i)*pi/180);

%

x3 = x2; % approximation with DC + 3 terms

i = 4;

x3 = x3 + mag_an(i)*cos(n(i)*w0*t + ph_an(i)*pi/180);

%

x4 = x3; % approximation with DC + 5 terms

for i = 5:6

x4 = x4 + mag_an(i)*cos(n(i)*w0*t + ph_an(i)*pi/180);

end
%

figure(3),subplot(221),plot(t,x1),grid,xlabel('Time (s)'),...

ylabel('Amplitude'),title('DC + 1 Term'),axis([-2 6 0 1]),...

subplot(222),plot(t,x2),grid,xlabel('Time (s)'),...

ylabel('Amplitude'),title('DC + 2 Terms'),axis([-2 6 0 1]),...

subplot(223),plot(t,x3),grid,xlabel('Time (s)'),...

ylabel('Amplitude'),title('DC + 3 Terms'),axis([-2 6 0 1]),...

subplot(224),plot(t,x4),grid,xlabel('Time (s)'),...

ylabel('Amplitude'),title('DC + 5 Terms'),axis([-2 6 0 1]),...

set(gcf,'Position',fig_size)

%

%
%         ***** MATLAB Code Stops Here *****




Fourier Series Example #2
MATLAB Code

%         ***** MATLAB Code Starts Here *****
%

%FOURIER_SERIES_02_MAT

%

fig_size = [232 84 774 624];

T0 = 8;

w0 = 2*pi/8;

t = linspace(-8,16,1001);

a0 = 0.25;

n = 1:50;
an = (1./(pi*n)) .* sin(n*pi/2);

bn = (1./(pi*n)) .* (1 - cos(n*pi/2));

x1 = a0;

for i = 1:10

x1 = x1 + an(i)*cos(i*w0*t) + bn(i)*sin(i*w0*t);

end

x2 = x1;

for i = 11:30

x2 = x2 + an(i)*cos(i*w0*t) + bn(i)*sin(i*w0*t);

end

x3 = x2;

for i = 31:50

x3 = x3 + an(i)*cos(i*w0*t) + bn(i)*sin(i*w0*t);

end

A0 = a0;

An = sqrt(an.^2 + bn.^2);

thn = atan2(-bn,an)*180/pi;

X0 = A0;

Xn = An/2;

figure(1),clf,plot([-8 -6],[1 1],'b-',[-6 -6],[1 0],'b--',[-6 0],[0 0],'b-
',[0 2],[1 1],'b-',[2 8],[0 0],'b-',...

[8 10],[1 1],'b-',[10 16],[0 0],'b-',[0 0],[0 1],'b--',[2 2],[1 0],'b--',[8
8],[0 1],'b--',...

[10 10],[1 0],'b--',[16 16],[0 1],'b--'),...

axis([-8 16 -.5 1.5]),plotax,xlabel('Time
(s)'),ylabel('Amplitude'),title('Periodic Pulse Train x(t)'),...

set(gcf,'Position',fig_size),text(5,-0.2,'T_0 = 8 s'),text(5,-0.3,'Pulse
width = T_0/4')

figure(2),clf,subplot(311),plot(t,x1),subplot(312),plot(t,x2),subplot(313),
plot(t,x3),...
subplot(311),ylabel('Amplitude'),title('Fourier Series Representation of
x(t) with 10 Terms'),...

subplot(312),ylabel('Amplitude'),title('Fourier Series Representation of
x(t) with 30 Terms'),...

subplot(313),ylabel('Amplitude'),title('Fourier Series Representation of
x(t) with 50 Terms'),xlabel('Time (s)'),...

for i = 1:3,subplot(3,1,i),...

hold on,plot([0 2],[1 1],'r-',[2 8],[0 0],'r-',[8 10],[1 1],'r-',[10 16],[0
0],'r-',...

[0 0],[0 1],'r--',[2 2],[1 0],'r--',[8 8],[0 1],'r--',[10 10],[1 0],'r--
',[16 16],[0 1],'r--',...

[-8 -6],[1 1],'r-',[-6 -6],[1 0],'r--',[-6 0],[0 0],'r-'),hold off,...

axis([-8 16 -0.5 1.5]),plotax

end

set(gcf,'Position',fig_size)

figure(3),clf,subplot(211),plot(0,a0,'ro',n,an,'o'),axis([-5 50 -0.2
0.5]),plotax,...

hold on,plot([10.5 10.5],[-0.2 0.5],'r--',[30.5 30.5],[-0.2 0.5],'r--
'),hold off,...

xlabel('Harmonic Number'),ylabel('Amplitude'),title('Trig Fourier Series
Coefficients a_n for x(t)'),...

subplot(212),plot(n,bn,'o'),axis([-5 50 -0.05 0.35]),plotax,...

hold on,plot([10.5 10.5],[-0.05 0.35],'r--',[30.5 30.5],[-0.05 0.35],'r--
'),hold off,...

xlabel('Harmonic Number'),ylabel('Amplitude'),title('Trig Fourier Series
Coefficients b_n for x(t)'),...

set(gcf,'Position',fig_size)

figure(4),clf,subplot(211),plot(0,A0,'ro',n*w0,An,'o'),axis([-2*w0 16 -0.1
0.5]),plotax,...

xlabel('Frequency (r/s)'),ylabel('Magnitde'),title('Cosine Fourier Series
Magnitudes A_n for x(t)'),...

subplot(212),plot(n*w0,thn,'o'),v=axis;axis([-2*w0 16 -200 10]),plotax,...

xlabel('Frequency (r/s)'),ylabel('Phase (deg)'),title('Cosine Fourier
Series Phases Theta_n for x(t)'),...

set(gcf,'Position',fig_size)
figure(5),clf,subplot(211),plot(0,X0,'ro',n*w0,Xn,'o',-n*w0,Xn,'o'),axis([-
16 16 -0.1 0.3]),plotax,...

xlabel('Frequency (r/s)'),ylabel('Magnitde'),title('Exponential Fourier
Series Magnitudes X_n for x(t)'),...

subplot(212),plot(n*w0,thn,'o',-n*w0,-thn,'o'),v=axis;axis([-16 16
v(3:4)]),plotax,...

xlabel('Frequency (r/s)'),ylabel('Phase (deg)'),title('Exponential Fourier
Series Phases Theta_n for x(t)'),...

set(gcf,'Position',fig_size)

clear i v

%




Technical discussion about Matlab and issues related to Digital Signal Processing.

    Your Email Here   Join this Group!


Post a new Thread

fourier series coefficients - Kurt - Dec 1 12:27:01 2009
hello all,
I have a one period square wave on the interval[0,2] defined as:
y(t)= 1, 0<=t<1
y(t)= 0, 1<=t<2
I need to find the fourier series coefficients,ck, with
k=-10,-9,...,9,10
I heard using a for loop would work but I am completely stuck on how to
move
through this problem.
All help is greatly appreciated,
Kurt
______________________________
New Code Sharing Section now Live on DSPRelated.com. Learn about the Reward Program for
Contributors here.




(You need to be a member of matlab -- send a blank email to matlab-subscribe@yahoogroups.com )

Re: fourier series coefficients - vishwa - Dec 3 7:52:08 2009
you can try

for k=-10:1:10
c(k+11) = here you enter the Ck equation; % you cant have negative indexing
in
MATLAB
end

Now

c gives you the coefficients

rgds
vishwanath

________________________________
From: Kurt <k...@sbcglobal.net>
To: m...@yahoogroups.com
Sent: Tue, 1 December, 2009 12:50:19 PM
Subject: [matlab] fourier series coefficients

Â
hello all,
I have a one period square wave on the interval[0,2] defined as:
y(t)= 1, 0<=t<1
y(t)= 0, 1<=t<2
I need to find the fourier series coefficients, ck, with
k=-10,-9,... ,9,10
I heard using a for loop would work but I am completely stuck on how to
move
through this problem.
All help is greatly appreciated,
Kurt

______________________________
New Code Sharing Section now Live on DSPRelated.com. Learn about the Reward Program for
Contributors here.




(You need to be a member of matlab -- send a blank email to matlab-subscribe@yahoogroups.com )

Re: fourier series coefficients - Vaibhav Singh - Dec 4 7:44:25 2009
Hey..

For fourier coeff u have to find the fft of the given sequence using
matlab.
Since u have to find the coeff for kranging from -10:1:10, i.e.21 points u
have to define ur function in time domain in 21 samples. Take the fft of
these 21 samples. The resultant is your desired fourier coeff .

Regards-vaibhav

On Thu, Dec 3, 2009 at 5:01 PM, vishwa <v...@yahoo.com> wrote:

> you can try
>
> for k=-10:1:10
> c(k+11) = here you enter the Ck equation; % you cant have negative
indexing
> in MATLAB
> end
>
> Now
>
> c gives you the coefficients
>
> rgds
> vishwanath
>
> ________________________________
> From: Kurt <k...@sbcglobal.net <keg1606%40sbcglobal.net>>
> To: m...@yahoogroups.com <matlab%40yahoogroups.com>
> Sent: Tue, 1 December, 2009 12:50:19 PM
> Subject: [matlab] fourier series coefficients
>
> hello all,
> I have a one period square wave on the interval[0,2] defined as:
> y(t)= 1, 0<=t<1
> y(t)= 0, 1<=t<2
> I need to find the fourier series coefficients, ck, with
> k=-10,-9,... ,9,10
> I heard using a for loop would work but I am completely stuck on how to
> move through this problem.
> All help is greatly appreciated,
> Kurt
>
>
>

--
Vaibhav Singh
BE(Hons.) Electronics And Instrumentation
BITS-Pilani
EE341.01: MATLAB M-FILE FOR PLOTTING TRUNCATED FOURIER SERIES AND ITS SPECTRA
MATLAB M-File example6.m:

%
% Filename: example6.m
%
% Description: This M-file plots the truncated Fourier Series
%              representation of a square wave as well as its
%              amplitude and phase spectrum.

clear;                              % clear all variables
clf;                                % clear all figures

N = 11;                             %   summation limit (use N odd)
wo = pi;                            %   fundamental frequency (rad/s)
c0 = 0;                             %   dc bias
t = -3:0.01:3;                      %   declare time values

figure(1)                           % put first two plots on figure 1

% Compute yce, the Fourier Series in complex exponential form

yce = c0*ones(size(t));             % initialize yce to c0

for n = -N:2:N,                    % loop over series index n (odd)
  cn = 2/(j*n*wo);                 % Fourier Series Coefficient
  yce = yce + real(cn*exp(j*n*wo*t)); % Fourier Series computation
end

subplot(2,1,1)
plot([-3 -2 -2 -1 -1 0 0 1 1 2 2 3],...       % plot original y(t)
      [-1 -1 1 1 -1 -1 1 1 -1 -1 1 1], ':');
hold;
plot(t,yce);                       % plot truncated exponential FS
xlabel('t (seconds)'); ylabel('y(t)');
ttle = ['EE341.01: Truncated Exponential Fourier Series with N = ',...
          num2str(N)];
title(ttle);
hold;

% Compute yt, the Fourier Series in trigonometric form

yt = c0*ones(size(t));              % initialize yt to c0

for n = 1:2:N,                     % loop over series index n (odd)
  cn = 2/(j*n*wo);                 % Fourier Series Coefficient
  yt = yt + 2*abs(cn)*cos(n*wo*t+angle(cn)); % Fourier Series computation
end

subplot(2,1,2)
plot([-3 -2 -2 -1 -1 0 0 1 1 2 2 3],...       % plot original y(t)
      [-1 -1 1 1 -1 -1 1 1 -1 -1 1 1], ':');
hold;                               % plot truncated trigonometric FS
plot(t,yt);
xlabel('t (seconds)'); ylabel('y(t)');
ttle = ['EE341.01: Truncated Trigonometric Fourier Series with N = ',...
          num2str(N)];
title(ttle);
hold;

% Draw the amplitude spectrum from exponential Fourier Series
figure(2)                          % put next plots on figure 2

subplot(2,1,1)
stem(0,c0);                        % plot c0 at nwo = 0

hold;
for n = -N:2:N,                    % loop over series index n
  cn = 2/(j*n*wo);                 % Fourier Series Coefficient
  stem(n*wo,abs(cn))               % plot |cn| vs nwo
end
for n = -N+1:2:N-1,                % loop over even series index n
  cn = 0;                          % Fourier Series Coefficient
  stem(n*wo,abs(cn));              % plot |cn| vs nwo
end

xlabel('w (rad/s)')
ylabel('|cn|')
ttle = ['EE341.01: Amplitude Spectrum with N = ',num2str(N)];
title(ttle);
grid;
hold;

% Draw the phase spectrum from exponential Fourier Series

subplot(2,1,2)
stem(0,angle(c0)*180/pi);          % plot angle of c0 at nwo = 0

hold;
for n = -N:2:N,                    % loop over odd series index n
  cn = 2/(j*n*wo);                 % Fourier Series Coefficient
  stem(n*wo,angle(cn)*180/pi);     % plot |cn| vs nwo
end
for n = -N+1:2:N-1,                % loop over even series index n
  cn = 0;                          % Fourier Series Coefficient
  stem(n*wo,angle(cn)*180/pi);     % plot |cn| vs nwo
end

xlabel('w (rad/s)')
ylabel('angle(cn) (degrees)')
ttle = ['EE341.01: Phase Spectrum with N = ',num2str(N)];
title(ttle);
grid;
hold;

MATLAB Plots Generated:
Hi,


       I am trying to write a function to generate Fourier series Coefficients of a given discrete time signal. For instance:


                                                            x = [1 2 3 4]
                                                            n = [0 1 2 3]


                      where x holds the values of the signal, and n holds the corresponding time indices.


                                                    My code for the function is:


                                                       function a = dtfs(x,n)
                                                         period = length(x);
                                                          for k = 1:period
                                           a = (1/period)*x(k)*exp(((-j*2*pi)/period)*n(k));
                                                                   a
                                                                 end


         i am just not getting the correct values for the fourier series coefficients. Am I setting up the formula wrong?


As you can probably tell, i am very very new to MATLAB and I'd also appreciate if someone may guide me in setting up my "for-
loop" with a vector to "catch" and store values of "a", so each time the for-loop repeats, the previous value a does not get over-
                                          written, instead they are all stored in a vector.


                                                       Thanks in advance :)


                                                              Regards


                                               Subject: Fourier Series Coefficients


                                                           From: Andrew


                                                   Date: 24 Oct, 2008 04:43:01


                                                          Message: 2 of 4

                                                      Reply to this message
                                                   Add author to My Watch List
                                                        View original format
                                                           Flag as spam

                               I'm guessing the formula, but hopefully the structure of it will help...


                                                       function a = dtfs(x,n)
                                                         period = length(x);
                                                       a = zeros(1, length(x))
                                                          for k = 1:period
                                                            for z = 1:period
                                         a(k) = a(k) + x(z) * exp((-j*2*pi)/period * (k-1) * n(z));
                                                                  end
                                                          a(k) = a(k) / period;
                                                       num2str(a(k), '%1.18f');
                                                                 end
Cheers,
                                                             Andrew



                                        > a = (1/period)*x(k)*exp(((-j*2*pi)/period)*n(k));
                                                               >a
                                                              > end




                 "Raz H" <dilster08@gmail.com> wrote in message <gdrclp$t9b$1@fred.mathworks.com>...
                                                              > Hi,
                                                                >
     > I am trying to write a function to generate Fourier series Coefficients of a given discrete time signal. For instance:
                                                                >
                                                          > x = [1 2 3 4]
                                                          > n = [0 1 2 3]
                                                                >
                    > where x holds the values of the signal, and n holds the corresponding time indices.
                                                                >
                                                 > My code for the function is:
                                                                >
                                                     > function a = dtfs(x,n)
                                                      > period = length(x);
                                                        > for k = 1:period
                                        > a = (1/period)*x(k)*exp(((-j*2*pi)/period)*n(k));
                                                               >a
                                                              > end
                                                                >
        > i am just not getting the correct values for the fourier series coefficients. Am I setting up the formula wrong?
                                                                >
> As you can probably tell, i am very very new to MATLAB and I'd also appreciate if someone may guide me in setting up my
"for-loop" with a vector to "catch" and store values of "a", so each time the for-loop repeats, the previous value a does not get
                                      over-written, instead they are all stored in a vector.
                                                                >
                                                     > Thanks in advance :)
                                                                >
                                                           > Regards


                                              Subject: Fourier Series Coefficients


                                                           From: Paul


                                                  Date: 24 Oct, 2008 06:35:05


                                                        Message: 3 of 4

                                                     Reply to this message
                                                  Add author to My Watch List
                                                      View original format
                                                          Flag as spam
"Andrew" <awbsmith@itee.uq.edu.au> wrote in message <gdrjol$so$1@fred.mathworks.com>...
                             > I'm guessing the formula, but hopefully the structure of it will help...
                                                                 >
                                                     > function a = dtfs(x,n)
                                                       > period = length(x);
                                                     > a = zeros(1, length(x))
                                                        > for k = 1:period
                                                        > for z = 1:period
                                    > a(k) = a(k) + x(z) * exp((-j*2*pi)/period * (k-1) * n(z));
                                                              > end
                                                      > a(k) = a(k) / period;
                                                    > num2str(a(k), '%1.18f');
                                                              > end
                                                                 >
                                                            > Cheers,
                                                            > Andrew
                                                                 >
                                                                 >
                                       > > a = (1/period)*x(k)*exp(((-j*2*pi)/period)*n(k));
                                                               >>a
                                                             > > end
                                                                 >
                                                                 >
                                                                 >
                                                                 >
                > "Raz H" <dilster08@gmail.com> wrote in message <gdrclp$t9b$1@fred.mathworks.com>...
                                                              > > Hi,
                                                                >>
    > > I am trying to write a function to generate Fourier series Coefficients of a given discrete time signal. For instance:
                                                                >>
                                                         > > x = [1 2 3 4]
                                                         > > n = [0 1 2 3]
                                                                >>
                   > > where x holds the values of the signal, and n holds the corresponding time indices.
                                                                >>
                                                 > > My code for the function is:
                                                                >>
                                                    > > function a = dtfs(x,n)
                                                      > > period = length(x);
                                                       > > for k = 1:period
                                       > > a = (1/period)*x(k)*exp(((-j*2*pi)/period)*n(k));
                                                               >>a
                                                             > > end
                                                                >>
       > > i am just not getting the correct values for the fourier series coefficients. Am I setting up the formula wrong?
                                                                >>
> > As you can probably tell, i am very very new to MATLAB and I'd also appreciate if someone may guide me in setting up my
"for-loop" with a vector to "catch" and store values of "a", so each time the for-loop repeats, the previous value a does not get
                                      over-written, instead they are all stored in a vector.
                                                                >>
                                                    > > Thanks in advance :)
                                                                >>
                                                           > > Regards
Was this a HW problem? It looks like one to me!


                                           Subject: Fourier Series Coefficients


                                                       From: Raz H


                                               Date: 24 Oct, 2008 06:54:02


                                                      Message: 4 of 4

                                                   Reply to this message
                                               Add author to My Watch List
                                                    View original format
                                                       Flag as spam

             "Paul" <par@ceri.memphis.edu> wrote in message <gdrqap$sl5$1@fred.mathworks.com>...
          > "Andrew" <awbsmith@itee.uq.edu.au> wrote in message <gdrjol$so$1@fred.mathworks.com>...
                         > > I'm guessing the formula, but hopefully the structure of it will help...
                                                             >>
                                                 > > function a = dtfs(x,n)
                                                   > > period = length(x);
                                                > > a = zeros(1, length(x))
                                                    > > for k = 1:period
                                                    > > for z = 1:period
                                > > a(k) = a(k) + x(z) * exp((-j*2*pi)/period * (k-1) * n(z));
                                                          > > end
                                                  > > a(k) = a(k) / period;
                                                > > num2str(a(k), '%1.18f');
                                                          > > end
                                                             >>
                                                        > > Cheers,
                                                        > > Andrew
                                                             >>
                                                             >>
                                   > > > a = (1/period)*x(k)*exp(((-j*2*pi)/period)*n(k));
                                                           >>>a
                                                         > > > end
                                                             >>
                                                             >>
                                                             >>
                                                             >>
            > > "Raz H" <dilster08@gmail.com> wrote in message <gdrclp$t9b$1@fred.mathworks.com>...
                                                          > > > Hi,
                                                            >>>
> > > I am trying to write a function to generate Fourier series Coefficients of a given discrete time signal. For instance:
                                                            >>>
                                                     > > > x = [1 2 3 4]
                                                     > > > n = [0 1 2 3]
                                                            >>>
               > > > where x holds the values of the signal, and n holds the corresponding time indices.
                                                            >>>
                                             > > > My code for the function is:
>>>
                                                   > > > function a = dtfs(x,n)
                                                    > > > period = length(x);
                                                      > > > for k = 1:period
                                      > > > a = (1/period)*x(k)*exp(((-j*2*pi)/period)*n(k));
                                                             >>>a
                                                            > > > end
                                                              >>>
      > > > i am just not getting the correct values for the fourier series coefficients. Am I setting up the formula wrong?
                                                              >>>
> > > As you can probably tell, i am very very new to MATLAB and I'd also appreciate if someone may guide me in setting up
my "for-loop" with a vector to "catch" and store values of "a", so each time the for-loop repeats, the previous value a does not
                                    get over-written, instead they are all stored in a vector.
                                                              >>>
                                                   > > > Thanks in advance :)
                                                              >>>
                                                         > > > Regards
                                                                >
                                      > Was this a HW problem? It looks like one to me!



                                                            @Andrew


     Thank you very much! I guess my loop was not set up correctly, plus I was not signifying the time indices correctly.


                                                             @Paul


   This was not a homework problem, though I'll be taking Signals soon, so I am trying to become familiar with MATLAB.


                                                  Thanks to all who replied! :)




            EE341.01: MATLAB M-FILE FOR PLOTTING TRUNCATED FOURIER SERIES

This example shows a MATLAB M-file for plotting a truncated Fourier Series. Various
numbers of terms are used.

MATLAB M-File example5.m:


%
% Filename: example5.m
%
% Description: Example to show how the truncated Fourier series in
%              complex exponential form approximates the real
%              signal. More and more terms are taken showing a
%              better and better representation of the original signal.
%

clear;                                    % clear all variables
clf;                                      % clear all figures
% Define parameters to plot original sawtooth

tr = [-1 0 0 1 1 2 2];
yr = [0 1 0 1 0 1 0];

% Plot Truncated Fourier Series Approximation (N = 1)

N = 1;                    %   define number of terms to use (n = -N..N)
c0 = 0.5;                 %   define dc bias coefficient
t = -1:0.001:2;           %   define time values for y(t)
y = c0 * ones(size(t));   %   let initial y = c0 (dc bias) for all times

for n = -N:-1,           % compute y for negative n
  cn = exp(j*pi/2)/(2*pi*n);
  y = y + real(cn * exp(j*n*2*pi*t));
end;
                         % compute y for positive n and add to y
for n = 1:N,             % found using negative n
  cn = exp(j*pi/2)/(2*pi*n);
  y = y + real(cn * exp(j*n*2*pi*t));
end;

subplot(2,2,1);           % plot approximation
plot(t,y);
hold;
plot(tr,yr,':');
hold;
xlabel('time (seconds)');
ylabel('y(t) approximation');
title('EE341.01: Truncated FS, -1<=n<=1');

% Plot Truncated Fourier Series Approximation (N = 2)

clear;                    %   clear all variables
N = 2;                    %   define number of terms to use (n = -N..N)
c0 = 0.5;                 %   define dc bias coefficient
t = -1:0.001:2;           %   define time values for y(t)
y = c0 * ones(size(t));   %   let initial y = c0 (dc bias) for all times

for n = -N:-1,           % compute y for negative n
  cn = exp(j*pi/2)/(2*pi*n);
  y = y + real(cn * exp(j*n*2*pi*t));
end;
                         % compute y for positive n and add to y
for n = 1:N,             % found using negative n
  cn = exp(j*pi/2)/(2*pi*n);
  y = y + real(cn * exp(j*n*2*pi*t));
end;

subplot(2,2,2);           % plot approximation
plot(t,y);
hold;
plot(tr,yr,':');
hold;
xlabel('time (seconds)');
ylabel('y(t) approximation');
title('EE341.01: Truncated FS, -2<=n<=2');

% Plot Truncated Fourier Series Approximation (N = 3)

clear;                    % clear all variables
N = 3;                    %   define number of terms to use (n = -N..N)
c0 = 0.5;                 %   define dc bias coefficient
t = -1:0.001:2;           %   define time values for y(t)
y = c0 * ones(size(t));   %   let initial y = c0 (dc bias) for all times

for n = -N:-1,           % compute y for negative n
  cn = exp(j*pi/2)/(2*pi*n);
  y = y + real(cn * exp(j*n*2*pi*t));
end;
                         % compute y for positive n and add to y
for n = 1:N,             % found using negative n
  cn = exp(j*pi/2)/(2*pi*n);
  y = y + real(cn * exp(j*n*2*pi*t));
end;

subplot(2,2,3);           % plot approximation
plot(t,y);
hold;
plot(tr,yr,':');
hold;
xlabel('time (seconds)');
ylabel('y(t) approximation');
title('EE341.01: Truncated FS, -3<=n<=3');

% Plot Truncated Fourier Series Approximation (N = 10)

clear;                    %   clear all variables
N = 10;                   %   define number of terms to use (n = -N..N)
c0 = 0.5;                 %   define dc bias coefficient
t = -1:0.001:2;           %   define time values for y(t)
y = c0 * ones(size(t));   %   let initial y = c0 (dc bias) for all times

for n = -N:-1,           % compute y for negative n
  cn = exp(j*pi/2)/(2*pi*n);
  y = y + real(cn * exp(j*n*2*pi*t));
end;
                         % compute y for positive n and add to y
for n = 1:N,             % found using negative n
  cn = exp(j*pi/2)/(2*pi*n);
  y = y + real(cn * exp(j*n*2*pi*t));
end;

subplot(2,2,4);           % plot approximation
plot(t,y);
hold;
plot(tr,yr,':');
hold;
xlabel('time (seconds)');
ylabel('y(t) approximation');
title('EE341.01: Truncated FS, -10<=n<=10');


MATLAB Plot Generated:
Fourier series example

Contenu connexe

Tendances

Digital Signal Processing[ECEG-3171]-Ch1_L02
Digital Signal Processing[ECEG-3171]-Ch1_L02Digital Signal Processing[ECEG-3171]-Ch1_L02
Digital Signal Processing[ECEG-3171]-Ch1_L02Rediet Moges
 
Frequency modulation
Frequency modulationFrequency modulation
Frequency modulationAkanksha_Seth
 
Discrete Time Fourier Transform
Discrete Time Fourier TransformDiscrete Time Fourier Transform
Discrete Time Fourier TransformWaqas Afzal
 
Fir filter design using windows
Fir filter design using windowsFir filter design using windows
Fir filter design using windowsSarang Joshi
 
Design of IIR filters
Design of IIR filtersDesign of IIR filters
Design of IIR filtersop205
 
DSP_2018_FOEHU - Lec 0 - Course Outlines
DSP_2018_FOEHU - Lec 0 - Course OutlinesDSP_2018_FOEHU - Lec 0 - Course Outlines
DSP_2018_FOEHU - Lec 0 - Course OutlinesAmr E. Mohamed
 
Circular convolution
Circular convolutionCircular convolution
Circular convolutionssuserb83554
 
Chapter 6m
Chapter 6mChapter 6m
Chapter 6mwafaa_A7
 
Decimation in time and frequency
Decimation in time and frequencyDecimation in time and frequency
Decimation in time and frequencySARITHA REDDY
 
Z TRANSFORM PROPERTIES AND INVERSE Z TRANSFORM
Z TRANSFORM PROPERTIES AND INVERSE Z TRANSFORMZ TRANSFORM PROPERTIES AND INVERSE Z TRANSFORM
Z TRANSFORM PROPERTIES AND INVERSE Z TRANSFORMTowfeeq Umar
 
Fourier series Introduction
Fourier series IntroductionFourier series Introduction
Fourier series IntroductionRizwan Kazi
 
Discrete fourier transform
Discrete fourier transformDiscrete fourier transform
Discrete fourier transformMOHAMMAD AKRAM
 
Chapter4 - The Continuous-Time Fourier Transform
Chapter4 - The Continuous-Time Fourier TransformChapter4 - The Continuous-Time Fourier Transform
Chapter4 - The Continuous-Time Fourier TransformAttaporn Ninsuwan
 
Overlap Add, Overlap Save(digital signal processing)
Overlap Add, Overlap Save(digital signal processing)Overlap Add, Overlap Save(digital signal processing)
Overlap Add, Overlap Save(digital signal processing)Gourab Ghosh
 
DSP_2018_FOEHU - Lec 06 - FIR Filter Design
DSP_2018_FOEHU - Lec 06 - FIR Filter DesignDSP_2018_FOEHU - Lec 06 - FIR Filter Design
DSP_2018_FOEHU - Lec 06 - FIR Filter DesignAmr E. Mohamed
 
DSP_2018_FOEHU - Lec 04 - The z-Transform
DSP_2018_FOEHU - Lec 04 - The z-TransformDSP_2018_FOEHU - Lec 04 - The z-Transform
DSP_2018_FOEHU - Lec 04 - The z-TransformAmr E. Mohamed
 

Tendances (20)

Butterworth filter
Butterworth filterButterworth filter
Butterworth filter
 
Digital Signal Processing[ECEG-3171]-Ch1_L02
Digital Signal Processing[ECEG-3171]-Ch1_L02Digital Signal Processing[ECEG-3171]-Ch1_L02
Digital Signal Processing[ECEG-3171]-Ch1_L02
 
Frequency modulation
Frequency modulationFrequency modulation
Frequency modulation
 
Discrete Time Fourier Transform
Discrete Time Fourier TransformDiscrete Time Fourier Transform
Discrete Time Fourier Transform
 
Z transform
Z transformZ transform
Z transform
 
Fir filter design using windows
Fir filter design using windowsFir filter design using windows
Fir filter design using windows
 
Design of IIR filters
Design of IIR filtersDesign of IIR filters
Design of IIR filters
 
Z transform ROC eng.Math
Z transform ROC eng.MathZ transform ROC eng.Math
Z transform ROC eng.Math
 
DSP_2018_FOEHU - Lec 0 - Course Outlines
DSP_2018_FOEHU - Lec 0 - Course OutlinesDSP_2018_FOEHU - Lec 0 - Course Outlines
DSP_2018_FOEHU - Lec 0 - Course Outlines
 
Properties of z transform day 2
Properties of z transform day 2Properties of z transform day 2
Properties of z transform day 2
 
Circular convolution
Circular convolutionCircular convolution
Circular convolution
 
Chapter 6m
Chapter 6mChapter 6m
Chapter 6m
 
Decimation in time and frequency
Decimation in time and frequencyDecimation in time and frequency
Decimation in time and frequency
 
Z TRANSFORM PROPERTIES AND INVERSE Z TRANSFORM
Z TRANSFORM PROPERTIES AND INVERSE Z TRANSFORMZ TRANSFORM PROPERTIES AND INVERSE Z TRANSFORM
Z TRANSFORM PROPERTIES AND INVERSE Z TRANSFORM
 
Fourier series Introduction
Fourier series IntroductionFourier series Introduction
Fourier series Introduction
 
Discrete fourier transform
Discrete fourier transformDiscrete fourier transform
Discrete fourier transform
 
Chapter4 - The Continuous-Time Fourier Transform
Chapter4 - The Continuous-Time Fourier TransformChapter4 - The Continuous-Time Fourier Transform
Chapter4 - The Continuous-Time Fourier Transform
 
Overlap Add, Overlap Save(digital signal processing)
Overlap Add, Overlap Save(digital signal processing)Overlap Add, Overlap Save(digital signal processing)
Overlap Add, Overlap Save(digital signal processing)
 
DSP_2018_FOEHU - Lec 06 - FIR Filter Design
DSP_2018_FOEHU - Lec 06 - FIR Filter DesignDSP_2018_FOEHU - Lec 06 - FIR Filter Design
DSP_2018_FOEHU - Lec 06 - FIR Filter Design
 
DSP_2018_FOEHU - Lec 04 - The z-Transform
DSP_2018_FOEHU - Lec 04 - The z-TransformDSP_2018_FOEHU - Lec 04 - The z-Transform
DSP_2018_FOEHU - Lec 04 - The z-Transform
 

En vedette

Solved numerical problems of fourier series
Solved numerical problems of fourier seriesSolved numerical problems of fourier series
Solved numerical problems of fourier seriesMohammad Imran
 
fourier series
fourier seriesfourier series
fourier series8laddu8
 
Application of fourier series
Application of fourier seriesApplication of fourier series
Application of fourier seriesGirish Dhareshwar
 
Fourier Specturm via MATLAB
Fourier Specturm via MATLABFourier Specturm via MATLAB
Fourier Specturm via MATLABZunAib Ali
 
Fourier Series for Continuous Time & Discrete Time Signals
Fourier Series for Continuous Time & Discrete Time SignalsFourier Series for Continuous Time & Discrete Time Signals
Fourier Series for Continuous Time & Discrete Time SignalsJayanshu Gundaniya
 
History and Real Life Applications of Fourier Analaysis
History and Real Life Applications of Fourier AnalaysisHistory and Real Life Applications of Fourier Analaysis
History and Real Life Applications of Fourier AnalaysisSyed Ahmed Zaki
 
Matlab: Spectral Analysis, Fourier Analysis, Filterbank Analysis
Matlab: Spectral Analysis, Fourier Analysis, Filterbank AnalysisMatlab: Spectral Analysis, Fourier Analysis, Filterbank Analysis
Matlab: Spectral Analysis, Fourier Analysis, Filterbank AnalysisDataminingTools Inc
 
Taylor
Taylor Taylor
Taylor UIS
 
Fourier series basic results
Fourier series basic resultsFourier series basic results
Fourier series basic resultsTarun Gehlot
 
graphs plotting in MATLAB
graphs plotting in MATLABgraphs plotting in MATLAB
graphs plotting in MATLABApurva Patil
 
Digitla Communication pulse shaping filter
Digitla Communication pulse shaping filterDigitla Communication pulse shaping filter
Digitla Communication pulse shaping filtermirfanjum
 

En vedette (20)

Solved numerical problems of fourier series
Solved numerical problems of fourier seriesSolved numerical problems of fourier series
Solved numerical problems of fourier series
 
Fourier series
Fourier seriesFourier series
Fourier series
 
Fourier series
Fourier seriesFourier series
Fourier series
 
fourier series
fourier seriesfourier series
fourier series
 
Fourier series 1
Fourier series 1Fourier series 1
Fourier series 1
 
Solved problems
Solved problemsSolved problems
Solved problems
 
Fourier series and transforms
Fourier series and transformsFourier series and transforms
Fourier series and transforms
 
Application of fourier series
Application of fourier seriesApplication of fourier series
Application of fourier series
 
Fourier Specturm via MATLAB
Fourier Specturm via MATLABFourier Specturm via MATLAB
Fourier Specturm via MATLAB
 
Fourier Series for Continuous Time & Discrete Time Signals
Fourier Series for Continuous Time & Discrete Time SignalsFourier Series for Continuous Time & Discrete Time Signals
Fourier Series for Continuous Time & Discrete Time Signals
 
History and Real Life Applications of Fourier Analaysis
History and Real Life Applications of Fourier AnalaysisHistory and Real Life Applications of Fourier Analaysis
History and Real Life Applications of Fourier Analaysis
 
AEM Fourier series
 AEM Fourier series AEM Fourier series
AEM Fourier series
 
Matlab: Spectral Analysis, Fourier Analysis, Filterbank Analysis
Matlab: Spectral Analysis, Fourier Analysis, Filterbank AnalysisMatlab: Spectral Analysis, Fourier Analysis, Filterbank Analysis
Matlab: Spectral Analysis, Fourier Analysis, Filterbank Analysis
 
Taylor
Taylor Taylor
Taylor
 
Designing a uniform filter bank using multirate concept
Designing a uniform filter bank using multirate conceptDesigning a uniform filter bank using multirate concept
Designing a uniform filter bank using multirate concept
 
Fourier series basic results
Fourier series basic resultsFourier series basic results
Fourier series basic results
 
graphs plotting in MATLAB
graphs plotting in MATLABgraphs plotting in MATLAB
graphs plotting in MATLAB
 
Digitla Communication pulse shaping filter
Digitla Communication pulse shaping filterDigitla Communication pulse shaping filter
Digitla Communication pulse shaping filter
 
Digfilt
DigfiltDigfilt
Digfilt
 
Taylorseries
TaylorseriesTaylorseries
Taylorseries
 

Similaire à Fourier series example

Applied Digital Signal Processing 1st Edition Manolakis Solutions Manual
Applied Digital Signal Processing 1st Edition Manolakis Solutions ManualApplied Digital Signal Processing 1st Edition Manolakis Solutions Manual
Applied Digital Signal Processing 1st Edition Manolakis Solutions Manualtowojixi
 
DSP LAB COMPLETE CODES.docx
DSP LAB COMPLETE CODES.docxDSP LAB COMPLETE CODES.docx
DSP LAB COMPLETE CODES.docxMUMAR57
 
Please use the same variables and only write the TODO part #!-usr-bi.pdf
Please use the same variables and only write the TODO part   #!-usr-bi.pdfPlease use the same variables and only write the TODO part   #!-usr-bi.pdf
Please use the same variables and only write the TODO part #!-usr-bi.pdfasenterprisestyagi
 
Matlab 2
Matlab 2Matlab 2
Matlab 2asguna
 
Linear transformation.ppt
Linear transformation.pptLinear transformation.ppt
Linear transformation.pptRaj Parekh
 
Use the same variable names and write the function F - Force(x-ks-kc-l.pdf
Use the same variable names and write the function F - Force(x-ks-kc-l.pdfUse the same variable names and write the function F - Force(x-ks-kc-l.pdf
Use the same variable names and write the function F - Force(x-ks-kc-l.pdfacteleshoppe
 
Basic simulation lab manual1
Basic simulation lab manual1Basic simulation lab manual1
Basic simulation lab manual1Janardhana Raju M
 
BS LAB Manual (1).pdf
BS LAB Manual  (1).pdfBS LAB Manual  (1).pdf
BS LAB Manual (1).pdfssuser476810
 
The Moore-Spiegel Oscillator
The Moore-Spiegel OscillatorThe Moore-Spiegel Oscillator
The Moore-Spiegel OscillatorAbhranil Das
 
signal homework
signal homeworksignal homework
signal homeworksokok22867
 
Amth250 octave matlab some solutions (1)
Amth250 octave matlab some solutions (1)Amth250 octave matlab some solutions (1)
Amth250 octave matlab some solutions (1)asghar123456
 
DIGITAL SIGNAL PROCESSING: Sampling and Reconstruction on MATLAB
DIGITAL SIGNAL PROCESSING: Sampling and Reconstruction on MATLABDIGITAL SIGNAL PROCESSING: Sampling and Reconstruction on MATLAB
DIGITAL SIGNAL PROCESSING: Sampling and Reconstruction on MATLABMartin Wachiye Wafula
 
Functional Regression Analysis
Functional Regression AnalysisFunctional Regression Analysis
Functional Regression AnalysisNeuroMat
 

Similaire à Fourier series example (20)

Applied Digital Signal Processing 1st Edition Manolakis Solutions Manual
Applied Digital Signal Processing 1st Edition Manolakis Solutions ManualApplied Digital Signal Processing 1st Edition Manolakis Solutions Manual
Applied Digital Signal Processing 1st Edition Manolakis Solutions Manual
 
DSP LAB COMPLETE CODES.docx
DSP LAB COMPLETE CODES.docxDSP LAB COMPLETE CODES.docx
DSP LAB COMPLETE CODES.docx
 
Dsp manual
Dsp manualDsp manual
Dsp manual
 
Please use the same variables and only write the TODO part #!-usr-bi.pdf
Please use the same variables and only write the TODO part   #!-usr-bi.pdfPlease use the same variables and only write the TODO part   #!-usr-bi.pdf
Please use the same variables and only write the TODO part #!-usr-bi.pdf
 
Numerical methods generating polynomial
Numerical methods generating polynomialNumerical methods generating polynomial
Numerical methods generating polynomial
 
Matlab programs
Matlab programsMatlab programs
Matlab programs
 
Matlab 2
Matlab 2Matlab 2
Matlab 2
 
Linear transformation.ppt
Linear transformation.pptLinear transformation.ppt
Linear transformation.ppt
 
Use the same variable names and write the function F - Force(x-ks-kc-l.pdf
Use the same variable names and write the function F - Force(x-ks-kc-l.pdfUse the same variable names and write the function F - Force(x-ks-kc-l.pdf
Use the same variable names and write the function F - Force(x-ks-kc-l.pdf
 
Basic simulation lab manual1
Basic simulation lab manual1Basic simulation lab manual1
Basic simulation lab manual1
 
BS LAB Manual (1).pdf
BS LAB Manual  (1).pdfBS LAB Manual  (1).pdf
BS LAB Manual (1).pdf
 
The Moore-Spiegel Oscillator
The Moore-Spiegel OscillatorThe Moore-Spiegel Oscillator
The Moore-Spiegel Oscillator
 
signal homework
signal homeworksignal homework
signal homework
 
Mat lab
Mat labMat lab
Mat lab
 
Amth250 octave matlab some solutions (1)
Amth250 octave matlab some solutions (1)Amth250 octave matlab some solutions (1)
Amth250 octave matlab some solutions (1)
 
DIGITAL SIGNAL PROCESSING: Sampling and Reconstruction on MATLAB
DIGITAL SIGNAL PROCESSING: Sampling and Reconstruction on MATLABDIGITAL SIGNAL PROCESSING: Sampling and Reconstruction on MATLAB
DIGITAL SIGNAL PROCESSING: Sampling and Reconstruction on MATLAB
 
Functional Regression Analysis
Functional Regression AnalysisFunctional Regression Analysis
Functional Regression Analysis
 
Dsp iit workshop
Dsp iit workshopDsp iit workshop
Dsp iit workshop
 
Md Nasfik
Md NasfikMd Nasfik
Md Nasfik
 
matlab.docx
matlab.docxmatlab.docx
matlab.docx
 

Dernier

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 

Dernier (20)

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 

Fourier series example

  • 1. Fourier Series Example MATLAB Code % ***** MATLAB Code Starts Here ***** % %FOURIER_SERIES_01_MAT % fig_size = [232 84 774 624]; x = [0.1 0.9 0.1]; % 1 period of x(t) x = [x x x x]; % 4 periods of x(t) tx = [-2 -1 0 0 1 2 2 3 4 4 5 6]; % time points for x(t) figure(1),plot(tx,x),grid,xlabel('Time (s)'),ylabel('Amplitude'),... title('Periodic Signal x(t)'),axis([-2 6 0 1]),... set(gcf,'Position',fig_size) % a0 = 0.5; % DC component of Fourier Series ph0 = 0; n = [1 3 5 7 9]; % Values of n to be evaluated an = -3.2 ./ (pi * n).^2; % Fourier Series coefficients mag_an = abs(an); ph_an = -180 * ones(1,length(n)); % n = [0 n]; mag_an = [a0 mag_an]; % Including a0 with a_n ph_an = [ph0 ph_an]; % figure(2),clf,subplot(211),plot(n,mag_an,'o'),grid,xlabel('Harmonic Number'),...
  • 2. ylabel('Magnitude'),title('Fourier Series Magnitude'),axis([0 10 0 0.6]),... set(gcf,'Position',fig_size) % subplot(212),plot(n,ph_an,'o'),grid,xlabel('Harmonic Number'),... ylabel('Phase (deg)'),title('Fourier Series Phase'),axis([0 10 -200 0]),... set(gcf,'Position',fig_size) % w0 = pi; % Fundamental Frequency t = [-2:0.002:6]; % time vector for approximations % x1 = 0; % approximation with DC + 1 term for i = 1:2 x1 = x1 + mag_an(i)*cos(n(i)*w0*t + ph_an(i)*pi/180); end % x2 = x1; % approximation with DC + 2 terms i = 3; x2 = x2 + mag_an(i)*cos(n(i)*w0*t + ph_an(i)*pi/180); % x3 = x2; % approximation with DC + 3 terms i = 4; x3 = x3 + mag_an(i)*cos(n(i)*w0*t + ph_an(i)*pi/180); % x4 = x3; % approximation with DC + 5 terms for i = 5:6 x4 = x4 + mag_an(i)*cos(n(i)*w0*t + ph_an(i)*pi/180); end
  • 3. % figure(3),subplot(221),plot(t,x1),grid,xlabel('Time (s)'),... ylabel('Amplitude'),title('DC + 1 Term'),axis([-2 6 0 1]),... subplot(222),plot(t,x2),grid,xlabel('Time (s)'),... ylabel('Amplitude'),title('DC + 2 Terms'),axis([-2 6 0 1]),... subplot(223),plot(t,x3),grid,xlabel('Time (s)'),... ylabel('Amplitude'),title('DC + 3 Terms'),axis([-2 6 0 1]),... subplot(224),plot(t,x4),grid,xlabel('Time (s)'),... ylabel('Amplitude'),title('DC + 5 Terms'),axis([-2 6 0 1]),... set(gcf,'Position',fig_size) % % % ***** MATLAB Code Stops Here ***** Fourier Series Example #2 MATLAB Code % ***** MATLAB Code Starts Here ***** % %FOURIER_SERIES_02_MAT % fig_size = [232 84 774 624]; T0 = 8; w0 = 2*pi/8; t = linspace(-8,16,1001); a0 = 0.25; n = 1:50;
  • 4. an = (1./(pi*n)) .* sin(n*pi/2); bn = (1./(pi*n)) .* (1 - cos(n*pi/2)); x1 = a0; for i = 1:10 x1 = x1 + an(i)*cos(i*w0*t) + bn(i)*sin(i*w0*t); end x2 = x1; for i = 11:30 x2 = x2 + an(i)*cos(i*w0*t) + bn(i)*sin(i*w0*t); end x3 = x2; for i = 31:50 x3 = x3 + an(i)*cos(i*w0*t) + bn(i)*sin(i*w0*t); end A0 = a0; An = sqrt(an.^2 + bn.^2); thn = atan2(-bn,an)*180/pi; X0 = A0; Xn = An/2; figure(1),clf,plot([-8 -6],[1 1],'b-',[-6 -6],[1 0],'b--',[-6 0],[0 0],'b- ',[0 2],[1 1],'b-',[2 8],[0 0],'b-',... [8 10],[1 1],'b-',[10 16],[0 0],'b-',[0 0],[0 1],'b--',[2 2],[1 0],'b--',[8 8],[0 1],'b--',... [10 10],[1 0],'b--',[16 16],[0 1],'b--'),... axis([-8 16 -.5 1.5]),plotax,xlabel('Time (s)'),ylabel('Amplitude'),title('Periodic Pulse Train x(t)'),... set(gcf,'Position',fig_size),text(5,-0.2,'T_0 = 8 s'),text(5,-0.3,'Pulse width = T_0/4') figure(2),clf,subplot(311),plot(t,x1),subplot(312),plot(t,x2),subplot(313), plot(t,x3),...
  • 5. subplot(311),ylabel('Amplitude'),title('Fourier Series Representation of x(t) with 10 Terms'),... subplot(312),ylabel('Amplitude'),title('Fourier Series Representation of x(t) with 30 Terms'),... subplot(313),ylabel('Amplitude'),title('Fourier Series Representation of x(t) with 50 Terms'),xlabel('Time (s)'),... for i = 1:3,subplot(3,1,i),... hold on,plot([0 2],[1 1],'r-',[2 8],[0 0],'r-',[8 10],[1 1],'r-',[10 16],[0 0],'r-',... [0 0],[0 1],'r--',[2 2],[1 0],'r--',[8 8],[0 1],'r--',[10 10],[1 0],'r-- ',[16 16],[0 1],'r--',... [-8 -6],[1 1],'r-',[-6 -6],[1 0],'r--',[-6 0],[0 0],'r-'),hold off,... axis([-8 16 -0.5 1.5]),plotax end set(gcf,'Position',fig_size) figure(3),clf,subplot(211),plot(0,a0,'ro',n,an,'o'),axis([-5 50 -0.2 0.5]),plotax,... hold on,plot([10.5 10.5],[-0.2 0.5],'r--',[30.5 30.5],[-0.2 0.5],'r-- '),hold off,... xlabel('Harmonic Number'),ylabel('Amplitude'),title('Trig Fourier Series Coefficients a_n for x(t)'),... subplot(212),plot(n,bn,'o'),axis([-5 50 -0.05 0.35]),plotax,... hold on,plot([10.5 10.5],[-0.05 0.35],'r--',[30.5 30.5],[-0.05 0.35],'r-- '),hold off,... xlabel('Harmonic Number'),ylabel('Amplitude'),title('Trig Fourier Series Coefficients b_n for x(t)'),... set(gcf,'Position',fig_size) figure(4),clf,subplot(211),plot(0,A0,'ro',n*w0,An,'o'),axis([-2*w0 16 -0.1 0.5]),plotax,... xlabel('Frequency (r/s)'),ylabel('Magnitde'),title('Cosine Fourier Series Magnitudes A_n for x(t)'),... subplot(212),plot(n*w0,thn,'o'),v=axis;axis([-2*w0 16 -200 10]),plotax,... xlabel('Frequency (r/s)'),ylabel('Phase (deg)'),title('Cosine Fourier Series Phases Theta_n for x(t)'),... set(gcf,'Position',fig_size)
  • 6. figure(5),clf,subplot(211),plot(0,X0,'ro',n*w0,Xn,'o',-n*w0,Xn,'o'),axis([- 16 16 -0.1 0.3]),plotax,... xlabel('Frequency (r/s)'),ylabel('Magnitde'),title('Exponential Fourier Series Magnitudes X_n for x(t)'),... subplot(212),plot(n*w0,thn,'o',-n*w0,-thn,'o'),v=axis;axis([-16 16 v(3:4)]),plotax,... xlabel('Frequency (r/s)'),ylabel('Phase (deg)'),title('Exponential Fourier Series Phases Theta_n for x(t)'),... set(gcf,'Position',fig_size) clear i v % Technical discussion about Matlab and issues related to Digital Signal Processing. Your Email Here Join this Group! Post a new Thread fourier series coefficients - Kurt - Dec 1 12:27:01 2009 hello all, I have a one period square wave on the interval[0,2] defined as: y(t)= 1, 0<=t<1 y(t)= 0, 1<=t<2 I need to find the fourier series coefficients,ck, with k=-10,-9,...,9,10
  • 7. I heard using a for loop would work but I am completely stuck on how to move through this problem. All help is greatly appreciated, Kurt ______________________________ New Code Sharing Section now Live on DSPRelated.com. Learn about the Reward Program for Contributors here. (You need to be a member of matlab -- send a blank email to matlab-subscribe@yahoogroups.com ) Re: fourier series coefficients - vishwa - Dec 3 7:52:08 2009 you can try for k=-10:1:10 c(k+11) = here you enter the Ck equation; % you cant have negative indexing in MATLAB end Now c gives you the coefficients rgds vishwanath ________________________________ From: Kurt <k...@sbcglobal.net> To: m...@yahoogroups.com Sent: Tue, 1 December, 2009 12:50:19 PM Subject: [matlab] fourier series coefficients  hello all, I have a one period square wave on the interval[0,2] defined as: y(t)= 1, 0<=t<1 y(t)= 0, 1<=t<2 I need to find the fourier series coefficients, ck, with k=-10,-9,... ,9,10 I heard using a for loop would work but I am completely stuck on how to move through this problem. All help is greatly appreciated, Kurt ______________________________ New Code Sharing Section now Live on DSPRelated.com. Learn about the Reward Program for Contributors here. (You need to be a member of matlab -- send a blank email to matlab-subscribe@yahoogroups.com ) Re: fourier series coefficients - Vaibhav Singh - Dec 4 7:44:25 2009
  • 8. Hey.. For fourier coeff u have to find the fft of the given sequence using matlab. Since u have to find the coeff for kranging from -10:1:10, i.e.21 points u have to define ur function in time domain in 21 samples. Take the fft of these 21 samples. The resultant is your desired fourier coeff . Regards-vaibhav On Thu, Dec 3, 2009 at 5:01 PM, vishwa <v...@yahoo.com> wrote: > you can try > > for k=-10:1:10 > c(k+11) = here you enter the Ck equation; % you cant have negative indexing > in MATLAB > end > > Now > > c gives you the coefficients > > rgds > vishwanath > > ________________________________ > From: Kurt <k...@sbcglobal.net <keg1606%40sbcglobal.net>> > To: m...@yahoogroups.com <matlab%40yahoogroups.com> > Sent: Tue, 1 December, 2009 12:50:19 PM > Subject: [matlab] fourier series coefficients > > hello all, > I have a one period square wave on the interval[0,2] defined as: > y(t)= 1, 0<=t<1 > y(t)= 0, 1<=t<2 > I need to find the fourier series coefficients, ck, with > k=-10,-9,... ,9,10 > I heard using a for loop would work but I am completely stuck on how to > move through this problem. > All help is greatly appreciated, > Kurt > > > -- Vaibhav Singh BE(Hons.) Electronics And Instrumentation BITS-Pilani
  • 9. EE341.01: MATLAB M-FILE FOR PLOTTING TRUNCATED FOURIER SERIES AND ITS SPECTRA MATLAB M-File example6.m: % % Filename: example6.m % % Description: This M-file plots the truncated Fourier Series % representation of a square wave as well as its % amplitude and phase spectrum. clear; % clear all variables clf; % clear all figures N = 11; % summation limit (use N odd) wo = pi; % fundamental frequency (rad/s) c0 = 0; % dc bias t = -3:0.01:3; % declare time values figure(1) % put first two plots on figure 1 % Compute yce, the Fourier Series in complex exponential form yce = c0*ones(size(t)); % initialize yce to c0 for n = -N:2:N, % loop over series index n (odd) cn = 2/(j*n*wo); % Fourier Series Coefficient yce = yce + real(cn*exp(j*n*wo*t)); % Fourier Series computation end subplot(2,1,1) plot([-3 -2 -2 -1 -1 0 0 1 1 2 2 3],... % plot original y(t) [-1 -1 1 1 -1 -1 1 1 -1 -1 1 1], ':'); hold; plot(t,yce); % plot truncated exponential FS xlabel('t (seconds)'); ylabel('y(t)'); ttle = ['EE341.01: Truncated Exponential Fourier Series with N = ',... num2str(N)]; title(ttle); hold; % Compute yt, the Fourier Series in trigonometric form yt = c0*ones(size(t)); % initialize yt to c0 for n = 1:2:N, % loop over series index n (odd) cn = 2/(j*n*wo); % Fourier Series Coefficient yt = yt + 2*abs(cn)*cos(n*wo*t+angle(cn)); % Fourier Series computation end subplot(2,1,2) plot([-3 -2 -2 -1 -1 0 0 1 1 2 2 3],... % plot original y(t) [-1 -1 1 1 -1 -1 1 1 -1 -1 1 1], ':'); hold; % plot truncated trigonometric FS plot(t,yt); xlabel('t (seconds)'); ylabel('y(t)'); ttle = ['EE341.01: Truncated Trigonometric Fourier Series with N = ',... num2str(N)]; title(ttle); hold; % Draw the amplitude spectrum from exponential Fourier Series
  • 10. figure(2) % put next plots on figure 2 subplot(2,1,1) stem(0,c0); % plot c0 at nwo = 0 hold; for n = -N:2:N, % loop over series index n cn = 2/(j*n*wo); % Fourier Series Coefficient stem(n*wo,abs(cn)) % plot |cn| vs nwo end for n = -N+1:2:N-1, % loop over even series index n cn = 0; % Fourier Series Coefficient stem(n*wo,abs(cn)); % plot |cn| vs nwo end xlabel('w (rad/s)') ylabel('|cn|') ttle = ['EE341.01: Amplitude Spectrum with N = ',num2str(N)]; title(ttle); grid; hold; % Draw the phase spectrum from exponential Fourier Series subplot(2,1,2) stem(0,angle(c0)*180/pi); % plot angle of c0 at nwo = 0 hold; for n = -N:2:N, % loop over odd series index n cn = 2/(j*n*wo); % Fourier Series Coefficient stem(n*wo,angle(cn)*180/pi); % plot |cn| vs nwo end for n = -N+1:2:N-1, % loop over even series index n cn = 0; % Fourier Series Coefficient stem(n*wo,angle(cn)*180/pi); % plot |cn| vs nwo end xlabel('w (rad/s)') ylabel('angle(cn) (degrees)') ttle = ['EE341.01: Phase Spectrum with N = ',num2str(N)]; title(ttle); grid; hold; MATLAB Plots Generated:
  • 11.
  • 12. Hi, I am trying to write a function to generate Fourier series Coefficients of a given discrete time signal. For instance: x = [1 2 3 4] n = [0 1 2 3] where x holds the values of the signal, and n holds the corresponding time indices. My code for the function is: function a = dtfs(x,n) period = length(x); for k = 1:period a = (1/period)*x(k)*exp(((-j*2*pi)/period)*n(k)); a end i am just not getting the correct values for the fourier series coefficients. Am I setting up the formula wrong? As you can probably tell, i am very very new to MATLAB and I'd also appreciate if someone may guide me in setting up my "for- loop" with a vector to "catch" and store values of "a", so each time the for-loop repeats, the previous value a does not get over- written, instead they are all stored in a vector. Thanks in advance :) Regards Subject: Fourier Series Coefficients From: Andrew Date: 24 Oct, 2008 04:43:01 Message: 2 of 4 Reply to this message Add author to My Watch List View original format Flag as spam I'm guessing the formula, but hopefully the structure of it will help... function a = dtfs(x,n) period = length(x); a = zeros(1, length(x)) for k = 1:period for z = 1:period a(k) = a(k) + x(z) * exp((-j*2*pi)/period * (k-1) * n(z)); end a(k) = a(k) / period; num2str(a(k), '%1.18f'); end
  • 13. Cheers, Andrew > a = (1/period)*x(k)*exp(((-j*2*pi)/period)*n(k)); >a > end "Raz H" <dilster08@gmail.com> wrote in message <gdrclp$t9b$1@fred.mathworks.com>... > Hi, > > I am trying to write a function to generate Fourier series Coefficients of a given discrete time signal. For instance: > > x = [1 2 3 4] > n = [0 1 2 3] > > where x holds the values of the signal, and n holds the corresponding time indices. > > My code for the function is: > > function a = dtfs(x,n) > period = length(x); > for k = 1:period > a = (1/period)*x(k)*exp(((-j*2*pi)/period)*n(k)); >a > end > > i am just not getting the correct values for the fourier series coefficients. Am I setting up the formula wrong? > > As you can probably tell, i am very very new to MATLAB and I'd also appreciate if someone may guide me in setting up my "for-loop" with a vector to "catch" and store values of "a", so each time the for-loop repeats, the previous value a does not get over-written, instead they are all stored in a vector. > > Thanks in advance :) > > Regards Subject: Fourier Series Coefficients From: Paul Date: 24 Oct, 2008 06:35:05 Message: 3 of 4 Reply to this message Add author to My Watch List View original format Flag as spam
  • 14. "Andrew" <awbsmith@itee.uq.edu.au> wrote in message <gdrjol$so$1@fred.mathworks.com>... > I'm guessing the formula, but hopefully the structure of it will help... > > function a = dtfs(x,n) > period = length(x); > a = zeros(1, length(x)) > for k = 1:period > for z = 1:period > a(k) = a(k) + x(z) * exp((-j*2*pi)/period * (k-1) * n(z)); > end > a(k) = a(k) / period; > num2str(a(k), '%1.18f'); > end > > Cheers, > Andrew > > > > a = (1/period)*x(k)*exp(((-j*2*pi)/period)*n(k)); >>a > > end > > > > > "Raz H" <dilster08@gmail.com> wrote in message <gdrclp$t9b$1@fred.mathworks.com>... > > Hi, >> > > I am trying to write a function to generate Fourier series Coefficients of a given discrete time signal. For instance: >> > > x = [1 2 3 4] > > n = [0 1 2 3] >> > > where x holds the values of the signal, and n holds the corresponding time indices. >> > > My code for the function is: >> > > function a = dtfs(x,n) > > period = length(x); > > for k = 1:period > > a = (1/period)*x(k)*exp(((-j*2*pi)/period)*n(k)); >>a > > end >> > > i am just not getting the correct values for the fourier series coefficients. Am I setting up the formula wrong? >> > > As you can probably tell, i am very very new to MATLAB and I'd also appreciate if someone may guide me in setting up my "for-loop" with a vector to "catch" and store values of "a", so each time the for-loop repeats, the previous value a does not get over-written, instead they are all stored in a vector. >> > > Thanks in advance :) >> > > Regards
  • 15. Was this a HW problem? It looks like one to me! Subject: Fourier Series Coefficients From: Raz H Date: 24 Oct, 2008 06:54:02 Message: 4 of 4 Reply to this message Add author to My Watch List View original format Flag as spam "Paul" <par@ceri.memphis.edu> wrote in message <gdrqap$sl5$1@fred.mathworks.com>... > "Andrew" <awbsmith@itee.uq.edu.au> wrote in message <gdrjol$so$1@fred.mathworks.com>... > > I'm guessing the formula, but hopefully the structure of it will help... >> > > function a = dtfs(x,n) > > period = length(x); > > a = zeros(1, length(x)) > > for k = 1:period > > for z = 1:period > > a(k) = a(k) + x(z) * exp((-j*2*pi)/period * (k-1) * n(z)); > > end > > a(k) = a(k) / period; > > num2str(a(k), '%1.18f'); > > end >> > > Cheers, > > Andrew >> >> > > > a = (1/period)*x(k)*exp(((-j*2*pi)/period)*n(k)); >>>a > > > end >> >> >> >> > > "Raz H" <dilster08@gmail.com> wrote in message <gdrclp$t9b$1@fred.mathworks.com>... > > > Hi, >>> > > > I am trying to write a function to generate Fourier series Coefficients of a given discrete time signal. For instance: >>> > > > x = [1 2 3 4] > > > n = [0 1 2 3] >>> > > > where x holds the values of the signal, and n holds the corresponding time indices. >>> > > > My code for the function is:
  • 16. >>> > > > function a = dtfs(x,n) > > > period = length(x); > > > for k = 1:period > > > a = (1/period)*x(k)*exp(((-j*2*pi)/period)*n(k)); >>>a > > > end >>> > > > i am just not getting the correct values for the fourier series coefficients. Am I setting up the formula wrong? >>> > > > As you can probably tell, i am very very new to MATLAB and I'd also appreciate if someone may guide me in setting up my "for-loop" with a vector to "catch" and store values of "a", so each time the for-loop repeats, the previous value a does not get over-written, instead they are all stored in a vector. >>> > > > Thanks in advance :) >>> > > > Regards > > Was this a HW problem? It looks like one to me! @Andrew Thank you very much! I guess my loop was not set up correctly, plus I was not signifying the time indices correctly. @Paul This was not a homework problem, though I'll be taking Signals soon, so I am trying to become familiar with MATLAB. Thanks to all who replied! :) EE341.01: MATLAB M-FILE FOR PLOTTING TRUNCATED FOURIER SERIES This example shows a MATLAB M-file for plotting a truncated Fourier Series. Various numbers of terms are used. MATLAB M-File example5.m: % % Filename: example5.m % % Description: Example to show how the truncated Fourier series in % complex exponential form approximates the real % signal. More and more terms are taken showing a % better and better representation of the original signal. % clear; % clear all variables clf; % clear all figures
  • 17. % Define parameters to plot original sawtooth tr = [-1 0 0 1 1 2 2]; yr = [0 1 0 1 0 1 0]; % Plot Truncated Fourier Series Approximation (N = 1) N = 1; % define number of terms to use (n = -N..N) c0 = 0.5; % define dc bias coefficient t = -1:0.001:2; % define time values for y(t) y = c0 * ones(size(t)); % let initial y = c0 (dc bias) for all times for n = -N:-1, % compute y for negative n cn = exp(j*pi/2)/(2*pi*n); y = y + real(cn * exp(j*n*2*pi*t)); end; % compute y for positive n and add to y for n = 1:N, % found using negative n cn = exp(j*pi/2)/(2*pi*n); y = y + real(cn * exp(j*n*2*pi*t)); end; subplot(2,2,1); % plot approximation plot(t,y); hold; plot(tr,yr,':'); hold; xlabel('time (seconds)'); ylabel('y(t) approximation'); title('EE341.01: Truncated FS, -1<=n<=1'); % Plot Truncated Fourier Series Approximation (N = 2) clear; % clear all variables N = 2; % define number of terms to use (n = -N..N) c0 = 0.5; % define dc bias coefficient t = -1:0.001:2; % define time values for y(t) y = c0 * ones(size(t)); % let initial y = c0 (dc bias) for all times for n = -N:-1, % compute y for negative n cn = exp(j*pi/2)/(2*pi*n); y = y + real(cn * exp(j*n*2*pi*t)); end; % compute y for positive n and add to y for n = 1:N, % found using negative n cn = exp(j*pi/2)/(2*pi*n); y = y + real(cn * exp(j*n*2*pi*t)); end; subplot(2,2,2); % plot approximation plot(t,y); hold; plot(tr,yr,':'); hold; xlabel('time (seconds)'); ylabel('y(t) approximation'); title('EE341.01: Truncated FS, -2<=n<=2'); % Plot Truncated Fourier Series Approximation (N = 3) clear; % clear all variables
  • 18. N = 3; % define number of terms to use (n = -N..N) c0 = 0.5; % define dc bias coefficient t = -1:0.001:2; % define time values for y(t) y = c0 * ones(size(t)); % let initial y = c0 (dc bias) for all times for n = -N:-1, % compute y for negative n cn = exp(j*pi/2)/(2*pi*n); y = y + real(cn * exp(j*n*2*pi*t)); end; % compute y for positive n and add to y for n = 1:N, % found using negative n cn = exp(j*pi/2)/(2*pi*n); y = y + real(cn * exp(j*n*2*pi*t)); end; subplot(2,2,3); % plot approximation plot(t,y); hold; plot(tr,yr,':'); hold; xlabel('time (seconds)'); ylabel('y(t) approximation'); title('EE341.01: Truncated FS, -3<=n<=3'); % Plot Truncated Fourier Series Approximation (N = 10) clear; % clear all variables N = 10; % define number of terms to use (n = -N..N) c0 = 0.5; % define dc bias coefficient t = -1:0.001:2; % define time values for y(t) y = c0 * ones(size(t)); % let initial y = c0 (dc bias) for all times for n = -N:-1, % compute y for negative n cn = exp(j*pi/2)/(2*pi*n); y = y + real(cn * exp(j*n*2*pi*t)); end; % compute y for positive n and add to y for n = 1:N, % found using negative n cn = exp(j*pi/2)/(2*pi*n); y = y + real(cn * exp(j*n*2*pi*t)); end; subplot(2,2,4); % plot approximation plot(t,y); hold; plot(tr,yr,':'); hold; xlabel('time (seconds)'); ylabel('y(t) approximation'); title('EE341.01: Truncated FS, -10<=n<=10'); MATLAB Plot Generated: