I am working on one of my first Matlab projects. I am trying to graph a sawtooth wave with 10-V Pk-Pk, 0-V average value. 50% duty cycle, 2.5 khz.

I first attempted to find a general equation for such a sawtooth wave.

$$\frac{-V_{pp}}{2} + \frac{jV_{pp}}{2\pi} \sum_{n=-\infty}^{\infty} \frac{1}{n}e^{jnw_0t}$$

From this, for the particular function I am trying to plot I have the equation

-5 + j*5/pi Sum (from n=-inf to inf) (1/n)e(j*5000*pint) $$\ -5 + \frac{j5}{\pi} \sum_{n=-\infty}^{\infty} \frac{1}{n}e^{j5000\pi nt}$$

And my code:


t = linspace(-2,10,1e4); % time vector [s]

%% constructing approximation x_N(t), N = 100

n = -100:100;                            % indices included in the summation, n is a 101 element vector
X = (5*1i)./(pi*n);                          % this constructs the coefficients of e^(*) terms
X(n==0) = 5;                             % C_0

% X is a horizontal vector with the coefficients of the exponential term
% corresponding each n-value from -100 to 100.

XM = repmat(X.',[1, length(t)]);         % This is a matrix that replicates the transpose of the
                                         % vector X as many times as t is long
                                         % now, XM is a matrix with identical values in each row. 
% Each column has a coefficient of an exponential term corresponding to an
% n-value, repeated throughout
basis = exp(1i*pi*5000*n.'*t);              % basis is a matrix containing the exponential part,
                                         % each column corresponding with each n-value
                                         % and each row corresponding time

x100 = -5 + sum(XM.*basis);                   % x100 is matrix resulting from summing down the rows of element-wise
                                         % multiplacation of XM and basis

if max(imag(x100))<1e-15  % this checks to see if the imaginary part of x
x100 = real(x100);      % is below Matlab's noise floor (approx 10^-15).
end                     % If so, we get rid of the imaginary part.

%% plots

% we will use these variables to keep the limits on our plots uniform
tmin = min(t); tmax = max(t); xmin = -6; xmax = 6;

%subplot(3,1,1);     % this command allows you to plot several graphs
%                    % on one page
axis([tmin tmax xmin xmax]);    % sets the plot's axis limits
title('N=100 approximation of Triangle Wave');
xlabel('time [s]'); ylabel('amplitude [a.u.]');

However the period here is 2 seconds, which seems totally wrong to me. I'm having trouble figuring out where I went wrong.

Also - I apologize, I am also having trouble getting MathJax to preview my math on my browser, I don't know what's wrong there. I am sorry for the messy looking representation of my math.

  • \$\begingroup\$ fixed up your first equation, at least into legal mathjax anyway, check it still expresses what you mean mathematically, I may have garbled it. You do the same for the second equation. \$\endgroup\$
    – Neil_UK
    Apr 17 '18 at 6:20

The linspace is probably not what you want it to be. A 2.5kHz signal has a period of 0.4ms

tmin = 1/2500; t = linspace(0,tmin*5,2000); % time vector [s]


tmin = 1/2500; t = linspace(-tmin,tmin*4,2000); % time vector [s]

I don't know why the amplitude is off, but at least the period is right.

enter image description here

  • \$\begingroup\$ Ah OK, great, thank you! Simpler than I thought. I guess with the larger linspace I just expected that it would look almost like a fuzzy line. This gives me something to think about, thank you. \$\endgroup\$
    – wsamples
    Apr 17 '18 at 0:37
  • \$\begingroup\$ If you like the answer upvote \$\endgroup\$
    – Voltage Spike
    Apr 17 '18 at 0:50
  • \$\begingroup\$ I did - upvotes are not public unless you have a certain reputation. \$\endgroup\$
    – wsamples
    Apr 17 '18 at 19:26
  • 1
    \$\begingroup\$ Why do you say the amplitude is off? The overshoot is expected for the truncated fourier series. en.wikipedia.org/wiki/Gibbs_phenomenon \$\endgroup\$ Apr 17 '18 at 19:55
  • \$\begingroup\$ @RussellBorogove the OP wanted a 10V-Pk Pk signal, I didn't have time to look at the amplitude. \$\endgroup\$
    – Voltage Spike
    Apr 17 '18 at 20:16

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.