Using the trigonometric Fourier series to develop MATLAB code to confirm correctness

77 views (last 30 days)
fs = 2/5 + sum(2/(n*pi)*sin(2*pi/5*n)*cos(2*pi/5*n*t)) x(t) = sum(rect((t-5*n)/2))
I really don't know how to plot two function. Please help me.
  3 Comments
wenyuan zhu
wenyuan zhu on 13 Jan 2014
There are two problems related together. Firstly,find the trigonometric Fourier series of the following periodic functions
I do the math and find out that the trig fourier series is fs = 2/5 + equation under below
Secondly, using the trigonometric Fourier series(fs) derived in Problem 1, develop a MATLAB code to confirm the correctness of the series. Basically, I am stuck on the second problem. Sorry for the confusion.
Umar
Umar on 12 Aug 2024

Hi @Mummana Sai Varun,

Below is a detailed MATLAB code that computes the Fourier series expansion for an exponentially rising signal.

 % Parameters
 a = 1; % Growth rate
 T = 2; % Period
 N = 10; % Number of Fourier coefficients
 % Time vector
 t = linspace(0, 4*T, 1000); % Time from 0 to 4 periods
 % Define the exponentially rising signal
 x_t = exp(a * mod(t, T)); % Modulo to create periodicity
 % Initialize Fourier coefficients
 a0 = (1/T) * integral(@(t) exp(a * mod(t, T)), 0, T);
 a_n = zeros(1, N);
 b_n = zeros(1, N);
 % Compute Fourier coefficients
for n = 1:N
    a_n(n) = (2/T) * integral(@(t) exp(a * mod(t, T)) .* cos(2 * pi * n * t / T), 0, T);
    b_n(n) = (2/T) * integral(@(t) exp(a * mod(t, T)) .* sin(2 * pi * n * t / T), 0, T);
 end
 % Construct the Fourier series
 fourier_series = a0 / 2; % Start with a0/2
 for n = 1:N
    fourier_series = fourier_series + a_n(n) * cos(2 * pi * n * t / T) + b_n(n) * sin(2 
 * pi * n * t / T);
 end
 % Plotting
 figure;
 plot(t, x_t, 'r', 'LineWidth', 1.5); % Original signal
 hold on;
 plot(t, fourier_series, 'b--', 'LineWidth', 1.5); % Fourier series approximation
 title('Fourier Series Approximation of Exponentially Rising Signal');
 xlabel('Time (t)');
 ylabel('Amplitude');
 legend('Original Signal', 'Fourier Series Approximation');
 grid on;
 hold off;
 Please see attached. 

Hope, this answers your question.

Sign in to comment.

Answers (2)

Patrik Ek
Patrik Ek on 13 Jan 2014
Edited: Patrik Ek on 22 Jan 2014
The solution is quite direct, it is more a matter of understanding. You have the fourier series given as a function of t. What you actually have calculated here is the complete fourier series, so to say the equation above should be exact a square wave. The function describes a set of discrete frequencies with
w = n*t/5 and amplitued An = 2*sin(2*pi*n/5)/(n*pi), n = 1,2,...,
Due to ortogonality of the trigonometric functions it is possible to superpositioning all the different frequencies, why it is a sum in the function. Matlab does however work with discrete time samples as well. The sampling frequency need to be much higher than the frequency of the highest order, if the plot are going to look good. So to say, if nMax = 10, you may want the time between the samples to be 0.05 or so (to get a smooth line, maybe I did not need to say that). The code required is something like,
tMax = 20; % 4 periods
t = 0:0.05:tMax;
nMax = 10; % order of the fourier series.
y = 2/5; %signal offset
for n = 1:nMax
y = y+2*sin(2*pi*n/5)*cos(2*pi*n*t/5)/n/pi; % sum all contributions
end
figure;
plot(t,y,'r');
hold on;
%Rectangular wave by looking at the function.
t0 = [0,0.999,1]; % first half wave
x0 = [1,1,0];
nPeriods = 4
t1 = [4,4.001,5.999,6];
t = [t0,t1];
x1 = [0 1 1 0];
x = [x0,x1];
for periodI = 3:nPeriods % first two periods by hand
t = [t,t1+5*(nPeriods-2)];
x = [x,x1];
end
plot(t,x,'k');
This will plot both lines in the same graph. and with variable accuracy parameters.
Sorry for the late answer BR/Patrik
  4 Comments
