From c1942eab180b4f0e0bc54352c891f647204f3ccf Mon Sep 17 00:00:00 2001 From: Ben Zhang Date: Sun, 5 Dec 2021 20:24:45 -0800 Subject: [PATCH] add sawtooth generator function --- src/generate_sawtooth.m | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/generate_sawtooth.m diff --git a/src/generate_sawtooth.m b/src/generate_sawtooth.m new file mode 100644 index 0000000..61fd0da --- /dev/null +++ b/src/generate_sawtooth.m @@ -0,0 +1,38 @@ +function x = generate_sawtooth(amplitude, frequency, phase, fs, duration, duty) +% generate_sawtooth: returns a matrix of sampled sawtooth wave + +% CONTRIBUTORS: +% Ben Zhang: Function author +% Arthur Lu: Format author + +% DOCUMENTATION: +% phase shift is in number of periods +% fs is the sampling frequency: how many sample points per second +% duration is time in seconds +% duty is a number between 0 and 1 + %duty does not apply for sawtooth wave + + % initialize local variables from input arguments + n = fs * duration; % number of samples (length of matrix) + dt = 1 / fs; % sampling period: time between two sample points + period = 1 / frequency; % period of the wave + + % initialize a one dimensional zero matrix to be populated + x = zeros(1, n); + + % populate the matrix + for i = 1:n + t = i * dt; % time of the i'th sample + st = mod(frequency * t - phase, 1); % Progression through cycle + + slope = 2 * amplitude / period; % the incline slope from start to amplitude + %part before the straght vertical line + if(st < period /2) + x(i) = slope * st; + + %part after the straght vertical line + else + x(i) = slope * st - amplitude; + + end +end \ No newline at end of file