0
$\begingroup$

I am trying to create a Power vs Frequency graph for different harmonics(x_5(t), x_20(t), and x_100(t)) of the half triangular wave. I want to see how the difference in power between the original and contracted harmonic signals depends on the harmonics.

I am pretty new to Matlab.

I'm looking for help with my situation?

what am I don wrong?

Matlab code: (code also in the link https://ctxt.io/2/AADgPEMlFQ)

clear all
clc
tMax    = 5;                        % duration of the pulse  
tStep   = 0.001;                    % resolution of time axis (inverse of sammpling frequency)
t       = 0:tStep:tMax-tStep;       % time axix 
D       = 2;                        % time instant corresponding to end of rising edge (A for this pulse)
A       = D;                        % maximum signal amplitude
tri      = zeros(1,length(t));      % half-trangular signal (vector with length = length of time axis)
f0      = 1/tMax;                   % tri(t) frequency
% construct the half-trangular signal x(t)
for i = 1:1:length(t)
    % if t value is less than D then x(t) = A
    if t(i) < D 
        tri(i) = i*tStep;
    else 
    % if t value is greater than D then x(t) = 0
        tri(i) = 0;
    end
end
% plot the half-triangular signal tri(t)
figure                                          % create a new figure object     
set(groot, 'defaultAxesTickLabelInterpreter','latex'); 
set(groot, 'defaultLegendInterpreter','latex');
plot(t,tri)                                     % plot using t as x-axis, and sq as y-axis
xlabel('$t [sec]$','interpreter','latex')       % insert label for x-axis
ylabel('$x(t)$','interpreter','latex')          % insert label for y-axis
title('Triangular Wave (One Period)')          % insert title for the figure 
ylim([-0.5 2.1]);
% Calculate Fourier series coefficients for signal
numberOfHarmonics = 20;     % number of sinsoids signal used to construct tri(t)
A_n               = zeros(1,numberOfHarmonics); % vector of A_n coefficients
B_n               = zeros(1,numberOfHarmonics); % vector of B_n coefficients
c0                = 0;                          % initialized with c0 = 0
% calculate A_n and B_n, and c0
for i = 1:1:numberOfHarmonics % i is index of a harmonic. 
    for j = 1:1:length(t) % j is index of t
        A_n(i) = A_n(i) + tStep*tri(j)*cos(2*pi*i*f0*t(j));
        B_n(i) = B_n(i) + tStep*tri(j)*sin(2*pi*i*f0*t(j));
    end
end
for j = 1:1:length(t) % j is index of t
    c0 = c0 + tStep*tri(j);
end
A_0 = c0/tMax;
A_n = A_n*2/tMax;
B_n = B_n*2/tMax;
% plot of A_n and B_n
figure
subplot(2,1,1)
stem(A_n)
ylim([-0.4 0.4])
xlabel('$n$','interpreter','latex')
ylabel('$A_n$','Interpreter',"latex")
subplot(2,1,2)
stem(B_n)
ylim([-0.8 0.8])
xlabel('$n$','interpreter','latex')
ylabel('$B_n$','Interpreter',"latex")

figure
n = length(t);          % number of samples
f = (0:n-1)*((1/tStep)/n);     % frequency range 
Power=((A_n)^2+(B_n)^2)*1/2+((A_0)^2/4);

plot(t,power)
xlabel('$n$','interpreter','latex')
ylabel('$A_n$','Interpreter',"latex")
$\endgroup$
1
  • $\begingroup$ Your code doesn't run and has syntax errors. It would also help if you state what you expected, what you are getting and how these are different. It looks like you are trying to simulate a continuous signal using a discrete model. That's tricky since your continuous signal is not bandlimited and hence you will get aliasing. $\endgroup$
    – Hilmar
    Jun 7 at 13:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy