Fourier Series Signal in Cosine Form and Exponential Form

5 次查看(过去 30 天)
I'm trying to make this graph in its Fourier Series Signal in Cosine and Exponential Form. Can you please correct my codes.
t= [-3 -3 -2 -2 -1 -1 0 0 1 1 2 2 3 3]; % values of t
xt= [1 -1 -1 1 1 -1 -1 1 1 -1 -1 1 1 -1]; % values of x
subplot(3,1,1) % to plot 3 graphs in a single page
plot(t,xt,'LineWidth',1.5)% plotting the original signal
title('Graph of the Original Signal')
xlabel('Time, (t)') % label the x-axis
ylabel('Amplitude, x(t)') % label the y-axis
grid on % putting a grid
subplot (3,1,2) % to plot 3 graphs in a single page
t = (-3:0.001:3); % exponential form
f = 0*t; % creates a zero valued function
N=5; % number of harmonics
for k=-N:1:N
if(k==0) % skip the zeroth term
continue;
end;
C_k = ((1)/(pi*1i*k))*(1-exp(-pi*1i*k)); % computes the k-th Fourier coefficient of the exponential form
f_k = C_k*exp(2*pi*1i*k*t); % k-th term of the series
f = f + f_k; % adds the k-th term to f
end
plot(t, f, 'LineWidth', 1.5);
title('Graph of the Fourier Series Signal in Exponential Form')
ylabel('Amplitude, x(t)') % label the y-axis
xlabel('Time, (t)') % label the x-axis
grid on % putting a grid
subplot (3,1,3)% to plot 3 graphs in a single page
a0=1/2; w0=pi; % cosine form
t=-3:0.001:3
sum=0;
for m=1:3:1
if mod(m+4-1,4)==0
Qm=0;
else Qm=-pi;
end
sum=sum+2/(m*pi)*cos(m*w0*t+Qm);
end
xt=a0+sum;
plot(t,xt,'LineWidth',1.5),hold on % plotting the fourier series signal
title('Graph of the Fourier Series Signal in Cosine Form')
ylabel('Amplitude, x(t)') % label the y-axis
xlabel('Time, (t)') % label the x-axis
grid on % putting a grid

回答(1 个)

Aashray
Aashray 2025-6-13
I went through the code that you have shared, and suggest the following changes:
  1. Use proper expression for a symmetric square wave:Ck = (1/(2i*pi*k)) * (exp(-1i*pi*k) - 1)
  2. Only plot real part using real(f_exp)
  3. Use sine terms with odd harmonics only (because square wave is odd function): bn = 4/(n*pi); xt_trig = xt_trig + bn * sin(n * w0 * t);
  4. Since the square wave is symmetric around zero: a0 = 0.
This correct code might help you understand the changes better:
% Original Signal Definition
t_discrete = [-3 -3 -2 -2 -1 -1 0 0 1 1 2 2 3 3];
xt_discrete = [1 -1 -1 1 1 -1 -1 1 1 -1 -1 1 1 -1];
subplot(3,1,1)
plot(t_discrete, xt_discrete, 'LineWidth', 1.5)
title('Graph of the Original Signal')
xlabel('Time, (t)')
ylabel('Amplitude, x(t)')
grid on
%% Exponential Fourier Series Approximation
subplot(3,1,2)
t = -3:0.001:3; % fine time vector
T = 2; % assume period = 2
w0 = 2*pi/T; % fundamental frequency
f_exp = zeros(size(t));
N = 20; % number of harmonics
for k = -N:N
if k == 0
continue; % skipping DC term, assume zero average
end
% Fourier coefficients for square wave from -1 to 1, amplitude ±1
Ck = (1/(2i*pi*k)) * (exp(-1i*pi*k) - 1);
f_exp = f_exp + Ck * exp(1i * k * w0 * t);
end
plot(t, real(f_exp), 'LineWidth', 1.5)
title('Fourier Series Signal in Exponential Form')
xlabel('Time, (t)')
ylabel('Amplitude, x(t)')
grid on
%% Trigonometric (Cosine/Sine) Fourier Series Approximation
subplot(3,1,3)
t = -3:0.001:3;
xt_trig = zeros(size(t));
a0 = 0; % DC term for symmetric square wave is zero
N = 20; % number of sine harmonics
for n = 1:N
if mod(n, 2) == 1 % odd harmonics only
bn = 4/(n*pi); % coefficient for sine terms of square wave
xt_trig = xt_trig + bn * sin(n * w0 * t);
end
end
xt_trig = a0 + xt_trig;
plot(t, xt_trig, 'LineWidth', 1.5)
title('Fourier Series Signal in Sine (Trigonometric) Form')
xlabel('Time, (t)')
ylabel('Amplitude, x(t)')
grid on
A side note: The second subplot (exponential form) shows peaks where valleys are expected, due to phase shifts in the exponential representation and the absence of DC term.

Community Treasure Hunt

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

Start Hunting!

Translated by