Merge pull request #15 from ltcptgeneral/generate_sawtooth
add sawtooth generator function
This commit is contained in:
commit
74978464b5
38
src/generate_sawtooth.m
Normal file
38
src/generate_sawtooth.m
Normal file
@ -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
|
Reference in New Issue
Block a user