MATLAB: Fourier Transformation HELP PLEASE

fourier series transformationMATLABmatlab help

%-------------%

% Part 1: Fouier Series.
%-------------%
clear all; close all; clc;
incrm = 0.01;
t = incrm:incrm:10;
xt = cos(2*t);
plot(t,xt,'LineWidth',3)
hold on
grid on
xlabel('t')
ylabel('x(t)')
legend('x(t)')
set(gca,'FontSize',13)
%%Fouier Series Approximation of x(t)
%---Fourier Coefficients---%
T0 = pi;
w0 = 2*pi/T0;
a0 = (1/T0) * trapz(t(1:1+T0/incrm), xt(1:1+T0/incrm), 2);
for nn = 1 : 17
an(nn) = (2/T0) * trapz(t(1:1+T0/incrm-1), xt(1:1+T0/incrm-1).*cos(nn*w0*t(1:1+T0/incrm-1)), 2);
bn(nn) = (2/T0) * trapz(t(1:1+T0/incrm-1), xt(1:1+T0/incrm-1).*sin(nn*w0*t(1:1+T0/incrm-1)), 2);
end
%---Fourier Series Approximation---%
sumk = 0;
for nn = 1 : 17
sumk = sumk + an(nn)*cos(nn*w0*t) + bn(nn)*sin(nn*w0*t);
end
xt_FS = a0 + sumk;
figure;
plot(t, xt, 'LineWidth', 3)
hold on
grid on
xlabel('t')
ylabel('x(t)')
plot(t,xt_Fs,'r','LineWidth',1.5)
ylim([-1,1])
legend('x(t)','FS Approximation')
set(gca,'FontSize',13)
%%Fourier Coefficients for Frequency Plot
%---Problem 3---%
w = w0 * (1 : 17);
%---Problem 4---%
figure('Position', [50,100,1200,400]);
subplot(1,2,1)
stem(w,an(nn),'MarkerSize', 14,'LineWidth', 2)
grid on
box on
xlabel('\omega')
ylabel('a_n')
set(gca,'FontSize', 13)
xlim([0 17])
ylim([-0.5 2])
ax = gca;
ax.XTick = 0:1:17;
ay = gca;
ay.YTick = -0.5:0.5:2;
subplot(1,2,2)
stem(w,bn(nn), 'MarkerSize', 14, 'LineWidth', 2)
grid on
box on
xlabel('\omega')
ylabel('b_n')
set(gca, 'FontSize', 13)
xlim([0 17])
ylim([-0.5 2])
ax = gca;
ax.XTick = 0:1:17;
ay = gca;
ay.YTick = -0.5:0.5:2;
I am getting 2 error codes and I don't know why!
Warning: Integer operands are required for colon operation when used as an index
Error using stem (line 43) X must be the same length as Y

Best Answer

clear all; close all; clc;
incrm = 0.01;
t = incrm:incrm:10;
xt = cos(2*t);
plot(t,xt,'LineWidth',3)
hold on
grid on
xlabel('t')
ylabel('x(t)')
legend('x(t)')
set(gca,'FontSize',13)
%%Fouier Series Approximation of x(t)
%---Fourier Coefficients---%
T0 = pi;
w0 = 2*pi/T0;
a0 = (1/T0) * trapz(t(1:1+T0/incrm), xt(1:1+T0/incrm), 2);
for nn = 1 : 17
an(nn) = (2/T0) * trapz(t(1:1+T0/incrm-1), xt(1:1+T0/incrm-1).*cos(nn*w0*t(1:1+T0/incrm-1)), 2);
bn(nn) = (2/T0) * trapz(t(1:1+T0/incrm-1), xt(1:1+T0/incrm-1).*sin(nn*w0*t(1:1+T0/incrm-1)), 2);
end
%---Fourier Series Approximation---%
sumk=cell(1,17)
sumk{1} = 0;
for nn = 1 : 17-1
sumk{nn+1} = sumk{nn} + an(nn)*cos(nn*w0*t) + bn(nn)*sin(nn*w0*t);
end
xt_FS = a0 + [sumk{:}]
figure;
plot(t, xt, 'LineWidth', 3)
hold on
grid on
xlabel('t')
ylabel('x(t)')
plot(t,xt_FS(1:numel(t)),'r','LineWidth',1.5)
ylim([-1,1])
legend('x(t)','FS Approximation')
set(gca,'FontSize',13)
%%Fourier Coefficients for Frequency Plot
%---Problem 3---%
w = w0 * (1 : 17);
%---Problem 4---%
figure%('Position', [50,100,1200,400]);
subplot(1,2,1)
stem(w,an,'MarkerSize', 14,'LineWidth', 2)
grid on
box on
xlabel('\omega')
ylabel('a_n')
set(gca,'FontSize', 13)
xlim([0 17])
ylim([-0.5 2])
ax = gca;
ax.XTick = 0:1:17;
ay = gca;
ay.YTick = -0.5:0.5:2;
subplot(1,2,2)
stem(w,bn, 'MarkerSize', 14, 'LineWidth', 2)
grid on
box on
xlabel('\omega')
ylabel('b_n')
set(gca, 'FontSize', 13)
xlim([0 17])
ylim([-0.5 2])
ax = gca;
ax.XTick = 0:1:17;
ay = gca;
ay.YTick = -0.5:0.5:2;
Related Question