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:
%% SAWTOOTH WAVE
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;
figure(1);
%subplot(3,1,1); % this command allows you to plot several graphs
% % on one page
plot(t,x100);
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.
Best Answer
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]
or
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.