the speed of gif is very slow!

2 views (last 30 days)
Niloufar
Niloufar on 7 Oct 2022
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

Answers (1)

Benjamin Thompson
Benjamin Thompson on 7 Oct 2022
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.

Categories

Find more on Mathematics 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!