Merge pull request #16 from ltcptgeneral/bz010-patch-1

Fix the equation for generating sawtooth wave
This commit is contained in:
Ben Zhang 2021-12-05 20:53:51 -08:00 committed by GitHub
commit b5158c6e83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,19 +20,22 @@ function x = generate_sawtooth(amplitude, frequency, phase, fs, duration, duty)
% initialize a one dimensional zero matrix to be populated % initialize a one dimensional zero matrix to be populated
x = zeros(1, n); x = zeros(1, n);
% populate the matrix % populate the matrix
for i = 1:n for i = 1:n
t = i * dt; % time of the i'th sample t = i * dt; % time of the i'th sample
st = mod(frequency * t - phase, 1); % Progression through cycle st = mod(frequency * t - phase, 1); % Progression through cycle
slope = 2 * amplitude / period; % the incline slope from start to amplitude slope = 2 * amplitude / period; % the incline slope from start to amplitude
mid = period / 2;
%part before the straght vertical line %part before the straght vertical line
if(st < period /2) if(st < mid)
x(i) = slope * st; x(i) = slope * st;
%part after the straght vertical line %part after the straght vertical line
else else
x(i) = slope * st - amplitude; x(i) = slope * (st - 0.5) - amplitude;
end end
end end