the speed of gif is very slow!

조회 수: 2 (최근 30일)
Niloufar
Niloufar 2022년 10월 7일
답변: Benjamin Thompson 2022년 10월 7일
I have generated a gif of fourier series of a pulse and the pulse but it takes a long time to run when I change N to larger numbers.what can I do ? Here is my code:
N = 10;
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,100).';
y = linspace(0,1,0.2);
a0 = (1/(2*p))*integral(f,-p,p);
an = zeros(1,N);
bn = zeros(1,N);
% calculate an and bn till 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
% create the gif
for n = 1:N
An = an(:,(1:n));
Bn = bn(:,(1:n));
fs = a0 + sum(An.*cos((1:n).*x) + Bn.*sin((1:n).*x),2);
hPlot = plot(x,fs,'color','red','LineWidth',2);
drawnow;
frame = getframe(1);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if(n~=N)
delete(hPlot);
end
end

답변 (1개)

Benjamin Thompson
Benjamin Thompson 2022년 10월 7일
Have you tried using the performance analysis tool in the MATLAB Editor to identify where the most time is being spent and this can help identify areas for improvement? How large a value of N are you trying to reach?
If you have access for parfor in the Parallel Computing Toolbox and a multi core machine that may help a lot.

카테고리

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