Ebenezer Arthur
Ebenezer Arthur on 17 Dec 2022
Hello i am trying to do the same with this code but i dont seem to get the period when t should be 0 or mirrored. can you take a look at my code.
T = 10;
V = 2;
x= 0:0.05:20 ; % x also known as t
w = 2*pi/T;
a0= V;
a1= 2*V./T* sin(T/2);
b1= 2*V./T *(1-cos(T/2));
a2= V/T *sin(T);
b2= V/T *(1-cos(T));
a3= 2*V/(3*T) * sin(3/2*T);
b3= 2*V/(3*T) *(1-cos(3/2*T));
%sq=square
f2= a0/2+ a1*cos(w*x) + b1*sin(w*x) ;
f3= a0/2+ a1*cos(w*x) + b1*sin(w*x) + a2*cos(2*w*x) + b2*sin(2*w*x);
f4= a0/2+ a1*cos(w*x) + b1*sin(w*x) + a2*cos(2*w*x) + b2*sin(2*w*x) + a3*cos(3*w*x) + b3*sin(3*w*x);
plot(x,f2,"g")
hold on
plot(x,f3,'b')
hold on
plot(x,f4,'r')
Umar
Umar on 12 Aug 2024
Hi,
In order to make sure that your functions exhibit the correct periodic behavior, consider the following adjustments:
If you are approximating a square wave or another periodic function, the coefficients should reflect that.
When plotting, ensure that the x-axis covers multiple periods. Since your period is 10, you can adjust the x vector to cover a range that is a multiple of the period:x = 0:0.05:30; % Extend to cover 3 periods You are using hold on correctly, but it is good practice to finalize the plot with labels and a legend for clarity:plot(x, f2, "g", 'DisplayName', 'f2');
plot(x, f3, 'b', 'DisplayName', 'f3');
plot(x, f4, 'r', 'DisplayName', 'f4');
xlabel('Time (t)');
ylabel('Function Value');
title('Fourier Series Approximation');
legend show;
grid on;
If you continue to experience issues, consider revisiting the mathematical basis for your Fourier series coefficients to ensure they align with the function you are modeling.

Sign in to comment.


Umar
Umar on 12 Aug 2024

Hi

Addressing your query regarding, “fr = 2/5 + sum(2/(n*pi)*sin(2*pi/5*n)*cos(2*pi/5*n*t)) x(t) = sum(rect((t-5*n)/2)) I really don't know how to plot two function. Please help me.”

Please see my response to comments below.

First, set up the parameters for computation. This includes defining the number of terms in the summation, the time variable, and initializing the result array.

N = 100; % Number of terms in the summation

t = linspace(0, 10, 1000); % Time variable from 0 to 10 with 1000 points

result = zeros(size(t)); % Initialize result array

Next, compute the summation by iterating through each term from 1 to N. For each term, calculate the current term of the series and accumulate it into the result array.

for n = 1:N

    % Calculate the current term of the series
    current_term = (2 * sin(2 * pi * n / 5) .* cos(2 * pi * n * t / 5)) / (n * 

pi); % Accumulate the result

    result = result + current_term;

end

Once the summation is computed, visualize the result using a plot. This will help you understand the behavior of the function over the specified time range.

figure; % Create a new figure window

plot(t, result, 'LineWidth', 2); % Plot the result with specified line width

title('Plot of the Summation Function'); % Add a title to the plot

xlabel('Time (t)'); % Label the x-axis

ylabel('Function Value'); % Label the y-axis

grid on; % Enable grid for better readability

Please see attached plot.

Hope this helps. Please let me know if you still have any further questions.

Categories

Find more on Mathematics and Optimization in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!