This repository has been archived on 2023-12-21. You can view files and clone it, but cannot push or open issues or pull requests.
Files
ece45-project/src/Generators/generate_sawtooth.m
2021-12-10 10:01:30 +08:00

41 lines
1.3 KiB
Matlab

function x = generate_sawtooth(amplitude, frequency, phase, fs, duration, duty)
% generate_sawtooth: returns a matrix of sampled sawtooth wave
% CONTRIBUTORS:
% Ben Zhang: Function 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
mid = period / 2;
%part before the straght vertical line
if(st < mid)
x(i) = slope * st; % amplitude from start to +amplitude
%part after the straght vertical line
else
x(i) = slope * (st - 0.5) - amplitude; %amplitude from -amplitude to start
end
end