How can I plot the fourier series of a rectangular pulse by calculating the coefficients?

조회 수: 37 (최근 30일)
I want to find the fourier series of this recangular pulse shown in the code until N by using this formula
but I don't get the desired output.here is my code.could anyone tell what is the problem of my code?
here is the output I want to get
close all; clear; clc;
N = 2;
f = @(x) rectangularPulse(-1,1,x);
x = -2:0.01:2;
%2*p is the period
p = 1;
% the main function
plot(x,f(x),'LineWidth',2);
grid;
hold on;
grid minor;
xlim([-2 2]);
ylim([-0.1 1.1]);
x = linspace(-2,2,9).';
y = linspace(0,1,0.2);
a0 = (1/(2*p))*integral(f,-p,p);
an = zeros(1,N);
bn = zeros(1,N);
for n=1:N
fan = @(x) rectangularPulse(-1,1,x).*cos((n*pi/p)*x);
an(1,n) = (1/p)*integral(fan,-p,p);
fbn = @(x) rectangularPulse(-1,1,x).*sin((n*pi/p)*x);
bn(1,n) = (1/p)*integral(fbn,-p,p);
end
fs = a0 + sum(an.*cos((1:N).*x) + bn.*sin((1:N).*x),2);
plot(x,fs,'LineWidth',2);

답변 (1개)

VBBV
VBBV 2022년 10월 6일
close all; clear; clc;
N = 2;
f = @(x) rectangularPulse(-1,1,x);
x = -2:0.001:2;
%2*p is the period
p = pi;
% the main function
plot(x,f(x),'LineWidth',2);
grid;
hold on;
grid minor;
xlim([-2 2]);
ylim([-0.1 1.1]);
x = linspace(-2,2,9).';
y = linspace(0,1,0.2);
a0 = (1/(2*p))*integral(f,-p,p);
an = zeros(1,N);
bn = zeros(1,N);
for n=1:N
fan = @(x) rectangularPulse(-1,1,x).*cos((n*pi/p)*x);
an(1,n) = (1/p)*integral(fan,-p,p);
fbn = @(x) rectangularPulse(-1,1,x).*sin((n*pi/p)*x);
bn(1,n) = (1/p)*integral(fbn,-p,p);
end
fs = a0 + sum(an.*cos((1:N).*x) + bn.*sin((1:N).*x),2);
plot(x,fs,'LineWidth',2);
  댓글 수: 5
VBBV
VBBV 2022년 10월 6일
If you try with N = 10 or higher it looks similar and closely approximates the rectangular pulse
VBBV
VBBV 2022년 10월 8일
Note that if you are comparing waveforms for N = 6 with other results as you shown, it may give different results. The output which you shown may have been obtained with a different upper limit summation for coefficients in the series.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by