Clear Filters
Clear Filters

Coding for summation of Fourier transform

2 views (last 30 days)
Tu Nguyen
Tu Nguyen on 3 Mar 2022
Answered: Ayush on 5 Oct 2023
clear all
close all
L = 1;
n = [-4:4];
syms x
rectn = rectangularPulse(-1,1,x);
F_rectn = fourier(rectn);
rp1 = real(F_rectn)*cos(2*pi*n*x/L);
F_0 = F_rectn(x==0);
y1 = zeros(1, numel(n));
for i = numel(n);
y1 = y1 + real(F_rectn).*cos(2*pi*n(i)*x/L);
y1 = y1 + F_0;
Hi all, I checked my code many times, but it returns the error of summation. Can anyone help me fix the code like the formula in the picture but eliminate the imaginary part?

Answers (1)

Ayush on 5 Oct 2023
Hi Nguyen,
I understand that you are trying to solve the equation given in the screen shot using MATLAB.
As per the code provided by you, you can make the range of “n” positive, n = [1,inf). One thing to notice is that, when you will do Fourier transform then you will have this equation:
F(w) = - (- sin(w) + cos(w)*1i)/w + (sin(w) + cos(w)*1i)/w
Thus, from above equation it can be observed, “w” cannot have the value as 0, as it will give undefine error. The function is dependent on “n/L” i.e., F(n/L) thus the range of “n” can be changed to [1:4], hence there will be non-empty value of Fourier transform. To execute this, you can use the following command which will substitute value of “w” with “n/L”.
F_rectn = fourier(rectn,n/L);
Next, you can make the following changes to the code:
F_0 = F_rectn(x==0);
The above changes will help you to check if “x” variable is equal to 0 or not, which will never be the case as “F_rectn” is in frequency domain, thus change the line as “F_0 = 2”. The value 2 can be observed from the Fourier transform curve.
The final changes made in the code are given below:
n = [1:4]; % making the range as positive
F_rectn = fourier(rectn,n/L); % Added n/L as the transformation variable as equation requires F(n/L)
%rp1 = real(F_rectn)*cos(2*pi*n*x/L); This line was not in use.
F_0 = 2; % Changed from F_rectn(x==0)
I hope this helps.

Community Treasure Hunt

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

Start Hunting!